def get(self, project, spider): with session_scope() as session: project = session.query(Project).filter_by(name=project).first() spider = session.query(Spider).filter_by(project_id=project.id, name=spider).first() job_settings = { setting.setting_key: setting.value for setting in session.query(SpiderSettings).filter_by( spider_id=spider.id) } # default setting values if 'concurrency' not in job_settings: job_settings['concurrency'] = 1 if 'timeout' not in job_settings: job_settings['timeout'] = 3600 template = get_template_loader().load('spidersettings.html') context = {} context['settings'] = job_settings context['project'] = project context['spider'] = spider spider_parameters = session.query(SpiderParameter)\ .filter_by(spider_id = spider.id)\ .order_by(SpiderParameter.parameter_key) context['spider_parameters'] = spider_parameters return self.write(template.generate(**context))
def tornado_process(src, filename): global loader, tornado template = tornado.template.Template(src, name='_.pug', loader=loader) generated = template.generate(missing=None) if isinstance(generated, six.binary_type): generated = generated.decode("utf-8") return generated
def out(self, template=None, **kwargs): self.check_session() template = loader.load(template or self.template or self.__class__.__name__+'.html') kwargs['session'] = self.session self.write(template.generate(**kwargs)) self.finish()
def copy_or_pump(src, dest, copy=False, appname=None, sqlite_path=None, extensions=None, data=None, tinydb_path=None, db_base_path=None, dbtype=None, cookie_secret=str(uuid.uuid4()), force=False): """ just copy files or pump them through the template engine before copying to out dir """ if not force and os.path.exists(dest): print(" skipping copy_or_pump: exists AND force = False ") else: if not copy: print(" pumping to ----->", dest) f = open(src, "r", encoding="utf-8") instr = f.read() f.close() template = tornado.template.Template(instr) out = template.generate( dbtype=dbtype, appname=appname, sqlite_path=sqlite_path, tinydb_path = tinydb_path, db_base_path=db_base_path, current_date=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"), cookie_secret=cookie_secret, extensions=extensions, data=data ) f = open(dest, "w", encoding="utf-8") f.write(out.decode("unicode_escape")) f.close() else: # just copy file print(" copying to ----->", dest ) print(" .. :" + str(shutil.copy( src, dest )))
def copy_or_pump(src, dest, copy=False, appname=None, sqlite_path=None, dbtype=None, cookie_secret=str(uuid.uuid4())): """ just copy files or pump them through the template engine before copying to out dir """ if not copy: print(" pumping to ----->", dest) f = open(src, "r", encoding="utf-8") instr = f.read() f.close() template = tornado.template.Template(instr) out = template.generate( dbtype=dbtype, appname=appname, sqlite_path=sqlite_path, current_date=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"), cookie_secret=cookie_secret) f = open(dest, "w", encoding="utf-8") f.write(out.decode("unicode_escape")) f.close() else: # just copy file print(" copying to ----->", dest) print(" .. :" + str(shutil.copy(src, dest)))
def create_issue_update(data): loader = tornado.template.Loader(os.path.join(os.path.dirname(__file__), 'templates')) template = loader.load('updated_pull_request.textile') return template.generate( data=data, tree_url=create_tree_url(data), )
def get(self): action = self.get_argument('action', default=None) if 'resolve-domain' == action: domain = self.get_argument('domain') ip = socket.gethostbyname(domain) self.write(ip) return if 'send-ping' == action: send_ping(self.get_argument('ip')) return if 'receive-pong' == action: while True: packet = try_receive_packet(icmp_socket) if not packet: break LOGGER.debug('received: %s' % repr(packet)) if not isinstance(packet.data, dpkt.icmp.ICMP): continue if not isinstance(packet.data.data, dpkt.icmp.ICMP.Echo): continue if 'foobar' != packet.data.data.data: continue self.write('received') return self.write('not received') return self.get_template_path() template = template_loader.load('self-check.html') self.write(template.generate())
def _generate(document): template_key = document.meta.type.lower() if template_key not in _document_templates: msg = u"TODO - document html generator for {0} documents." return msg.format(template_key) template = _document_templates[template_key] return template.generate(doc=document, FieldInfo=_FieldInfo, TemplateInfo=_TemplateInfo)
def finish(self, *args, **kwargs): self.set_header('Content-Type', 'text/html; charset=UTF-8') self._write_buffer = template.generate( resp=json.dumps("".join(self._write_buffer)), origin=json.dumps(urlparse.urljoin(self.get_argument('_origin'), '/janky'))) return tornado.web.RequestHandler.finish(self, *args, **kwargs)
def generate(self, templateFile): """使用模板页生成页面(使用self._dynamic_value定义变量) 参数: - templateFile:模板文件(仅文件名;自动从模板文件夹加载模板文件) """ template = self._template_loader.load(templateFile) template.autoescape = False self.write(template.generate(**self._dynamic_value))
def post(self): try: entries = self.session['entries'] except KeyError: entries = [] entry = self.get_argument("entry") entries.append(entry) self.session['entries'] = entries self.write(template.generate(entries=entries))
def render_b2_tpl(self, request): class Foo: def __init__(self, i, epoch): self.i = i self.epoch = epoch # evaluates Foo() and time() for each loop objects = (Foo(i, time()) for i in xrange(100)) template = self.loader.load('b2_tpl.html') self.finish(request, template.generate(objects=objects))
def __call__(self, request): if self.debug: print '\nTemplateRequestHandler' resource_path = self.getResourcePath(request.uri) with open(resource_path, 'r') as _file_: template = _file_.read() variables = self.server_config.project.attrs template = tornado.template.Template(template) content = template.generate(server_url=self.getHostUrl(request), **variables) request.write(self.constructResponse(content))
def wrap_render(template, *args, **kwargs): if isinstance(template, tornado.template.Template): out = template.generate(*args, **kwargs) else: out = template return """<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Tor Weather</title><style type="text/css">body{margin:40px auto;max-width:650px;line-height:1.6;font-size:18px;color:#444;padding:0 10px}h1,h2,h3{line-height:1.2}</style></head><body> """ + out + "</body></html>"
def render_header(self, request): title = "Cabecera menéame" globals = {} globals['favicon'] = "icon.jpg" globals['noindex'] = True globals['tags'] = 'test1, test2' globals['description'] = 'Pruebas de descripción' globals['server_name'] = 'www.meneame.net' globals['base_url'] = '/' globals['base_static'] = 'http://static.meneame.net' globals['js_main'] = 'general01.php' globals['security_key'] = 'xxxxxxxxxxxxxxxxxxxxxxx' globals['thumbnail'] = 'void.jpg' globals['thumbnail_logo'] = 'void.jpg' globals['extra_head'] = '' globals['extra_js'] = ('void.js', 'void1.js') globals['q'] = 'q=aslkdlñak ñlsakd ñlaks&kasjlkadjlajsdljkl' globals['lang'] = 'es' globals['css_main'] = 'main.css' globals['css_color'] = 'main.css' template = self.loader.load('header.html') context = { 'title': title, # wrap global into dict-with-attr-access object # workaround meneame's we-like-to-access-undefined-variables-in-templates 'globals': M_O(globals), 'current_user': None, } parts = [] parts.append(template.generate(**context)) parts.append(template.generate(**context)) parts.append(template.generate(**context)) self.finish(request, ''.join(parts))
def create_issue_update(data): def make_past_tense(verb): if not verb.endswith('d'): return verb + 'd' return verb loader = tornado.template.Loader(os.path.join(os.path.dirname(__file__), 'templates')) template = loader.load('updated_pull_request.textile') return template.generate( data=data, tree_url=create_tree_url(data), make_past_tense=make_past_tense, )
def post(self): form = BlockchainAddy(self.request.arguments) details = '' if form.validate(): for f in self.request.arguments: details += self.get_argument(f, default=None, strip=False) loader = tornado.template.Loader("templates") template = loader.load("address_template.html") input_details = blockchain_api.read_blockchain_address(details) self.write(template.generate( address=details, details=input_details)) else: self.set_status(400) self.write(form.errors)
def copy_or_pump( src, dest, copy=False, appname=None, sqlite_path=None, extensions=None, data=None, tinydb_path=None, db_base_path=None, dbtype=None, cookie_secret=str(uuid.uuid4()), force=False, ): """ just copy files or pump them through the template engine before copying to out dir """ if not force and os.path.exists(dest): print(" skipping copy_or_pump: exists AND force == False ") else: if not copy: # print(" pumping to ----->", dest) f = open(src, "r", encoding="utf-8") instr = f.read() f.close() template = tornado.template.Template(instr) out = template.generate( dbtype=dbtype, appname=appname, sqlite_path=sqlite_path, tinydb_path=tinydb_path, db_base_path=db_base_path, current_date=datetime.datetime.now().strftime( "%Y/%m/%d %H:%M:%S"), cookie_secret=cookie_secret, extensions=extensions, data=data, ) f = open(dest, "w", encoding="utf-8") f.write(out.decode("utf-8")) f.close() else: # just copy file # print(" copying to ----->", dest ) # print(" .. :" + str(shutil.copy( src, dest ))) shutil.copy(src, dest) print(" done!")
def __gen_content_deskjs(self): ''' @summary: 输出deskjs的content,gen_content当引擎为desk.js时实际调用的输出函数 ''' try: self.__deskjs_contents except: raise RuntimeError("Your Yaml Must Contain At least One Page!") template = tornado.template.Template(DESKJS_TEMPLATE) result = template.generate(slide_content=self.__deskjs_contents, title = self.__settings['TITLE'], author_name=self.__settings['AUTHOR']['name'], author_email=self.__settings['AUTHOR']['email'], path=self.__settings['ENGINE_PATH'], theme=self.__settings['THEME'], custom_css = self.__custom_css ) return result
def render_page(page): hilite = markdown.extensions.codehilite.CodeHiliteExtension(noclasses=True) loader = tornado.template.Loader('templates') layout = u''' {{% extends "base.tmpl %}} {{% block body %}} {} {{% end %}} '''.format(markdown.markdown(page.body, extensions=['extra', hilite])) template = tornado.template.Template(layout, loader=loader) settings = { 'page' : page, 'dateutil' : dateutil, 'itertools' : itertools, } print(template.generate(**settings).decode())
def __call__(self, request): if self.debug: print '\nAppleFrostTemplateHandlerMethods' print " processing request for", request.uri resource_path = self.getResourcePath(request.uri) if self.debug: print 'resource path', resource_path with open(resource_path, 'r') as _file_: template = tornado.template.Template(_file_.read()) request_dict = self.requestAsDict(request) parameters = self.extractTemplateParameters(request_dict) content = template.generate(csf_server_url=self.getHostUrl(request), **parameters) content = content.replace('"', '"').replace("'", "'") request.write(self.constructResponse(content))
def create_issue_update(pullrequest, data): def make_past_tense(verb): if not verb.endswith('d'): return verb + 'd' return verb loader = tornado.template.Loader( os.path.join(os.path.dirname(__file__), 'templates') ) template = loader.load('updated_pull_request.textile') return template.generate( data=data, head_url=create_tree_url(data, 'head'), base_url=create_tree_url(data, 'base'), make_past_tense=make_past_tense, commits=get_commits_from_pr(pullrequest), )
def render_b2_1_tpl(self, request): class Foo: def __init__(self, i, epoch): # coerce to str or escape() will fail # because expects a string self.i = str(i) self.epoch = epoch # evaluates Foo() and time() for each loop objects = (Foo(i, datetime.today()) for i in xrange(100)) template = self.loader.load('b2_filters_tpl.html') # Haanga uses native php's date function # we will use strftime which is already available # in the template with the datetime module # We pass RFC 2822 compatible date format to emulate php's r format date_format = '%a, %d %b %Y %H:%M:%S +0000' self.finish(request, template.generate(objects=objects, date_format=date_format))
def __call__(self, request): if self.debug: print '\nGDDToolBlockingPageHandler' print " processing request for", request.uri resource_path = self.getResourcePath(request.uri) if self.debug: print 'resource path', resource_path with open(resource_path, 'r') as _file_: template = tornado.template.Template(_file_.read()) request_dict = self.requestAsDict(request) parameter_dict = self.extractTemplateParameters(request_dict) content = template.generate(csf_server_url=self.getHostUrl(request), **parameter_dict) #print "\n\nrequest for", request.uri #print content, '\n\n' request.write(self.constructResponse(content.replace('"', '"')))
def ponzi_process(page_id, out_nodes, options, s3=None): templates = options["templates"] text = options["textset"] output_dir = options["output_dir"] # select random template from templates template = random.choice(templates) content_len = random.randint(1, options["textlen"]) content = ["<p>{}</p>".format(paragraph) for paragraph in [random.choice(text) for _ in range(content_len)]] for node in out_nodes: link = """<a href="/{0}.html">Node {0}</a>""".format(node) content.insert(random.randint(1, content_len), link) page = template.generate(title=page_id, content="\n".join(content)) file_name = "{}.html".format(page_id) out_file = os.path.join(output_dir, file_name) with open(out_file, "w") as f: f.write(page) if options["s3"]["use"]: s3.send_file(out_file, file_name)
#!/usr/local/bin/python -u import json import os.path import tornado import tornado.template FILE_ROOT = os.path.normpath(os.path.dirname(__file__)) TEMPLATE_FILE = "./templates/index.html.tmpl" DATA_FILE = "data.json" template_loader = tornado.template.Loader(FILE_ROOT, whitespace="oneline") template = template_loader.load(TEMPLATE_FILE) with open(os.path.join(FILE_ROOT, DATA_FILE), "r") as fh: template_vars = json.load(fh) print template.generate(**template_vars)
def get(self): self.write(template.generate(entries=self.session.get('entries', [])))
def get(self): loader = tornado.template.Loader(".") template = loader.load("mainframe/templates/index.html") self.write(template.generate(settings=settings))
def send_email(cls, to, message, filters, store=None): ''' Sends a email to each user in `to`. :param to: list of tlog.base.user.User :param message: str :param filters: list of tlog.base.filter.Filter :param store: tlog.receiver.store.Store Specify `store` if any information should used from the stored log message. ''' recipients = [] for user in to: recipients.append(user.notification_types.get('send_email')['data']) template = tornado.template.Template( ''' <html> <body> <p style="font-size:14px;">{{ message }}</p> <table> {% if severity %} <tr> <th align="right">Level:</th><td>{{ severity }}</td> </tr> {% end %} {% if store %} <tr> <th align="right">Server:</th><td>{{ store.hostname }}</td> </tr> <tr> <th align="right">External id:</th><td>{{ store.external_id }}</td> </tr> <tr> <th align="right">Message hash:</th><td>{{ store.message_hash }}</td> </tr> <tr> <th align="right">Times seen:</th><td>{{ store.log_group.times_seen + 1 }}</td> </tr> <tr> <th align="right">URL:</th><td><a href="{{ urljoin(base_url, '/log_group/{}'.format(store.log_group.id)) }}">{{ urljoin(base_url, '/log_group/{}'.format(store.log_group.id)) }}</a></td> </tr> {% end %} </table> <p>You received this message because of the following filters:</p> <table> {% for filter_ in filters %} <tr> <td><a href="{{ urljoin(base_url, '/filter/{}'.format(filter_.id)) }}">{{ filter_.name }}</a></td> </tr> {% end %} </table> </body> </html> ''' ) severity = SYSLOG_SEVERITY[4] if store: severity = SYSLOG_SEVERITY[store.level] Mail.send( recipients=recipients, subject='{}: {}'.format(severity, message), message=template.generate( severity=severity, message=message, filters=filters, store=store, base_url=Config.data['url'], urljoin=urlparse.urljoin, ), ) return True
def loop(i): # loads template and calculates time() every loop template = self.loader.load('b1_tpl.html') return template.generate(i=i, epoch=time())
def tornado_process(str): global loader, tornado template = tornado.template.Template(str, name='_.jade', loader=loader) return template.generate().decode("utf-8")
def combine_css(path, container, settings_dir=None, log=True): """ Combines all application and plugin .css template files into one big one; saved to the given *path*. Templates will be rendered using the given *container* as the replacement for templates use of '#{{container}}'. If given, *settings_dir* will be used to determine which applications and plugins should be included in the dump based on what is enabled. If *log* is ``False`` messages indicating where the files have been saved will not be logged (useful when rendering CSS for programatic use). """ if container.startswith('#'): # This is just in case (don't want ##gateone) container = container.lstrip('#') if not settings_dir: settings_dir = os.path.join(GATEONE_DIR, 'settings') all_settings = get_settings(settings_dir) enabled_plugins = [] enabled_applications = [] embedded = False url_prefix = '/' if 'gateone' in all_settings['*']: # The check above will fail in first-run situations enabled_plugins = all_settings['*']['gateone'].get( 'enabled_plugins', []) enabled_applications = all_settings['*']['gateone'].get( 'enabled_applications', []) embedded = all_settings['*']['gateone'].get('embedded', False) url_prefix = all_settings['*']['gateone'].get('url_prefix', False) plugins_dir = os.path.join(GATEONE_DIR, 'plugins') pluginslist = os.listdir(plugins_dir) pluginslist.sort() applications_dir = os.path.join(GATEONE_DIR, 'applications') appslist = os.listdir(applications_dir) appslist.sort() global_themes_dir = os.path.join(GATEONE_DIR, 'templates', 'themes') themes = os.listdir(global_themes_dir) theme_writers = {} for theme in themes: combined_theme_path = "%s_theme_%s" % ( path.split('.css')[0], theme) theme_writers[theme] = io.open(combined_theme_path, 'w') themepath = os.path.join(global_themes_dir, theme) with io.open(themepath) as css_file: theme_writers[theme].write(css_file.read()) # NOTE: We skip gateone.css because that isn't used when embedding with io.open(path, 'w') as f: # Gate One plugins # TODO: Add plugin theme files to this for plugin in pluginslist: if enabled_plugins and plugin not in enabled_plugins: continue css_dir = os.path.join(plugins_dir, plugin, 'templates') if os.path.isdir(css_dir): filelist = os.listdir(css_dir) filelist.sort() for filename in filelist: filepath = os.path.join(css_dir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: f.write(css_file.read() + u'\n') # Gate One applications for application in appslist: if enabled_applications: # Only export CSS of enabled apps if application not in enabled_applications: continue css_dir = os.path.join(GATEONE_DIR, 'applications', application, 'templates') subdirs = [] plugins_dir = os.path.join( applications_dir, application, 'plugins') if os.path.isdir(css_dir): filelist = os.listdir(css_dir) filelist.sort() for filename in filelist: filepath = os.path.join(css_dir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: f.write(css_file.read() + u'\n') elif os.path.isdir(filepath): subdirs.append(filepath) while subdirs: subdir = subdirs.pop() filelist = os.listdir(subdir) filelist.sort() for filename in filelist: filepath = os.path.join(subdir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: combined = css_file.read() + u'\n' if os.path.split(subdir)[1] == 'themes': theme_writers[filename].write(combined) else: f.write(combined) elif os.path.isdir(filepath): subdirs.append(filepath) app_settings = all_settings['*'].get(application, None) enabled_app_plugins = [] if app_settings: enabled_app_plugins = app_settings.get( 'enabled_plugins', []) if os.path.isdir(plugins_dir): pluginslist = os.listdir(plugins_dir) pluginslist.sort() # Gate One application plugins for plugin in pluginslist: # Only export JS of enabled app plugins if enabled_app_plugins: if plugin not in enabled_app_plugins: continue css_dir = os.path.join( plugins_dir, plugin, 'templates') if os.path.isdir(css_dir): filelist = os.listdir(css_dir) filelist.sort() for filename in filelist: filepath = os.path.join(css_dir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: f.write(css_file.read() + u'\n') elif os.path.isdir(os.path.join( css_dir, filename)): subdirs.append(filepath) while subdirs: subdir = subdirs.pop() filelist = os.listdir(subdir) filelist.sort() for filename in filelist: filepath = os.path.join(subdir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: with io.open(filepath) as css_file: combined = css_file.read() + u'\n' _dir = os.path.split(subdir)[1] if _dir == 'themes': theme_writers[filename].write( combined) else: f.write(combined) elif os.path.isdir(filepath): subdirs.append(filepath) f.flush() for writer in theme_writers.values(): writer.flush() writer.close() # Now render the templates asis = lambda x: x # Used to disable autoescape import tornado.template loader = tornado.template.Loader(os.path.split(path)[0], autoescape="asis") template = loader.load(path) css_data = template.generate( asis=asis, container=container, url_prefix=url_prefix, embedded=embedded) # Overwrite it with the rendered version with io.open(path, 'wb') as f: f.write(css_data) if log: logging.info(_( "Non-theme CSS has been combined and saved to: %s" % path)) for theme in theme_writers.keys(): combined_theme_path = "%s_theme_%s" % ( path.split('.css')[0], theme) template = loader.load(combined_theme_path) css_data = template.generate( asis=asis, container=container, url_prefix=url_prefix, embedded=embedded) with io.open(combined_theme_path, 'wb') as f: f.write(css_data) if log: logging.info(_( "The %s theme CSS has been combined and saved to: %s" % (theme.split('.css')[0], combined_theme_path)))
def tornado_process (str): global loader, tornado template = tornado.template.Template(str,name='_.jade',loader=loader) return template.generate().decode("utf-8")
def get(self): template = template_loader.load("dashboard.html") self.write(template.generate(dns_service_status=dns_service.status, tcp_service_status=tcp_service.status))
def send_email(cls, to, message, filters, store=None): ''' Sends a email to each user in `to`. :param to: list of tlog.base.user.User :param message: str :param filters: list of tlog.base.filter.Filter :param store: tlog.receiver.store.Store Specify `store` if any information should used from the stored log message. ''' recipients = [] for user in to: recipients.append( user.notification_types.get('send_email')['data']) template = tornado.template.Template(''' <html> <body> <p style="font-size:14px;">{{ message }}</p> <table> {% if severity %} <tr> <th align="right">Level:</th><td>{{ severity }}</td> </tr> {% end %} {% if store %} <tr> <th align="right">Server:</th><td>{{ store.hostname }}</td> </tr> <tr> <th align="right">External id:</th><td>{{ store.external_id }}</td> </tr> <tr> <th align="right">Message hash:</th><td>{{ store.message_hash }}</td> </tr> <tr> <th align="right">Times seen:</th><td>{{ store.log_group.times_seen + 1 }}</td> </tr> <tr> <th align="right">URL:</th><td><a href="{{ urljoin(base_url, '/log_group/{}'.format(store.log_group.id)) }}">{{ urljoin(base_url, '/log_group/{}'.format(store.log_group.id)) }}</a></td> </tr> {% end %} </table> <p>You received this message because of the following filters:</p> <table> {% for filter_ in filters %} <tr> <td><a href="{{ urljoin(base_url, '/filter/{}'.format(filter_.id)) }}">{{ filter_.name }}</a></td> </tr> {% end %} </table> </body> </html> ''') severity = SYSLOG_SEVERITY[4] if store: severity = SYSLOG_SEVERITY[store.level] Mail.send( recipients=recipients, subject='{}: {}'.format(severity, message), message=template.generate( severity=severity, message=message, filters=filters, store=store, base_url=Config.data['url'], urljoin=urlparse.urljoin, ), ) return True
def get(self): form = BlockchainAddy() loader = tornado.template.Loader("templates") template = loader.load("blockchainaddy.html") self.write(template.generate(form=form))
def combine_css(path, container, settings_dir=None, log=True): """ Combines all application and plugin .css template files into one big one; saved to the given *path*. Templates will be rendered using the given *container* as the replacement for templates use of '#{{container}}'. If given, *settings_dir* will be used to determine which applications and plugins should be included in the dump based on what is enabled. If *log* is ``False`` messages indicating where the files have been saved will not be logged (useful when rendering CSS for programatic use). """ if container.startswith( '#'): # This is just in case (don't want ##gateone) container = container.lstrip('#') if not settings_dir: settings_dir = os.path.join(GATEONE_DIR, 'settings') all_settings = get_settings(settings_dir) enabled_plugins = [] enabled_applications = [] embedded = False url_prefix = '/' if 'gateone' in all_settings['*']: # The check above will fail in first-run situations enabled_plugins = all_settings['*']['gateone'].get( 'enabled_plugins', []) enabled_applications = all_settings['*']['gateone'].get( 'enabled_applications', []) embedded = all_settings['*']['gateone'].get('embedded', False) url_prefix = all_settings['*']['gateone'].get('url_prefix', False) plugins_dir = os.path.join(GATEONE_DIR, 'plugins') pluginslist = os.listdir(plugins_dir) pluginslist.sort() applications_dir = os.path.join(GATEONE_DIR, 'applications') appslist = os.listdir(applications_dir) appslist.sort() global_themes_dir = os.path.join(GATEONE_DIR, 'templates', 'themes') themes = os.listdir(global_themes_dir) theme_writers = {} for theme in themes: combined_theme_path = "%s_theme_%s" % (path.split('.css')[0], theme) theme_writers[theme] = io.open(combined_theme_path, 'w') themepath = os.path.join(global_themes_dir, theme) with io.open(themepath) as css_file: theme_writers[theme].write(css_file.read()) # NOTE: We skip gateone.css because that isn't used when embedding with io.open(path, 'w') as f: # Gate One plugins # TODO: Add plugin theme files to this for plugin in pluginslist: if enabled_plugins and plugin not in enabled_plugins: continue css_dir = os.path.join(plugins_dir, plugin, 'templates') if os.path.isdir(css_dir): filelist = os.listdir(css_dir) filelist.sort() for filename in filelist: filepath = os.path.join(css_dir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: f.write(css_file.read() + u'\n') # Gate One applications for application in appslist: if enabled_applications: # Only export CSS of enabled apps if application not in enabled_applications: continue css_dir = os.path.join(GATEONE_DIR, 'applications', application, 'templates') subdirs = [] plugins_dir = os.path.join(applications_dir, application, 'plugins') if os.path.isdir(css_dir): filelist = os.listdir(css_dir) filelist.sort() for filename in filelist: filepath = os.path.join(css_dir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: f.write(css_file.read() + u'\n') elif os.path.isdir(filepath): subdirs.append(filepath) while subdirs: subdir = subdirs.pop() filelist = os.listdir(subdir) filelist.sort() for filename in filelist: filepath = os.path.join(subdir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: combined = css_file.read() + u'\n' if os.path.split(subdir)[1] == 'themes': theme_writers[filename].write(combined) else: f.write(combined) elif os.path.isdir(filepath): subdirs.append(filepath) app_settings = all_settings['*'].get(application, None) enabled_app_plugins = [] if app_settings: enabled_app_plugins = app_settings.get('enabled_plugins', []) if os.path.isdir(plugins_dir): pluginslist = os.listdir(plugins_dir) pluginslist.sort() # Gate One application plugins for plugin in pluginslist: # Only export JS of enabled app plugins if enabled_app_plugins: if plugin not in enabled_app_plugins: continue css_dir = os.path.join(plugins_dir, plugin, 'templates') if os.path.isdir(css_dir): filelist = os.listdir(css_dir) filelist.sort() for filename in filelist: filepath = os.path.join(css_dir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: f.write(css_file.read() + u'\n') elif os.path.isdir(os.path.join(css_dir, filename)): subdirs.append(filepath) while subdirs: subdir = subdirs.pop() filelist = os.listdir(subdir) filelist.sort() for filename in filelist: filepath = os.path.join(subdir, filename) if filename.endswith('.css'): with io.open(filepath) as css_file: with io.open(filepath) as css_file: combined = css_file.read() + u'\n' _dir = os.path.split(subdir)[1] if _dir == 'themes': theme_writers[filename].write( combined) else: f.write(combined) elif os.path.isdir(filepath): subdirs.append(filepath) f.flush() for writer in theme_writers.values(): writer.flush() writer.close() # Now render the templates asis = lambda x: x # Used to disable autoescape import tornado.template loader = tornado.template.Loader(os.path.split(path)[0], autoescape="asis") template = loader.load(path) css_data = template.generate(asis=asis, container=container, url_prefix=url_prefix, embedded=embedded) # Overwrite it with the rendered version with io.open(path, 'wb') as f: f.write(css_data) if log: logging.info( _("Non-theme CSS has been combined and saved to: %s" % path)) for theme in theme_writers.keys(): combined_theme_path = "%s_theme_%s" % (path.split('.css')[0], theme) template = loader.load(combined_theme_path) css_data = template.generate(asis=asis, container=container, url_prefix=url_prefix, embedded=embedded) with io.open(combined_theme_path, 'wb') as f: f.write(css_data) if log: logging.info( _("The %s theme CSS has been combined and saved to: %s" % (theme.split('.css')[0], combined_theme_path)))
import pygments.formatters if __name__ == "__main__": filename = sys.argv[1] p = parser.Parser(open(filename)) functions = p.parse_functions() loader = tornado.template.Loader('./', autoescape=None) template = loader.load('base.html') lexer = pygments.lexers.PythonLexer() formatter = pygments.formatters.HtmlFormatter(cssclass='highlight', style='monokai', linenos='inline') cssfilename = './style.css' cssfile = open(cssfilename, 'w') cssfile.write(formatter.get_style_defs('.highlight')) cssfile.close() def highlight(code): formatter.linenostart = code.min_line + 1 return pygments.highlight(code.body, lexer, formatter) styled_functions = map(highlight, functions) print template.generate(functions=functions, styled_functions=styled_functions, filename=filename, cssfilename=cssfilename)
def combine_css(path, container, settings_dir=None): """ Combines all application and plugin .css template files into one big one; saved to the given *path*. Templates will be rendered using the given *container* as the replacement for templates use of '#{{container}}'. If given, *settings_dir* will be used to determine which applications and plugins should be included in the dump based on what is enabled. """ # A couple partials to save some space/typing resource = lambda s: resource_string('gateone', s).decode('utf-8') resource_fn = lambda s: resource_filename('gateone', s) resource_dir = lambda s: resource_listdir('gateone', s) if container.startswith( '#'): # This is just in case (don't want ##gateone) container = container.lstrip('#') if not settings_dir: settings_dir = resource_filename('gateone', '/settings') all_settings = get_settings(settings_dir) enabled_plugins = [] enabled_applications = [] embedded = False url_prefix = '/' if 'gateone' in all_settings['*']: # The check above will fail in first-run situations enabled_plugins = all_settings['*']['gateone'].get( 'enabled_plugins', []) enabled_applications = all_settings['*']['gateone'].get( 'enabled_applications', []) embedded = all_settings['*']['gateone'].get('embedded', False) url_prefix = all_settings['*']['gateone'].get('url_prefix', False) plugins_dir = resource_fn('/plugins') pluginslist = resource_dir('/plugins') pluginslist.sort() applications_dir = resource_fn('/applications') appslist = resource_dir('/applications') appslist.sort() logger.info(_("Combining all Gate One CSS into a single file...")) global_themes_dir = resource_fn('/templates/themes') themes = resource_dir('/templates/themes') theme_writers = {} for theme in themes: combined_theme_path = "%s_theme_%s" % (path.split('.css')[0], theme) theme_writers[theme] = io.open(combined_theme_path, 'w') theme_relpath = '/templates/themes/' + theme themepath = resource_fn(theme_relpath) logger.info(_("Concatenating: %s") % theme_relpath) theme_writers[theme].write( u"\n/* ------ theme_relpath: %s ------ */\n" % theme_relpath) theme_writers[theme].write(resource(theme_relpath)) # NOTE: We skip gateone.css because that isn't used when embedding with io.open(path, 'w') as f: # Gate One plugins for plugin in pluginslist: if enabled_plugins and plugin not in enabled_plugins: continue plugin_theme_path = '/plugins/%s/templates/themes' % plugin plugin_css_path = '/plugins/%s/templates' % plugin # NOTE: Using resource_filename() here so that it gets unpacked if # necessary: theme_dir = resource_fn(plugin_theme_path) if os.path.isdir(theme_dir): filelist = resource_dir(plugin_theme_path) filelist.sort() for filename in filelist: filepath = os.path.join(plugin_theme_path, filename) if filename.endswith('.css'): logger.info(_("Concatenating: %s") % filepath) theme = os.path.split(filename)[1] theme_writers[theme].write( u"\n/* ------ filepath: %s ------ */\n" % filepath) theme_writers[theme].write(resource(filepath) + u'\n') css_dir = resource_fn(plugin_css_path) if os.path.isdir(css_dir): filelist = resource_dir(css_dir) filelist.sort() for filename in filelist: filepath = os.path.join(css_dir, filename) if filename.endswith('.css'): logger.info(_("Concatenating: %s") % filepath) f.write(u"\n/* ------ filepath: %s ------ */\n" % filepath) f.write(resource(filepath) + u'\n') # Gate One applications for application in appslist: if enabled_applications: # Only export JS of enabled apps if application not in enabled_applications: continue app_templates_path = '/applications/%s/templates' % application app_templates_dir = resource_fn(app_templates_path) if os.path.isdir(app_templates_dir): filelist = resource_dir(app_templates_path) filelist.sort() for filename in filelist: filepath = os.path.join(app_templates_path, filename) if filename.endswith('.css'): logger.info(_("Concatenating: %s") % filepath) f.write(u"\n/* ------ filepath: %s ------ */\n" % filepath) f.write(resource(filepath) + u'\n') app_themes_path = '/applications/%s/templates/themes' % application app_themes_dir = resource_fn(app_themes_path) if os.path.isdir(app_themes_dir): filelist = resource_dir(app_themes_path) filelist.sort() for filename in filelist: filepath = os.path.join(app_themes_path, filename) if filename.endswith('.css'): logger.info(_("Concatenating: %s") % filepath) theme = os.path.split(filename)[1] theme_writers[theme].write( u"\n/* ------ filepath: %s ------ */\n" % filepath) theme_writers[theme].write(resource(filepath) + u'\n') app_settings = all_settings['*'].get(application, None) enabled_app_plugins = [] if app_settings: enabled_app_plugins = app_settings.get('enabled_plugins', []) app_plugins = '/applications/%s/plugins' % application plugins_dir = resource_filename('gateone', app_plugins) if os.path.isdir(plugins_dir): # Gate One application plugins pluginslist = resource_dir(app_plugins) pluginslist.sort() plugin_templates_path = app_plugins + '/{plugin}/templates' plugin_themes_path = app_plugins + '/{plugin}/templates/themes' for plugin in pluginslist: # Only export CSS of enabled app plugins if enabled_app_plugins: if plugin not in enabled_app_plugins: continue templates_path = plugin_templates_path.format( plugin=plugin) templates_dir = resource_fn(templates_path) if os.path.isdir(templates_dir): filelist = resource_dir(templates_path) filelist.sort() for filename in filelist: filepath = os.path.join(templates_path, filename) if filename.endswith('.css'): logger.info(_("Concatenating: %s") % filepath) f.write( u"\n/* ------ filepath: %s ------ */\n" % filepath) f.write(resource(filepath) + u'\n') themes_path = plugin_themes_path.format(plugin=plugin) themes_dir = resource_fn(themes_path) if os.path.isdir(themes_dir): filelist = resource_dir(themes_path) filelist.sort() for filename in filelist: filepath = os.path.join(themes_path, filename) if filename.endswith('.css'): logger.info(_("Concatenating: %s") % filepath) theme = os.path.split(filename)[1] theme_writers[theme].write( u"\n/* ------ filepath: %s ------ */\n" % filepath) theme_writers[theme].write( resource(filepath) + u'\n') f.flush() for writer in theme_writers.values(): writer.flush() writer.close() # Now render the templates asis = lambda x: x # Used to disable autoescape import tornado.template loader = tornado.template.Loader(os.path.split(path)[0], autoescape="asis") template = loader.load(path) css_data = template.generate(asis=asis, container=container, url_prefix=url_prefix, embedded=embedded) css_data += "\n/* ------ path: %s ------ */\n" % path # Overwrite it with the rendered version with io.open(path, 'wb') as f: f.write(css_data) logger.info(_("Non-theme CSS has been combined and saved to: %s" % path)) for theme in theme_writers.keys(): combined_theme_path = "%s_theme_%s" % (path.split('.css')[0], theme) template = loader.load(combined_theme_path) css_data = ("\n/* ------ combined_theme_path: %s ------ */\n" % combined_theme_path) css_data += template.generate(asis=asis, container=container, url_prefix=url_prefix, embedded=embedded) new_css_data = '@charset "UTF-8";\n' for line in css_data.split('\n'): if '@charset' not in line: new_css_data += line + '\n' css_data = new_css_data with io.open(combined_theme_path, 'wb') as f: f.write(css_data) logger.info( _("The %s theme CSS has been combined and saved to: %s") % (theme.split('.css')[0], combined_theme_path)) logger.info( _("Don't forget to set '\"send_css\": false' in your 10server.conf to " "disable sending of CSS"))
def render_template(self, template, **kwargs): template = self._loader.load(template) rendered = template.generate(**kwargs) self.write(rendered)
def run(options): db = connection[settings.DATABASE_NAME] only_email_addresses = options.only_email_addresses only_email_addresses = [ x.strip() for x in only_email_addresses.split(',') if x.strip() ] subject = options.subject template_filename = os.path.abspath(os.path.normpath(options.messagetemplate)) template_code = open(template_filename).read() template = tornado.template.Template(template_code) log_filename = options.messagetemplate + '.log' try: done_user_ids = [ x.strip() for x in open(log_filename).read().splitlines() if x.strip() ] except IOError: done_user_ids = [] unsubscribers = [ x['user'] for x in db.UserSettings.find({'unsubscribe_emails': True}, ('user',)) ] _locations = {} for user in db.User.find({'email': {'$ne': None}}): if not user['email'] or user['_id'] in unsubscribers: continue if str(user['_id']) in done_user_ids: print "Skipping", user['email'] continue if only_email_addresses: if user['email'] in only_email_addresses: print "ONLY", user['email'] else: continue if user['current_location'] not in _locations: _locations[user['current_location']] = ( db.Location.find_one({'_id': user['current_location']}) ) user_settings = db.UserSettings.find_one({'user': user['_id']}) assert user_settings data = { 'SIGNATURE': settings.SIGNATURE, 'user': user, 'user_settings': user_settings, 'current_location': _locations[user['current_location']], 'unsubscribe_uri': '/unsubscribe/%s' % user.tokenize_id(user['_id'], 12) } body = template.generate(**data) if options.dry_run: email_backend = 'tornado_utils.send_mail.backends.console.EmailBackend' else: email_backend = 'tornado_utils.send_mail.backends.pickle.EmailBackend' send_email( email_backend, subject, body, "Around The World <*****@*****.**>",#settings.ADMIN_EMAILS[0], #"Around The World <%s>" % settings.ADMIN_EMAILS[0], [user['email']], ) if not options.dry_run: open(log_filename, 'a').write('%s\n' % user['_id']) else: print "emailed", user['email']
import tornado import tornado.template import pygments import pygments.lexers import pygments.formatters if __name__ == "__main__": filename = sys.argv[1] p = parser.Parser(open(filename)) functions = p.parse_functions() loader = tornado.template.Loader('./', autoescape=None) template = loader.load('base.html') lexer = pygments.lexers.PythonLexer() formatter = pygments.formatters.HtmlFormatter(cssclass='highlight', style='monokai', linenos='inline') cssfilename = './style.css' cssfile = open(cssfilename, 'w') cssfile.write(formatter.get_style_defs('.highlight')) cssfile.close() def highlight(code): formatter.linenostart = code.min_line + 1 return pygments.highlight(code.body, lexer, formatter) styled_functions = map(highlight, functions) print template.generate(functions=functions, styled_functions=styled_functions, filename=filename, cssfilename=cssfilename)
def __render_markdown(template_str,**kwds): ''' @summary: 渲染markdown为m函数的模板,默认不进行转码 ''' template = tornado.template.Template("{% autoescape None %}"+template_str) return template.generate(m=markdown.markdown,**kwds)