Extending rcmt¶
It is possible to use rcmt as library and add custom Sources or Encodings.
Add a custom Source¶
import rcmt
import rcmt.encoding
import rcmt.source
class MyCustomRepository(rcmt.source.Repository):
"""
Implement all methods of class rcmt.source.Repository here.
"""
pass
class MyCustomSource(rcmt.source.Base):
def list_repositories(self) -> list[rcmt.source.Repository]:
return [MyCustomRepository()]
class MyCustomEncoding(rcmt.encoding.Encoding):
"""
Implement all methods of class rcmt.encoding.Encoding here.
"""
# Basic setup
opts = rcmt.options_from_config("<path to rcmt config file>")
opts.matcher_path = "<path to run file>"
opts.packages_paths = ["<path to packages directory>"]
# Add the custom Source
opts.sources["custom"] = MyCustomSource()
# Add the custom Encoding
opts.encoding_registry.register(enc=MyCustomEncoding(), extensions=[".myc"])
# Execute rcmt
rcmt.execute(opts)
Add a custom Action to a Manifest¶
import os.path
from rcmt.package import Manifest
from rcmt.package.action import Action
class HelloWorld(Action):
"""
HelloWorld action creates the file "hello-world.txt" in the root of a repository.
It writes "Hello World" to it.
"""
def apply(self, pkg_path: str, repo_path: str, tpl_data: dict) -> None:
file = os.path.join(repo_path, "hello-world.txt")
with open(file, "w+") as f:
f.write("Hello World")
f.write("\n")
with Manifest(name="custom-action") as manifest:
manifest.add_action(HelloWorld())
Write a custom Action using GlobMixin
¶
from rcmt.package import Manifest
from rcmt.package.action import Action, GlobMixin
class HelloWorld(GlobMixin, Action):
"""
HelloWorld action appends the string "Hello World" to the end of each file that
matches the glob selector.
"""
def process_file(self, path: str, pkg_path: str, tpl_data: dict):
with open(path, "a") as f:
f.write("Hello World")
with Manifest(name="custom-action") as manifest:
# This selector matches all files with the extension "txt" in the root of a repository.
manifest.add_action(HelloWorld(selector="*.txt"))