def testFile(self): if IGNORE_TEST: return dir_path = os.path.dirname(os.path.realpath(__file__)) parent_path = os.path.dirname(dir_path) src_path = os.path.join(parent_path, "Example") src_path = os.path.join(src_path, "Demo") src_path = os.path.join(src_path, "sample.tmpl") TemplateProcessor.processFile(src_path, "/tmp/out.mdl")
def _testProcessCommand(self, template, is_processed, is_command, processor=None): """ Evaluates the processing of a template line :return TemplateProcessor: """ if processor is None: processor = TemplateProcessor(template) line, line_type = processor._extractor.do() self.assertEqual(processor._processCommand(), is_processed) is_not_none = processor._command is not None self.assertEqual(is_command, line_type == LINE_COMMAND) return processor
def _setupModel(self): """ Processes the template model reads: _template writes: _template_model, _antimony_model """ lines = [] with open(self._template) as file: lines.append(file.read()) self._template_model = "\n".join(lines) names = ['B', 'R', 'T2', 'Y', 'Z'] values = [B_CONC, R_CONC, T2_CONC, Y_CONC, Z_CONC] pairs = zip(names, values) suffixes = ["%s = %s" % (n, v) for n, v in pairs] for suffix in suffixes: self._template_model += suffix + "\n" processor = TemplateProcessor(self._template_model) self._antimony_model = processor.do()
def testDo2(self): template = ''' %s DefineVariables Begin %s api.addDefinitions({'s': ['a', 'b'], 't': ['x', 'y'] }) %s DefineVariables End %s J{s}{t}: S1{s}{t} -> S2{s}{t}; k1*S1{s}{t}''' % \ (COMMAND_START, COMMAND_END, COMMAND_START, COMMAND_END) self.processor = TemplateProcessor(template) lines = self.processor.do() actual_lines = 1 + lines.count('\n') expected_lines = 4 + template.count('\n') self.assertEqual(actual_lines, expected_lines) self.assertTrue("addDefinitions" in lines) substitutions = ["Jax", "Jbx", "Jay", "Jby"] result = all([s in lines for s in substitutions]) self.assertTrue(result)
def _testExpand(self, template, variable): """ :param str template: template to expand :param str variable: variable to check """ self.processor = TemplateProcessor(template) lines = self.processor.do() try: for val in DEFINITIONS[variable]: str_val = "J%s" % str(val) if not str_val in lines: import pdb pdb.set_trace() self.assertTrue(str_val in lines) except Exception as exp: import pdb pdb.set_trace() pass
def ep_render_template(library, store, template, output=None): """Render a template using the given store from the gien library. If output is not provided, the path identified in the template will be used as the output target. Arguments: library: A open file pointer to the library. store: The name of the keystore. template: A open file pointer to the template to render. output: A optional open file pointer to write the rendered template to. """ logging.debug("Loading library: {}".format(library.name)) lib = Library(fp=library) logging.debug("Loading keystore: {}".format(store)) ks = lib.get_keystore(store) logging.debug("Loading template: {}".format(template.name)) tmpl = TemplateProcessor(template) if output: logging.info("Rendering template with override: {} => {}".format( template.name, output)) tmpl.render_fp(ks, output) else: logging.info("Rendering template: {} => {}".format( template.name, tmpl.target_path)) tmpl.render_file(ks)
def _generate_cards(src_word: str, pos: str, word_rank: int, subsections: List[wtp.Section]) -> List[Card]: """ Extract relevant definitions from a part-of-speech section and format them appropriately for further processing. """ # Detect remains of templates whose definitions should be deleted completely p = re.compile(r"^.*\$\$[^\{\}]*\$\$.*$") entries = [] for pos_section in subsections: # Retrieve all definitions from the section items = [ wtp.WikiText(def_item) for def_list in pos_section.get_lists() for def_item in def_list.items ] # Format definition as plain text formatted_items = [ item.plain_text(replace_templates=False).strip() for item in items ] # Evaluate template expressions processed_items = [ tp.process_templates(formatted_item) for formatted_item in formatted_items ] # Clean up unwanted definitions cleaned_items = [ processed_item.strip() for processed_item in processed_items if not p.match(processed_item) ] # Allow for differentiated styling of remarks in parentheses for readability finalized_items = [ CardDataGenerator._add_parenthesed_styling(cleaned_item) for cleaned_item in cleaned_items ] # Generate Card true_rank = word_rank if pos_section.title.lower() == pos.lower( ) else 1_000_000 + word_rank entries.append( Card(src_word, pos_section.title, true_rank, finalized_items)) return entries
def __init__(self, *args, **kwargs): TemplateProcessor.__init__(self, *args, **kwargs) self._getting_deps = False self.deps = None
from configuration_reader import JsonConfigReader, JsonConfigValidator from enums.terraform_mode import TerraformMode from inventory.inventory_creator import InventoryCreator from inventory.inventory_writer import InventoryWriter from process_manager import ProcessManager from template_processor import TemplateProcessor cwd = os.getcwd() args = parse() config = JsonConfigReader([JsonConfigValidator()]).read(file_name=args.config) terraform_directory = args.terraform_directory ansible_directory = args.ansible_directory mode = TerraformMode.APPLY.value if args.mode == 'apply' else TerraformMode.DESTROY.value output_parameters_path = os.path.join(terraform_directory, 'parameters.tfvars') template_processor = TemplateProcessor('utils/parameters.j2', output_parameters_path) template_processor.process(config) status_code = ProcessManager('terraform').with_cwd(args.terraform_directory)\ .with_args(mode, '-var-file="parameters.tfvars"', '-auto-approve')\ .start(lambda data: print(data)).wait() print(f'Terraform status code {status_code}') if status_code == 0 and mode == TerraformMode.APPLY.value: configuration = Configuration(config) tenant_id = configuration.azure.tenant_id client_id = configuration.azure.client_id client_secret = configuration.azure.client_secret inventory_creator = InventoryCreator(AzureHttpClient(), configuration) inventory = inventory_creator.create(
def setUp(self): self.processor = TemplateProcessor(TEMPLATE_STG2)
class TestTemplateProcessor(unittest.TestCase): def setUp(self): self.processor = TemplateProcessor(TEMPLATE_STG2) def testConstructor(self): if IGNORE_TEST: return self.assertIsNotNone(self.processor._extractor) def _testExpand(self, template, variable): """ :param str template: template to expand :param str variable: variable to check """ self.processor = TemplateProcessor(template) lines = self.processor.do() try: for val in DEFINITIONS[variable]: str_val = "J%s" % str(val) if not str_val in lines: import pdb pdb.set_trace() self.assertTrue(str_val in lines) except Exception as exp: import pdb pdb.set_trace() pass def testDo(self): if IGNORE_TEST: return self._testExpand(TEMPLATE_STG2, 'a') self._testExpand(TEMPLATE_STG3, 'c') self._testExpand(TEMPLATE_STG4, 'm') def testDo2(self): template = ''' %s DefineVariables Begin %s api.addDefinitions({'s': ['a', 'b'], 't': ['x', 'y'] }) %s DefineVariables End %s J{s}{t}: S1{s}{t} -> S2{s}{t}; k1*S1{s}{t}''' % \ (COMMAND_START, COMMAND_END, COMMAND_START, COMMAND_END) self.processor = TemplateProcessor(template) lines = self.processor.do() actual_lines = 1 + lines.count('\n') expected_lines = 4 + template.count('\n') self.assertEqual(actual_lines, expected_lines) self.assertTrue("addDefinitions" in lines) substitutions = ["Jax", "Jbx", "Jay", "Jby"] result = all([s in lines for s in substitutions]) self.assertTrue(result) def testExpandErrorInDefinition(self): if IGNORE_TEST: return with self.assertRaises(ValueError): processor = TemplateProcessor(TEMPLATE_BAD) result = processor.do() def testNoDefinintion(self): if IGNORE_TEST: return self.processor = TemplateProcessor(TEMPLATE_NO_DEFINITION) with self.assertRaises(ValueError): lines = self.processor.do() def testFile(self): if IGNORE_TEST: return dir_path = os.path.dirname(os.path.realpath(__file__)) parent_path = os.path.dirname(dir_path) src_path = os.path.join(parent_path, "Example") src_path = os.path.join(src_path, "Demo") src_path = os.path.join(src_path, "sample.tmpl") TemplateProcessor.processFile(src_path, "/tmp/out.mdl") def _testProcessCommand(self, template, is_processed, is_command, processor=None): """ Evaluates the processing of a template line :return TemplateProcessor: """ if processor is None: processor = TemplateProcessor(template) line, line_type = processor._extractor.do() self.assertEqual(processor._processCommand(), is_processed) is_not_none = processor._command is not None self.assertEqual(is_command, line_type == LINE_COMMAND) return processor def testProcessCommand(self): if IGNORE_TEST: return self._testProcessCommand("line 1", False, False) self._testProcessCommand( "%s DefineVariables Begin %s" % (COMMAND_START, COMMAND_END), True, True) line = '''%s DefineVariables Begin %s a = 32 %s DefineVariables End %s aa{a} + bb{d}''' \ % (COMMAND_START, COMMAND_END, COMMAND_START, COMMAND_END) processor = self._testProcessCommand(line, True, True) self._testProcessCommand(line, True, False, processor=processor) self._testProcessCommand(line, True, True, processor=processor) self._testProcessCommand(line, False, False, processor=processor)
def testNoDefinintion(self): if IGNORE_TEST: return self.processor = TemplateProcessor(TEMPLATE_NO_DEFINITION) with self.assertRaises(ValueError): lines = self.processor.do()
def testExpandErrorInDefinition(self): if IGNORE_TEST: return with self.assertRaises(ValueError): processor = TemplateProcessor(TEMPLATE_BAD) result = processor.do()
""" Running the template pre-processor standalone. Input: Templated Antimony model (stdin) Output: Expanded Antimony model (stdout) """ import fileinput import os import sys directory = os.path.dirname(os.path.abspath(__file__)) path = os.path.join(directory, "TemplateSB") sys.path.append(path) from template_processor import TemplateProcessor template_stg = '' for line in fileinput.input(): template_stg += "\n" + line processor = TemplateProcessor(template_stg) expanded_stg = processor.do() sys.stdout.write(expanded_stg)