diff options
| -rw-r--r-- | patternutils/config.py | 34 | ||||
| -rw-r--r-- | patternutils/types.py | 4 |
2 files changed, 26 insertions, 12 deletions
diff --git a/patternutils/config.py b/patternutils/config.py index 11d6216..785dbb3 100644 --- a/patternutils/config.py +++ b/patternutils/config.py @@ -13,6 +13,7 @@ from .types import ( RegexExtensions, TemplateCommands, TemplateExtensions, + TemplatePrefer, ) CONFIG_PATH = Path(click.get_app_dir("patternutils")) @@ -20,24 +21,31 @@ CONFIG_PATH = Path(click.get_app_dir("patternutils")) @overload def load_template( - template_name: str, command_name: RegexCommands + template_name: str, + command_name: RegexCommands, + prefer: TemplatePrefer = None, ) -> tuple[str, RegexExtensions]: ... @overload def load_template( - template_name: str, command_name: TemplateCommands + template_name: str, + command_name: TemplateCommands, + prefer: TemplatePrefer = None, ) -> tuple[str, TemplateExtensions]: ... def load_template( - template_name: str, command_name: Optional[PredefinedCommands] + template_name: str, + command_name: Optional[PredefinedCommands], + prefer: TemplatePrefer = None, ) -> tuple[str, PredefinedExtensions]: """Load a template from the config directory. Args: template_name: The name of the template file (without extension). command_name: The command requesting the template. + prefer: Set preference for global directory Returns: A tuple containing the template content as a string and its engine type. @@ -48,7 +56,7 @@ def load_template( ValueError: If the extracted template extension is invalid. """ - files = find_templates(template_name, command_name) + files = find_templates(template_name, command_name, prefer) if not files: raise FileNotFoundError(f"No template found for '{template_name}'.") @@ -73,7 +81,9 @@ def load_template( def find_templates( - template_name: str, command_name: Optional[PredefinedCommands] = None + template_name: str, + command_name: Optional[PredefinedCommands] = None, + prefer: TemplatePrefer = None, ) -> list[str]: """Finds matching template files based on the template name and command. @@ -88,12 +98,14 @@ def find_templates( ValueError: If `command_name` is invalid. """ - if command_name is None: - # CONFIG_PATH/*/TEMPLATE_NAME.* - path_sub_regex = ".*" - extension = ".*" - else: - path_sub_regex = f"(globals|{command_name})" + extension = ".*" + path_sub_regex = "globals" if prefer == "global" else ".*" + + if command_name is not None: + if prefer == "command": + path_sub_regex = f"{command_name}" + elif prefer is None: + path_sub_regex = f"(globals|{command_name})" if command_name in get_args(RegexCommands): extensions = get_args(RegexExtensions) diff --git a/patternutils/types.py b/patternutils/types.py index ad27ebf..6b2a5ac 100644 --- a/patternutils/types.py +++ b/patternutils/types.py @@ -1,4 +1,4 @@ -from typing import Literal +from typing import Literal, Optional PlainCommands = Literal["pmediainfo"] @@ -12,3 +12,5 @@ PredefinedCommands = Literal[RegexCommands, TemplateCommands] PredefinedExtensions = Literal[TemplateExtensions, RegexExtensions] CommandNames = Literal[PlainCommands, TemplateCommands, RegexCommands] + +TemplatePrefer = Optional[Literal["global", "command"]] |
