コード例 #1
0
 def render_mock_template(self):
     loader = FileSystemLoader(os.path.dirname(os.path.realpath(__file__)))
     env = Environment(trim_blocks=True, loader=loader)
     template = env.get_template("mock.cfg.j2")
     return template.render(self.distro_info)
コード例 #2
0
ファイル: reporthtml.py プロジェクト: xme/CAPEv2
    def run(self, results):
        """Writes report.
        @param results: Cuckoo results dict.
        @raise CuckooReportError: if fails to write report.
        """
        if not HAVE_JINJA2:
            raise CuckooReportError("Failed to generate HTML report: "
                                    "Jinja2 Python library is not installed")

        shots_path = os.path.join(self.analysis_path, "shots")
        if os.path.exists(shots_path):
            shots = []
            counter = 1
            for shot_name in os.listdir(shots_path):
                if not shot_name.endswith(".jpg"):
                    continue

                shot_path = os.path.join(shots_path, shot_name)

                if os.path.getsize(shot_path) == 0:
                    continue

                shot = {}
                shot["id"] = os.path.splitext(File(shot_path).get_name())[0]
                shot["data"] = base64.b64encode(open(
                    shot_path, "rb").read()).decode("utf-8")
                shots.append(shot)

                counter += 1

            shots.sort(key=lambda shot: shot["id"])
            results["shots"] = shots
        else:
            results["shots"] = []

        env = Environment(autoescape=True)
        env.loader = FileSystemLoader(os.path.join(CUCKOO_ROOT, "data",
                                                   "html"))

        try:
            tpl = env.get_template("report.html")
            html = tpl.render({"results": results, "summary_report": False})
        except UndefinedError as e:
            raise CuckooReportError(
                "Failed to generate summary HTML report: {} ".format(e))
        except TemplateNotFound as e:
            raise CuckooReportError(
                "Failed to generate summary HTML report: {} {} ".format(
                    e, e.name))
        except (TemplateSyntaxError, TemplateAssertionError) as e:
            raise CuckooReportError(
                "Failed to generate summary HTML report: {} on {}, line {} ".
                format(e, e.name, e.lineno))
        try:
            with codecs.open(os.path.join(self.reports_path, "report.html"),
                             "w",
                             encoding="utf-8") as report:
                report.write(html)
        except (TypeError, IOError) as e:
            raise CuckooReportError("Failed to write HTML report: %s" % e)

        return True
コード例 #3
0
def env():
    environment = Environment()
    environment.loader = FileSystemLoader('.')
    return environment
コード例 #4
0
ファイル: tags.py プロジェクト: avsyap/fitbit
# -*- coding: utf-8 -*-
"""
    sphinx.util.tags
    ~~~~~~~~~~~~~~~~

    :copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

# (ab)use the Jinja parser for parsing our boolean expressions
from jinja2 import nodes
from jinja2.parser import Parser
from jinja2.environment import Environment

env = Environment()


class BooleanParser(Parser):
    """
    Only allow condition exprs and/or/not operations.
    """
    def parse_compare(self):
        token = self.stream.current
        if token.type == 'name':
            if token.value in ('true', 'false', 'True', 'False'):
                node = nodes.Const(token.value in ('true', 'True'),
                                   lineno=token.lineno)
            elif token.value in ('none', 'None'):
                node = nodes.Const(None, lineno=token.lineno)
            else:
                node = nodes.Name(token.value, 'load', lineno=token.lineno)
コード例 #5
0
    def generate_html(self, file_desc):
        """Generate a json file from a memory map for D3

        Positional arguments:
        file_desc - the file to write out the final report to
        """
        tree_text = {"name": ".text", "value": 0, "delta": 0}
        tree_bss = {"name": ".bss", "value": 0, "delta": 0}
        tree_data = {"name": ".data", "value": 0, "delta": 0}
        for name, dct in self.modules.items():
            cur_text = tree_text
            cur_bss = tree_bss
            cur_data = tree_data
            modules = name.split(sep)
            while True:
                try:
                    cur_text["value"] += dct['.text']
                    cur_text["delta"] += dct['.text']
                except KeyError:
                    pass
                try:
                    cur_bss["value"] += dct['.bss']
                    cur_bss["delta"] += dct['.bss']
                except KeyError:
                    pass
                try:
                    cur_data["value"] += dct['.data']
                    cur_data["delta"] += dct['.data']
                except KeyError:
                    pass
                if not modules:
                    break
                next_module = modules.pop(0)
                cur_text = self._move_up_tree(cur_text, next_module)
                cur_data = self._move_up_tree(cur_data, next_module)
                cur_bss = self._move_up_tree(cur_bss, next_module)
        if self.old_modules:
            for name, dct in self.old_modules.items():
                cur_text = tree_text
                cur_bss = tree_bss
                cur_data = tree_data
                modules = name.split(sep)
                while True:
                    try:
                        cur_text["delta"] -= dct['.text']
                    except KeyError:
                        pass
                    try:
                        cur_bss["delta"] -= dct['.bss']
                    except KeyError:
                        pass
                    try:
                        cur_data["delta"] -= dct['.data']
                    except KeyError:
                        pass
                    if not modules:
                        break
                    next_module = modules.pop(0)
                    if not any(
                        cld['name'] == next_module
                        for cld in cur_text['children']
                    ):
                        break
                    cur_text = self._move_up_tree(cur_text, next_module)
                    cur_data = self._move_up_tree(cur_data, next_module)
                    cur_bss = self._move_up_tree(cur_bss, next_module)

        tree_rom = {
            "name": "ROM",
            "value": tree_text["value"] + tree_data["value"],
            "delta": tree_text["delta"] + tree_data["delta"],
            "children": [tree_text, tree_data]
        }
        tree_ram = {
            "name": "RAM",
            "value": tree_bss["value"] + tree_data["value"],
            "delta": tree_bss["delta"] + tree_data["delta"],
            "children": [tree_bss, tree_data]
        }

        jinja_loader = FileSystemLoader(dirname(abspath(__file__)))
        jinja_environment = Environment(loader=jinja_loader,
                                        undefined=StrictUndefined)

        template = jinja_environment.get_template("memap_flamegraph.html")
        name, _ = splitext(basename(file_desc.name))
        if name.endswith("_map"):
            name = name[:-4]
        if self.tc_name:
            name = "%s %s" % (name, self.tc_name)
        data = {
            "name": name,
            "rom": json.dumps(tree_rom),
            "ram": json.dumps(tree_ram),
        }
        file_desc.write(template.render(data))
        return None
コード例 #6
0
ファイル: server.py プロジェクト: zack53/pyoidc
def main():
    parser = argparse.ArgumentParser(description='Example OIDC Provider.')
    parser.add_argument("-p", "--port", default=80, type=int)
    parser.add_argument("-b", "--base", default="https://localhost", type=str)
    parser.add_argument("-d", "--debug", action="store_true")
    parser.add_argument("settings")
    args = parser.parse_args()

    # Load configuration
    with open(args.settings, "r") as f:
        settings = yaml.load(f)

    issuer = args.base.rstrip("/")

    template_dirs = settings["server"].get("template_dirs", "templates")
    jinja_env = Environment(loader=FileSystemLoader(template_dirs))
    authn_broker, auth_routing = setup_authentication_methods(settings["authn"],
                                                              jinja_env)

    # Setup userinfo
    userinfo_conf = settings["userinfo"]
    cls = make_cls_from_name(userinfo_conf["class"])
    i = cls(**userinfo_conf["kwargs"])
    userinfo = UserInfo(i)

    client_db = {}
    session_db = create_session_db(issuer,
                                   secret=rndstr(32), password=rndstr(32))
    provider = Provider(issuer, session_db, client_db, authn_broker,
                        userinfo, AuthzHandling(), verify_client, None)
    provider.baseurl = issuer
    provider.symkey = rndstr(16)

    # Setup keys
    path = os.path.join(os.path.dirname(__file__), "static")
    try:
        os.makedirs(path)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise e
        pass
    jwks = keyjar_init(provider, settings["provider"]["keys"])
    name = "jwks.json"
    with open(os.path.join(path, name), "w") as f:
        f.write(json.dumps(jwks))

    #TODO: I take this out and it still works, what was this for?
    #provider.jwks_uri.append(
    #    "{}/static/{}".format(provider.baseurl, name))

    # Mount the WSGI callable object (app) on the root directory
    app_routing = setup_endpoints(provider)
    app_routing["/.well-known/openid-configuration"] = pyoidcMiddleware(
        provider.providerinfo_endpoint)
    app_routing["/.well-known/webfinger"] = pyoidcMiddleware(
        partial(_webfinger, provider))
    routing = dict(list(auth_routing.items()) + list(app_routing.items()))
    routing["/static"] = make_static_handler(path)
    dispatcher = WSGIPathInfoDispatcher(routing)
    server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', args.port), dispatcher)

    # Setup SSL
    if provider.baseurl.startswith("https://"):
        server.ssl_adapter = BuiltinSSLAdapter(
        settings["server"]["cert"], settings["server"]["key"],
        settings["server"]["cert_chain"])

    # Start the CherryPy WSGI web server
    try:
        print("Server started: {}".format(issuer))
        server.start()
    except KeyboardInterrupt:
        server.stop()
コード例 #7
0
    import psycopg2
except ImportError:
    from psycopg2ct import compat
    compat.register()
    import psycopg2

from myflick.routes import url_map
from myflick.db.mappers import Session

project_path = dirname(dirname(dirname(__file__)))

# set up template environment
templates = pathjoin(project_path, 'templates')
template_cache = environ['OPENSHIFT_DATA_DIR'] + 'template_cache'
tpl_env = Environment(loader=FileSystemLoader(templates),
                      bytecode_cache=FileSystemBytecodeCache(template_cache),
                      auto_reload=int(environ['TEMPLATES_AUTORELOAD']))
tpl_env.globals.update({'url_quote': url_quote})

appspace = {}
controllers_path = 'myflick.controllers.'
url_bind = url_map.bind_to_environ


def dispatch(urls):
    """Returns controller class, action name and args given match on the rules"""
    try:
        endpoint, args = urls.match()
    except HTTPException, exc:
        sys.stderr.write('Routing error:\n    %s\n' % str(exc))
        endpoint, args = 'index|notfound', {}
コード例 #8
0
    def render_templates(self):
        env = Environment()
        env.loader = FileSystemLoader('./templates')
        tmpl = env.get_template('base.html')

        return tmpl.render(self.services)
コード例 #9
0
        super(MarkdownExtension, self).__init__(environment)
        environment.extend(markdowner=markdown.Markdown(
            extensions=['codehilite']))

    def parse(self, parser):
        lineno = parser.stream.next().lineno
        body = parser.parse_statements(['name:endmarkdown'], drop_needle=True)
        return nodes.CallBlock(self.call_method('_markdown_support'), [], [],
                               body).set_lineno(lineno)

    def _markdown_support(self, caller):
        return self.environment.markdowner.convert(caller()).strip()


# template's environment
env = Environment(extensions=[MarkdownExtension])


def wrap_jinja2(content, layout):
    """Wrap the markdown text with Jinja2 headers.
    """
    return '{% extends "' + layout + '" %}\n' +\
           '{% block postcontent %}\n' +\
           '{% markdown %}\n' +\
           content + '\n' +\
           '{% endmarkdown %}\n' +\
           '{% endblock %}'


def strip_jinja2(body):
    """Strip jinja2 strings from the document.
コード例 #10
0
 def setUp(self):
     os.mkdir('tests/www/')
     self.env = Environment()
     self.env.loader = FileSystemLoader('tests/files/')
コード例 #11
0
class SystemCSerializer(SystemCSerializer_value, SystemCSerializer_type,
                        SystemCSerializer_statements, SystemCSerializer_ops,
                        GenericSerializer):
    """
    Serialized used to convert HWT design to SystemC code
    """
    fileExtension = '.cpp'
    _keywords_dict = {kw: LangueKeyword() for kw in SYSTEMC_KEYWORDS}
    env = Environment(
        loader=PackageLoader('hwt', 'serializer/systemC/templates'))
    moduleTmpl = env.get_template('module.cpp.template')
    methodTmpl = env.get_template("method.cpp.template")
    ifTmpl = env.get_template("if.cpp.template")
    switchTmpl = env.get_template("switch.cpp.template")

    @classmethod
    def getBaseContext(cls):
        return SystemCCtx(cls.getBaseNameScope(), 0, None, None)

    @classmethod
    def comment(cls, comentStr):
        return "\n".join(["/*", comentStr, "*/"])

    @classmethod
    def PortItem(cls, p, ctx):
        d = cls.DIRECTION(p.direction)
        p.name = ctx.scope.checkedName(p.name, p)
        p.getInternSig().name = p.name
        if isinstance(p.getInternSig()._interface, Clk):
            return "sc_%s_clk %s;" % (d, p.name)

        return "sc_%s<%s> %s;" % (d, cls.HdlType(p._dtype, ctx), p.name)

    @classmethod
    def DIRECTION(cls, d):
        return d.name.lower()

    @classmethod
    def Entity(cls, ent, ctx):
        cls.Entity_prepare(ent, ctx)
        # [TODO] separate declarations from definitions
        doc = ent.__doc__
        if doc and id(doc) != id(Entity.__doc__):
            return cls.comment(doc) + "\n"
        return ""

    @classmethod
    def GenericItem(cls, g, ctx):
        # [TODO] params currently serialized evaluated
        return ""

    @classmethod
    def Architecture_var(cls, v, serializerVars, extraTypes,
                         extraTypes_serialized, ctx, childCtx):
        """
        :return: list of extra discovered processes
        """
        v.name = ctx.scope.checkedName(v.name, v)
        serializedVar = cls.SignalItem(v, childCtx, declaration=True)
        serializerVars.append(serializedVar)

    @classmethod
    def Architecture(cls, arch, ctx):
        serializerVars = []
        procs = []
        extraTypes = set()
        extraTypes_serialized = []
        arch.variables.sort(key=lambda x: (x.name, x._instId))
        arch.componentInstances.sort(key=lambda x: x._name)

        childCtx = ctx.withIndent()
        ports = [cls.PortItem(pi, childCtx) for pi in arch.entity.ports]

        extraProcesses = []
        for v in arch.variables:
            cls.Architecture_var(v, serializerVars, extraTypes,
                                 extraTypes_serialized, ctx, childCtx)

        arch.processes.extend(extraProcesses)
        arch.processes.sort(key=lambda x: (x.name, maxStmId(x)))
        for p in arch.processes:
            procs.append(cls.HWProcess(p, childCtx))

        # architecture names can be same for different entities
        # arch.name = scope.checkedName(arch.name, arch, isGlobal=True)
        processesSensitivity = []
        sensitivityCtx = ctx.forSensitivityList()
        for p in arch.processes:
            sens = [cls.asHdl(s, sensitivityCtx) for s in p.sensitivityList]
            processesSensitivity.append((p.name, sens))

        return cls.moduleTmpl.render(
            processesSensitivity=processesSensitivity,
            name=arch.getEntityName(),
            ports=ports,
            signals=serializerVars,
            extraTypes=extraTypes_serialized,
            processes=procs,
            processObjects=arch.processes,
            componentInstances=arch.componentInstances,
            DIRECTION=DIRECTION,
        )
コード例 #12
0
from hwt.hdlObjects.constants import SENSITIVITY
from hwt.hdlObjects.statements import IfContainer
from hwt.hdlObjects.types.enum import Enum
from hwt.hdlObjects.types.enumVal import EnumVal
from hwt.hdlObjects.value import Value
from hwt.serializer.exceptions import SerializerException
from hwt.serializer.nameScope import LangueKeyword, NameScope
from hwt.serializer.simModel.value import SimModelSerializer_value
from hwt.serializer.simModel.ops import SimModelSerializer_ops
from hwt.serializer.simModel.types import SimModelSerializer_types
from hwt.serializer.utils import maxStmId
from hwt.synthesizer.param import evalParam
from hwt.synthesizer.rtlLevel.mainBases import RtlSignalBase
from hwt.hdlObjects.types.bits import Bits

env = Environment(loader=PackageLoader('hwt', 'serializer/simModel/templates'))
unitTmpl = env.get_template('modelCls.py')
processTmpl = env.get_template('process.py')
ifTmpl = env.get_template("if.py")

simCls_reservedWords = [
    'sim', 'self'
    'reload', 'vecT', 'Array', 'ArrayVal', 'convertBits__val', 'BitsVal',
    'SLICE', 'Enum'
    'DIRECTION', 'SENSITIVITY', 'convertSimInteger__val', 'simHInt', 'SIM_INT',
    'simBitsT', 'SIM_BIT', 'convertSimBits__val', 'SimModel', 'sensitivity',
    'connectSimPort', 'simEvalCond', 'mkUpdater', 'mkArrayUpdater'
    'Concat', 'power'
    'RtlNetlist'
    'SimSignal'
    'SliceVal'
コード例 #13
0
    def config(self):
        """Performs configurations. Raises exception on error."""
        config_path = Conf.get(self.index, "cortx>software>consul>config_path",
                               "/etc/consul.d")
        data_path = Conf.get(self.index, "cortx>software>consul>data_path",
                             "/opt/consul")
        os.makedirs(config_path, exist_ok=True)
        os.makedirs(data_path, exist_ok=True)
        content = ""
        with open("/usr/lib/systemd/system/consul.service", "r+") as f:
            content = f.read()
            content = re.sub("config-dir=.*", f"config-dir={config_path}",
                             content)
            content = re.sub(
                "ConditionFileNotEmpty=.*",
                f"ConditionFileNotEmpty={config_path}/consul.hcl", content)
            f.seek(0)
            f.truncate()
            f.write(content)

        command = "systemd-analyze verify consul.service"
        _, err, returncode = SimpleProcess(command).run()
        if returncode != 0:
            raise ConsulSetupError(
                returncode,
                "Consul Setup systemd service file validation failed with error: %s",
                err)

        command = "systemctl daemon-reload"
        _, err, returncode = SimpleProcess(command).run()
        if returncode != 0:
            raise ConsulSetupError(
                returncode,
                "Consul Setup systemd daemon-reload failed with error: %s",
                err)

        bind_addr = Conf.get(
            self.index,
            f"server_node>{Conf.machine_id}>network>data>private_interfaces[0]"
        )
        # server_node_fqdn have fqdn of nodes on which consul will run in server
        # mode. It is used for retry-join config
        server_node_fqdns = []
        bootstrap_expect = 0
        is_server_node = False
        for machine_id in Conf.get(self.index, "server_node").keys():
            if "consul_server" in Conf.get(self.index,
                                           f"server_node>{machine_id}>roles",
                                           []):
                bootstrap_expect += 1
                if machine_id != Conf.machine_id:
                    server_node_fqdns.append(
                        Conf.get(
                            self.index,
                            f"server_node>{machine_id}>network>data>private_fqdn"
                        ))
                else:
                    is_server_node = True

        env = Environment(
            loader=FileSystemLoader("/opt/seagate/cortx/utils/conf"))
        template = env.get_template('consul.hcl.tmpl')
        template.stream(bind_addr=bind_addr,
                        data_dir=data_path,
                        retry_join=json.dumps(server_node_fqdns),
                        server=str(is_server_node).lower(),
                        bootstrap_expect=bootstrap_expect).dump(
                            f"{config_path}/consul.hcl")

        command = f"consul validate {config_path}/consul.hcl"

        _, err, returncode = SimpleProcess(command).run()
        if returncode != 0:
            raise ConsulSetupError(
                returncode,
                "Consul Setup config file %s validation failed with error :%s",
                f"{config_path}/consul.hcl", err)
        command = f"chown -R consul:consul {config_path} {data_path}"
        _, err, returncode = SimpleProcess(command).run()
        if returncode != 0:
            raise ConsulSetupError(
                returncode,
                "Consul Setup changing ownership failed for %s %s with error: %s",
                config_path, data_path, err)
コード例 #14
0
def create_env():
    x = ((FileSystemLoader, django_settings.TEMPLATE_DIRS),
         (PackageLoader, django_settings.INSTALLED_APPS))
    loaders = [loader(p) for loader, places in x for p in places]
    env = Environment(loader=ChoiceLoader(loaders), extensions=[ViewletExtension])
    return env
コード例 #15
0
ファイル: wordshifts.py プロジェクト: andyreagan/sentidict
from IPython.display import Javascript, HTML, publish_display_data
from IPython import get_ipython
from os.path import isfile, isdir, abspath, join, dirname
from .utils import *
from .dictionaries import *
from jinja2 import FileSystemLoader
from jinja2.environment import Environment

env = Environment(loader=FileSystemLoader(dirname(__file__)))

# borrowed from MPLD3
WWW_JS_DIR = "https://andyreagan.github.io/hedotools/js/"
# don't include version, that's handled by hedotools
D3_URL = WWW_JS_DIR + "d3"
HEDOTOOLS_URL = WWW_JS_DIR + "hedotools"

# we'll have copied to the _working directory_
LOCAL_JS_DIR = "static"  # join(getcwd(), "static")
D3_LOCAL = join(LOCAL_JS_DIR, "d3.min.js")
HEDOTOOLS_LOCAL = join(LOCAL_JS_DIR, "hedotools.min.js")


def require_JS(local=False):
    if local:
        flat = {
            "application/javascript":
            env.get_template("templates/jupyter-require.js").render({
                "d3_url":
                D3_LOCAL,
                "hedotools_url":
                HEDOTOOLS_LOCAL
コード例 #16
0
from tickee.db.models.tickettype import TicketTypeEventPartAssociation, \
    TicketType
from tickee.db.models.user import User
from tickee.events.manager import lookup_event_by_id
from tickee.orders.states import PURCHASED
from tickee.users.manager import lookup_user_by_id
from tickee.users.tasks import mail_user
import datetime
import logging
import random
import sqlahelper
import transaction

Session = sqlahelper.get_session()

env = Environment(loader=PackageLoader('tickee', 'templates'))
blogger = logging.getLogger('blm.events')


@task(name="routine.event_in_48_hours_reminder", ignore_result=True)
def event_in_48_hours_reminder():
    """Looks up all happening in 48 hours and sends notification mails"""
    # find all events happening between now and 48 hours
    now = datetime.datetime.utcnow()
    in_48_hours = now + datetime.timedelta(hours=48)
    events = Session.query(Event).join(EventPart).filter(EventPart.starts_on >= now)\
                                                 .filter(EventPart.starts_on < in_48_hours).all()
    for event in set(events):
        # only send if not sent before
        if not event.meta.get('notifications_sent', False):
            event_notification.delay(event.id)
コード例 #17
0
ファイル: ext.py プロジェクト: fhelmli/homeNOWG2
def babel_extract(fileobj, keywords, comment_tags, options):
    """Babel extraction method for Jinja templates.

    .. versionchanged:: 2.3
       Basic support for translation comments was added.  If `comment_tags`
       is now set to a list of keywords for extraction, the extractor will
       try to find the best preceeding comment that begins with one of the
       keywords.  For best results, make sure to not have more than one
       gettext call in one line of code and the matching comment in the
       same line or the line before.

    .. versionchanged:: 2.5.1
       The `newstyle_gettext` flag can be set to `True` to enable newstyle
       gettext calls.

    .. versionchanged:: 2.7
       A `silent` option can now be provided.  If set to `False` template
       syntax errors are propagated instead of being ignored.

    :param fileobj: the file-like object the messages should be extracted from
    :param keywords: a list of keywords (i.e. function names) that should be
                     recognized as translation functions
    :param comment_tags: a list of translator tags to search for and include
                         in the results.
    :param options: a dictionary of additional options (optional)
    :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
             (comments will be empty currently)
    """
    extensions = set()
    for extension in options.get('extensions', '').split(','):
        extension = extension.strip()
        if not extension:
            continue
        extensions.add(import_string(extension))
    if InternationalizationExtension not in extensions:
        extensions.add(InternationalizationExtension)

    def getbool(options, key, default=False):
        return options.get(key, str(default)).lower() in \
            ('1', 'on', 'yes', 'true')

    silent = getbool(options, 'silent', True)
    environment = Environment(
        options.get('block_start_string', BLOCK_START_STRING),
        options.get('block_end_string', BLOCK_END_STRING),
        options.get('variable_start_string', VARIABLE_START_STRING),
        options.get('variable_end_string', VARIABLE_END_STRING),
        options.get('comment_start_string', COMMENT_START_STRING),
        options.get('comment_end_string', COMMENT_END_STRING),
        options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,
        options.get('line_comment_prefix') or LINE_COMMENT_PREFIX,
        getbool(options, 'trim_blocks', TRIM_BLOCKS),
        getbool(options, 'lstrip_blocks', LSTRIP_BLOCKS),
        NEWLINE_SEQUENCE,
        getbool(options, 'keep_trailing_newline', KEEP_TRAILING_NEWLINE),
        frozenset(extensions),
        cache_size=0,
        auto_reload=False)

    if getbool(options, 'newstyle_gettext'):
        environment.newstyle_gettext = True

    source = fileobj.read().decode(options.get('encoding', 'utf-8'))
    try:
        node = environment.parse(source)
        tokens = list(environment.lex(environment.preprocess(source)))
    except TemplateSyntaxError as e:
        if not silent:
            raise
        # skip templates with syntax errors
        return

    finder = _CommentFinder(tokens, comment_tags)
    for lineno, func, message in extract_from_ast(node, keywords):
        yield lineno, func, message, finder.find_comments(lineno)
コード例 #18
0
from os import path

from jinja2.environment import Environment
from jinja2.loaders import FileSystemLoader

_templates_path = path.dirname(__file__)
_templates_path = path.join(_templates_path, 'templates')

_env = Environment(loader=FileSystemLoader(_templates_path))


def render(template, **context):
    rendered = _env.get_template(template).render(context)
    return rendered
コード例 #19
0
    def process_layout(layout_schema=None, interactions=None):
        env = Environment()

        env.loader = FileSystemLoader(PORTAL_ROOT + '/templates')
        tmpl_unparsed = env.get_template('ion_ux.html').render()
        tmpl = ET.fromstring(tmpl_unparsed.encode('utf-8'))

        # Body element for appending scripts/templates
        body_elmt = tmpl.find('body')

        layout_schema = LayoutApi.get_new_layout_schema()

        resource_types = []
        metadata_processed = []

        # Do not create HTML elmts for subattributes
        excluded_sub_attributes = ['table_ooi', 'chart_ooi']

        for view_id in DEFINED_VIEWS:
            view = layout_schema['spec']['elements'][view_id]
            script_elmt = ET.SubElement(body_elmt, 'script')
            script_elmt.set('id', view_id)
            script_elmt.set('type', 'text/template')

            # BEGIN BASIC PAGE STRUCTURE
            # Creating page structure via Twitter Bootstrap
            # conventions. This will be optimized.

            # Fluid row to hold page heading
            row_heading = ET.SubElement(script_elmt, 'div')
            row_heading.set('class', 'row-fluid heading')

            # Fluid row to hold columns for main page
            row_container = ET.SubElement(script_elmt, 'div')
            row_container.set('class', 'row-fluid')

            # Page heading
            v00_elmt = ET.SubElement(row_heading, 'div')
            v00_elmt.set('class', 'v00 span12')
            group_h1_elmt = ET.SubElement(v00_elmt, 'h1')
            group_h1_elmt.text = view['label']

            # Page content - left and right columns
            v01_elmt = ET.SubElement(row_container, 'div')
            v01_elmt.set('class', 'v01 span3')
            v02_elmt = ET.SubElement(row_container, 'div')
            v02_elmt.set('class', 'v02 span9')

            # END BASIC PAGE STRUCTURE

            # GROUPS -------------------------------------------------------------------

            groups = {}
            for gr_idx, gr_element in enumerate(view['embed']):
                group_elid = gr_element['elid']
                group_link_id = group_elid + str(randint(0, 1000))
                group_position = gr_element['pos']
                group = layout_schema['spec']['elements'][group_elid]

                # Find the page element for the group
                if group_position == 'V00':
                    parent_elmt = v00_elmt
                elif group_position == 'V01':
                    parent_elmt = v01_elmt
                else:
                    parent_elmt = v02_elmt

                # Active boolean for CSS
                group_is_active = False
                if not group_position in groups.keys():
                    group_is_active = True

                    group_container_elmt = ET.SubElement(parent_elmt, 'div')
                    group_container_elmt.attrib['id'] = group_elid
                    group_container_elmt.attrib['class'] = 'group'

                    # Create ul for navigation
                    group_ul_elmt = ET.SubElement(group_container_elmt, 'ul')
                    group_ul_elmt.attrib['class'] = 'nav nav-tabs'

                    # Create group_block container
                    group_block_container_elmt = ET.SubElement(
                        group_container_elmt, 'div')
                    group_block_container_elmt.attrib['class'] = 'tab-content'

                    # Track positioning elements in dict
                    groups.update({
                        group_position: {
                            'ul_elmt':
                            group_ul_elmt,
                            'group_container_elmt':
                            group_container_elmt,
                            'group_block_container_elmt':
                            group_block_container_elmt
                        }
                    })
                else:
                    group_ul_elmt = groups[group_position]['ul_elmt']
                    group_block_container_elmt = groups[group_position][
                        'group_block_container_elmt']

                # Create li and a elements
                group_li_elmt = ET.SubElement(group_ul_elmt, 'li')
                group_a_elmt = ET.SubElement(group_li_elmt, 'a')
                group_a_elmt.attrib['href'] = '#' + group_link_id
                group_a_elmt.attrib['data-toggle'] = 'tab'
                # FOR TROUBLESHOOTING
                # group_a_elmt.text = group['label'] + ' (' + group_position + ')'
                group_a_elmt.text = group['label']

                # Create group div inside of tab-content
                group_elmt = ET.SubElement(group_block_container_elmt, 'div')
                group_elmt.attrib['id'] = group_link_id
                group_elmt.attrib['class'] = 'tab-pane row-fluid'

                # END GROUPS -------------------------------------------------------------------

                # Blocks
                for bl_element in group['embed']:
                    block_elid = bl_element['elid']
                    block_position = bl_element['pos']
                    block = layout_schema['spec']['elements'][block_elid]

                    if block.has_key('ie'):
                        block_res_type = block['ie']['ie_name']
                    else:
                        block_res_type = ''

                    block_widget_id = block['wid']
                    block_widget = layout_schema['spec']['widgets'][
                        block_widget_id]
                    block_widget_type = block_widget['name']

                    if not block_res_type in resource_types:
                        resource_types.append(block_res_type)

                    # Set li class based on block_res_type
                    li_css_class = group_li_elmt.get('class')
                    group_css_class = group_elmt.get('class')
                    if li_css_class is None:  # Catch empty/unset class on first item
                        li_css_class = ''
                    if not block_res_type in li_css_class:
                        li_css_class += ' %s' % block_res_type  #block['ie']['ie_name']
                    if group_is_active:
                        if not 'active' in li_css_class:
                            li_css_class += ' active'
                        if not 'active' in group_css_class:
                            group_css_class += ' active'

                    group_li_elmt.attrib['class'] = li_css_class
                    group_elmt.attrib['class'] = group_css_class

                    # Set div class based on block_res_type
                    # block_css_class = group_elmt.get('class')
                    block_css_class = None
                    if block_css_class is None:
                        block_css_class = ''
                    if not block_res_type in block_css_class:
                        block_css_class += ' %s' % block_res_type
                    if group_is_active:
                        block_css_class += ' active'

                    ##################################################################
                    # BLOCK LAYOUT
                    # Determine layout structure of blocks within group
                    ##################################################################

                    wide_container = False
                    if block['embed']:
                        for at_element in block['embed']:
                            attribute = layout_schema['spec']['elements'][
                                at_element['elid']]
                            attribute_widget_type = layout_schema['spec'][
                                'widgets'][attribute['wid']]['name']
                            if attribute_widget_type in ('table_ooi',
                                                         'chart_ooi'):
                                wide_container = True
                    if wide_container:
                        block_container = ET.SubElement(group_elmt, 'div')
                        block_container.attrib['class'] = 'row-fluid'
                        block_elmt = ET.SubElement(block_container, 'div')
                        block_css_class += ' span12'
                    else:
                        block_elmt = ET.SubElement(group_elmt, 'div')
                        block_css_class += ' block'
                        if group_position not in ('V00', 'V01'):
                            block_css_class += ' span3'

                    # Set block class based on attribute widget type
                    # if group_position not in ('V00', 'V01') and attribute_widget_type not in ('table_ooi', 'chart_ooi'):
                    #     block_css_class += ' span3'
                    # elif attribute_widget_type in ('table_ooi', 'chart_ooi'):
                    #     block_css_class += ' clear span12'

                    block_elmt.attrib['class'] = block_css_class
                    block_elmt.attrib['style'] = 'display:none;'
                    block_elmt.attrib['id'] = block_elid

                    # FOR TROUBLESHOOTING
                    # block_h3_elmt.text = 'Block: %s (%s) (%s) (%s)' % (block['label'], block_elid, block_widget['name'], block_position)
                    if not group_position == 'V00':
                        if not attribute_widget_type == 'table_ooi':
                            block_h3_elmt = ET.SubElement(block_elmt, 'h3')
                            block_h3_elmt.text = block['label']

                    block_container_elmt = ET.SubElement(block_elmt, 'div')
                    # block_container_elmt.set('class', 'content-wrapper')

                    # Attributes
                    for at_element in block['embed']:
                        attribute_elid = at_element['elid']
                        attribute_position = at_element['pos']
                        attribute_data_path = at_element['dpath']
                        attribute_level = at_element['olevel']
                        attribute = layout_schema['spec']['elements'][
                            attribute_elid]

                        # Widget
                        attribute_widget_id = attribute['wid']
                        attribute_widget_type = layout_schema['spec'][
                            'widgets'][attribute_widget_id]['name']

                        attribute_elmt = ET.SubElement(block_container_elmt,
                                                       'div')

                        attribute_elmt.set('id', attribute_elid)
                        attribute_elmt.set('data-position', attribute_position)
                        attribute_elmt.set('data-path', attribute_data_path)
                        attribute_elmt.set('data-level', attribute_level)
                        attribute_elmt.set('data-label', attribute['label'])

                        # Set additional class based on spec['graphics] name
                        # corresponding CSS class applies the sprite.
                        if attribute_widget_type == 'image_ooi':
                            image_class = layout_schema['spec']['graphics'][
                                attribute['gfx']]['name']
                            attribute_elmt.set(
                                'class',
                                '%s %s' % (attribute_widget_type, image_class))
                            attribute_elmt.text = '&nbsp;'
                        else:
                            attribute_elmt.set('class', attribute_widget_type)
                            # FOR TROUBLESHOOTING
                            # attribute_elmt.text = 'Attribute: %s (%s) (%s) (%s) (%s)' % (attribute['label'], attribute['name'], attribute_elid, attribute_widget_type, attribute_position)
                            attribute_elmt.text = '%s (%s)' % (
                                attribute['label'], attribute['name'])

                        if attribute_widget_type not in ('table_ooi',
                                                         'chart_ooi'):
                            block_container_elmt.set('class',
                                                     'content-wrapper')

                        # Generate metadata for nested elements, ex. tables and attribute groups
                        if attribute_widget_type in (
                                'table_ooi', 'attribute_group_ooi'
                        ) and attribute_elid not in metadata_processed:
                            metadata_processed.append(attribute_elid)
                            metadata = []
                            for embedded_attribute in attribute['embed']:
                                embedded_object = layout_schema['spec'][
                                    'elements'][embedded_attribute['elid']]
                                embedded_widget_type = layout_schema['spec'][
                                    'widgets'][
                                        embedded_attribute['wid']]['name']

                                metadata_items = [
                                    embedded_widget_type,
                                    embedded_object['label'],
                                    embedded_attribute['dpath'],
                                    embedded_attribute['pos'],
                                    embedded_attribute['olevel']
                                ]
                                if attribute_widget_type == 'attribute_group_ooi':
                                    meta_elmt_id = 'ATTRIBUTE_GROUP_' + attribute_elid
                                    metadata_items.append(
                                        embedded_attribute['elid'])
                                    metadata_items.append(
                                        embedded_attribute['dpath'])
                                elif attribute_widget_type == 'table_ooi':
                                    meta_elmt_id = 'TABLE_' + attribute_elid

                                metadata.append(metadata_items)

                            # Append metadata to body as a JSON script
                            meta_elmt = ET.SubElement(body_elmt, 'script')
                            meta_elmt.set('id', meta_elmt_id)
                            meta_elmt.text = "var %s=%s" % (
                                meta_elmt_id, json.dumps(metadata))

                        # if attribute['embed'] and not attribute_widget_type in excluded_sub_attributes:
                        #     for sub_at_element in attribute['embed']:
                        #         sub_attribute_elid = sub_at_element['elid']
                        #         sub_attribute_position = sub_at_element['pos']
                        #         sub_attribute_data_path = sub_at_element['dpath']
                        #         sub_attribute_level = sub_at_element['olevel']
                        #         sub_attribute = layout_schema['spec']['elements'][sub_attribute_elid]
                        #
                        #         sub_attribute_widget_id = sub_attribute['wid']
                        #         sub_attribute_widget_type = layout_schema['spec']['widgets'][sub_attribute_widget_id]['name']
                        #
                        #         sub_attribute_elmt = ET.SubElement(block_elmt, 'div')
                        #         sub_attribute_elmt.set('id', sub_attribute_elid)
                        #         sub_attribute_elmt.set('class', sub_attribute_widget_type)
                        #         sub_attribute_elmt.set('data-path', sub_attribute_data_path)
                        #         sub_attribute_elmt.set('data-level', sub_attribute_level)
                        #         sub_attribute_elmt.set('data-label', sub_attribute['label'])
                        #
                        #         # sub_attribute_elmt.text = '%s (%s) (%s) (%s) (%s)' % (sub_attribute['label'], sub_attribute['name'], sub_attribute_elid, sub_attribute_widget_type, sub_attribute_position)
                        #         sub_attribute_elmt.text = '%s (%s)' % (sub_attribute['label'], sub_attribute['name'])

        layout_elmt = ET.SubElement(body_elmt, 'script')
        layout_elmt.set('id', 'layout')
        layout_elmt.text = "var LAYOUT=%s;" % json.dumps(layout_schema)

        resource_types_elmt = ET.SubElement(body_elmt, 'script')
        resource_types_elmt.set('id', 'resource_types')
        resource_types_elmt.text = "var RESOURCE_TYPES=%s" % json.dumps(
            resource_types)

        init_script_elmt = ET.Element('script')
        init_script_elmt.set('type', 'text/javascript')
        init_script_elmt.text = "$(function(){dyn_do_init();});"
        body_elmt.append(init_script_elmt)

        tmpl = ET.tostring(tmpl)
        h = HTMLParser.HTMLParser()
        return h.unescape(tmpl)
コード例 #20
0
ファイル: jinja_renderer.py プロジェクト: mplanchard/Kyoukai
 def environment(self) -> Environment:
     if self._environment is None:
         self._environment = Environment(loader=self._loader)
     return self._environment
コード例 #21
0
 def setUp(self):
     self.loader = FileSystemLoader(self.TEMPLATE_PATH)
     self.env = Environment(loader=self.loader,
                            autoescape=True,
                            extensions=['jinja2.ext.with_', 'jinja2.ext.autoescape'])
     self.temp_dir = tempfile.mkdtemp()
コード例 #22
0
def create_wta_network_report(file_prefix,
                              contrast_range,
                              num_trials,
                              reports_dir,
                              edesc,
                              regenerate_network_plots=True,
                              regenerate_trial_plots=True):

    make_report_dirs(reports_dir)

    report_info = Struct()
    report_info.edesc = edesc
    report_info.trials = []

    (data_dir, data_file_prefix) = os.path.split(file_prefix)

    total_trials = num_trials * len(contrast_range)
    trial_contrast = np.zeros([total_trials, 1])
    trial_max_bold = np.zeros(total_trials)
    trial_max_input = np.zeros([total_trials, 1])
    trial_max_rate = np.zeros([total_trials, 1])
    trial_rt = np.zeros([total_trials, 1])

    report_info.contrast_accuracy = np.zeros([len(contrast_range), 1])

    max_bold = []
    for j, contrast in enumerate(contrast_range):
        contrast_max_bold = []
        report_info.contrast_accuracy[j] = 0.0
        for i in range(num_trials):
            file_name = '%s.contrast.%0.4f.trial.%d.h5' % (file_prefix,
                                                           contrast, i)
            print('opening %s' % file_name)
            data = FileInfo(file_name)

            if not i:
                report_info.wta_params = data.wta_params
                report_info.voxel_params = data.voxel_params
                report_info.num_groups = data.num_groups
                report_info.trial_duration = data.trial_duration
                report_info.background_rate = data.background_rate
                report_info.stim_start_time = data.stim_start_time
                report_info.stim_end_time = data.stim_end_time
                report_info.network_group_size = data.network_group_size
                report_info.background_input_size = data.background_input_size
                report_info.task_input_size = data.task_input_size

            trial_idx = j * num_trials + i
            trial = create_trial_report(
                data,
                reports_dir,
                contrast,
                i,
                regenerate_plots=regenerate_trial_plots)
            trial_contrast[trial_idx] = trial.input_contrast
            if not math.isnan(trial.max_bold):
                trial_max_bold[trial_idx] = trial.max_bold
            else:
                if j > 1 and max_bold[j - 1] < 1.0 and max_bold[j - 2] < 1.0:
                    trial_max_bold[trial_idx] = max_bold[j - 1] + (
                        max_bold[j - 1] - max_bold[j - 2])
                elif j > 0 and max_bold[j - 1] < 1.0:
                    trial_max_bold[trial_idx] = max_bold[j - 1] * 2.0
                else:
                    trial_max_bold[trial_idx] = 1.0

            report_info.contrast_accuracy[j] += trial.correct

            trial_max_input[trial_idx] = trial.max_input
            trial_max_rate[trial_idx] = trial.max_rate
            trial_rt[trial_idx] = trial.rt
            report_info.trials.append(trial)

            contrast_max_bold.append(trial_max_bold[trial_idx])
        report_info.contrast_accuracy[j] /= float(num_trials)
        mean_contrast_bold = np.mean(np.array(contrast_max_bold))
        max_bold.append(mean_contrast_bold)

    clf = LinearRegression()
    clf.fit(trial_max_input, trial_max_rate)
    a = clf.coef_[0]
    b = clf.intercept_
    report_info.io_slope = a
    report_info.io_intercept = b
    report_info.io_r_sqr = clf.score(trial_max_input, trial_max_rate)

    furl = 'img/input_output_rate.png'
    fname = os.path.join(reports_dir, furl)
    report_info.input_output_rate_url = furl
    if regenerate_network_plots or not os.path.exists(fname):
        fig = plt.figure()
        plt.plot(trial_max_input, trial_max_rate, 'x')
        x_min = np.min(trial_max_input)
        x_max = np.max(trial_max_input)
        plt.plot([x_min, x_max], [x_min, x_max], '--')
        plt.plot([x_min, x_max], [a * x_min + b, a * x_max + b], '--')
        plt.xlabel('Max Input Rate')
        plt.ylabel('Max Population Rate')
        save_to_png(fig, fname)
        save_to_eps(fig, os.path.join(reports_dir,
                                      'img/input_output_rate.eps'))
        plt.close()

    report_info.bold = create_bold_report(
        reports_dir,
        trial_contrast,
        trial_max_bold,
        trial_max_rate,
        trial_rt,
        regenerate_plot=regenerate_network_plots)

    report_info.roc = create_roc_report(
        file_prefix,
        report_info.num_groups,
        contrast_range,
        num_trials,
        reports_dir,
        regenerate_plot=regenerate_network_plots)

    #create report
    template_file = 'wta_network_instance.html'
    env = Environment(loader=FileSystemLoader(TEMPLATE_DIR))
    template = env.get_template(template_file)

    output_file = 'wta_network.%s.html' % data_file_prefix
    fname = os.path.join(reports_dir, output_file)
    stream = template.stream(rinfo=report_info)
    stream.dump(fname)

    return report_info
コード例 #23
0
def render(template_name, folder='templates', **kwargs):
    env = Environment()
    env.loader = FileSystemLoader(folder)
    tmpl = env.get_template(template_name)
    return tmpl.render(**kwargs)
コード例 #24
0
def main():
    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
    password = getpass()

    cisco_rtr1 = {
        'hostname': '184.105.247.70',
        'device_type': 'ios',
        'password': password,
        'username': '******',
        'optional_args': {}
    }
    cisco_rtr2 = {
        'hostname': '184.105.247.71',
        'device_type': 'ios',
        'password': password,
        'username': '******',
        'optional_args': {}
    }
    arista_sw1 = {
        'hostname': '184.105.247.72',
        'device_type': 'eos',
        'password': password,
        'username': '******',
        'optional_args': {}
    }
    arista_sw2 = {
        'hostname': '184.105.247.73',
        'device_type': 'eos',
        'password': password,
        'username': '******',
        'optional_args': {}
    }
    jnpr_srx1 = {
        'hostname': '184.105.247.76',
        'device_type': 'junos',
        'password': password,
        'username': '******',
        'optional_args': {}
    }
    cisco_nxos = {
        'hostname': 'nxos1.twb-tech.com',
        'device_type': 'nxos',
        'password': password,
        'username': '******',
        'optional_args': {
            'port': '8443',
            'nxos_protocol': 'https'
        }
    }

    # devices = (cisco_rtr1, cisco_rtr2, arista_sw1, arista_sw2, jnpr_srx1, cisco_nxos)
    # devices = (cisco_rtr1, cisco_rtr2)
    devices = (cisco_rtr1, arista_sw1, cisco_nxos)

    napalm_conns = []

    template_vars = {
        'dns1': '1.1.1.1',
        'dns2': '8.8.8.8',
    }
    template_file = 'dns.j2'

    for a_device in devices:
        # print("\n")
        # pprint(a_device)
        print("\n")
        device_type = a_device.pop('device_type')
        template_path = '/home/tarmstrong/projects/pynetauto/7class/{}/'.format(
            device_type)
        dns_file = '{}/dns.txt'.format(template_path)

        jinja_env = Environment(undefined=StrictUndefined)
        jinja_env.loader = FileSystemLoader(template_path)
        template = jinja_env.get_template(template_file)
        dns_config = template.render(
            **template_vars)  ## why not (**template_vars)  ????

        with open(dns_file, 'w') as f:
            f.write(dns_config)

        print(" ********************  DEVICE START  **********************")
        print("The config created is as such: \n{}".format(dns_config))
        choice = input("Continue to commit changes? (y/n)")
        if choice == "y" or "yes":
            driver = get_network_driver(device_type)
            device = driver(**a_device)
            napalm_conns.append(device)
            print("\nDevice created! Host: {}".format(a_device['hostname']))
            device.open()
            print("\nDevice connection opened! Type: {}".format(device_type))
            print("Loading template")
            device.load_merge_candidate(filename=dns_file)
            print(device.compare_config())
            device.commit_config()

            ping_google(device)
            print("\n\n")
        else:
            print(
                "terminating operations... terminating operations... Abort... Abort"
            )
コード例 #25
0
# Environment is used for the following benefits -
# if your template is in a different folder then environment can be used (file handling)
# Also, if a variable is left blank it won't throw an error but just fill that part in the template with blank value. Environment will take care of this.
# if your template has reference to other templates, environment will make that possible as well.

from __future__ import unicode_literals, print_function
from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment

env = Environment(
    undefined=StrictUndefined
)  #Whenever any variable is undefined throw an error. This is useful for error handling.
env.loader = FileSystemLoader(
    [".", './Templates/']
)  #This is to take the file from a directory which is defined. It will look one at a time current directoty --> Template

my_vars = {"bgp_as": 22, "router_id": "1.1.1.1", "peer1": "20.20.20.20"}

template_file = 'bgp_config.j2'
template = env.get_template(template_file)
output = template.render(**my_vars)
print(output)
コード例 #26
0
ファイル: Templater.py プロジェクト: robkegeenen/5SIA0
 def loadTemplate(self, fname):
     #get template from file
     loader  = FileSystemLoader(os.path.dirname(fname))
     env     = Environment(loader=loader)
     relpath = os.path.relpath(fname, os.path.dirname(fname))
     self.template = env.get_template(relpath)
コード例 #27
0
    def run(self, results):
        """Writes report.
        @param results: Cuckoo results dict.
        @raise CuckooReportError: if fails to write report.
        """
        if not HAVE_JINJA2:
            raise CuckooReportError("Failed to generate summary HTML report: "
                                    "Jinja2 Python library is not installed")

        shots_path = os.path.join(self.analysis_path, "shots")
        if os.path.exists(shots_path):
            shots = []
            counter = 1
            for shot_name in os.listdir(shots_path):
                if not shot_name.endswith(".jpg"):
                    continue

                shot_path = os.path.join(shots_path, shot_name)

                if os.path.getsize(shot_path) == 0:
                    continue

                output = BytesIO()

                # resize the image to thumbnail size, as weasyprint can't handle resizing
                try:
                    img = Image.open(shot_path)
                    img = img.resize((150, 100), PIL.Image.ANTIALIAS)
                    img.save(output, format="JPEG")
                except Exception as e:
                    # print(e)
                    pass

                shot = {}
                shot["id"] = os.path.splitext(File(shot_path).get_name())[0]
                shot["data"] = base64.b64encode(
                    output.getvalue()).decode("utf-8")
                shots.append(shot)
                output.close()

                counter += 1

            shots.sort(key=lambda shot: shot["id"])
            results["shots"] = shots
        else:
            results["shots"] = []

        env = Environment(autoescape=True)
        env.globals['malware_config'] = malware_config
        env.loader = FileSystemLoader(os.path.join(CUCKOO_ROOT, "data",
                                                   "html"))
        try:
            tpl = env.get_template("report.html")
            html = tpl.render({"results": results, "summary_report": True})
        except UndefinedError as e:
            raise CuckooReportError(
                "Failed to generate summary HTML report: {} ".format(e))
        except TemplateNotFound as e:
            raise CuckooReportError(
                "Failed to generate summary HTML report: {} {} ".format(
                    e, e.name))
        except (TemplateSyntaxError, TemplateAssertionError) as e:
            raise CuckooReportError(
                "Failed to generate summary HTML report: {} on {}, line {} ".
                format(e, e.name, e.lineno))
        try:
            with codecs.open(os.path.join(self.reports_path,
                                          "summary-report.html"),
                             "w",
                             encoding="utf-8") as report:
                report.write(html)
        except (TypeError, IOError) as e:
            raise CuckooReportError("Failed to write summary HTML report: %s" %
                                    e)

        return True
コード例 #28
0
ファイル: tasks.py プロジェクト: Tickee/BLM
def mail_order(order_id, fake=False, as_guest=False, auto_retry=False):
    """
    Sends the mail of the order to the user.
    """
    # fetch information for template
    try:
        order = om.lookup_order_by_id(order_id)

        # generate template
        env = Environment(loader=PackageLoader('tickee', 'templates'),
                          extensions=['jinja2.ext.with_'])

        validate_email(order.user.email)

        if not order.user.email:
            raise ex.OrderError("user has no email.")

        if len(order.get_tickets()) == 0:
            raise ex.TicketError("no tickets found.")

        # send out a mail per event
        tickets_per_event = order.get_tickets_per_event()
        for event in tickets_per_event:
            tickets = tickets_per_event[event]

            # send generated mail
            blogger.info(
                'sending mail for "order %s - event %s" to user %s (%s)' %
                (order_id, event.id, order.user.id, order.user.email))

            if not fake:
                htmlbody = env.get_template('mail_order_of_event.html')\
                              .render(event=event,
                                      tickets=tickets,
                                      order=order,
                                      as_guest=as_guest,
                                      account=order.account)
                plainbody = env.get_template('mail_order_of_event.txt')\
                               .render(event=event,
                                       tickets=tickets,
                                       order=order,
                                       as_guest=as_guest,
                                       account=order.account)

                send_email("Tickee Ticketing <*****@*****.**>",
                           order.user.email,
                           "Your tickets for '%s' are here!" % event.name,
                           htmlbody, plainbody)

        order.meta['tickets_sent'] = datetime.datetime.utcnow().strftime(
            "%d-%m-%Y %H:%M:%S UTC%z")
        log_crm("order", order.id,
                dict(action="mailed", addressee=order.user.email))
        transaction.commit()
        return True
    except Exception as e:
        tlogger.exception("failed sending mail for order %s: %s" %
                          (order_id, e))
        order.meta['tickets_sent'] = "failed @ %s" % datetime.datetime.utcnow(
        ).strftime("%d-%m-%Y %H:%M:%S UTC%z")
        transaction.commit()
        if auto_retry:
            mail_order.retry(exc=e)
        return False
コード例 #29
0
#!/usr/bin/env python3
"""
simple exercise for jinja rendering
"""

from __future__ import unicode_literals, print_function
from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment

env = Environment(undefined=StrictUndefined)
env.loader = FileSystemLoader(".")

template_vars = {}

template_file = "incl_template_1.j2"
template = env.get_template(template_file)
output = template.render(**template_vars)
print(output)

コード例 #30
0
ファイル: generate.py プロジェクト: NotMoreZZ/mysite
import yaml
from jinja2 import FileSystemLoader, Template
from jinja2.environment import Environment

templatedir = 'src/templates'
finaldir = os.getcwd() + '/output'

# get data
with open(templatedir + '/techs.yml', 'r') as techsfile:
    techs = yaml.safe_load(techsfile)
with open(templatedir + '/works.yml', 'r') as worksfile:
    works = yaml.safe_load(worksfile)
with open(templatedir + '/contacts.yml', 'r') as contfile:
    contacts = yaml.safe_load(contfile)

# load jinjas
jinjaenv = Environment()
jinjaenv.loader = FileSystemLoader(templatedir)

# html compile
jinja = 'index.html'
template = jinjaenv.get_template(jinja)
html = template.render(techs=techs, works=works, contacts=contacts)
with open('{}/{}'.format(finaldir, jinja), 'w') as page:
    page.write(html)

# scc compile
scssdir = 'src/scss'
cssdir = finaldir + '/css'
sass.compile(dirname=(scssdir, cssdir), output_style='compressed')