summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--patternutils/commands/patterncfg.py9
-rw-r--r--patternutils/config.py49
2 files changed, 50 insertions, 8 deletions
diff --git a/patternutils/commands/patterncfg.py b/patternutils/commands/patterncfg.py
index 83168cf..f619196 100644
--- a/patternutils/commands/patterncfg.py
+++ b/patternutils/commands/patterncfg.py
@@ -1,21 +1,22 @@
+import itertools
import os
import os.path
from typing import Optional
import click
-from .. import config
-
try:
from pygments import highlight
from pygments.formatters import TerminalFormatter
- from pygments.lexers import Python3Lexer, DjangoLexer
+ from pygments.lexers import DjangoLexer, Python3Lexer
except ImportError:
has_pygments = False
else:
has_pygments = True
+from .. import config
+
CURRENT_SUPPORTED_PROGRAMS = ("pmatch", "tmv", "targs", "tln")
@@ -26,7 +27,7 @@ def patterncfg() -> None:
@patterncfg.command(name="init", help="initialize config folders")
def init() -> None:
- for dir in CURRENT_SUPPORTED_PROGRAMS:
+ for dir in itertools.chain(CURRENT_SUPPORTED_PROGRAMS, ("globals",)):
os.makedirs(os.path.join(config.CONFIG_PATH, dir), exist_ok=True)
diff --git a/patternutils/config.py b/patternutils/config.py
index 2bce26c..a124644 100644
--- a/patternutils/config.py
+++ b/patternutils/config.py
@@ -1,11 +1,15 @@
import glob
import os.path
+import re
from typing import List, Optional, Tuple
import click
CONFIG_PATH = click.get_app_dir("patternutils")
+REGEX_PROGRAMS = ("pmatch",)
+TEMPLATE_PROGRAMS = ("tmv", "targs", "tln")
+
def load_template(template_name: str, command_name: Optional[str]) -> Tuple[str, str]:
"""Load a template from the config directory."""
@@ -27,7 +31,44 @@ def load_template(template_name: str, command_name: Optional[str]) -> Tuple[str,
def find_templates(template_name: str, command_name: Optional[str] = None) -> List[str]:
- template_glob = "{}.*".format(template_name)
- command_glob = "*" if command_name is None else command_name
- command_glob = os.path.join(CONFIG_PATH, command_glob)
- return glob.glob(os.path.join(command_glob, template_glob))
+
+ regex = os.path.join(
+ CONFIG_PATH, "{path_sub_regex}", r"{template_name}\.{extension}"
+ )
+
+ if command_name is None:
+ # CONFIG_PATH/*/TEMPLATE_NAME.*
+ path_sub_regex = ".*"
+ extension = ".*"
+ elif command_name in REGEX_PROGRAMS:
+ path_sub_regex = "(globals|{command_name})"
+ extension = "regex"
+ elif command_name in TEMPLATE_PROGRAMS:
+ # CONFIG_PATH/globals/TEMPLATE_NAME.jinja2
+ # CONFIG_PATH/globals/TEMPLATE_NAME.python
+ # CONFIG_PATH/COMMAND_NAME/TEMPLATE_NAME.jinja2
+ # CONFIG_PATH/COMMAND_NAME/TEMPLATE_NAME.python
+ path_sub_regex = "(globals|{command_name})"
+ extension = "(jinja2|python)"
+ else:
+ # Fail over if command name is not know
+ # This should normally not happen
+ # CONFIG_PATH/COMMAND_NAME/TEMPLATE_NAME.*
+ path_sub_regex = "{command_name}"
+ extension = ".*"
+
+ # We need to format command_name twice, as it can be used in the
+ # path_sub_regex
+ regex = regex.format(
+ path_sub_regex=path_sub_regex.format(command_name=command_name),
+ command_name=command_name,
+ template_name=template_name,
+ extension=extension,
+ )
+ compiled_regex = re.compile(regex)
+
+ files = filter(
+ compiled_regex.search, glob.iglob(os.path.join(CONFIG_PATH, "*", "*"))
+ )
+
+ return list(files)