def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) for hive in self.hives: try: for vals in Registry.Registry(hive).open("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\TypedPaths").values(): last_write = Registry.Registry(hive).open("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\TypedPaths").timestamp() key = Registry.Registry(hive).open("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\TypedPaths").name() value = vals.name() data = vals.value() if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key=key, \ value=value, \ data=data) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key=key, \ value=value, \ data=data) + "\n") except Registry.RegistryKeyNotFoundException: continue
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) dict = {} #for hive in self.hives: dict.update(self.processKeys()) #print dict for key, val in dict.iteritems(): last_write = val[0] key_name = key stub_path = val[1] if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ stub_path=stub_path) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ stub_path=stub_path) + "\n")
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) for hive in self.hives: for entry in self.getUserAssist(hive): last_write = entry[0] sub_key = entry[1] runcount = entry[2] windate = entry[3] data = entry[4] if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ sub_key=sub_key, \ runcount=runcount, \ windate=windate, \ data=data) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ sub_key=sub_key, \ runcount=runcount, \ windate=windate, \ data=data) + "\n")
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) usbstor_dict = dict() usb_keys = ["USBSTOR", "USB"] for hive in self.hives: current = HelperFunctions(hive).CurrentControlSet() for k in usb_keys: try: key = Registry.Registry(hive).open('%s\\Enum\\%s' % (current, k)) for sks in key.subkeys(): key_lastwrite = sks.timestamp() #SYSTEM\ControlSet001\Enum\USBSTOR\Disk&Ven_&Prod_USB_DISK_20X&Rev_PMAP device_class_id = sks.name() for uniqueSN in key.subkey(device_class_id).subkeys(): unique_sn_lastwrite = uniqueSN.timestamp() try: #Enum\USBSTOR\Disk&Ven_GoPro&Prod_Storage&Rev_1.0\123456789ABC&0\FriendlyName friendly_name = uniqueSN.value("FriendlyName").value() except Registry.RegistryValueNotFoundException: friendly_name = "None" usbstor_dict.setdefault(device_class_id, []).append((key_lastwrite, \ unique_sn_lastwrite, \ uniqueSN.name(), \ friendly_name)) except Registry.RegistryKeyNotFoundException as e: continue for key, val in usbstor_dict.iteritems(): for vals in val: key_lastwrite = vals[0] unique_sn_lastwrite = vals[1] unique_sn = vals[2] friendly_name = vals[3] if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(key_lastwrite=key_lastwrite, \ key=key, \ friendly_name=friendly_name, \ unique_sn_lastwrite=unique_sn_lastwrite, \ unique_sn=unique_sn) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(key_lastwrite=key_lastwrite, \ key=key, \ friendly_name=friendly_name, \ unique_sn_lastwrite=unique_sn_lastwrite, \ unique_sn=unique_sn) + "\n")
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) terminal_server_list = [] for hive in self.hives: current = HelperFunctions(hive).CurrentControlSet() key = Registry.Registry(hive).open('%s\\Control\\Terminal Server' % (current)) try: terminal_server_list.append((key.subkey("WinStations").subkey("RDP-Tcp").timestamp(), \ key.subkey("WinStations").subkey("RDP-Tcp").value("UserAuthentication").name(), \ key.subkey("WinStations").subkey("RDP-Tcp").value("UserAuthentication").value())) except Registry.RegistryValueNotFoundException: continue try: terminal_server_list.append((key.subkey("Wds").subkey("rdpwd").timestamp(), \ key.subkey("Wds").subkey("rdpwd").value("StartupPrograms").name(), \ key.subkey("Wds").subkey("rdpwd").value("StartupPrograms").value())) except Registry.RegistryValueNotFoundException: continue try: last_write = key.timestamp() terminal_server_list.append((last_write, \ key.value("fDenyTSConnections").name(), key.value("fDenyTSConnections").value())) terminal_server_list.append((last_write, \ key.value("DeleteTempDirsOnExit").name(), key.value("DeleteTempDirsOnExit").value())) terminal_server_list.append((last_write, \ key.value("fSingleSessionPerUser").name(), key.value("fSingleSessionPerUser").value())) except Registry.RegistryValueNotFoundException: continue for entry in terminal_server_list: key_name = key.name() last_write = entry[0] value = entry[1] data = entry[2] if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ value=value, \ data=data) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ value=value, \ data=data) + "\n")
def rec(reg, depth=None): key_path = "\t" * depth + reg.path() if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(key_path=key_path) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(key_path=key_path) + "\n") for subkey in reg.subkeys(): rec(subkey, depth + 1)
def build(path, compress=False): """Builds the task presenter for the project located at the specified path.""" project_dir = os.path.realpath(path) json = get_project_json(os.path.join(project_dir, "project.json")) if not is_valid_project_json(json): print "Error! The 'project.json' file is not valid." sys.exit(1) template = Environment(loader=FileSystemLoader(searchpath=LAYOUT_DIR)).get_template("base.html") short_name = json["short_name"].strip() why_ = json["why"].strip() questions_ = json["questions"] get_next_question_ = get_questionnaire_flow_handler(questions_) # Assign the help to its corresponding question. help = get_project_help(os.path.join(project_dir, "help")) if len(help) > 0: for question in questions_: key = str(question["id"]) try: question[u"help"] = help[key] except: pass # Build the template and tutorial. js_ = get_template_js(compress) # Collects JS common to the whole template css_ = get_template_css(compress) # Collects CSS common to the whole template js_ += get_project_js(project_dir, compress) css_ += get_project_css(project_dir, compress) with open(os.path.join(project_dir, "template.html"), "w") as output: html = template.render(is_tutorial=False, questions=questions_, css=css_, js=js_, slug=short_name, why=why_, get_next_question=get_next_question_) # if compress: # html = htmlmin.minify(html, remove_comments=True, remove_empty_space=True) output.write(html.encode("UTF-8")) # Build the tutorial. with open(os.path.join(project_dir, "tutorial.html"), "w") as output: tutorial_ = get_project_tutorial(project_dir) if tutorial_ is not None: html = "" html = template.render(is_tutorial=True, questions=questions_, css=css_, js=js_, slug=short_name, why=why_, get_next_question=get_next_question_, tutorial=tutorial_) # if compress: # html = htmlmin.minify(html, remove_comments=True, remove_empty_space=True) output.write(html.encode("UTF-8"))
class PushBulletPlugin(Plugin): URL = 'https://api.pushbullet.com/v2/pushes' def __init__(self, **kwargs): super(PushBulletPlugin, self).__init__(**kwargs) self.agent = requests.Session() self.agent.headers.update({'Content-Type': 'application/json'}) path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '_pushbullet') self.template = Environment( loader=FileSystemLoader(path)).get_template('template.txt') @asyncio.coroutine def execute(self, account, history, params=[]): for param in params: yield from self._execute(account, history, param) @asyncio.coroutine def _execute(self, account, history, param): p = param param = self.options[param] user, target = param.get('user'), param.get('target') title = param.get('title').format(**self.format_args(account, history)) token = param.get('token') content = self.template.render(**history.as_dict()) data = { 'type': 'note', 'title': title, 'body': content, } r = self.agent.post(self.URL, data=json.dumps(data), headers={'Authorization': 'Bearer {}'.format(token)}) if r.status_code == 200: logger.info('"{}" 계좌의 내역을 성공적으로 PushBullet으로 발송했습니다. ({})'.format( account.account, p))
def serialize(self, format='json-ld', with_mime=False, template=None, prefix=None, fields=None, **kwargs): js = self.jsonld(prefix=prefix, **kwargs) if template is not None: rtemplate = Environment(loader=BaseLoader).from_string(template) content = rtemplate.render(**self) mimetype = 'text' elif fields is not None: # Emulate field selection by constructing a template content = json.dumps(jmespath.search(fields, js)) mimetype = 'text' elif format == 'json-ld': content = json.dumps(js, indent=2, sort_keys=True) mimetype = "application/json" elif format in ['turtle', 'ntriples']: content = json.dumps(js, indent=2, sort_keys=True) logger.debug(js) context = [self._context, {'prefix': prefix, '@base': prefix}] g = Graph().parse( data=content, format='json-ld', prefix=prefix, context=context) logger.debug( 'Parsing with prefix: {}'.format(kwargs.get('prefix'))) content = g.serialize(format=format, prefix=prefix).decode('utf-8') mimetype = 'text/{}'.format(format) else: raise Error('Unknown outformat: {}'.format(format)) if with_mime: return content, mimetype else: return content
def send(recipient, subject, title, content, button_link, button_caption): email_template = Environment(loader=FileSystemLoader(cfg['template_dir'])).get_template('email.html') html = transform(email_template.render( body=plain_text_to_html(content), title=title, button_caption=button_caption, button_link=button_link )) # TODO - remove this recipient = '*****@*****.**' # sender = MAIL_SENDER sender = '*****@*****.**' data = { "from": sender, "to": recipient, "subject": subject, "html": html, "text": "" } requests.post( MAIL_SERVER, auth=MAIL_AUTH, data=data )
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) dict = {} for hive in self.hives: dict.update(self.processServices(hive)) for key, val in dict.iteritems(): last_write = val[0] key_name = key image_path = val[1] type_name = val[2] display_name = val[3] start_type = val[4] service_dll = val[5] if self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, key_name=key_name, \ image_path=image_path, type_name=type_name, \ display_name=display_name, start_type=start_type, \ service_dll=service_dll) + "\n") elif self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, key_name=key_name, \ image_path=image_path, type_name=type_name, \ display_name=display_name, start_type=start_type, \ service_dll=service_dll) + "\n")
def output_blob(self): """Output to the template after all messages.""" if self.current_file_linted is None: # There were no files checked. print('[ERROR]: No files were checked.') return template_f = self.linter.config.pyta_template_file template = Environment(loader=FileSystemLoader(TEMPLATES_DIR)).get_template(template_f) # Embed resources so the output html can go anywhere, independent of assets. # with open(os.path.join(TEMPLATES_DIR, 'pyta_logo_markdown.png'), 'rb+') as image_file: # # Encode img binary to base64 (+33% size), decode to remove the "b'" # pyta_logo_base64_encoded = b64encode(image_file.read()).decode() # Date/time (24 hour time) format: # Generated: ShortDay. ShortMonth. PaddedDay LongYear, Hour:Min:Sec dt = str(datetime.now().strftime('%a. %b. %d %Y, %I:%M:%S %p')) output_path = os.path.join(os.getcwd(), self.linter.config.pyta_output_file) with open(output_path, 'w') as f: f.write(template.render(date_time=dt, # pyta_logo=pyta_logo_base64_encoded, reporter=self)) print('Opening your report in a browser...') output_url = 'file:///{}'.format(output_path) webbrowser.open(output_url, new=2)
def send(recipient, subject, title, content, button_link, button_caption): email_template = Environment(loader=FileSystemLoader(cfg['template_dir'])).get_template('email.html') html = transform(email_template.render( body=plain_text_to_html(content), title=title, button_caption=button_caption, button_link=button_link )) sender = MAIL_SENDER data = { "from": sender, "to": recipient, "subject": subject, "html": html, "text": "Please enable HTML emails." } requests.post( MAIL_SERVER, auth=MAIL_AUTH, data=data )
def main(): if len(sys.argv) != 3: print "Usage: %s <docs dir> <output dir>" % (sys.argv[0]) return 1 # Get a list of documents and parse their content. documents = [] for doc in glob.glob(os.path.join(sys.argv[1], '*.txt')): # Assume that the first line is the title, and that there are # two lines after it before the actual content. f = open(doc, 'r') title = f.readline().strip() f.readline() f.readline() # Parse the document. html = markdown(f.read()).encode(sys.stdout.encoding or "utf-8", 'xmlcharrefreplace') f.close() # Work out the output file name. name = os.path.splitext(os.path.basename(doc))[0] + '.html' # Add it to the list. documents.append((name, title, html)) # Create the template loader. tpl = Environment(loader=FileSystemLoader(os.path.join(sys.argv[1], 'markdown'))).get_template('template.html') # For each document, write the template. for doc in documents: f = open(os.path.join(sys.argv[2], doc[0]), 'w') f.write(tpl.render(name=doc[0], title=doc[1], content=doc[2], docs=documents)) f.close()
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) for hive in self.hives: try: wordwheel = Registry.Registry(hive).open("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\WordWheelQuery") key = wordwheel.name() last_write = wordwheel.timestamp() mruorder = wordwheel.value("MRUListEx").value() for entry in struct.unpack("%dI" % (len(mruorder)/4), mruorder): for words in wordwheel.values(): if words.name() == str(entry): value = words.name() data = words.value() else: continue if self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key=key, \ value=value, \ data=data) + "\n") elif self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key=key, \ value=value, \ data=data) + "\n") except Registry.RegistryKeyNotFoundException as e: pass
def get_document(uri, params=None): """ Attempt to fetch user data from URL or file. And render, replacing parameters """ if uri is None: raise ValueError("Document URI cannot be None") try: urlparse(uri) data = fetch_from_url(uri) except requests.exceptions.MissingSchema: # Not a url. possibly a file. uri = os.path.expanduser(uri) uri = os.path.abspath(uri) if os.path.isabs(uri): try: data = open(uri, 'r').read() except IOError as err: err_msg = "Failed to read from file: {}".format(err) raise DataError(err_msg) else: # Not url nor file. err_msg = "Unknown UserData source: {}".format(uri) raise DataError(err_msg) except requests.exceptions.ConnectionError as err: err_msg = "Failed to read from URL: {}".format(err) raise DataError(err_msg) data_tpl = Environment().from_string(data) return data_tpl.render(params)
def html_report(datasets, mem_type='WorkingSetPrivate', mem_unit='KB',\ output_path='out.html', pools_to_monitor=None, top_txt=None, bot_txt=None): """ produces an html report datasets: the data to plot along with labels mem_type: (optional) what type of memory you'd like to monitor mem_unit: (optional) the memory units output_path: where to save the html report """ if pools_to_monitor: datasets = dict((k,v) for k, v in datasets.items() if k in pools_to_monitor) datasets = dict((str(k),v) for k, v in datasets.items()) for val in datasets.values(): if 'label' in val: val['label'] = str(val['label']) context = { 'datasets': datasets, 'xlabel': 'time (seconds)', 'ylabel': '%s (%s)' % (mem_type, mem_unit), 'top_txt': top_txt, 'bot_txt': bot_txt } source = open(TEMPLATE_FILE, 'r').read() jinja_template = JinjaEnvironment().from_string(source) out = jinja_template.render(context) out_file = open(output_path, 'w') out_file.write(out) out_file.close() print("The html report is saved to %s" % os.path.realpath(output_path))
def _write_template(dst_filepath, template_package, template_dir, template_name, template_values, dry_run): template = Environment(loader=PackageLoader(template_package, template_dir)).get_template(template_name) content = template.render(**template_values) dirpath(os.path.dirname(dst_filepath), dry_run=dry_run) valid("File %(filename)s written.", filename=dst_filepath) if not dry_run: with codecs.open(dst_filepath, "w", encoding="utf-8") as fd: fd.write(content)
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) for hive in self.hives: for entry in self.processSysinternals(hive): last_write = entry[0] key_name = entry[1] if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name) + "\n")
def cli(ctx, template, values): cfg_dir = ctx.config.base template_file = os.path.join(cfg_dir, template) f = _open_file(template_file, 'r') jtemplate = Environment().from_string(f.read()) values = json.load(values) rendered = jtemplate.render(**values) click.echo(rendered)
def get_jinja_config(self, opt, template=None): def load_template(path): if os.path.exists(path): return open(path).read() loader = ChoiceLoader([ FunctionLoader(load_template), PackageLoader('isotoma.recipe.apache', 'templates'), ]) template = Environment(loader=loader).get_template(template or self.options['template']) return template.render(opt)
def run(self): node = nodes.Element() node.document = self.state.document env = self.state.document.settings.env docname = env.docname template_filename = self.options.get("file") debug_template = self.options.get("debug") cxt = (self.app.config.jinja_contexts[self.arguments[0]].copy() if self.arguments else {}) cxt["options"] = { "header_char": self.options.get("header_char") } if template_filename: if debug_template is not None: print('') print('********** Begin Jinja Debug Output: Template Before Processing **********') print('********** From {} **********'.format(docname)) reference_uri = directives.uri(os.path.join('source', template_filename)) template_path = urllib.url2pathname(reference_uri) encoded_path = template_path.encode(sys.getfilesystemencoding()) imagerealpath = os.path.abspath(encoded_path) with codecs.open(imagerealpath, encoding='utf-8') as f: print(f.read()) print('********** End Jinja Debug Output: Template Before Processing **********') print('') tpl = Environment( loader=FileSystemLoader( self.app.config.jinja_base, followlinks=True) ).get_template(template_filename) else: if debug_template is not None: print('') print('********** Begin Jinja Debug Output: Template Before Processing **********') print('********** From {} **********'.format(docname)) print('\n'.join(self.content)) print('********** End Jinja Debug Output: Template Before Processing **********') print('') tpl = Environment( loader=FileSystemLoader( self.app.config.jinja_base, followlinks=True) ).from_string('\n'.join(self.content)) new_content = tpl.render(**cxt) if debug_template is not None: print('') print('********** Begin Jinja Debug Output: Template After Processing **********') print(new_content) print('********** End Jinja Debug Output: Template After Processing **********') print('') new_content = StringList(new_content.splitlines(), source='') sphinx.util.nested_parse_with_titles( self.state, new_content, node) return node.children
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) recentdocs_root = [] for hive in self.hives: recentdocs = Registry.Registry(hive).open("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs") key = recentdocs.name() last_write = recentdocs.timestamp() mruorder = recentdocs.value("MRUListEx").value() for entry in struct.unpack("%dI" % (len(mruorder)/4), mruorder): for docs in recentdocs.values(): if docs.name() == str(entry): recentdocs_root.append((recentdocs.timestamp(), key, "RootMRU", entry, docs.value().split('\x00\x00')[0])) else: continue for subkeys in Registry.Registry(hive).open("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RecentDocs").subkeys(): mruorder = subkeys.value("MRUListEx").value() for values in subkeys.values(): for entry in struct.unpack("%dI" % (len(mruorder)/4), mruorder): if str(values.name()) == str(entry): recentdocs_root.append((subkeys.timestamp(), key, subkeys.name(), values.name(), values.value().split('\x00\x00')[0])) else: continue for entry in recentdocs_root: last_write = entry[0] key_name = entry[1] key = entry[2] value = entry[3] data = entry[4] if self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ key=key, \ value=value, \ data=data) + "\n") elif self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ key=key, \ value=value, \ data=data) + "\n")
def init_str(images): init_template = textwrap.dedent( """ domains: {% for vm_name, template in images.viewitems() %} {{ vm_name }}: memory: 1024 nics: - net: net-02 - net: net-01 disks: - template_name: {{ template }} type: template name: root dev: sda format: qcow2 metadata: {{ vm_name }}: {{ vm_name }} artifacts: - /should/not/exist - /root/custom - /var/log - /etc/hosts - /etc/resolv.conf - /etc/sysconfig - /etc/NetworkManager - /root/virt-sysprep-firstboot.log - /root/extract-{{ vm_name }}-dead - /root/extract-{{ vm_name }}-normal groups: group{{ loop.index % 2 }} {% endfor %} nets: net-01: type: nat dhcp: start: 100 end: 254 management: true dns_domain_name: lago.local net-02: type: nat gw: 192.168.210.4 dhcp: start: 100 end: 254 """ ) template = Environment(loader=BaseLoader()).from_string(init_template) return template.render(images=images)
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) winlogon_list = [] for hive in self.hives: key = Registry.Registry(hive).open("Microsoft\\Windows NT\\CurrentVersion\\Winlogon") last_write = key.timestamp() try: winlogon_list.append((key.value("Shell").name(), key.value("Shell").value())) winlogon_list.append((key.value("Userinit").name(), key.value("Userinit").value())) winlogon_list.append((key.value("Taskman").name(), key.value("Taskman").value())) except Registry.RegistryValueNotFoundException: continue for entry in winlogon_list: last_write key_name = key.name() value = entry[0] data = entry[1] if self.format is not None: template = Environment().from_string(self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ value=value, \ data=data) + "\n") elif self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key_name=key_name, \ value=value, \ data=data) + "\n")
def get_html(doc, doclist, print_format=None): from jinja2 import Environment if isinstance(doc, basestring) and isinstance(doclist, basestring): bean = webnotes.bean(doc, doclist) doc = bean.doc doclist = bean.doclist template = Environment().from_string(get_print_format_name(doc.doctype, print_format or webnotes.form_dict.format)) doctype = webnotes.get_doctype(doc.doctype) args = {"doc": doc, "doclist": doclist, "doctype": doctype, "webnotes": webnotes, "utils": webnotes.utils} html = template.render(args) return html
def write_jinja_config(self, opt): """ Write the config out, using the jinja2 templating method """ dirname, basename = os.path.split(self.options['template']) loader = ChoiceLoader([ FileSystemLoader(dirname), PackageLoader('isotoma.recipe.apache', 'templates'), ]) template = Environment(loader=loader).get_template(basename) rendered = template.render(opt) cfgfilename = self.options['configfile'] open(cfgfilename, "w").write(rendered)
def run_task(self, fw_spec): if self.get("use_global_spec"): self._load_params(fw_spec) else: self._load_params(self) with open(self.template_file) as f: t = Environment(loader=FileSystemLoader(self.template_dir), autoescape=True).from_string(f.read()) output = t.render(self.context) write_mode = 'w+' if self.append_file else 'w' with open(self.output_file, write_mode) as of: of.write(output)
def render(self, template, context, ordered=True): """ Performs the rendering of template and context data using Jinja2. :param template: Full path to the Jinja2 template :param context: A dictionary of variables to be rendered againt the template """ c = self.parse_json_yaml(context, ordered=ordered) t = Environment(loader=BaseLoader).from_string(template) return t.render(c)
def get_html(doc, doclist): from jinja2 import Environment from core.doctype.print_format.print_format import get_print_format template = Environment().from_string(get_print_format(doc.doctype, webnotes.form_dict.format)) doctype = webnotes.get_doctype(doc.doctype) args = { "doc": doc, "doclist": doclist, "doctype": doctype, "webnotes": webnotes, "utils": webnotes.utils } html = template.render(args) return html
def render_product_page(product: dto.ProductDescription) -> str: env = Environment().from_string(source=PRODUCT_TEMPLATE) result = env.render( name=product.name, header=product.header, summary=product.summary, features_header=product.features_header, features=product.features, standard_accessory_header=product.standard_accessory_header, standard_accessory=product.standard_accessory, detailed_description_header=product.detailed_description_header, detailed_description=product.detailed_description, optional_accessory_header=product.optional_accessory_header, optional_accessory=product.optional_accessory, technical_specification_header=product.technical_specification_header, technical_specification_dict=product.technical_specification_dict, ) return result
def render(self, template, context, ordered=True): """ Performs the rendering of template and context data using Jinja2. :param template: Full path to the Jinja2 template :param context: A dictionary of variables to be rendered againt the template """ # setting ordered=False may be problematic, but it is required until # ansible supports OrderedDict in templates, which can't happen until # it stops supporting python 2.6 c = self.parse_json_yaml(context, ordered=False) t = Environment(loader=BaseLoader).from_string(str(template)) return t.render(c)
def render_string(self, template_name, **kargs): """docstring for render_string""" template = Environment(loader=FileSystemLoader( os.path.join(os.path.dirname(__file__), 'views'))).get_template( template_name) host = self.request.headers['HOST'] if not host.startswith("localhost"): host = "taskpad.me" l10n = GetMessages(self, self.accept_lang) return template.render(user_browser=self.user_browser, domain=host, l10n=l10n, **kargs)
def create_html_report(template_vars, template_path, path, name='output.html'): """ Renders a HTML template via Jinja2 using the provided template variables and the template itself. :param template_vars: (dict) Variable pool which Jinja2 uses to generate dynamic content. :param template_str: (str) Template in string form. :return: (Template) Populated HTML template. """ logging.info('render report ...') template_string = open(template_path).read() # Creates the template from template string template = Environment(loader=BaseLoader).from_string(template_string) # Populates the template with values retrieves from the DataFrame rendered_template = template.render(template_vars) output_path = os.path.join(path, name) logging.info('write report to {}'.format(output_path)) with open(output_path, "w") as text_file: text_file.write(rendered_template)
def generate_models( models_path: Path, templates_path: Path, models: List[Model], ): """Generate models file. :param models_path: path to models file :param templates_path: path to templates dir :param models: models for store """ template = Environment( loader=FileSystemLoader(str(templates_path)), autoescape=True, ).get_template('models') rendered_model = template.render(models=models) with open(models_path, 'w') as file_d: file_d.write(rendered_model)
def main(): versions = [] for x in os.listdir('.'): if os.path.isdir(x): if x != ".git": versions.append(x) def splitit(s): try: return map(int, s.split('.')) except ValueError: return s versions.sort(key=splitit) t = Environment().from_string(index_tpl) f = open('index.html', 'w') f.write(t.render(versions=versions)) f.close()
def get_html(doc, name=None, print_format=None): from jinja2 import Environment if isinstance(doc, basestring) and isinstance(name, basestring): doc = frappe.get_doc(doc, name) template = Environment().from_string(get_print_format_name(doc.doctype, print_format or frappe.form_dict.format)) meta = frappe.get_meta(doc.doctype) args = { "doc": doc, "meta": meta, "frappe": frappe, "utils": frappe.utils } html = template.render(args) return html
def manifest(cap_symbols, region_symbols, architecture, targets): """ Generates a c file from CSPACE_TEMPLATE_FILE with some runtime information about CSpace slots and special address ranges """ temp_file = open(CSPACE_TEMPLATE_FILE, 'r').read() template = Environment(loader=BaseLoader).from_string(temp_file) for (e, ccspace) in targets: name = os.path.basename(e) if ccspace: data = template.render({ 'slots': cap_symbols[name], 'symbols': region_symbols[name], 'progname': name, 'ipc_buffer_symbol': "mainIpcBuffer" }) ccspace.write(data)
def generate_text_sections_in_right_menu(self) -> str: """Generate the text sections in right menu. Returns: str: HTML code for the right menu text section. """ template = self.text_sections_in_right_menu_template if template == "__DEFAULT__": template = Config.default_text_sections_in_right_menu_template # Generate HTML code with open(Config.templates_path.joinpath(template)) as tem_han: template = Environment( loader=FileSystemLoader(Config.templates_path) ).from_string(tem_han.read()) right_menu_text = "" for item in Config.text_sections_in_right_menu: right_menu_text += template.render(**item) return right_menu_text
def prepare_email_content(runresult, subject_name): """ :param runresult: 生成的简要分析结果 :param subject_name: html名称 :return: email conetnt """ batch_result = {} batch_result['report_name'] = subject_name batch_result['time_start'] = runresult["start_time"] batch_result['testsRun'] = runresult["testsRun"] batch_result['failures'] = runresult["failures"] batch_result['successes'] = runresult["successes"] batch_result['tests'] = runresult["tests"] batch_result['error_list'] = runresult["error_list"] report_template = Environment(loader=FileSystemLoader(BASE_DIR)).get_template('./templates/email_report.html') return report_template.render(batch_result)
def get_requirements(self, optional=False): if not any(r.is_optional() == optional for r in self.package.all_requires): return with self.requirements_path.open(encoding='utf-8') as f: document = f.read() template = Environment().from_string(document) document = template.render( package=self.package, optional=optional, format_vcs=self._format_vcs, ) # rm junk document = document.replace(' ', '') # sort lines lines = sorted(line for line in document.split('\n') if line) document = '\n'.join(lines) + '\n' return document
def render(template_name, folder=TEMPLATE_DIR, **kwargs): """ :param template_name: имя шаблона :param folder: папка в которой ищем шаблон :param kwargs: параметры :return: """ file_path = BASE_DIR / folder / template_name if file_path.exists(): # open template file with open(file_path, encoding='utf-8') as f: template_str = f.read() template = Environment( loader=FileSystemLoader(BASE_DIR / folder)).from_string(template_str) return template.render(**kwargs) else: return b'404 PAGE Not Found'
def ProcessPlugin(self): env = Environment(keep_trailing_newline=True, loader=PackageLoader('regparse', 'templates')) lastvisited = [ "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedMRU", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedPidlMRU", "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\LastVisitedPidlMRULegacy" ] for hive in self.hives: for k in lastvisited: try: lvmru = Registry.Registry(hive).open(k) key = lvmru.name() last_write = lvmru.timestamp() mruorder = lvmru.value("MRUListEx").value() for entry in struct.unpack("%dI" % (len(mruorder) / 4), mruorder): for values in lvmru.values(): if str(values.name()) == str(entry): value = values.name() data = (values.value().split('\x00\x00')[0]) if self.format_file is not None: with open(self.format_file[0], "rb") as f: template = env.from_string(f.read()) sys.stdout.write(template.render(last_write=last_write, \ key=key, \ mruorder=mruorder, \ value=value, \ data=data) + "\n") elif self.format is not None: template = Environment().from_string( self.format[0]) sys.stdout.write(template.render(last_write=last_write, \ key=key, \ mruorder=mruorder, \ value=value, \ data=data) + "\n") except (Registry.RegistryKeyNotFoundException, Registry.RegistryValueNotFoundException): continue
def summary(self, login, show_output=False): path = os.path.join( "tasks", self.subject.abbr, str(self.semester), self.teacher.username, self.timeslot ) while path: try: with open(os.path.join(path, "summary.md")) as f: from evaluator.script import Script output = [] def p(s): output.append(s) meta = { 'login': login } variables = None if os.path.exists(os.path.join(BASE_DIR, path, "summary.py")): s = Script(os.path.join(BASE_DIR, path), meta, p, 'summary.py') variables = s.call('readme_vars') if not variables: variables = {} variables = {**variables, **meta} env = Environment(loader=FileSystemLoader(path)).from_string(f.read()) md = env.render(**variables) err = '' if output and show_output: for o in output: logging.error(o) err = '\n<pre class="text-danger">' + ('<br>'.join(output)) + '</pre>' return md + err except FileNotFoundError: pass path = path[:-1] return ""
def init_str(images): init_template = textwrap.dedent( """ domains: {% for vm_name, template in images.viewitems() %} {{ vm_name }}: memory: 1024 nics: - net: net-02 - net: net-01 disks: - template_name: {{ template }} type: template name: root dev: sda format: qcow2 artifacts: - /var/log - /etc/hosts - /etc/resolv.conf - /etc/sysconfig - /etc/NetworkManager groups: group{{ loop.index % 2 }} {% endfor %} nets: net-01: type: nat dhcp: start: 100 end: 254 management: true dns_domain_name: lago.local net-02: type: nat gw: 192.168.210.4 dhcp: start: 100 end: 254 """ ) template = Environment(loader=BaseLoader()).from_string(init_template) return template.render(images=images)
def run_task(self, fw_spec): if self.get("use_global_spec"): self._load_params(fw_spec) else: self._load_params(self) # modify context dynamically with values from fw_spec for context_key, fw_spec_key in self.context_inputs.items(): self.context[context_key] = get_nested_dict_value( fw_spec, fw_spec_key) with open(self.template_file) as f: t = Environment(loader=FileSystemLoader(self.template_dir), autoescape=True).from_string(f.read()) output = t.render(self.context) write_mode = 'w+' if self.append_file else 'w' with open(self.output_file, write_mode) as of: of.write(output)
def export(archive_dir): css = pkgutil.get_data('slackviewer', 'static/viewer.css').decode('utf-8') tmpl = Environment( loader=PackageLoader('slackviewer')).get_template("export_single.html") export_file_info = get_export_info(archive_dir) r = Reader(export_file_info["readable_path"]) channel_list = sorted([{ "channel_name": k, "messages": v } for (k, v) in r.compile_channels().items()], key=lambda d: d["channel_name"]) html = tmpl.render(css=css, generated_on=datetime.now(), workspace_name=export_file_info["workspace_name"], source_file=export_file_info["basename"], channels=channel_list) outfile = open(export_file_info["stripped_name"] + '.html', 'w') outfile.write(html.encode('utf-8'))
def subscribe(): """ This is the function for handling subscription requests. It will error clearly if a new phone number wasn't passed in when the page was requested. :return: A tuple of an HTML page and an HTTP Status code. """ logging.info('subscribe page started.') try: form_response = request.form subscriber_number = form_response['subscriber_number'] if not subscriber_number: raise Exception('No subscriber number was passed in!') logging.info('subscribe page checking subscriber_number is valid.') # Checking if subscriber_number matches valid format: +18885554444 carrier._is_mobile(number_type(phonenumbers.parse(subscriber_number))) logging.info( 'subscribe page got valid subscriber_number of {}.'.format( subscriber_number)) # Try to subscribe the number. topic_arn = 'arn:aws:sns:us-west-2:277012880214:BunAlert' snsclient = boto3.client('sns', region_name='us-west-2') logging.info('subscribe page made SNS client.') subscribe_response = snsclient.subscribe( TopicArn=topic_arn, Protocol='sms', Endpoint='{}'.format(subscriber_number)) logging.info('subscribe page subscribed the new subscriber_number.') # If all's good, render the response page. template = Environment( loader=FileSystemLoader('html/')).get_template('subscribe.j2') page = template.render(subscriber_number=subscriber_number) logging.info('subscribe page returned.') return page, 200 except Exception as e: logging.info('subscribe page errored.') page = 'Something went wrong! Exception: {}'.format(e) logging.error(page) return page, 400
def _make_property_html(property): template_dir = path.join(path.dirname(path.realpath(__file__)), 'templates') template = Environment( loader=FileSystemLoader(template_dir)).get_template('property.html') return template.render( uri=property[0], fid=property[1].get('fid'), title=property[1].get('title'), description=property[1].get('description'), scopeNote=property[1].get('scopeNote'), supers=property[1].get('supers'), subs=property[1].get('subs'), domains=property[1]['domains'], domainIncludes=property[1]['domainIncludes'], ranges=property[1]['ranges'], rangeIncludes=property[1]['rangeIncludes'], )
def generate_tag_cloud(self) -> str: """Generate the tag cloud. Returns: str: HTML code for the tag cloud. """ template = self.tag_cloud_template if template == "__DEFAULT__": template = Config.default_tag_cloud_template # Generate HTML code with open(Config.templates_path.joinpath(template)) as tem_han: template = Environment( loader=FileSystemLoader(Config.templates_path) ).from_string(tem_han.read()) html_str = template.render( tags=self.list_of_tags[:min(Config.maximal_tag_cloud_size, len(self.list_of_tags))] ) return html_str
def display_messages(self, layout: BaseLayout) -> None: """Hook for displaying the messages of the reporter This will be called whenever the underlying messages needs to be displayed. For some reporters, it probably doesn't make sense to display messages as soon as they are available, so some mechanism of storing them could be used. This method can be implemented to display them after they've been aggregated. """ grouped_messages = { path: self.group_messages(msgs) for path, msgs in self.messages.items() } template_f = self.linter.config.pyta_template_file template = Environment( loader=FileSystemLoader(TEMPLATES_DIR)).get_template(template_f) # Embed resources so the output html can go anywhere, independent of assets. # with open(os.path.join(TEMPLATES_DIR, 'pyta_logo_markdown.png'), 'rb+') as image_file: # # Encode img binary to base64 (+33% size), decode to remove the "b'" # pyta_logo_base64_encoded = b64encode(image_file.read()).decode() # Date/time (24 hour time) format: # Generated: ShortDay. ShortMonth. PaddedDay LongYear, Hour:Min:Sec dt = str(datetime.now().strftime("%a. %b. %d %Y, %I:%M:%S %p")) # Render the jinja template rendered_template = template.render( date_time=dt, reporter=self, grouped_messages=grouped_messages, os=os, enumerate=enumerate, ) # If a filepath was specified, write to the file if self.out is not sys.stdout: self.writeln(rendered_template) else: rendered_template = rendered_template.encode("utf8") self._open_html_in_browser(rendered_template)
def callback(self, args): config = { 'devops_home': ybutils.YB_DEVOPS_HOME, 'cloud': self.cloud.name } file_name = 'provision_instance.py.j2' try: config.update(vars(args)) data_dir = os.path.dirname(get_datafile_path(file_name)) template = Environment( loader=FileSystemLoader(data_dir)).get_template(file_name) with open(os.path.join(args.destination, args.name), 'w') as f: f.write(template.render(config)) os.chmod(f.name, stat.S_IRWXU) print json.dumps({'script_path': f.name}) except Exception as e: logging.error(e) print json.dumps( {"error": "Unable to create script: {}".format(e.message)})
def get_setup(self): # render template with self.setup_path.open(encoding='utf-8') as f: document = f.read() template = Environment().from_string(document) document = template.render( package=self.package, format_vcs=self._format_vcs, ) # format by yapf style = CreateGoogleStyle() document, _changed = FormatCode(document, style_config=style) # remove empty strings while '\n\n' in document: document = document.replace('\n\n', '\n') # format by autopep8 document = fix_code(document) return document
def create_pdf_from_html_template(path_to_the_template, output_folder): if path_to_the_template[-4:] == FILE_DOCX: print('Invalid file format') exit() template_html_file = path_to_the_template.split("/")[-1] path_to_the_template = path_to_the_template[:-len(template_html_file)] template = Environment(loader=FileSystemLoader( searchpath=path_to_the_template)).get_template(template_html_file) for i in range(1, len(sheet.col(0))): outputText = template.render(parser_excel_file(i)) html_file = open(TMP_HTML_TEMPLATE, 'w') html_file.write(outputText) html_file.close() pdfkit.from_file('template.html', output_folder + '/Invoice{0}.pdf'.format(i)) os.remove(TMP_HTML_TEMPLATE)
def load_template(template_dir, filename, message_dict, lang): """ This function loads a template file by provided language. """ from jinja2 import Environment, FileSystemLoader if not isinstance(template_dir, basestring): return False if not os.path.isdir(template_dir): return False try: f = '.'.join([filename, lang]) if os.path.exists(os.path.join(template_dir, f)): filename = f template = Environment( loader=FileSystemLoader(template_dir)).get_template(filename) return template.render(message_dict) except OSError: return False
def do_generate(cnf): for f in glob.iglob(product_instance_dir + '/src/tpl/**/*.tpl', recursive=True): try: print("read template file: ", f) pp.pprint(cnf) str_tpl = open(f, 'r').read() obj_tpl = Environment(loader=BaseLoader).from_string(str_tpl) rendered = obj_tpl.render(cnf['env']) print(cnf) pp.pprint(rendered) tgt_fle = f.replace('/src/tpl', '', 1).replace('.tpl', '') # print (rendered) print(rendered, file=open(tgt_fle, 'w')) print("output ready rendered file : ", tgt_fle) except Exception as e: print("RENDERING EXCEPTION: \n", str(e)) traceback.print_stack() print("STOP generating templates")
def __generate_vxlan_shutdown_script(self, net_uuid): template_sh = self.agent.get_os_plugin().read_file( os.path.join(self.DIR, 'templates', 'vxlan_destroy.sh')) br_name = 'br-{}'.format(net_uuid.split('-')[0]) vxlan_name = 'vxl-{}'.format(net_uuid.split('-')[0]) file_name = '{}_dnsmasq.pid'.format(br_name) pid_file_path = os.path.join(self.BASE_DIR, self.DHCP_DIR, file_name) net_sh = Environment().from_string(template_sh) net_sh = net_sh.render(bridge=br_name, vxlan_intf_name=vxlan_name, dnsmasq_pid_file=pid_file_path) file_name = '{}_stop.sh'.format(br_name) self.agent.get_os_plugin().store_file(net_sh, self.BASE_DIR, file_name) chmod_cmd = 'chmod +x {}'.format(os.path.join(self.BASE_DIR, file_name)) self.agent.get_os_plugin().execute_command(chmod_cmd, True) return file_name
def generator_one_table(table_name, table): context = {'name': table_name, 'table': table} source = """\ #!/usr/bin/python from sqlalchemy import Column, ForeignKey, Integer, String, create_engine from sqlalchemy.orm import sessionmaker DB_CONNECT_STRING = 'mysql+mysqldb://root:[email protected]/nova?charset=utf8' #engine = create_engine(DB_CONNECT_STRING, echo=True) engine = create_engine(DB_CONNECT_STRING, echo=False) DB_Session = sessionmaker(bind=engine) session = DB_Session() for row in session.execute('select * from ${name} ;').fetchall(): row=dict(zip(row.keys(), row.values())); % for cell in table ${cell}=row["${cell}"]+"_${cell}_data"; % endfor session.execute("insert into ${name} values( \\ % for cell in table '${cell}', \\ % endfor )"); session.commit(); \ """ jinja_template = JinjaEnvironment( line_statement_prefix='%', variable_start_string="${", variable_end_string="}").from_string(source) #print jinja_template.environment.compile(source, raw=True) str = jinja_template.render(context) if not os.path.isdir("data/"): os.mkdir("data/") f = open("data/%s.py" % table_name, "w") f.write("%s" % str) f.close()
def main(): if len(sys.argv) != 3: print "Usage: %s <docs dir> <output dir>" % (sys.argv[0]) return 1 # Get a list of documents and parse their content. documents = [] for doc in glob.glob(os.path.join(sys.argv[1], '*.txt')): # Assume that the first line is the title, and that there are two lines # after it before the actual content. Skip any document that does not # have a H1 at the start. f = open(doc, 'r') title = f.readline().strip() underline = f.readline().strip() if len(title) != len(underline) or underline[0] != '=': continue f.readline() # Parse the document. html = markdown(f.read()).encode(sys.stdout.encoding or "utf-8", 'xmlcharrefreplace') f.close() # Work out the output file name. name = os.path.splitext(os.path.basename(doc))[0] + '.html' # Add it to the list. documents.append((name, title, html)) # Create the template loader. tpl = Environment(loader=FileSystemLoader( os.path.join(sys.argv[1], 'markdown'))).get_template('template.html') # For each document, write the template. for doc in documents: f = open(os.path.join(sys.argv[2], doc[0]), 'w') f.write( tpl.render(name=doc[0], title=doc[1], content=doc[2], docs=documents)) f.close()
def generate_app_pages(self): # create all directories self.create_root_directory(self.output_directory) appimage_template = Environment( loader=self.file_system_loader).from_string(APPTEMPLATE) # iterate and generate app pages for app in progressbar(self.apps, redirect_stdout=True): appimage = AppImage(app, token=get_github_token(args)) path_to_appfolder = \ os.path.join(self.output_directory, appimage.title.lower()) # make the app folder if os.path.exists(path_to_appfolder): print(Fore.YELLOW + "[STATIC][{}] Directory exists.".format(appimage.title) + Fore.RESET) else: os.makedirs(path_to_appfolder) # write html file print( Fore.GREEN + "[STATIC][{}] Processing HTML files.".format(appimage.title) + Fore.RESET) with open(os.path.join(path_to_appfolder, 'index.html'), 'w') as w: w.write( appimage_template.render( appimage=appimage, library_issue_tracker="https://github.com/srevinsaju" "/appimage2.github.io/", catalog=Catalog())) self.json.append(appimage.json_data()) with open(os.path.join(path_to_appfolder, 'core.json'), 'w') as w: json.dump(appimage.get_app_metadata(), w) shields_badge = appimage.shields_badge() with open(os.path.join(path_to_appfolder, 'shields.json'), 'w') as w: json.dump(shields_badge, w) # write json file self.write_json_index()