def interpolate(self, globals_, default_args, environ): environment = TemplateEnvironment() if globals_: globals_ = self._interpolate(environment, globals_, globals_) if default_args: context = merge_dicts(globals_, default_args) default_args = self._interpolate(environment, default_args, context) if environ: environ = self._interpolate(environment, environ, globals_) return globals_, default_args, environ
### keystone-paste.ini ### template_name = 'keystone-paste.ini' template_dict = { 'context' : { # Subsitutions to be performed 'admin_token' : args.adm_token if args.adm_token is not None else None, }, 'path' : '/etc/keystone/keystone-paste.ini', 'user' : 'root', 'group' : 'root', 'mode' : 0644 } template_list[template_name] = template_dict # Load in the files from the folder template_loader = FileSystemLoader(template_location) template_env = TemplateEnvironment(loader=template_loader, lstrip_blocks=True, trim_blocks=True, keep_trailing_newline=True) # Load in expected templates for template_item in template_list: # Attempt to load the template try: template_list[template_item]['template'] = template_env.get_template( template_item) except TemplateNotFound as e: errormsg = "The template file %s was not found in %s (returned %s)," % ( template_item, template_location, e) errormsg += " terminating..." print errormsg sys.exit( 0
def do_work(self, task): try: output_table, log = [], StringIO() # Process task arguments model_parameters = { field.id: field.to_primitive() for field in task.model.parameters or [] } task_arguments = json.loads(task.arguments) for field_name in set(model_parameters.keys()).intersection( task_arguments.keys()): if model_parameters[field_name].get('type') == 'matrix': task_arguments[field_name] = [ v.split(',') for v in task_arguments[field_name] if v is not None ] # Generate model file from the template model_template = TemplateEnvironment().from_string( task.model.template) model_text = model_template.render(model_name=task.model.name, args=task_arguments) # Run the job with GAMS Python API job = self.GAMS_INSTANCE.add_job_from_string(model_text) job.run(output=log) # Collects the execution data that will be saved as results for symbol in job.out_db: out = dict(name=symbol.name, description=symbol.text, type=symbol.__class__.__name__[4:].lower(), domains=[ d if isinstance(d, basestring) else d.name for d in symbol.domains ]) if isinstance(symbol, GamsEquation): out['subtype'] = symbol.equtype elif isinstance(symbol, GamsParameter): out['values'] = [ dict(elements=rec.keys, value=rec.value) for rec in symbol ] elif isinstance(symbol, GamsSet): out['elements'] = [ x for y in [rec.keys for rec in symbol] for x in y ] elif isinstance(symbol, GamsVariable): out['subtype'] = symbol.vartype if isinstance(symbol, GamsEquation) or isinstance( symbol, GamsVariable): out['values'] = [ dict(elements=record.keys, level=record.level, marginal=record.marginal, scale=record.scale, upper=repr(record.upper) if math.isinf(record.upper) else record.upper, lower=repr(record.lower) if math.isinf(record.lower) else record.lower) for record in symbol ] output_table.append(out) # Clean internal status log_lines = [ line for line in log.getvalue().splitlines(True) if line[:3] not in ['---', '***'] ] # Remove license information license_line = next((line for line, text in enumerate(log_lines) if text.startswith('Licensee: ')), -1) if license_line >= 0: del log_lines[license_line] del log_lines[license_line] if not log_lines[license_line].startswith('\n'): log_lines.insert(license_line, '\n') # Encoding log as Base64 log = b64encode(''.join(log_lines)) # Read output files output_files = {} if task.model.output_options.get('files'): for output_file in task.model.output_options['files']: file_path = path.join(job.workspace.working_directory, output_file) if path.isfile(file_path): with open(file_path, mode='rU', encoding='utf-8') as f: output_files[output_file] = b64encode(f.read()) return dict(log=log, files=output_files, table=output_table) except (TemplateError, GamsException) as error: raise RuntimeError(error.__class__.__name__ + ': ' + error.message)