def prepare_environment(env=None):
    try:
        loader = PackageLoader("glycresoft_sqlalchemy.web_app", "html")
        loader.list_templates()
    except:
        loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'html'))
    if env is None:
        env = Environment(loader=loader, extensions=[FragmentCacheExtension])
    else:
        env.loader = loader
    env.add_extension(FragmentCacheExtension)
    env.fragment_cache = dict()
    env.filters["n_per_row"] = n_per_row
    env.filters['highlight_sequence_site'] = highlight_sequence_site
    env.filters['plot_glycoforms'] = plot_glycoforms
    env.filters['chromatogram'] = plot_chromatogram
    env.filters['svg_plot'] = svg_plot
    env.filters['png_plot'] = png_plot
    env.filters['fsort'] = fsort
    env.filters['glycopeptide_string'] = glycopeptide_string
    env.filters['glycan_composition_string'] = glycan_composition_string
    env.filters["glycopeptide_match_logo"] = glycopeptide_match_logo
    env.filters["formula"] = formula
    env.globals
    return env
예제 #2
0
def prepare_environment(env=None):
    try:
        loader = PackageLoader("glypy", "search/results_template")
        loader.list_templates()
    except:
        loader = FileSystemLoader(os.path.join(os.path.dirname(__file__), 'results_template'))
    if env is None:
        env = Environment(loader=loader, extensions=[FragmentCacheExtension])
    else:
        env.loader = loader
        env.add_extension(FragmentCacheExtension)
    env.filters["collect_fragments"] = collect_fragments
    env.filters["all_matches"] = fetch_all_matches
    env.filters["strip_derivatize"] = strip_derivatize_glycoct
    env.filters["scientific_notation"] = scientific_notation
    env.filters["cfg_plot"] = cfg_plot
    env.filters["simple_cfg_plot"] = simple_plot
    env.filters["min"] = min
    env.filters["max"] = max
    env.filters["unique"] = unique
    env.filters["limit_sigfig"] = limit_sigfig
    env.filters['css_escape'] = css_escape
    env.filters['greek_fragment_name'] = greek_fragment_names
    env.fragment_cache = dict()
    return env
예제 #3
0
    def setup(self, config):

        # Listen for HTTP connections
        port = 6280
        iface = None

        if config is not None:
            if 'port' in config:
                port = int(config['port'])
            if 'interface' in config:
                iface = config['interface']

        if iface is None:
            iface = '0.0.0.0'

        templateLoader = PackageLoader('downpour.web', 'templates')
        self.templateFactory = Environment(loader=templateLoader)
        templateDir = os.path.dirname(templateLoader.get_source(
                self.templateFactory, 'base.html')[1]);

        # Custom filters for templateFactory
        self.templateFactory.filters['progressbar'] = self.progressbar
        self.templateFactory.filters['healthmeter'] = self.healthmeter
        self.templateFactory.filters['intervalformat'] = self.intervalformat
        self.templateFactory.filters['timestampformat'] = self.timestampformat
        self.templateFactory.filters['urlencode'] = urllib.quote
        self.templateFactory.filters['workinglink'] = self.workinglink
        self.templateFactory.filters['librarylink'] = self.librarylink

        root = SiteRoot(templateDir + '/media', self.application)
        site = server.Site(root)
        site.requestFactory = requestFactory(self)
        site.sessionFactory = sessionFactory(self)

        self.tryListen(port, site, get_interface(iface))
예제 #4
0
파일: views.py 프로젝트: janoliver/FS-Tools
def export(request, rhp_id):
    try:
        rhp = Rhp.objects.get(pk=rhp_id)
    except Rhp.DoesNotExist:
        raise Http404

    # create the jinja2 evironment for latex response
    # loader = FileSystemLoader('/path/to/templates')

    loader = PackageLoader('rhp', 'templates/latex')
    latex_helper = LatexHelper(loader)

    context = {
        'rhp': rhp,
        'vlu': rhp.vlu,
        'fragen': Frage.objects.select_related(),
        'fragensets': Fragenset.objects.select_related(),
        'optionen': Option.objects.select_related(),
        'vorlesungen': rhp.vlu.vorlesungen.select_related(),
        'artikel': rhp.artikel.all(),
        }

    files = []
    tmpfiles = []
    for tpl in latex_helper.env.list_templates():
        if tpl and (tpl.find('.tex') > 0 or tpl.find('.sty') > 0):
            template = latex_helper.env.get_template(tpl)
            f = tempfile.NamedTemporaryFile()
            f.write(template.render(context).encode('utf8'))
            f.flush()
            tmpfiles.append((tpl, f))
        else:
            files.append((tpl, loader.get_source(latex_helper.env,
                         tpl)[1]))

    # return as a zip file. from here: https://code.djangoproject.com/wiki/CookBookDynamicZip

    response = HttpResponse(mimetype='application/zip')
    response['Content-Disposition'] = 'filename=' + rhp.name + '.zip'

    buffer = StringIO()
    zip = zipfile.ZipFile(buffer, 'w', zipfile.ZIP_DEFLATED)

    for (name, f) in tmpfiles:
        zip.write(f.name, rhp.name + '/' + name)
        f.close()

    for (name, f) in files:
        zip.write(f, rhp.name + '/' + name)

    zip.close()
    buffer.flush()

    response.write(buffer.getvalue())
    buffer.close()
    return response
예제 #5
0
파일: cli.py 프로젝트: mivade/templar
def gen_from_template(template, directory, defaults_path, no_prompt):
    """Generate the new file from a template."""
    loader = PackageLoader("templar")
    template = osp.split(template)[-1]
    if template not in loader.list_templates():
        loader = FileSystemLoader(directory)
        if template not in loader.list_templates():
            raise RuntimeError("Template {} not found!".format(template))

    env = Environment(loader=loader)
    output = env.get_template(template)

    defaults = load_defaults(defaults_path)
    try:
        defaults = defaults[template]
    except KeyError:
        defaults = defaults["DEFAULT"]

    parsed = env.parse(env.loader.get_source(env, template)[0])
    variables = meta.find_undeclared_variables(parsed)

    kwargs = {}
    if no_prompt:
        for var in defaults:
            if var in defaults:
                default = defaults[var]
            else:
                default = ""
            kwargs[var] = default
    else:
        for var in variables:
            if var in defaults:
                default = defaults[var]
                prompt = var + " [{}]: ".format(default)
            else:
                prompt = var + ": "
                default = None

            resp = input(prompt).strip()
            if resp == "" and default is not None:
                resp = default
            kwargs[var] = resp

    result = output.render(**kwargs)
    print(result)
    return result
예제 #6
0
 def get_source(self, environment, template):
     if template[:1] == '!':
         template = template[1:]
     else:
         pieces = split_path_safely(template)
         if pieces is None:
             raise TemplateNotFound()
         theme = get_theme()
         if theme is None:
             raise RuntimeError('theme not found')
         fn = path.join(theme.template_path, *pieces)
         if path.isfile(fn):
             with open(fn, 'r') as f:
                 contents = f.read().decode(self.encoding)
             mtime = path.getmtime(fn)
             def uptodate():
                 try:
                     return path.getmtime(fn) == mtime
                 except OSError:
                     return False
             return contents, fn, uptodate
     return PackageLoader.get_source(self, environment, template)
예제 #7
0
import unittest
from xml.sax import saxutils
from jinja2 import Environment, PackageLoader

# ------------------------------------------------------------------------
# The redirectors below are used to capture output during testing. Output
# sent to sys.stdout and sys.stderr are automatically captured. However
# in some cases sys.stdout is already cached before HTMLTestRunner is
# invoked (e.g. calling logging.basicConfig). In order to capture those
# output, use the redirectors for the cached stream.
#
# e.g.
#   >>> logging.basicConfig(stream=HTMLTestRunner.stdout_redirector)
#   >>>

env = Environment(loader=PackageLoader('pytestreport', 'templates'))


class OutputRedirector(object):
    """ Wrapper to redirect stdout or stderr """
    def __init__(self, fp):
        self.fp = fp

    def write(self, s):
        self.fp.write(bytes(s, 'UTF-8'))

    def writelines(self, lines):
        self.fp.writelines(lines)

    def flush(self):
        self.fp.flush()
예제 #8
0
"""Handle Generation of rendered XML Content from Jinja2 Templates."""
from jinja2 import Environment, PackageLoader
import jinja2.meta

import logging

LOGGER = logging.getLogger(__name__)

Loader = PackageLoader(package_name="mpesa", package_path="drc/templates")
environment = Environment(loader=Loader)


def get_variables(template_name, logger=LOGGER):
    """Return all undeclared variables in template."""
    template_source = environment.loader.get_source(environment,
                                                    template_name)[0]
    parsed_content_ast = environment.parse(template_source)
    variables = jinja2.meta.find_undeclared_variables(parsed_content_ast)
    logger.debug(f"{__name__}.get_variables({template_name}) = {variables}.")
    return list(variables)


def generate_login(context: dict) -> str:
    """Return generated template string using context."""
    template = environment.get_template("login_request.xml")
    content = template.render(context)
    print(content)
    return content


def generate_c2b(context: dict) -> str:
예제 #9
0
    local_subscriber_ids = mk_full_id_dict(PATH, LOCAL_SUB_FILE, 'subscriber')
    if local_subscriber_ids:
        logging.info(
            'ID ALIAS MAPPER: local_subscriber_ids added to subscriber_ids dictionary'
        )
        subscriber_ids.update(local_subscriber_ids)

    local_peer_ids = mk_full_id_dict(PATH, LOCAL_PEER_FILE, 'peer')
    if local_peer_ids:
        logging.info(
            'ID ALIAS MAPPER: local_peer_ids added peer_ids dictionary')
        peer_ids.update(local_peer_ids)

    # Jinja2 Stuff
    env = Environment(loader=PackageLoader('monitor', 'templates'),
                      autoescape=select_autoescape(['html', 'xml']))

    dtemplate = env.get_template('hblink_table.html')
    btemplate = env.get_template('bridge_table.html')

    # Create Static Website index file
    index_html = get_template(PATH + 'index_template.html')
    index_html = index_html.replace('<<<system_name>>>', REPORT_NAME)
    if CLIENT_TIMEOUT > 0:
        index_html = index_html.replace(
            '<<<timeout_warning>>>',
            'Continuous connections not allowed. Connections time out in {} seconds'
            .format(CLIENT_TIMEOUT))
    else:
        index_html = index_html.replace('<<<timeout_warning>>>', '')
예제 #10
0
from opencv_pg.pipeline_launcher import launch_pipeline

from jinja2 import Environment, PackageLoader, select_autoescape

env = Environment(
    loader=PackageLoader("opencv_pg", "docs/source_docs"),
    autoescape=select_autoescape(["html"]),
)

from opencv_pg.models.pipeline import Pipeline
from opencv_pg.models.window import Window
from opencv_pg.models.base_transform import BaseTransform
from opencv_pg.models import params
from opencv_pg.models.params import Param
from opencv_pg.models import transforms
from opencv_pg.models import support_transforms
#import PYME.ui.autoFoldPanel as afp
import PYME.ui.manualFoldPanel as afp
import numpy as np
#import pandas as pd
import matplotlib.pyplot as plt
#import pylab

from PYME.recipes.tracking import TrackFeatures

from PYME.DSView import htmlServe
import cherrypy

from PYME.Analysis.graphing_filters import offline_plotting, movieplot, movieplot2

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('PYME.DSView.modules', 'templates'))
env.filters['movieplot'] = movieplot2


#from PYME.Analysis.Tracking import tracking
#from PYME.Analysis.Tracking import trackUtils

class TrackList(wx.ListCtrl):
    def __init__(self, parent):
        wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES|wx.LC_SINGLE_SEL, size=(250, 400))
        #listmix.CheckListCtrlMixin.__init__(self)
        self.InsertColumn(0, 'Track ID')
        self.InsertColumn(1, 'Length')
        self.InsertColumn(2, 'Enabled')
        
        self.clumps = []
예제 #12
0
파일: makehtml.py 프로젝트: x37v/stm32-rs
"""
makehtml.py
Copyright 2017, 2018 Adam Greig
Licensed under the MIT and Apache 2.0 licenses.

Generates a webpage for a given SVD file containing details on every
peripheral and register and their level of coverage.
"""

import os.path
import argparse
import multiprocessing
import xml.etree.ElementTree as ET
from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader('makehtml', ''))


def generate_index_page(devices):
    print("Generating Index")
    template = env.get_template('makehtml.index.template.html')
    return template.render(devices=devices)


def generate_device_page(device):
    template = env.get_template('makehtml.template.html')
    return template.render(device=device)


def short_access(accs):
    return {
예제 #13
0
 def __init__(self, templates_path):
     self.env = Environment(
         loader=PackageLoader("generate", "./templates"),
         autoescape=select_autoescape(["html", "xml"]),
     )
예제 #14
0
def metadata_convert(path, bucket=None):
    """Prepare metatdata prior to datacube indexing

    Given a directory containing landsat surface reflectance bands and a MLT.txt
    file, prepares a metadata string with the appropriate formating.

    Args:
        path (str): Path of the directory containing the surface reflectance bands
            and the Landsat metadata file.
        bucket (str or None): Name of the s3 bucket containing the data. If ``None``
            (default), data are considered to be on a mounted filesystem

    Examples:
        >>> from madmex.ingestion.landsat_espa import metadata_convert
        >>> from glob import glob

        >>> scene_list = glob('/path/to/scenes/*')
        >>> yaml_list = [metadata_convert(x) for x in scene_list]

        >>> with open('/path/to/metadata_out.yaml', 'w') as dst:
        >>>     for yaml in yaml_list:
        >>>         dst.write(yaml)
        >>>         dst.write('\n---\n')

    Returns:
        str: The content of the metadata for later writing to file.
    """
    pattern = re.compile(
        r'[A-Z0-9]{4}_[A-Z0-9]{4}_\d{6}_\d{8}_\d{8}_01_(T1|T2|RT)\.xml')
    if bucket is None:
        # Check that path is a dir and contains appropriate files
        if not os.path.isdir(path):
            raise ValueError('Argument path= is not a directory')
        mtl_file_list = glob(os.path.join(path, '*.xml'))
        # Filter list of xml files with regex (there could be more than one in case
        # some bands have been opend in qgis for example)
        mtl_file_list = [x for x in mtl_file_list if pattern.search(x)]
        print(mtl_file_list)
        if len(mtl_file_list) != 1:
            raise ValueError('Could not identify a unique xml metadata file')
        mtl_file = mtl_file_list[0]
        # Start parsing xml
        root = ET.parse(mtl_file).getroot()
    else:
        file_list = s3.list_files(bucket=bucket, path=path)
        pattern = re.compile(r'.*\.xml$')
        mtl_file_list = [x for x in file_list if pattern.search(x)]
        if len(mtl_file_list) != 1:
            raise ValueError('Could not identify a unique xml metadata file')
        mtl_file = mtl_file_list[0]
        # REad xml as string
        xml_str = s3.read_file(bucket, mtl_file)
        # generate element tree root
        root = ET.fromstring(xml_str)
        path = s3.build_rasterio_path(bucket, path)

    ns = 'http://espa.cr.usgs.gov/v2'
    # Build datetime from date and time
    date_str = root.find('ns:global_metadata/ns:acquisition_date',
                         namespaces={
                             'ns': ns
                         }).text
    time_str = root.find('ns:global_metadata/ns:scene_center_time',
                         namespaces={
                             'ns': ns
                         }).text
    dt = '%sT%s' % (date_str, time_str[:8])
    # satellite sensor metadata
    instrument = root.find('ns:global_metadata/ns:instrument',
                           namespaces={
                               'ns': ns
                           }).text
    satellite = root.find('ns:global_metadata/ns:satellite',
                          namespaces={
                              'ns': ns
                          }).text
    # Scene corners in projected coordinates
    ulx = float(
        root.find(
            'ns:global_metadata/ns:projection_information/ns:corner_point[@location="UL"]',
            namespaces={
                'ns': ns
            }).attrib['x'])
    uly = float(
        root.find(
            'ns:global_metadata/ns:projection_information/ns:corner_point[@location="UL"]',
            namespaces={
                'ns': ns
            }).attrib['y'])
    lrx = float(
        root.find(
            'ns:global_metadata/ns:projection_information/ns:corner_point[@location="LR"]',
            namespaces={
                'ns': ns
            }).attrib['x'])
    lry = float(
        root.find(
            'ns:global_metadata/ns:projection_information/ns:corner_point[@location="LR"]',
            namespaces={
                'ns': ns
            }).attrib['y'])
    utm_zone = int(
        root.find(
            'ns:global_metadata/ns:projection_information/ns:utm_proj_params/ns:zone_code',
            namespaces={
                'ns': ns
            }).text)
    crs = CRS({'proj': 'utm', 'zone': utm_zone})
    # Get coorner coordinates in long lat by transforming from projected values
    p = Proj(crs)
    ul_lon, ul_lat = p(ulx, uly, inverse=True)
    lr_lon, lr_lat = p(lrx, lry, inverse=True)
    ll_lon, ll_lat = p(ulx, lry, inverse=True)
    ur_lon, ur_lat = p(lrx, uly, inverse=True)
    # Prepare metadata fields
    meta_out = {
        'id':
        uuid.uuid5(uuid.NAMESPACE_URL, path),
        'dt':
        dt,
        'll_lat':
        ll_lat,
        'lr_lat':
        lr_lat,
        'ul_lat':
        ul_lat,
        'ur_lat':
        ur_lat,
        'll_lon':
        ll_lon,
        'lr_lon':
        lr_lon,
        'ul_lon':
        ul_lon,
        'ur_lon':
        ur_lon,
        'll_x':
        ulx,
        'lr_x':
        lrx,
        'ul_x':
        ulx,
        'ur_x':
        lrx,
        'll_y':
        lry,
        'lr_y':
        lry,
        'ul_y':
        uly,
        'ur_y':
        uly,
        'crs':
        crs.wkt,
        'blue':
        os.path.join(
            path,
            root.find('ns:bands/ns:band[@name="%s"]/ns:file_name' %
                      LANDSAT_BANDS[instrument]['blue'],
                      namespaces={
                          'ns': 'http://espa.cr.usgs.gov/v2'
                      }).text),
        'green':
        os.path.join(
            path,
            root.find('ns:bands/ns:band[@name="%s"]/ns:file_name' %
                      LANDSAT_BANDS[instrument]['green'],
                      namespaces={
                          'ns': 'http://espa.cr.usgs.gov/v2'
                      }).text),
        'red':
        os.path.join(
            path,
            root.find('ns:bands/ns:band[@name="%s"]/ns:file_name' %
                      LANDSAT_BANDS[instrument]['red'],
                      namespaces={
                          'ns': 'http://espa.cr.usgs.gov/v2'
                      }).text),
        'nir':
        os.path.join(
            path,
            root.find('ns:bands/ns:band[@name="%s"]/ns:file_name' %
                      LANDSAT_BANDS[instrument]['nir'],
                      namespaces={
                          'ns': 'http://espa.cr.usgs.gov/v2'
                      }).text),
        'swir1':
        os.path.join(
            path,
            root.find('ns:bands/ns:band[@name="%s"]/ns:file_name' %
                      LANDSAT_BANDS[instrument]['swir1'],
                      namespaces={
                          'ns': 'http://espa.cr.usgs.gov/v2'
                      }).text),
        'swir2':
        os.path.join(
            path,
            root.find('ns:bands/ns:band[@name="%s"]/ns:file_name' %
                      LANDSAT_BANDS[instrument]['swir2'],
                      namespaces={
                          'ns': 'http://espa.cr.usgs.gov/v2'
                      }).text),
        'qual':
        os.path.join(
            path,
            root.find('ns:bands/ns:band[@name="pixel_qa"]/ns:file_name',
                      namespaces={
                          'ns': 'http://espa.cr.usgs.gov/v2'
                      }).text),
        'instrument':
        instrument,
        'platform':
        satellite,
    }
    # Load template
    env = Environment(loader=PackageLoader('madmex', 'templates'))
    template = env.get_template('landsat_espa.yaml')
    out = template.render(**meta_out)
    return out
예제 #15
0
from collections import Iterable
from .options import BaseOptions, ChartOptions, \
    ColorsOptions, CreditsOptions, ExportingOptions, \
    GlobalOptions, LabelsOptions, LangOptions, \
    LegendOptions, LoadingOptions, NavigatorOptions, NavigationOptions, \
    PlotOptions, RangeSelectorOptions, ScrollbarOptions, SeriesData, SubtitleOptions, TitleOptions, \
    TooltipOptions, xAxisOptions, yAxisOptions, MultiAxis

from .highstock_types import Series, SeriesOptions
from .common import Levels, Formatter, CSSObject, SVGObject, JSfunction, RawJavaScriptText, \
    CommonObject, ArrayObject, ColorObject

CONTENT_FILENAME = "./content.html"
PAGE_FILENAME = "./page.html"

pl = PackageLoader('highcharts.highstock', 'templates')
jinja2_env = Environment(lstrip_blocks=True, trim_blocks=True, loader=pl)

template_content = jinja2_env.get_template(CONTENT_FILENAME)
template_page = jinja2_env.get_template(PAGE_FILENAME)


class Highstock(object):
    """
    Highstock Base class.
    """
    #: chart count
    count = 0

    # this attribute is overriden by children of this
    # class
예제 #16
0
class PixiedustGenerate(JupyterApp):
    env = Environment(loader=PackageLoader('install', 'templates'))

    def __init__(self, **kwargs):
        super(PixiedustGenerate, self).__init__(**kwargs)
        self.step = 1

    def hilite(self, message):
        return '\x1b[%sm%s\x1b[0m' % (';'.join(['32', '1']), message)

    def confirm(self, message, action="Keep"):
        answer = input(self.hilite(message) + "\n\t" + action + " y/n [y]? ")
        return 'y' if answer == '' else answer

    def input(self, message, validate=None, allowEmpty=False):
        value = None
        while value == None:
            value = input(self.hilite(message) )
            if not allowEmpty and value == "":
                value=None
            if validate is not None:
                v = validate(value)
                if not v[0]:
                    print(v[1])
                    value = None
        return value

    def start(self):
        try:
            self.files = []
            self.doStart()
        except KeyboardInterrupt:
            print("\nPixieDust generator aborted by user")

    def getStep(self):
        step = self.step
        self.step += 1
        return step

    def doStart(self):
        self.projectName = self.input(self.hilite("Step {}: Please enter the name of your project: ".format( self.getStep() )))
        self.location = os.getcwd()
        answer = self.confirm(self.hilite("Step {0}: Directory for your project: {1}".format( self.getStep(), self.location )))
        if answer != 'y':
            self.location = self.input(
                self.hilite("Please enter a directory for your project: "),
                lambda _: (os.path.exists(_), "Directory {} does not exist. Please try again or CTRL+C to abort".format(_))
            )
        
        #Check if directory already exists
        self.fullPath = os.path.join(self.location, self.projectName )
        if os.path.exists( self.fullPath ):
            print("Folder {} already exist. Exiting...".format(self.fullPath))
            return

        self.projectAuthor = self.input(self.hilite("Step {}: [Optional] Please enter the project author: ".format(self.getStep() )), allowEmpty=True)
        self.projectAuthorEmail = self.input(self.hilite("Step {}: [Optional] Please enter the project author email address: ".format(self.getStep() )), allowEmpty=True)
        self.projectUrl = self.input(self.hilite("Step {}: [Optional] Please enter the project url: ".format(self.getStep() )), allowEmpty=True)

        projectTypes = OrderedDict([
            ("1", ("Display Visualization", self.doDisplayVisualization)),
            ("2", ("Chart Renderer", self.doChartRenderer))
        ])
        print(self.hilite("Step {}: Please enter the project type:".format( self.getStep())))
        for key, value in iteritems(projectTypes):
            print("\t{0}.{1}".format(key, value[0]))
        projectType = self.input(self.hilite("Project type:"))

        if projectType not in projectTypes:
            print("Project Type {0} is not supported. Exiting...".format(projectType) )

        #Create the directory
        os.mkdir(self.fullPath)
        os.mkdir(os.path.join(self.fullPath, self.projectName))

        #Create the project artifacts
        self.writeFile(["setup.pytpl","LICENSE.tpl","MANIFEST.intpl","readme.mdtpl","setup.cfgtpl"])

        #Call out the generator function specific to the project type
        projectTypes.get(projectType)[1]()

    def writeFile(self, templates, dirName = None, targetFile=None):
        fullPath = self.fullPath if dirName is None else os.path.join(self.fullPath, dirName)
        for template in templates:
            if template.endswith("tpl"):
                if targetFile is None or len(templates) > 1:
                    targetFile = template[:-3]
                    if targetFile.endswith("."):
                        targetFile = targetFile[:-1]
                    index = targetFile.rfind('/')
                    targetFile = targetFile[index+1:] if index >= 0 else targetFile
                fullPathTargetFile = os.path.join(fullPath, targetFile)
                self.files.append(fullPathTargetFile)
                with open( fullPathTargetFile, "wb" ) as f:
                    f.write(PixiedustGenerate.env.get_template(template).render(this=self).encode("utf-8","ignore"))


    def doDisplayVisualization(self):
        print("Generating Display Visualization bootstrap code...")
        self.displayClassName = self.input(self.hilite(
            "Step {0}. Please enter the Display Handler class name. e.g {1}Handler: ".format(self.getStep(), self.projectName.capitalize())
        ))
        self.writeFile(["display/__init__.pytpl", "display/display.pytpl"], dirName=self.projectName)

        templateDir = os.path.join(self.fullPath,self.projectName,"templates")
        os.mkdir(templateDir)
        self.writeFile(["display/helloWorld.htmltpl"], dirName=templateDir)

        self.displaySuccess()

    def doChartRenderer(self):
        print("Generating Chart Renderer bootstrap code")
        self.rendererClassName = self.input(self.hilite(
            "Step {0}. Please enter the prefix for renderer class name. e.g {1}: ".format(self.getStep(), self.projectName.capitalize())
        ))

        chartIds = [("barChart","bar"),("lineChart","line"),("scatterPlot","scatter"),
            ("pieChart","pie"),("mapView",None),("histogram","hist")]
        self.chartIds = self.input(self.hilite(
            "Step {0}. Please enter one or more chart id (comma separated) to be associated with this renderer. e.g {1}. Leave empty to add all: ".format(self.getStep(), [c[0] for c in chartIds] )
        ),allowEmpty=True)

        if self.chartIds == "":
            self.chartIds = [c[0] for c in chartIds]
        else:
            self.chartIds = self.chartIds.split(",")

        #write the init and base
        self.writeFile(["chart/__init__.pytpl", "chart/rendererBaseDisplay.pytpl"], dirName=self.projectName)

        for c in self.chartIds:
            self.chartId = c
            knownCharts = [x for x in chartIds if x[0]==c]
            self.plotKind = "line" if len(knownCharts)==0 or knownCharts[0][1] is None else knownCharts[0][1]
            self.writeFile(["chart/rendererDisplay.pytpl"], dirName=self.projectName, targetFile="{0}Display.py".format(c))

        self.displaySuccess()

    def displaySuccess(self):
        print("Successfully generated boostrap code with following files:")
        for f in self.files:
            print("\t{}".format(f))
예제 #17
0
from Cryptodome.PublicKey import RSA

from jinja2 import Environment, PackageLoader, select_autoescape

from . import tools
from .exceptions import DeploymentDoesNotExists, VersionOSNotSupported, SettingTypeError, \
                        VagrantBoxDoesNotExist, NodeDoesNotExist, NoSourcePortForPortForwarding, \
                        ServicePortForwardingNotSupported, DeploymentAlreadyExists, \
                        ServiceNotFound

METADATA_FILENAME = ".metadata"

logger = logging.getLogger(__name__)

jinja_env = Environment(loader=PackageLoader('seslib', 'templates'),
                        trim_blocks=True)


class GlobalSettings(object):
    WORKING_DIR = os.path.join(Path.home(), '.sesdev')
    CONFIG_FILE = os.path.join(WORKING_DIR, 'config.yaml')

    @classmethod
    def init(cls, working_dir):
        cls.WORKING_DIR = working_dir
        os.makedirs(cls.WORKING_DIR, exist_ok=True)


OS_BOX_MAPPING = {
    'leap-15.1':
예제 #18
0
md_bp.static('/static/md', CONFIG.BASE_DIR + '/static/md')


@md_bp.listener('before_server_start')
def setup_db(rank_bp, loop):
    global motor_base
    motor_base = MotorBase()


@md_bp.listener('after_server_stop')
def close_connection(rank_bp, loop):
    motor_base = None


# jinjia2 config
env = Environment(loader=PackageLoader('views.md_blueprint',
                                       '../templates/md'),
                  autoescape=select_autoescape(['html', 'xml', 'tpl']))


def template(tpl, **kwargs):
    template = env.get_template(tpl)
    return html(template.render(kwargs))


@md_bp.route("/setting")
async def admin_setting(request):
    user = request['session'].get('user', None)
    if user:
        try:
            motor_db = motor_base.get_db()
            data = await motor_db.user.find_one({'user': user})
예제 #19
0
from AssemblyUtil.AssemblyUtilClient import AssemblyUtil
from AssemblyUtil.baseclient import ServerError as AssemblyUtilError
from Workspace.WorkspaceClient import Workspace as _Workspace
from DataFileUtil.DataFileUtilClient import DataFileUtil as _DFUClient
from DataFileUtil.baseclient import ServerError as _DFUError


def log(message, prefix_newline=False):
    """
    Logging function, provides a hook to suppress or redirect log messages.
    """
    print(('\n' if prefix_newline else '') + '{0:.2f}'.format(time.time()) +
          ': ' + str(message))


env = Environment(loader=PackageLoader('kb_mash', 'kb_object_utils/templates'),
                  autoescape=select_autoescape(['html']))


class KBObjectUtils:

    KBASE_DBS = {'KBaseRefseq'}

    def __init__(self, config):
        self.scratch = os.path.abspath(config['scratch'])
        self.tmp = os.path.join(self.scratch, str(uuid.uuid4()))
        self._mkdir_p(self.tmp)
        self.callbackURL = os.environ['SDK_CALLBACK_URL']
        self.ws_url = config['workspace-url']
        self.dfu = _DFUClient(self.callbackURL)
예제 #20
0
파일: utils.py 프로젝트: forexblog/getlino
    import grp
except ImportError:
    grp = None  # e.g. on Windows
import configparser
import subprocess
import click
# import platform
import distro
import collections
import getpass
from contextlib import contextmanager
import virtualenv
from jinja2 import Environment, PackageLoader
from .setup_info import SETUP_INFO

JINJA_ENV = Environment(loader=PackageLoader('getlino', 'templates'))

# currently getlino supports only nginx, maybe we might add other web servers
# USE_NGINX = True

BATCH_HELP = "Whether to run in batch mode, i.e. without asking any questions.  "\
             "Don't use this on a machine that is already being used."

# note that we double curly braces because we will run format() on this string:
LOGROTATE_CONF = """\
# generated by getlino
{logfile} {{
    weekly
    missingok
    rotate 156
    compress
예제 #21
0
from sanic import Sanic
from sanic.response import html

from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader('1_load_from_template', 'templates'))

app = Sanic(__name__)


@app.route('/')
async def test(request):
    data = {'name': 'Trịnh Minh Cường'}

    template = env.get_template(
        'index.html')  # Load index.html từ thư mục templates
    html_content = template.render(name=data["name"])
    return html(html_content)


app.run(host="0.0.0.0", port=8000)
예제 #22
0
from app import mythic, links, use_ssl
from sanic import response
from jinja2 import Environment, PackageLoader
from sanic_jwt.decorators import scoped, inject_user
from app.routes.routes import respect_pivot

env = Environment(loader=PackageLoader("app", "templates"), autoescape=True)


@mythic.route("/reporting/full_timeline")
@inject_user()
@scoped("auth:user")
async def ui_full_timeline(request, user):
    template = env.get_template("reporting_full_timeline.html")
    content = template.render(
        links=await respect_pivot(links, request),
        name=user["username"],
        http="https" if use_ssl else "http",
        ws="wss" if use_ssl else "ws",
        config=user["ui_config"],
        view_utc_time=user["view_utc_time"],
        view_mode=user["view_mode"],
    )
    return response.html(content)


@mythic.route("/reporting/attack_mapping")
@inject_user()
@scoped("auth:user")
async def attack_mappings(request, user):
    template = env.get_template("mitre_attack_mappings.html")
예제 #23
0
 def make_unlocalized_jinja_env(cls):
     return Environment(
             loader=PackageLoader('assembl', 'templates'),
             extensions=['jinja2.ext.i18n'])
예제 #24
0
def render_query(**kwargs) -> str:
    """Render the main query."""
    env = Environment(loader=PackageLoader("bigquery_etl", "glam/templates"))
    sql = env.get_template("bucket_counts_v1.sql")
    return reformat(sql.render(**kwargs))
예제 #25
0
파일: base.py 프로젝트: lapd-devops/osia
 def get_environment(cls) -> Environment:
     """Method loads jinja templates"""
     if cls.__env is None:
         cls.__env = Environment(loader=PackageLoader("osia.installer"))
     return cls.__env
예제 #26
0
파일: utils.py 프로젝트: mlzboy/generator
def include_block(block):
    template = block_env.get_template(block.template_name)
    return template.render(**block.fields)


class ThemeConfig(object):
    theme = 'default'


loaders = []

local_templates = os.path.join(os.getcwd(), 'col/templates')
if os.path.exists(local_templates):
    loaders.append(FileSystemLoader(local_templates))

loaders.append(PackageLoader('zmei_generator', 'templates'))

for entry_point in pkg_resources.iter_entry_points('zmei.templates'):
    loaders.append(FileSystemLoader(entry_point.load().__path__._path))

loader = ChoiceLoader(loaders)

block_env = Environment(loader=loader,
                        variable_start_string='<{',
                        variable_end_string='}>',
                        block_start_string='<%',
                        block_end_string='%>')

bcc = FileSystemBytecodeCache()

env = Environment(loader=loader, bytecode_cache=bcc)
예제 #27
0
import os
import sqlite3
from flask import Flask
from flask import redirect
from flask import request
from flask import send_from_directory
from flask import session
from jinja2 import Environment
from jinja2 import PackageLoader
import hashlib

_ARQUIVO_BANCO_ = './banco.sqlite'

app = Flask(__name__, static_url_path='/static')
env = Environment(loader=PackageLoader(__name__, 'templates'))

# Checa se o banco de dados já foi criado
if not os.path.isfile(_ARQUIVO_BANCO_):
    con = sqlite3.connect(_ARQUIVO_BANCO_)
    cursor = con.cursor()
    cursor.execute('''
        CREATE TABLE usuario (
            id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
            email TEXT NOT NULL,
            senha TEXT NOT NULL
        );
    ''')
    con.close()

예제 #28
0
def render_pdf(report, dash_url, max_runs=5):
    """
    Create a PDF report using LaTeX.

    Parameters
    ----------
    report: :py:class:`solarforecastarbiter.datamodel.Report`
    dash_url: str
        URL of the Solar Forecast Arbiter dashboard to use when building links.
    max_runs: int, default 5
        Maximum number of times to run pdflatex

    Returns
    -------
    bytes
        The rendered PDF report

    Notes
    -----
    This code was inspired by the latex package available at
    https://github.com/mbr/latex/ under the following license:

    Copyright (c) 2015, Marc Brinkmann
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

    * Neither the name of latex nor the names of its
      contributors may be used to endorse or promote products derived from
      this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    """  # NOQA
    env = Environment(loader=ChoiceLoader([
        PackageLoader('solarforecastarbiter.reports', 'templates/pdf'),
        PackageLoader('solarforecastarbiter.reports', 'templates'),
    ]),
                      autoescape=False,
                      lstrip_blocks=True,
                      trim_blocks=True,
                      block_start_string='\\BLOCK{',
                      block_end_string='}',
                      variable_start_string='\\VAR{',
                      variable_end_string='}',
                      comment_start_string='\\#{',
                      comment_end_string='}',
                      line_statement_prefix='%-',
                      line_comment_prefix='%#')
    env.filters['html_to_tex'] = _html_to_tex
    env.filters['link_filter'] = _link_filter
    env.filters['pretty_json'] = _pretty_json
    env.filters['unique_flags_filter'] = _unique_flags_filter
    kwargs = _get_render_kwargs(report, dash_url, False)
    with tempfile.TemporaryDirectory() as _tmpdir:
        tmpdir = Path(_tmpdir)
        logfile, auxfile = _prepare_latex_support_files(tmpdir, env, kwargs)
        _save_figures_to_pdf(tmpdir, report)
        _compile_files_into_pdf(tmpdir, logfile, auxfile, max_runs)
        return (tmpdir / 'out.pdf').read_bytes()
예제 #29
0
    def run(self, args):
        args.path = args.path.resolve()
        if args.path.exists():
            if not args.path.is_dir():
                raise CommandError("{} is not a directory".format(args.path))
            if next(args.path.iterdir(), False):
                raise CommandError("{} is not empty".format(args.path))
            logger.debug("Using existing project directory '%s'", args.path)
        else:
            logger.debug("Creating project directory '%s'", args.path)
            args.path.mkdir()

        if args.author is None:
            gecos = pwd.getpwuid(os.getuid()).pw_gecos.split(',', 1)[0]
            if not gecos:
                raise CommandError(
                    "Author not given, and nothing in GECOS field")
            logger.debug("Setting author to %r from GECOS field", gecos)
            args.author = gecos

        if not args.name:
            args.name = args.path.name
            logger.debug("Set project name to '%s'", args.name)

        if not re.match(r"[a-z][a-z0-9-]*[a-z0-9]$", args.name):
            raise CommandError("{} is not a valid charm name".format(
                args.name))

        context = {
            "name": args.name,
            "author": args.author,
            "year": date.today().year,
            "class_name":
            "".join(re.split(r"\W+", args.name.title())) + "Charm",
            "series": yaml.dump(args.series.split(","),
                                default_flow_style=True),
        }

        env = Environment(
            loader=PackageLoader('charmcraft', 'templates/init'),
            autoescape=False,  # no need to escape things here :-)
            keep_trailing_newline=
            True,  # they're not text files if they don't end in newline!
            optimized=False,  # optimization doesn't make sense for one-offs
            undefined=StrictUndefined)  # fail on undefined

        _todo_rx = re.compile("TODO: (.*)")
        todos = []
        executables = ["run_tests", "src/charm.py"]
        for template_name in env.list_templates():
            if not template_name.endswith(".j2"):
                continue
            template = env.get_template(template_name)
            template_name = template_name[:-3]
            logger.debug("Rendering %s", template_name)
            path = args.path / template_name
            path.parent.mkdir(parents=True, exist_ok=True)
            with path.open("wt", encoding="utf8") as fh:
                out = template.render(context)
                fh.write(out)
                for todo in _todo_rx.findall(out):
                    todos.append((template_name, todo))
                if template_name in executables:
                    make_executable(fh)
                    logger.debug("  made executable")
        logger.info("All done.")
        if todos:
            logger.info(
                "There are some notes about things we think you should do.")
            logger.info(
                "These are marked with ‘TODO:’, as is customary. Namely:")
            w = max(len(i[0]) for i in todos)
            for fn, todo in todos:
                logger.info("%*s: %s", w + 2, fn, todo)
예제 #30
0
from jinja2 import Template, Environment, PackageLoader

env = Environment(loader=PackageLoader('parpe', 'templates'))


def create_job_file():
    num_starts = 3
    ll = {
        'job_name': 'testname',
        'steps': [],
        'input_data_file': 'input.h5',
    }

    ll['steps'].append({
        'step_name': 'preprocess',
        'class': 'micro',
        'node': 1,
        'wall_clock_limit': '02:00:00',
        'energy_policy_tag':
            'simulation2_energy_tag_ipopt_hierarchical_test',
        'body': 'snakemake preprocess'
    })

    for start_idx in range(num_starts):
        ll['steps'].append({
            'step_name': f'optimize_ms_{start_idx}',
            'class': 'micro',
            'node': 1,
            'wall_clock_limit': '02:00:00',
            'energy_policy_tag':
                'simulation2_energy_tag_ipopt_hierarchical_test',
예제 #31
0
"""
Authentication endpoints
"""
from .helpers import (push_groups_to_redis, get_kong_token, status_string,
                      authenticate, get_data)
import falcon
import json

from jinja2 import Environment, PackageLoader, select_autoescape

env = Environment(loader=PackageLoader('authenticationservice', 'templates'),
                  autoescape=select_autoescape(['html', 'xml']))


class AuthenticationResource(object):
    @staticmethod
    def on_get(req, resp):

        template = env.get_template('login.html')
        resp.body = template.render()
        resp.content_type = 'text/html'
        resp.status = falcon.HTTP_200

    @staticmethod
    def on_post(req, resp):
        parsed = get_data(req)
        username = parsed.get('username')
        password = parsed.get('password')
        client_id = parsed.get('client_id')
        client_secret = parsed.get('client_secret')
        user = authenticate(username=username, password=password)
예제 #32
0
generated code to scrape schema.org data from web pages.
"""

import os
import sys
import json
import argparse

_current_dir = os.path.dirname(os.path.realpath(__file__))
# jinja2 is in chromium's third_party directory
# Insert at front to override system libraries, and after path[0] == script dir
sys.path.insert(
    1, os.path.join(_current_dir, *([os.pardir] * 2 + ['third_party'])))
import jinja2
from jinja2 import Environment, PackageLoader, select_autoescape
env = Environment(loader=PackageLoader('generate_schema_org_code', ''))
env.trim_blocks = True
env.lstrip_blocks = True

SCHEMA_ORG_PREFIX = 'http://schema.org/'


def schema_org_id(object_name):
    return SCHEMA_ORG_PREFIX + object_name


def object_name_from_id(the_id):
    """Get the object name from a schema.org ID."""
    return the_id[len(SCHEMA_ORG_PREFIX):]

예제 #33
0
 def __init__(self):
     PackageLoader.__init__(self, 'solace')
예제 #34
0
# built-in
import re
from logging import getLogger

# external
from jinja2 import Environment, PackageLoader

logger = getLogger('dephell')
env = Environment(loader=PackageLoader('dephell', 'templates'), )

REPLACEMENTS = (
    ('</div>', '\n\n'),
    ('</ul>', '\n\n'),
    ('</ol>', '\n\n'),
    ('</li>', '\n'),
    ('</p>', '\n'),
    ('<ul>', '\n'),
    ('<ol>', '\n'),
    ('<li>', ' + '),
    ('<hr/>', '\n' + '—' * 80 + '\n'),
)
REX_BEGINNING = re.compile(r'(\n[ \t]+)')


# https://github.com/dephell/dephell/issues/11
def html2text(text: str) -> str:
    text = REX_BEGINNING.sub('', text)
    for tag, char in REPLACEMENTS:
        text = text.replace(tag, char)
    for tag, _ in REPLACEMENTS:
        text = text.replace(tag.replace('/', ''), '')
예제 #35
0
    def encode(self, api_input, action):
        """Transform input to dpd compatible xml."""
        if not (action in DPD_ACTIONS):
            raise InvalidApiInput('action %s not in %s' %
                                  (action, ', '.join(DPD_ACTIONS)))

        api = DpdApi()
        if not api.validate(api_input):
            raise InvalidApiInput('Input error : %s' % api.errors(api_input))
        data = api.normalize(api_input)

        # add some rules which are hard to implement with
        # cerberus.
        # TODO: add additional schemas for that
        if data['service']['product'] == 'DPD_Predict':
            if len(data['service']['dropOffLocation']) > 0:
                raise InvalidApiInput(
                    "dropOffLocation can't be used with predict")
            if data['service']['notifications'] != 'Predict':
                log.info('Notification forced to predict because of product')
                data['service']['notifications'] = 'Predict'

        if data['service']['product'] == 'DPD_Classic':
            if len(data['service']['dropOffLocation']) > 0:
                raise InvalidApiInput(
                    "dropOffLocation can't be used with classic")
            if data['service']['notifications'] == 'Predict':
                raise InvalidApiInput(
                    "Predict notifications can't be used with classic")

        if data['service']['product'] == 'DPD_Relais':
            if len(data['service']['dropOffLocation']) < 1:
                raise InvalidApiInput(
                    "dropOffLocation is mandatory for this product")
            if data['service']['notifications'] == 'Predict':
                raise InvalidApiInput(
                    "Predict notifications can't be used with Relais")

        data['service']['shippingDate'] = (datetime.strptime(
            data['service']['shippingDate'], '%Y/%M/%d').strftime('%d/%M/%Y'))

        output_format = data['service']['labelFormat']
        if data['service']['labelFormat'] in ('PNG', 'ZPL'):
            # WS doesn't handle zpl yet, we convert it later
            # png is named Default, WTF DPD?
            data['service']['labelFormat'] = 'Default'

        env = Environment(loader=PackageLoader('roulier',
                                               '/carriers/dpd/templates'),
                          extensions=['jinja2.ext.with_'])

        template = env.get_template("dpd_%s.xml" % action)
        return {
            "body":
            template.render(service=data['service'],
                            parcel=data['parcel'],
                            sender_address=data['from_address'],
                            receiver_address=data['to_address']),
            "headers":
            data['auth'],
            "output_format":
            output_format
        }
예제 #36
0
from app import apfell, auth, links
from sanic import response
from jinja2 import Environment, PackageLoader
from app.forms.payloads_form import Payloads_JXA_Form

env = Environment(loader=PackageLoader('app', 'templates'))


@apfell.route("/payloads/jxa", methods=['GET', 'POST'])
@auth.login_required(user_keyword='user')
async def payloads_jxa(request, user):
    form = Payloads_JXA_Form(request)
    errors = {}
    success = ""
    if request.method == 'POST' and form.validate():
        callback_host = form.callback_host.data
        callback_port = form.callback_port.data
        obfuscation = form.obfuscation.data
        output_directory = form.output_directory.data
        callback_interval = form.callback_interval.data

        try:
            # take these inputs to create our payload and write it to output_directory
            base_jxa = open('./app/payloads/JXA.js', 'r')
            custom_jxa = open(output_directory, 'w')
            # read base_jxa and write it out to custom_jxa with our modifications
            # for now, obfuscation doesn't do anything
            for line in base_jxa:
                if "C2 = new RestC2(10" in line:
                    custom_jxa.write("C2 = new RestC2(" +
                                     str(callback_interval) + ", \"http://" +
예제 #37
0
파일: app.py 프로젝트: atokop/tango-core
 def get_source(self, environment, template):
     try:
         return PackageLoader.get_source(self, environment, template)
     except TemplateNotFound:
         template = 'default/' + template
         return PackageLoader.get_source(self, environment, template)
예제 #38
0
from jinja2 import Environment
from jinja2 import PackageLoader
from jinja2 import select_autoescape

env = Environment(
    loader=PackageLoader("swagger_codegen", "templates"),
    autoescape=select_autoescape(["html", "xml"]),
)