Skip to content

Commit aad44b3

Browse files
committed
✨ support cli option -e, user defined extension. resolves #257
1 parent f7f0809 commit aad44b3

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

moban/constants.py

+2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
LABEL_VERSION = "version"
3838
LABEL_GROUP = "group"
3939
LABEL_DEFINE = "define"
40+
LABEL_EXTENSION = "extension"
4041
CLI_DICT = "user_dict"
42+
EXTENSION_DICT = "user_extensions"
4143

4244
DEFAULT_CONFIGURATION_DIRNAME = ".%s.cd" % PROGRAM_NAME
4345
DEFAULT_TEMPLATE_DIRNAME = ".%s.td" % PROGRAM_NAME

moban/core/moban_factory.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ def __init__(self):
2222
self.options_registry = {}
2323

2424
def register_extensions(self, extensions):
25-
self.extensions.update(extensions)
25+
for template_type in extensions.keys():
26+
if template_type in self.extensions:
27+
self.extensions[template_type] = self.extensions[
28+
template_type
29+
].union(extensions[template_type])
30+
else:
31+
self.extensions[template_type] = extensions[template_type]
2632

2733
def register_options(self, template_types):
2834
# need the value of 'template_types'

moban/main.py

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import logging
1313
import argparse
1414
import logging.config
15+
from collections import defaultdict
1516

1617
from moban import (
1718
core,
@@ -41,6 +42,7 @@ def main():
4142
options[constants.CLI_DICT] = handle_custom_variables(
4243
options.pop(constants.LABEL_DEFINE)
4344
)
45+
handle_custom_variables(options.pop(constants.LABEL_EXTENSION))
4446
OPTIONS.update(options)
4547
handle_verbose(options[constants.LABEL_VERBOSE])
4648

@@ -155,6 +157,12 @@ def create_parser():
155157
nargs="+",
156158
help="to take a list of VAR=VALUEs",
157159
)
160+
parser.add_argument(
161+
"-e",
162+
"--%s" % constants.LABEL_EXTENSION,
163+
nargs="+",
164+
help="to add an extension to TEMPLATE_TYPE=EXTENSION_NAME",
165+
)
158166
return parser
159167

160168

@@ -265,6 +273,16 @@ def handle_custom_variables(list_of_definitions):
265273
return custom_data
266274

267275

276+
def handle_custom_extensions(list_of_definitions):
277+
user_extensions = defaultdict(set)
278+
if list_of_definitions:
279+
for definition in list_of_definitions:
280+
key, value = definition.split("=")
281+
user_extensions[key].append(value)
282+
283+
core.ENGINES.register_options(user_extensions)
284+
285+
268286
def handle_verbose(verbose_level):
269287
if verbose_level > len(LOG_LEVEL):
270288
verbose_level = 3

0 commit comments

Comments
 (0)