Example #1
0
def _is_template(template_str):
    template_str = to_unicode(template_str)
    template = Template(template_str)
    try:
        return template_str != template.render({})
    except exceptions.UndefinedError:
        return True
Example #2
0
File: age.py Project: xuanblo/jcvi
def traits(args):
    """
    %prog traits directory

    Make HTML page that reports eye and skin color.
    """
    p = OptionParser(traits.__doc__)
    opts, args = p.parse_args(args)

    if len(args) < 1:
        sys.exit(not p.print_help())

    samples = []
    for folder in args:
        targets = iglob(folder, "*-traits.json")
        if not targets:
            continue
        filename = targets[0]
        js = json.load(open(filename))
        js["skin_rgb"] = make_rgb(
            js["traits"]["skin-color"]["L"],
            js["traits"]["skin-color"]["A"],
            js["traits"]["skin-color"]["B"])
        js["eye_rgb"] = make_rgb(
            js["traits"]["eye-color"]["L"],
            js["traits"]["eye-color"]["A"],
            js["traits"]["eye-color"]["B"])
        samples.append(js)

    template = Template(traits_template)
    fw = open("report.html", "w")
    print >> fw, template.render(samples=samples)
    logging.debug("Report written to `{}`".format(fw.name))
    fw.close()
Example #3
0
    def process_dockerfile(self):
        """
        Read source dockerfile --> Render with jinja --> Write to outfile
        """
        source_dockerfile = self.args["--dockerfile"]

        with open(source_dockerfile, "r") as stream:
            log.info("Reading source file...")
            template = Template(stream.read())

        # Update the jinja environment with all custom functions & filters
        self._update_env(template.environment)

        context = self.config.get("env", {})
        log.info("Rendering context")

        for k, v in context.items():
            log.info("  * %s: %s" % (k, v))

        log.info("Rendering Dockerfile...")
        out_data = template.render(**context)

        log.debug("\n******\nWriting to file\n*******")
        log.debug(out_data)

        if "--outfile" not in self.args:
            log.debug("No --outfile <FILE> was specified. Defaulting to Dockerfile")
            self.outfile = "Dockerfile"
        else:
            self.outfile = self.args['--outfile']

        with open(self.outfile, "w") as stream:
            log.info("Writing to outfile...")
            stream.write(out_data)
 def __render(self):
     with open(self.template, 'r') as fp:
         tmpl = fp.read()
     template = Template(tmpl)
     rtxt = template.render(clusters = self.clusters)
     with open(self.target_path, 'w') as fp:
         fp.write(rtxt)
Example #5
0
 def run_instances(self):
     min_count = int(self.querystring.get('MinCount', ['1'])[0])
     image_id = self.querystring.get('ImageId')[0]
     user_data = self.querystring.get('UserData')
     new_reservation = ec2_backend.add_instances(image_id, min_count, user_data)
     template = Template(EC2_RUN_INSTANCES)
     return template.render(reservation=new_reservation)
Example #6
0
    def to_python(self):
        url = self.ast.left.url
        method = self.ast.method.name
        params = {}
        headers = {}
        body = {}
        for option in self.ast.right.options if self.ast.right else []:
            if isinstance(option.key, QueryStringNode):
                if isinstance(option.value, ValueNode):
                    params[option.key.key] = option.value.value
                elif isinstance(option.value, ShellNode):
                    params[option.key.key] = commands.getstatusoutput(option.value.value)[1]
            elif isinstance(option.key, HeaderNode):
                headers[option.key.key] = option.value.value
            elif isinstance(option.key, BodyNode):
                body[option.key.key] = option.value.value

        template = Template('Hello {{ name }}!')
        template.render(name='John Doe')
        return '''
    params = {params}
    data = {data}
    headers = {headers}
    requests.{method}('{url}', params=params, data=data, body=body, headers=headers)
        '''.format(url=url, method=method.lower(), params=params, data=body, headers=headers)
Example #7
0
    def format_notification(body):
        body['fullname'] = body.get('fullname', body['name'])
        NOTIFICATION_TEMPLATE = Template("""Build #{{build.number}} \
{{build.status}} for Job {{fullname}} ({{build.full_url}})
{% if build.scm %}Based on {{build.scm.url}}/commit/{{build.scm.commit}} \
({{build.scm.branch}}){% endif %}""")
        return NOTIFICATION_TEMPLATE.render(body)
Example #8
0
 def create_security_group(self):
     name = self.querystring.get('GroupName')[0]
     description = self.querystring.get('GroupDescription', [None])[0]
     vpc_id = self.querystring.get("VpcId", [None])[0]
     group = ec2_backend.create_security_group(name, description, vpc_id=vpc_id)
     template = Template(CREATE_SECURITY_GROUP_RESPONSE)
     return template.render(group=group)
Example #9
0
def renderEq(fname, inline, scale):
    #env = Environment(loader=FileSystemLoader('./'))
    #template = env.get_template('display_eq.j2')
    if inline:
        template = Template(INLINE_TEMPLATE)
    else:
        template = Template(DISPLAY_TEMPLATE)

    eq_str = open(fname,'r').read()
    print eq_str

    out_str = template.render(eq=eq_str[:-1])
    print out_str

    outfile=open('/var/tmp/eq_render.tex','w')
    outfile.write(out_str)
    outfile.close()

#def call_latex():
    #call latex to convert tex->dvi
    subprocess.call(["latex","--output-directory=/var/tmp/","/var/tmp/eq_render.tex"])

#def call_dvi2svg():
    #call dvi2svgm to convert dvi->svg
    subprocess.call(["dvisvgm","-a","-e","-n","-c"+str(scale)+","+str(scale), "/var/tmp/eq_render.dvi", "--output=eq_render.svg"])
Example #10
0
def show(ctx, path, order):
    router = Router(open(ctx.obj['CONFIG']))
    route = router.match(path)
    logging.debug("Matched route: %s" % route)
    if not route:
        print 'No queries matched'
        return
    es = Elasticsearch(hosts=route.get('elasticsearch_url'))
    request_body = {}
    for non_mandatory_key in ['sort', 'query']:
        value = route.get(non_mandatory_key)
        if value:
            request_body[non_mandatory_key] = value
    if order == 'asc':
        request_body['sort'] = {'@timestamp': 'asc'}
    elif order == 'desc':
        request_body['sort'] = {'@timestamp': 'desc'}
    elif order:
        click.echo("Unknown order format: %s" % order, err=True)
        return 1
    logging.debug("Query: %s" % (request_body,))
    result = es.search(index=route.get('index'), doc_type=None, body=request_body)
    hits = result['hits']['hits']
    template = Template(route.get("format", "{{ __at_timestamp }} {{ message }}"))
    for hit in hits:
        doc = hit['_source']
        doc['__at_timestamp'] = doc.get('@timestamp')
        print template.render(doc)
Example #11
0
    def as_supervisor_program(self):
        config = """[program:{{program_name}}]
command     = {{cmd}}
directory   = {{base_dir}}
autostart   = true
autorestart = true
stopsignal  = KILL
killasgroup = true
stopasgroup = true
environment = {{env}}
stdout_logfile = {{stdout}}
stderr_logfile = {{stderr}}


"""

        env = Template(config)

        return env.render({
            "program_name" : self.config['name'],
            "base_dir"     : self.config['base_dir'],
            "env"          : self.get_env_str(),
            "cmd"          : self.config['cmd'],
            "stdout"       : self.config['stdout_file'],
            "stderr"       : self.config['stderr_file'],
        })
Example #12
0
  def render_container(self, body):
    """ Render the "body" HTML inside of a bootstrap container page. """
    template = Template("""
    <!DOCTYPE html>
    <html>
      <head><title>Kudu test results</title>
      <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
      <style>
        .new-date { border-bottom: 2px solid #666; }
        #flaky-rate tr :nth-child(1) { width: 70%; }

        /* make sparkline data not show up before loading */
        .inlinesparkline { color: #fff; }
        /* fix sparkline tooltips */
        .jqstooltip {
          -webkit-box-sizing: content-box;
          -moz-box-sizing: content-box;
          box-sizing: content-box;
        }
      </style>
    </head>
    <body>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.min.js"></script>
      <div class="container-fluid">
      {{ body }}
      </div>
    </body>
    </html>
    """)
    return template.render(body=body)
Example #13
0
 def __init__(self, parameter_name):
     template = Template(ERROR_RESPONSE)
     super(MissingParameterError, self).__init__()
     self.description = template.render(
         code="Missing Parameter",
         message="Missing parameter {0}".format(parameter_name),
     )
Example #14
0
def generate_index(api=True, single=False, **kwds):
    from jinja2 import Template
    with open("source/index.rst.template") as f:
        t = Template(f.read())

    with open("source/index.rst","w") as f:
        f.write(t.render(api=api,single=single,**kwds))
def gen_tensorflow_client_string(generated_tensor_data, model_name):
  """
  Generate TensorFlow SDK in JavaScript.

  Args:
    generated_tensor_data: Example is {"keys": [[1.0], [2.0]], "features": [[1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1]]}
  """

  code_template = """#!/usr/bin/env node

var request = require("request");

var options = {
    uri: "http://127.0.0.1:8500",
    method: "POST",
    json: {"model_name": "{{ model_name }}", "data": {{ tensor_data }} }
};

request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body)
    } else {
        console.log(error)
    }
});
  """

  generated_tensor_data_string = json.dumps(generated_tensor_data)
  template = Template(code_template)
  generate_code = template.render(
      model_name=model_name, tensor_data=generated_tensor_data_string)
  logging.debug("Generate the code in JavaScript:\n{}".format(generate_code))

  return generate_code
Example #16
0
    def test_call_with_args(self):
        t = Template(
            """{% macro dump_users(users) -%}
        <ul>
          {%- for user in users -%}
            <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li>
          {%- endfor -%}
          </ul>
        {%- endmacro -%}

        {% call(user) dump_users(list_of_user) -%}
          <dl>
            <dl>Realname</dl>
            <dd>{{ user.realname|e }}</dd>
            <dl>Description</dl>
            <dd>{{ user.description }}</dd>
          </dl>
        {% endcall %}"""
        )

        assert [
            x.strip()
            for x in t.render(
                list_of_user=[{"username": "******", "realname": "something else", "description": "test"}]
            ).splitlines()
        ] == [
            "<ul><li><p>apo</p><dl>",
            "<dl>Realname</dl>",
            "<dd>something else</dd>",
            "<dl>Description</dl>",
            "<dd>test</dd>",
            "</dl>",
            "</li></ul>",
        ]
    def test_call_with_args(self):
        t = Template("""{% macro dump_users(users) -%}
        <ul>
          {%- for user in users -%}
            <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li>
          {%- endfor -%}
          </ul>
        {%- endmacro -%}

        {% call(user) dump_users(list_of_user) -%}
          <dl>
            <dl>Realname</dl>
            <dd>{{ user.realname|e }}</dd>
            <dl>Description</dl>
            <dd>{{ user.description }}</dd>
          </dl>
        {% endcall %}""")

        assert [x.strip() for x in t.render(list_of_user=[{
            'username':'******',
            'realname':'something else',
            'description':'test'
        }]).splitlines()] == [
            '<ul><li><p>apo</p><dl>',
            '<dl>Realname</dl>',
            '<dd>something else</dd>',
            '<dl>Description</dl>',
            '<dd>test</dd>',
            '</dl>',
            '</li></ul>'
        ]
Example #18
0
    def construct(self, event):

        try:
            event["header"][self.key]["template"]
        except KeyError:
            self.logging.error('Header information event["header"]["%s"]["template"] was expected but not found. Event purged.'%(self.key))
            raise

        for key in self.header_templates:
            try:
                template = JinjaTemplate(event["header"][self.key][key])
                event["header"][self.key][key] = template.render(**event["data"])
            except Exception as err:
                self.logging.warning("Failed to convert header key %s.  Reason: %s"%(key))
                raise

        try:
            template = self.templates.get_template(event["header"][self.key]["template"])
        except Exception as err:
            self.logging.error("Template %s does not exist as a file in directory %s."%(event["header"][self.key]["template"], self.location))
            raise
        else:
            try:
                event["data"] = template.render(**event["data"])
            except Exception as err:
                self.logging.error('There was an error processing the template. Reason: %s'%(err))
                raise
        return event
Example #19
0
def main():
    db = dataset.connect('sqlite:///reddit.db')
    if not os.path.exists('./deploy'):
        os.mkdir('./deploy')

    def get_items_from_day(date):
        return db.query('SELECT title, link, min(rank) as rank, (upvotes - downvotes) as votes, subreddit FROM status ' + \
               ('JOIN entry ON status.eid=entry.id WHERE rank <= 10 %s GROUP BY eid ORDER BY rank, (upvotes - downvotes) DESC;' % date))

    all_items = {}
    def process_items_from_day(items):
        data = []
        for item in filter(lambda item: item['link'] is not None, [item for item in items]):
            item = process(item)
            if item['link'] not in all_items:
                all_items[item['link']] = True
                data.append(item)
        return data

    collections = ['AND observed > date("now", "start of day", "-1 day") AND observed < date("now", "start of day")',
                   'AND observed > date("now", "start of day", "-2 day") AND observed < date("now", "start of day", "-1 day")',
                   'AND observed > date("now", "start of day", "-3 day") AND observed < date("now", "start of day", "-2 day")']
    collections = [process_items_from_day(get_items_from_day(date)) for date in collections]

    with open('./templates/newsletter.html', 'r') as newspaper:
        template = Template(newspaper.read())
        html = template.render(title="Reddit News Agency", edition=len(os.listdir('./deploy')),
                               collections=collections).encode('utf-8')
        f = open('./deploy/' + str(int(time())) + '.html', 'w')
        f.write(html)
        requests.post('http://reddit-snews-agency.herokuapp.com/', data=html, headers={
            'Authorization': '9f9fa431c64a86da8324bb370d05377bbf49dbf9'
        })
Example #20
0
def index():
    current_time = ctime()
    data = open('html/index.html').read()
    # new_data = data.replace('@{time}',current_time)
    template = Template(data)
    result = template.render(name='ying', age='29', time=current_time,user_list=["Ying","Dora","Ericsson","Avaya"],num=1)
    return result.encode('utf-8')
Example #21
0
    def _make_sql_query(self, chunk):
        """ Format template and return sql query string """

        assert type(chunk) == dict, "Input data is not dictionary!"
        sql = """
INSERT INTO
    results 
        (
            'filename',
            'buee',
            'is_cheating'
        )
VALUES
    {% for val in values.keys() %}
        (
            "{{val}}",
            {{values[val]['buee']}},
            {{1 if values[val]['is_cheating'] else 0 }}
        ){% if not loop.last %},{% endif %}
    {%endfor%}
;
"""
        template = Template(sql)
        template = template.render(values=chunk)
        return template
Example #22
0
    def _get_cluster_services(self, cluster):
        """Return a list of services from a cluster name

        """
        services_list = []
        if ('vars' in cluster):
            try:
                j2 = Template(str(cluster['services']))
                services_yaml = j2.render(cluster['vars'])
                services = yaml.load(services_yaml)
            except ValueError:
                for l in cluster['vars']:
                    j2 = Template(str(cluster['services']))
                    services_yaml = j2.render(l)
                    services = yaml.load(services_yaml)
            cluster['services'] = services

        for service in cluster['services']:
            service['cluster'] = {}
            service['cluster']['images'] = cluster['images']
            service['cluster']['name'] = cluster['name']
            service['cluster']['hosts'] = cluster.get('hosts')
            service['cluster']['vars'] = cluster.get('vars')
            services_list.append(service)
        return services_list
Example #23
0
    def list_or_change_tags_for_resource_request(self, request, full_url, headers):
        self.setup_class(request, full_url, headers)

        parsed_url = urlparse(full_url)
        id_ = parsed_url.path.split("/")[-1]
        type_ = parsed_url.path.split("/")[-2]

        if request.method == "GET":
            tags = route53_backend.list_tags_for_resource(id_)
            template = Template(LIST_TAGS_FOR_RESOURCE_RESPONSE)
            return 200, headers, template.render(
                resource_type=type_, resource_id=id_, tags=tags)

        if request.method == "POST":
            tags = xmltodict.parse(
                self.body)['ChangeTagsForResourceRequest']

            if 'AddTags' in tags:
                tags = tags['AddTags']
            elif 'RemoveTagKeys' in tags:
                tags = tags['RemoveTagKeys']

            route53_backend.change_tags_for_resource(id_, tags)
            template = Template(CHANGE_TAGS_FOR_RESOURCE_RESPONSE)

            return 200, headers, template.render()
Example #24
0
    def get(self, request,
            app_label=None, actor=None,
            pk=None, fldname=None, tplname=None, **kw):

        if request.method == 'GET':

            rpt = requested_actor(app_label, actor)
            elem = rpt.get_row_by_pk(None, pk)
            if elem is None:
                raise http.Http404("%s %s does not exist." % (rpt, pk))

            TextFieldTemplate = rt.modules.tinymce.TextFieldTemplate
            if tplname:
                tft = TextFieldTemplate.objects.get(pk=int(tplname))
                if settings.SITE.trusted_templates:
                    #~ return http.HttpResponse(tft.text)
                    template = JinjaTemplate(tft.text)
                    context = dict(request=request,
                                   instance=elem, **rt.modules)
                    return http.HttpResponse(template.render(**context))
                else:
                    return http.HttpResponse(tft.text)

            qs = TextFieldTemplate.objects.all().order_by('name')

            templates = []
            for obj in qs:
                url = dd.plugins.tinymce.build_plain_url(
                    'templates',
                    app_label, actor, pk, fldname, str(obj.pk))
                templates.append([
                    str(obj.name), url, str(obj.description)])
            js = "var tinyMCETemplateList = %s;" % py2js(templates)
            return http.HttpResponse(js, content_type='text/json')
        raise http.Http404("Method %r not supported" % request.method)
Example #25
0
    def health_check_response(self, request, full_url, headers):
        self.setup_class(request, full_url, headers)

        parsed_url = urlparse(full_url)
        method = request.method

        if method == "POST":
            properties = xmltodict.parse(self.body)['CreateHealthCheckRequest'][
                'HealthCheckConfig']
            health_check_args = {
                "ip_address": properties.get('IPAddress'),
                "port": properties.get('Port'),
                "type": properties['Type'],
                "resource_path": properties.get('ResourcePath'),
                "fqdn": properties.get('FullyQualifiedDomainName'),
                "search_string": properties.get('SearchString'),
                "request_interval": properties.get('RequestInterval'),
                "failure_threshold": properties.get('FailureThreshold'),
            }
            health_check = route53_backend.create_health_check(
                health_check_args)
            template = Template(CREATE_HEALTH_CHECK_RESPONSE)
            return 201, headers, template.render(health_check=health_check)
        elif method == "DELETE":
            health_check_id = parsed_url.path.split("/")[-1]
            route53_backend.delete_health_check(health_check_id)
            return 200, headers, DELETE_HEALTH_CHECK_RESPONSE
        elif method == "GET":
            template = Template(LIST_HEALTH_CHECKS_RESPONSE)
            health_checks = route53_backend.get_health_checks()
            return 200, headers, template.render(health_checks=health_checks)
    def parameters_example(self, template_subfolder, template_name, parameters):
        parameters = parameters.copy()
        parameters_template_file = os.path.join(
            self.repo_root,
            "pages_builder/parameters_template.html"
        )

        parameters_template = Template(
            open(parameters_template_file).read()
        )

        if "title" in parameters:
            # title is a parameter reserved for naming the pattern
            # in the documentation
            parameters.pop("title", None)

        presented_parameters = {
            key: json.dumps(value, indent=4)
            for key, value in parameters.iteritems()
        }

        return parameters_template.render(
            {
                "parameters": presented_parameters,
                "file": os.path.join("toolkit", template_subfolder, template_name) + ".html"
            }
        )
Example #27
0
    def to_tjp(self):
        """overridden to_tjp method
        """
        from jinja2 import Template

        template = Template(defaults.tjp_vacation_template)
        return template.render({'vacation': self})
Example #28
0
    def list_hosted_zones_by_name_response(self, request, full_url, headers):
        self.setup_class(request, full_url, headers)
        parsed_url = urlparse(full_url)
        query_params = parse_qs(parsed_url.query)
        dnsname = query_params.get("dnsname")

        if dnsname:
            dnsname = dnsname[0]    # parse_qs gives us a list, but this parameter doesn't repeat
            # return all zones with that name (there can be more than one)
            zones = [zone for zone in route53_backend.get_all_hosted_zones() if zone.name == dnsname]
        else:
            # sort by names, but with domain components reversed
            # see http://boto3.readthedocs.io/en/latest/reference/services/route53.html#Route53.Client.list_hosted_zones_by_name

            def sort_key(zone):
                domains = zone.name.split(".")
                if domains[-1] == "":
                    domains = domains[-1:] + domains[:-1]
                return ".".join(reversed(domains))

            zones = route53_backend.get_all_hosted_zones()
            zones = sorted(zones, key=sort_key)

        template = Template(LIST_HOSTED_ZONES_BY_NAME_RESPONSE)
        return 200, headers, template.render(zones=zones)
Example #29
0
 def DrawTransacLinksStr(self, PathList, ForPlugin = False):
     URL, TransacPath, ReqPath, ResHeadersPath, ResBodyPath = PathList
     template = Template("""
         <!-- Start Transactions Links -->
         <a href="{{ Transaction_URL }}" class="label label-info" target="_blank">
             Site
         </a>
         <a href="{{ Base_Path }}{{ Transaction_Path }}" class="label" target="_blank">
             F
         </a>
         <a href="{{ Base_Path }}{{ Request_Path }}" class="label" target="_blank">
             R
         </a>
         <a href="{{ Base_Path }}{{ Resource_Headers_Path }}" class="label" target="_blank">
             H
         </a>
         <a href="{{ Base_Path }}{{ Resource_Body_Path }}" class="label" target="_blank">
             B
         </a>
         <!-- End Transactions Links -->
                     """)
     vars = {
             "Transaction_URL": URL,
             "Transaction_Path":  TransacPath,
             "Request_Path": ReqPath,
             "Resource_Headers_Path": ResHeadersPath ,
             "Resource_Body_Path": ResBodyPath,
             "Base_Path": "../" if not ForPlugin else "../../../../"
         }
     return template.render(vars)
Example #30
0
def construct_events_page(events):
    utc_now = datetime.utcnow().replace(tzinfo=parser.tz.tzutc())
    events = deepcopy(events)
    events_t = []
    for e in events:
        if e.get('start_time'):
            e['start_time'] = parser.parse(e['start_time'])
            e['display_date'] = e['start_time'].strftime('%B %-d, %Y')
            if e['start_time'] <= utc_now:
                events_t.append(e)
    events_t.sort(key=lambda x: x['start_time'], reverse=True)
    events = events_t

    with open('templates/feed.j2') as f:
        template = Template(f.read())
    for event in events:
        for image in event.get('photos', []):
            if image.get('url_small'):
                suffix = '_small'
            else:
                suffix = ''
            image['url'] = path.join(
                '/feed/images',
                image['id'] + suffix + '.jpg',
                )
    return template.render(events=events)
def generateFile(filename, mid):
    myFields = []
    packetTypeUnique = []
    totalPacketSize = 0
    currFieldPos = 0
    for myType in soup.find_all('mtype', id=mid):
        var = myType['typeuniq'].split('=')[0].replace(
            ".", "_").encode('ascii').strip()
        val = int(myType['typeuniq'].split('=')[1].replace(
            ".", "_").encode('ascii'))
        packetTypeUnique.append((var, val))
        currPacketSize = 0
        for myField in myType.find_all('mfield'):
            #only include the field if its byte location has not been specified by another field:
            if myField.msize.contents[0].split(';')[
                    0] != 'unspecified' and myField.msize.contents[0].split(
                        ';')[0] != '0' and len(
                            myField.mvalue.contents
                        ) > 0 and myField.mvalue.contents[0].split(
                            ';')[0] != 'unspecified':
                totalPacketSize += int(myField.msize.contents[0].split(';')[0])
                #fix up the mname field:
                mname = myField.mname.contents[0].split(';')[0].replace(
                    "(", "").replace(")", "").replace(" ",
                                                      "_").replace(".", "_")
                #fix up the vocab field to work with C++ (hex bytes)
                mvalue = ""
                mvalueItem = myField.mvalue.contents[0].split(';')[0]
                if mvalueItem.strip() != ';' and mvalueItem.strip() != '':
                    if myField.msize.contents[0].split(
                            ';')[0] == '1' or myField.msize.contents[0].split(
                                ';'
                            )[0] == '2' or myField.msize.contents[0].split(
                                ';')[0] == '4' or myField.msize.contents[
                                    0].split(';')[0] == '8':
                        mvalue = "0x" + mvalueItem
                    else:
                        mvalueItem = mvalueItem.decode("hex")
                        mvalue = ''.join(
                            ["0x%02X " % ord(x) for x in mvalueItem]).strip()
                if int(myField.mpos.contents[0].split(';')
                       [0]) == currFieldPos or len(myFields) == 0:
                    #print 'NEW: adding new',myField.mname.contents[0].split(';')[0].replace("(","").replace(")","").replace(" ","_").replace(".","_")
                    myFields.append(
                        (myType['id'], mname,
                         myField.msize.contents[0].split(';')[0],
                         myField.mentropy.contents[0].split(';')[0], mvalue,
                         myField.mpos.contents[0].split(';')[0]))
                    currFieldPos = int(
                        myField.mpos.contents[0].split(';')[0]) + int(
                            myField.msize.contents[0].split(';')[0])
                elif int(
                        myField.mpos.contents[0].split(';')[0]) < currFieldPos:
                    #print 'LESS: found pos less than existing: ',myField.mname.contents[0].split(';')[0].replace("(","").replace(")","").replace(" ","_").replace(".","_")
                    #print "TEST: ", myField.msize.contents[0].split(';')[0],'>',int(myFields[-1][2])
                    if int(myField.msize.contents[0].split(';')[0]) > int(
                            myFields[-1][2]):
                        #print 'RES: replacing because curr is sample pos and bigger than previous'
                        #print 'before:',myFields[-1]
                        rpId = myType['id']
                        rpName = mname.split(';')[0]
                        rpSize = myField.msize.contents[0].split(';')[0]
                        rpEnt = myField.mentropy.contents[0].split(';')[0]
                        rpVal = calcStrOr(
                            myFields[-1][4], int(myFields[-1][2]),
                            myField.mvalue.contents[0].split(';')[0],
                            int(rpSize))
                        rpPos = myField.mpos.contents[0].split(';')[0]
                        myFields[-1] = (rpId, rpName, rpSize, rpEnt, rpVal,
                                        rpPos)
                        currFieldPos = int(rpPos) + int(rpSize)
                        #print 'after:',myFields[-1]
                    else:
                        {}  #print 'RES: not bigger'
                else:
                    {
                    }  #print 'IGNORED:',myField.mname.contents[0].split(';')[0].replace("(","").replace(")","").replace(" ","_").replace(".","_")'

    hFile = Template('''
#ifndef {{jinjaModelName|upper}}_H
#define {{jinjaModelName|upper}}_H

#include "ns3/application.h"
#include "ns3/traced-callback.h"
#include "ns3/nstime.h"
#include "ns3/average.h"
#include "ns3/simulator.h"
#include <map>
#include "{{jinjaModelName}}-PacketFactory.h"
#include "ns3/{{jinjaModelName}}Grammar.h"

namespace ns3 {

class Socket;

/**
 * \\ingroup applications
 * \\defgroup {{jinjaModelName}} {{jinjaModelName}}
 */

/**
 * \\ingroup {{jinjaModelName}}
 * \\brief {{jinjaTodo}}
 *
 * Note: {{jinjaTodo}}
 */
class {{jinjaModelName}} : public Application
{
public:
  /**
   * \\brief Get the type ID.
   * \\return the object TypeId
   */
  static TypeId GetTypeId (void);

  /**
   * create a {{jinjaTodo}}
   */
  {{jinjaModelName}} ();
  virtual ~{{jinjaModelName}} ();

private:

  // inherited from Application base class.
  virtual void StartApplication (void);
  virtual void StopApplication (void);
  virtual void DoDispose (void);
  /**
   * \\brief Return the application ID in the node.
   * \\returns the application id
   */
  uint32_t GetApplicationId (void) const;
  /**
   * \\brief Receive an {{todo}}
   * \\param socket the receiving socket
   *
   * This function is called by lower layers through a callback.
   */
  void Receive (Ptr<Socket> socket);
  /**
   * \\brief {{jinjaTodo}}
   */
  void Send ();

  /// Remote address
  Ipv4Address m_remote;
  /// Wait  interval seconds between sending each packet
  Time m_interval;
  /**
   * Specifies  the number of data bytes to be sent.
   */
  uint32_t m_size;
  /// The socket we send packets from
  Ptr<Socket> m_socket;
  /// ICMP ECHO sequence number
  uint16_t m_seq;
  /// TracedCallback for RTT measured by ICMP ECHOs
  TracedCallback<Time> m_traceRtt;
  /// produce extended output if true
  bool m_verbose;
  /// received packets counter
  uint32_t m_recv;
  /// Start time (when packet was sent)
  Time m_started;
  /// Average rtt is ms
  Average<double> m_avgRtt;
  /// Next packet will be sent
  EventId m_next;
  /// All sent but not answered packets. Map icmp seqno -> when sent
  std::map<uint16_t, Time> m_sent;
  /// Grammar state machine associated with the model
  StateMachine *sm;
  /// will instantiate communication if true
  bool m_instantiator;
  /// id associated with this model instance
  uint32_t m_id;
};

} // namespace ns3

#endif /* {{jinjaModelName|upper}}_H */
''')
    ofile = open(filename, 'w')
    ofile.write(
        hFile.render(jinjaPacketTypes=mid,
                     jinjaModelName=modelName,
                     jinjaFieldNames=myFields,
                     todo='TODO',
                     defaultDataType='char'))  #'UNKNOWN_DataType'))
Example #32
0
from jinja2 import Template

from common  import *

template = Template('Hello {{ name }}!')

print template.render(name='John Doe')
Example #33
0
def render_str(string,dic={}):
	tpl = Template(string)
	return tpl.render(dic)
def execute_force_invites(trace_id, body_object, organization_id):
    pm_logger = common_utils.begin_logger(trace_id, __name__,
                                          inspect.currentframe())

    # parse json
    try:
        body_object_json = json.loads(body_object)
    except Exception as e:
        return common_utils.error_exception(MsgConst.ERR_REQUEST_202,
                                            HTTPStatus.BAD_REQUEST, e,
                                            pm_logger, True)

    caller_service_name = common_utils.get_value("callerServiceName",
                                                 body_object_json, None)
    mail_lang = common_utils.get_value("mailLang", body_object_json, None)
    mail_address = common_utils.get_value("mailAddress", body_object_json,
                                          None)
    authority = common_utils.get_value("authority", body_object_json, None)

    # validate param execute invite unregistered
    list_error = validate_param_invite_unregistered_user(
        trace_id, mail_lang, caller_service_name, mail_address, authority)
    if list_error:
        return common_utils.error_validate(MsgConst.ERR_REQUEST_201,
                                           HTTPStatus.UNPROCESSABLE_ENTITY,
                                           list_error, pm_logger)

    # get list cognito users
    try:
        list_users = aws_common.get_cognito_user_pools(trace_id, mail_address)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_COGNITO_501,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if list_users:
        return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT,
                                         pm_logger)

    # regist Cognito UserPools
    temporary_password = ''
    pattern = re.compile(CommonConst.FORMAT_PASSWORD_TEMPORARY)
    while pattern.match(temporary_password) is None:
        temporary_password = common_utils.get_password_temporary(
            CommonConst.NUMBER_CHARACTERS_PASSWORD_TEMPORARY)

    user_attributes = [{"Name": "email", "Value": mail_address}]
    user_name = common_utils.get_uuid4()
    message_action = MessageAction.Suppress
    try:
        aws_common.process_admin_create_user_pools(trace_id, user_name,
                                                   user_attributes,
                                                   temporary_password,
                                                   message_action)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_COGNITO_501,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # enable confirm email
    try:
        user_attributes = [{'Name': 'email_verified', 'Value': 'true'}]
        aws_common.update_cognito_user_attributes(trace_id, user_name,
                                                  user_attributes)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_COGNITO_501,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # get affiliation
    try:
        affiliation = pm_affiliation.get_affiliation(user_name,
                                                     organization_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if affiliation and affiliation["InvitationStatus"] != InvitationStatus.Deny:
        return common_utils.error_common(MsgConst.ERR_302, HTTPStatus.CONFLICT,
                                         pm_logger)

    # get organization
    try:
        organization = pm_organizations.get_organization(
            trace_id, organization_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)
    if len(organization) == 0:
        return common_utils.error_common(MsgConst.ERR_301,
                                         HTTPStatus.NOT_FOUND, pm_logger)

    # create affiliation
    try:
        pm_affiliation.create_affiliation(trace_id, mail_address, user_name,
                                          organization_id, authority,
                                          InvitationStatus.Invited)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_DB_403,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # Get data affiliation
    try:
        affiliation_result = pm_affiliation.get_affiliation(
            user_name, organization_id, True)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # Get data user_attribute
    try:
        user_attribute = pm_userAttribute.query_key(trace_id)
    except PmError as e:
        return common_utils.error_exception(MsgConst.ERR_402,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # S3から通知メール送信設定ファイルを取得します。
    try:
        config = FileUtils.read_yaml(trace_id, CommonConst.S3_SETTING_BUCKET,
                                     CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL)
    except PmError as e:
        pm_logger.error("メール送信設定ファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            CommonConst.NOTIFY_CONFIG_CIS_RESULT_MAIL))
        return common_utils.error_exception(MsgConst.ERR_S3_702,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # メッセージ本文を作成します。
    path_file_template = config[
        CommonConst.KEY_GET_PATH_FILE_TEMPLATE_USER_INVITE_MAIL.format(
            language=mail_lang, serviceName=caller_service_name)]
    try:
        template_body_mail = FileUtils.read_decode(
            trace_id, CommonConst.S3_SETTING_BUCKET, path_file_template)
    except PmError as e:
        pm_logger.error("招待メール本文テンプレートファイルの取得に失敗しました。:s3://{0}/{1}".format(
            common_utils.get_environ(CommonConst.S3_SETTING_BUCKET),
            path_file_template))
        return common_utils.error_exception(MsgConst.ERR_S3_702,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    # SESで通知メールを送信します。
    bcc_addresses = [mail_address]
    user_name_sign_in = common_utils.get_value("UserName", user_attribute,
                                               None)
    if not user_name_sign_in:
        try:
            affiliation_sign_in = pm_affiliation.get_affiliation(
                trace_id, organization_id)
        except PmError as e:
            return common_utils.error_exception(
                MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e,
                pm_logger, True)
        user_name_sign_in = common_utils.get_value("MailAddress",
                                                   affiliation_sign_in, None)

    organization_name = common_utils.get_value("OrganizationName",
                                               organization, None)
    time_zone = date_utils.get_time_zone_by_language(mail_lang)
    time_to_live_date = date_utils.get_current_date() + timedelta(days=6)
    time_to_live = date_utils.toString(time_to_live_date,
                                       date_utils.PATTERN_YYYYMMDD_SLASH,
                                       time_zone)
    template_body_mail = Template(template_body_mail)
    context = {
        'mailAddress': mail_address,
        'userName': user_name_sign_in,
        'organizationName': organization_name,
        'temporaryPassword': temporary_password,
        'timeToLive': time_to_live
    }
    body_mail = template_body_mail.render(context)
    mail_subject = config[CommonConst.KEY_MAIL_SUBJECT_USER_INVITE.format(
        language=mail_lang, serviceName=caller_service_name)]
    mail_from = config[CommonConst.KEY_INVITE_MAIL_FROM_SERVICE.format(
        serviceName=caller_service_name)]
    ses_region = config['ses.region']
    try:
        aws_common.send_email(user_name, ses_region, mail_from, bcc_addresses,
                              mail_subject, body_mail)
    except PmError as e:
        pm_logger.error("通知メール送信に失敗しました。")
        return common_utils.error_exception(MsgConst.ERR_SES_801,
                                            HTTPStatus.INTERNAL_SERVER_ERROR,
                                            e, pm_logger, True)

    response = common_utils.get_response_by_response_body(
        HTTPStatus.CREATED, affiliation_result)

    # return data response
    return common_utils.response(response, pm_logger)
Example #35
0
# -*- coding: utf-8 -*-
"""
根据工具类型, 渲染所需表单"""

from __future__ import unicode_literals
import os

from jinja2 import Environment, PackageLoader, Template

here = os.path.dirname(os.path.abspath(__file__))

basic_form_group_template = Template("""
<div class="form-group" name="{{ ARG_NAME }}" {{ addition_info }}>
{{ form_group_content }}
</div>
""")


class FormViewAdapter(object):
    def __init__(self, app, templates_dir='templates'):
        if hasattr(app, 'config') and 'APP_NAME' in app.config:
            self.name = app.config['APP_NAME']
        else:
            self.name = app
        self.env = Environment(loader=PackageLoader(self.name, templates_dir))

    def render_js(self, arg_dict):
        """
        Render a js."""
        js_list = []
the_word = "kenya"


html_file = open("data-hold/word-" + the_word + ".html", "w", encoding = "utf-8")
body_template = Template(
"""
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>My CA Twitter words</title>
   <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
    <section class="container">
        <h1>Congress folks who tweeted about "{{ word }}"</h1>
        <div class="row">
           <div class="col-sm-6">
              <h2>Democrats</h2>
              {{ d_tweets }}
           </div>
           <div class="col-sm-6">
              <h2>Republicans</h2>
              {{ r_tweets }}
           </div>
     </section>
  </body>
  </html>
""")

# Documentation on Twitter's embed tweet code:
# https://dev.twitter.com/web/embedded-tweets
embed_tweet = Template("""
Example #37
0
from pathlib import Path

from jinja2 import Template

__all__ = ('search_result_template', )

with open(Path('wiki_search/static/search_result.html')) as template:
    search_result_template = Template(template.read())
Example #38
0
{{ agent }}.sinks.{{ name }}_sink.hdfs.minBlockReplicas = 1
{{ agent }}.sinks.{{ name }}_sink.hdfs.rollInterval = 0
{{ agent }}.sinks.{{ name }}_sink.hdfs.rollSize = 0 
{{ agent }}.sinks.{{ name }}_sink.hdfs.rollCount = 0
{{ agent }}.sinks.{{ name }}_sink.hdfs.idleTimeout = 5
{{ agent }}.sinks.{{ name }}_sink.hdfs.filePrefix = %{basename}

{{ agent }}.channels.{{ name }}_ch.type = memory
###############################################################
###############################################################
{% endfor %}
'''
if len(argv) > 1:
    config = argv[1]
else:
    print('#Set Config File to flume_source_sink.ini')
    config = 'conf_template/flume_source_sink.ini'

config_info = RawConfigParser()
config_info.optionxform = str

config_info.read(config)
dirdict = {
    name: dict(info)
    for name, info in dict(config_info).items() if name != 'DEFAULT'
}
agent = config_info['DEFAULT']['agent']
flume_template = Template(text)
rt = flume_template.render(agent=agent, dirdict=dirdict)
stdout.write(rt)
def Sequential(
    input_args: str,
    modules: List[Union[Tuple[Callable, str], Callable]],
) -> torch.nn.Module:
    r"""An extension of the :class:`torch.nn.Sequential` container in order to
    define a sequential GNN model.
    Since GNN operators take in multiple input arguments,
    :class:`torch_geometric.nn.Sequential` expects both global input
    arguments, and function header definitions of individual operators.
    If omitted, an intermediate module will operate on the *output* of its
    preceding module:

    .. code-block:: python

        from torch.nn import Linear, ReLU
        from torch_geometric.nn import Sequential, GCNConv

        model = Sequential('x, edge_index', [
            (GCNConv(in_channels, 64), 'x, edge_index -> x'),
            ReLU(inplace=True),
            (GCNConv(64, 64), 'x, edge_index -> x'),
            ReLU(inplace=True),
            Linear(64, out_channels),
        ])

    where ``'x, edge_index'`` defines the input arguments of :obj:`model`,
    and ``'x, edge_index -> x'`` defines the function header, *i.e.* input
    arguments *and* return types, of :class:`~torch_geometric.nn.conv.GCNConv`.

    In particular, this also allows to create more sophisticated models,
    such as utilizing :class:`~torch_geometric.nn.models.JumpingKnowledge`:

    .. code-block:: python

        from torch.nn import Linear, ReLU, Dropout
        from torch_geometric.nn import Sequential, GCNConv, JumpingKnowledge
        from torch_geometric.nn import global_mean_pool

        model = Sequential('x, edge_index, batch', [
            (Dropout(p=0.5), 'x -> x'),
            (GCNConv(dataset.num_features, 64), 'x, edge_index -> x1'),
            ReLU(inplace=True),
            (GCNConv(64, 64), 'x1, edge_index -> x2'),
            ReLU(inplace=True),
            (lambda x1, x2: [x1, x2], 'x1, x2 -> xs'),
            (JumpingKnowledge("cat", 64, num_layers=2), 'xs -> x'),
            (global_mean_pool, 'x, batch -> x'),
            Linear(2 * 64, dataset.num_classes),
        ])

    Args:
        input_args (str): The input arguments of the model.
        modules ([(str, Callable) or Callable]): A list of modules (with
            optional function header definitions).
    """

    input_args = [x.strip() for x in input_args.split(',')]

    # We require the first entry of the input list to define arguments:
    assert len(modules) > 0 and isinstance(modules[0], (tuple, list))

    # A list holding the callable function and the input and output names:
    calls: List[Tuple[Callable, List[str], List[str]]] = []

    for module in modules:
        if isinstance(module, (tuple, list)) and len(module) >= 2:
            module, desc = module[:2]
            in_desc, out_desc = parse_desc(desc)
        elif isinstance(module, (tuple, list)):
            module = module[0]
            in_desc = out_desc = calls[-1][2]
        else:
            in_desc = out_desc = calls[-1][2]

        calls.append((module, in_desc, out_desc))

    root = os.path.dirname(osp.realpath(__file__))
    with open(osp.join(root, 'sequential.jinja'), 'r') as f:
        template = Template(f.read())

    cls_name = f'Sequential_{uuid1().hex[:6]}'
    module_repr = template.render(
        cls_name=cls_name,
        input_args=input_args,
        calls=calls,
    )

    # Instantiate a class from the rendered module representation.
    module = class_from_module_repr(cls_name, module_repr)()
    for i, (submodule, _, _) in enumerate(calls):
        setattr(module, f'module_{i}', submodule)
    return module
    def execute_config(self,
                       target_groups,
                       log_level=1,
                       out_file=None,
                       var_tree={}):
        """
        :param target_groups: obtained by parsing the targets file by lib.targets_parser.TargetParser
        :param log_level: between 0 - 1
        :param out_file: output file path. default (datetime.datetime.now().result.log)
        :return:
        """
        json_result = []
        ordered_group_config = self.config_parser.order(self.config_groups)
        #print("ordered groups")
        #print(ordered_group_config)
        #print("\n\n")
        host_tree = {}
        if var_tree:
            for group in var_tree:
                for host in var_tree[group]:
                    host_tree[host] = var_tree[group][host]

        for group_config in ordered_group_config:
            execution_devices = []
            if group_config["name"].split(":")[0] == "all":
                for device_list in target_groups.values():
                    execution_devices += device_list
            else:
                execution_devices = target_groups[group_config["name"].split(
                    ":")[0]]

            for device in execution_devices:
                device_log = ""
                result_dict = {
                    "name": device.get("name", "UNSPECIFIED"),
                    "ip": device["ip"],
                    "username": device["username"],
                }
                conn = BaseConnection(ip=device["ip"],
                                      username=device["username"],
                                      password=device["password"],
                                      hostname=device.get("name", ""))
                print("connecting to device {}, ip: {}...".format(
                    device.get("name", "UNSPECIFIED"), device["ip"]))

                try:
                    conn.connect()
                    device_shell = conn.get_shell()
                except Exception as e:
                    log_line = "execption {} when connecting to device.".format(
                        str(e))
                    print(log_line)
                    result_dict["success"] = False
                    result_dict["result"] = log_line
                    json_result.append(result_dict)
                    continue

                # TODO render CMDs with host variables
                # From the (to be created) HostConfigRenderer Class
                # replace the below group_config with host_config list returned by the HostConfigRenderer Class
                config_template = Template("\n".join(group_config["config"]))
                if not var_tree:
                    config_text = config_template.render()
                else:
                    config_text = config_template.render(
                        **host_tree.get(device["ip"]))

                for cmd in config_text.split("\n"):
                    print("cmd: {}".format(cmd))

                    try:
                        out = conn.shell_exec(device_shell, cmd)
                        print(out)
                        device_log += out
                    except Exception as e:
                        log_line = "failed to execute <{}> due to execption: {}\nskipping this device".format(
                            cmd, str(e))
                        print(log_line)
                        result_dict["success"] = False
                        result_dict["result"] = device_log
                        try:
                            conn.close()
                        except:
                            pass
                        break
                result_dict["result"] = device_log
                result_dict["success"] = True
                json_result.append(result_dict)
                try:
                    conn.close()
                except:
                    pass

        with open(out_file, "w") as log:
            for result_dict in json_result:
                log.write(
                    "#############################################################################\n"
                )
                log.write("DEVICE {}, IP {}, USERNAME {}, SUCCESS {}\n".format(
                    result_dict["name"], result_dict["ip"],
                    result_dict["username"], result_dict["success"]))
                log.write(result_dict["result"])

        return json_result
Example #41
0
 def get_prop_setter_body(self, attribute):
     if attribute.attr_assignment:
         template = self.SETTER_TEMPLATE_ASSIGN
     else:
         template = self.SETTER_TEMPLATE_DEFAULT
     return Template(template).render(object=self, trim_blocks=True)
Example #42
0
# OMF imports
import omf.feeder as feeder
from omf.solvers import nrelsam2013
import random
from omf.weather import zipCodeToClimateName

# Model metadata:
fileName = os.path.basename(__file__)
modelName = fileName[0:fileName.rfind('.')]
tooltip = "The solarCashflow model allows a utility to calculate what impact member owned solar systems will have on their costs."

# Our HTML template for the interface:
with open(pJoin(__neoMetaModel__._myDir, modelName + ".html"),
          "r") as tempFile:
    template = Template(tempFile.read())


def work(modelDir, inputDict):
    ''' Run the model in its directory. '''
    inputDict["climateName"], latforpvwatts = zipCodeToClimateName(
        inputDict["zipCode"])
    shutil.copy(
        pJoin(__neoMetaModel__._omfDir, "data", "Climate",
              inputDict["climateName"] + ".tmy2"),
        pJoin(modelDir, "climate.tmy2"))
    # Set up SAM data structures.
    ssc = nrelsam2013.SSCAPI()
    dat = ssc.ssc_data_create()
    # Required user inputs.
    ssc.ssc_data_set_string(dat, "file_name", modelDir + "/climate.tmy2")
Example #43
0
		file.write(hostname)

relay = "{\"host\":\"\",\"username\":\"\",\"password\":\"\",\"port\":\"\"}"
oldRelay = ""
relayChanged = 0
if os.path.exists("/var/nomx/relay"):
	with open("/var/nomx/relay", "r") as file:
		oldRelay = file.read()
if os.path.exists("/etc/relay.json"):
	with open("/etc/relay.json", "r") as file:
		relay = file.read()

if relay != oldRelay:
	print("[%s] Relay has changed. Updating..." % datetime.datetime.now().isoformat())
	with open("/etc/postfix/template.jinja2", "r") as templateFile:
		template = Template(templateFile.read())
		with open("/etc/postfix/main.cf", "w") as conf:
			relayVars = json.loads(relay)
			relayVars["serial"] = getserial()
			conf.write(template.render(relayVars))
			call(["/usr/sbin/service", "postfix", "reload"])
			with open("/var/nomx/relay", "w") as file:
				file.write(relay)

changed = 0

oldIp = ""
if os.path.exists("/var/nomx/ip"):
	with open("/var/nomx/ip", 'r') as file:
		oldIp = file.read()
if ip != oldIp:
Example #44
0
 def render(self):
     return Template(self.OBJECT_TEMPLATE).render(object=self)
Example #45
0
def read_template_html_file(filename):
    content = Template(Path(filename).read_text())
    return content
# coding: utf-8
import sys
import os
from jinja2 import Template

template = Template("Your input: {}".format(sys.argv[1] if len(sys.argv) > 1 else '<empty>'))
template.globals['os'] = os

print template.render()
Example #47
0
# -*- coding: utf-8 -*-
from jinja2 import Template


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age


per = Person("Alex", 33)

tm = Template("Мне {{p.age}} лет ривет {{ p.name }}")
msg = tm.render(p=per)

print(msg)
def process_needfilters(app, doctree, fromdocname):
    # Replace all needlist nodes with a list of the collected needs.
    # Augment each need with a backlink to the original location.
    env = app.builder.env

    # NEEDFILTER
    for node in doctree.traverse(Needfilter):
        if not app.config.needs_include_needs:
            # Ok, this is really dirty.
            # If we replace a node, docutils checks, if it will not lose any attributes.
            # But this is here the case, because we are using the attribute "ids" of a node.
            # However, I do not understand, why losing an attribute is such a big deal, so we delete everything
            # before docutils claims about it.
            for att in ('ids', 'names', 'classes', 'dupnames'):
                node[att] = []
            node.replace_self([])
            continue

        id = node.attributes["ids"][0]
        current_needfilter = env.need_all_needfilters[id]
        all_needs = env.needs_all_needs

        if current_needfilter["layout"] == "list":
            content = []

        elif current_needfilter["layout"] == "diagram":
            content = []
            try:
                if "sphinxcontrib.plantuml" not in app.config.extensions:
                    raise ImportError
                from sphinxcontrib.plantuml import plantuml
            except ImportError:
                content = nodes.error()
                para = nodes.paragraph()
                text = nodes.Text("PlantUML is not available!",
                                  "PlantUML is not available!")
                para += text
                content.append(para)
                node.replace_self(content)
                continue

            plantuml_block_text = ".. plantuml::\n" \
                                  "\n" \
                                  "   @startuml" \
                                  "   @enduml"
            puml_node = plantuml(plantuml_block_text, **dict())
            puml_node["uml"] = "@startuml\n"
            puml_connections = ""

        elif current_needfilter["layout"] == "table":
            content = nodes.table()
            tgroup = nodes.tgroup()
            id_colspec = nodes.colspec(colwidth=5)
            title_colspec = nodes.colspec(colwidth=15)
            type_colspec = nodes.colspec(colwidth=5)
            status_colspec = nodes.colspec(colwidth=5)
            links_colspec = nodes.colspec(colwidth=5)
            tags_colspec = nodes.colspec(colwidth=5)
            tgroup += [
                id_colspec, title_colspec, type_colspec, status_colspec,
                links_colspec, tags_colspec
            ]
            tgroup += nodes.thead(
                '',
                nodes.row('', nodes.entry('', nodes.paragraph('', 'ID')),
                          nodes.entry('', nodes.paragraph('', 'Title')),
                          nodes.entry('', nodes.paragraph('', 'Type')),
                          nodes.entry('', nodes.paragraph('', 'Status')),
                          nodes.entry('', nodes.paragraph('', 'Links')),
                          nodes.entry('', nodes.paragraph('', 'Tags'))))
            tbody = nodes.tbody()
            tgroup += tbody
            content += tgroup

        all_needs = list(all_needs.values())
        found_needs = procces_filters(all_needs, current_needfilter)

        line_block = nodes.line_block()
        for need_info in found_needs:
            if current_needfilter["layout"] == "list":
                para = nodes.line()
                description = "%s: %s" % (need_info["id"], need_info["title"])

                if current_needfilter["show_status"] and need_info[
                        "status"] is not None:
                    description += " (%s)" % need_info["status"]

                if current_needfilter["show_tags"] and need_info[
                        "tags"] is not None:
                    description += " [%s]" % "; ".join(need_info["tags"])

                title = nodes.Text(description, description)

                # Create a reference
                if not need_info["hide"]:
                    ref = nodes.reference('', '')
                    ref['refdocname'] = need_info['docname']
                    ref['refuri'] = app.builder.get_relative_uri(
                        fromdocname, need_info['docname'])
                    ref['refuri'] += '#' + need_info['target_node']['refid']
                    ref.append(title)
                    para += ref
                else:
                    para += title

                line_block.append(para)
            elif current_needfilter["layout"] == "table":
                row = nodes.row()
                row += row_col_maker(app,
                                     fromdocname,
                                     env.needs_all_needs,
                                     need_info,
                                     "id",
                                     make_ref=True)
                row += row_col_maker(app, fromdocname, env.needs_all_needs,
                                     need_info, "title")
                row += row_col_maker(app, fromdocname, env.needs_all_needs,
                                     need_info, "type_name")
                row += row_col_maker(app, fromdocname, env.needs_all_needs,
                                     need_info, "status")
                row += row_col_maker(app,
                                     fromdocname,
                                     env.needs_all_needs,
                                     need_info,
                                     "links",
                                     ref_lookup=True)
                row += row_col_maker(app, fromdocname, env.needs_all_needs,
                                     need_info, "tags")
                tbody += row
            elif current_needfilter["layout"] == "diagram":
                # Link calculation
                # All links we can get from docutils functions will be relative.
                # But the generated link in the svg will be relative to the svg-file location
                # (e.g. server.com/docs/_images/sqwxo499cnq329439dfjne.svg)
                # and not to current documentation. Therefore we need to add ../ to get out of the image folder.
                try:
                    link = "../" + app.builder.get_target_uri(need_info['docname']) \
                           + "?highlight={0}".format(urlParse(need_info['title'])) \
                           + "#" \
                           + need_info['target_node']['refid'] \
                        # Gets mostly called during latex generation

                except NoUri:
                    link = ""

                diagram_template = Template(env.config.needs_diagram_template)
                node_text = diagram_template.render(**need_info)

                puml_node[
                    "uml"] += '{style} "{node_text}" as {id} [[{link}]] {color}\n'.format(
                        id=need_info["id"],
                        node_text=node_text,
                        link=link,
                        color=need_info["type_color"],
                        style=need_info["type_style"])
                for link in need_info["links"]:
                    puml_connections += '{id} --> {link}\n'.format(
                        id=need_info["id"], link=link)

        if current_needfilter["layout"] == "list":
            content.append(line_block)

        if current_needfilter["layout"] == "diagram":
            puml_node["uml"] += puml_connections

            # Create a legend

            if current_needfilter["show_legend"]:
                puml_node["uml"] += "legend\n"
                puml_node["uml"] += "|= Color |= Type |\n"
                for need in app.config.needs_types:
                    puml_node[
                        "uml"] += "|<back:{color}> {color} </back>| {name} |\n".format(
                            color=need["color"], name=need["title"])
                puml_node["uml"] += "endlegend\n"
            puml_node["uml"] += "@enduml"
            puml_node["incdir"] = os.path.dirname(
                current_needfilter["docname"])
            puml_node["filename"] = os.path.split(
                current_needfilter["docname"])[1]  # Needed for plantuml >= 0.9
            content.append(puml_node)

        if len(content) == 0:
            nothing_found = "No needs passed the filters"
            para = nodes.line()
            nothing_found_node = nodes.Text(nothing_found, nothing_found)
            para += nothing_found_node
            content.append(para)
        if current_needfilter["show_filters"]:
            para = nodes.paragraph()
            filter_text = "Used filter:"
            filter_text += " status(%s)" % " OR ".join(
                current_needfilter["status"]) if len(
                    current_needfilter["status"]) > 0 else ""
            if len(current_needfilter["status"]) > 0 and len(
                    current_needfilter["tags"]) > 0:
                filter_text += " AND "
            filter_text += " tags(%s)" % " OR ".join(
                current_needfilter["tags"]) if len(
                    current_needfilter["tags"]) > 0 else ""
            if (len(current_needfilter["status"]) > 0
                    or len(current_needfilter["tags"]) > 0) and len(
                        current_needfilter["types"]) > 0:
                filter_text += " AND "
            filter_text += " types(%s)" % " OR ".join(
                current_needfilter["types"]) if len(
                    current_needfilter["types"]) > 0 else ""

            filter_node = nodes.emphasis(filter_text, filter_text)
            para += filter_node
            content.append(para)

        node.replace_self(content)
    def get_olfactory_transduction_kernel(self):

        template = Template("""
    #include "stdio.h"
    #define NUM_OF_NEURON {{num_neurons}}
    #define cap 4.299e-3          // capacitance, [4.299e-3nF]
    #define cc1lin 1.224          // Ca2+ association rate with CaM, [s^-1]
    #define cc2 22.89             // CaCaM dissociation rate into Ca and CaM, [s^-1]
    #define ck1lin 12.72          // CaMK activation rate by CaCaM, [s^-1]
    #define ck2 0.5564            // CaMK deactivation rate, [s^-1]
    #define clmax 1.013           // maximal g of Cl(Ca) channels, [1.013nS]
    #define cnmax 1.277           // maximal g of CNG channels, [1.277nS]
    #define cx1lin 1.171          // IX activation rate by Ca2+, [s^-1]
    #define cx2 16.12             // IX deactivation rate, [s^-1]
    #define ef 2.162              // Ca2+ extrusion rate constant by NCX, [s^-1]
    #define F 9.649e4             // Faraday's constant, [C/mol]
    #define Cvol 4.2191e-7        // ciliary volume, []
			     // Calculated from Fvol=4.071e-2, table 5
    #define gl 4.575              // leak current conductance, [4.575nS]
    #define Gtot 1                // total number of G-proteins, [part]
    #define hmc1 1.23             // cAMP concentration needed to achieve half-maximal
			     // activation of the CNG channel, [1.23uM]
    #define hmc2 2.604            // Ca2+ concentration needed to achieve half-maximal
			     // activation of the Cl(Ca) channel, [2.604uM]
    #define inf 1.26              // CNG current carried by Ca, [1.26uM*pC^-1]
    #define inhmax 1.396          // maximal CNG channel inhibition factor
    #define k1 0.02351            // odorant binding rate to receptor, [0.02351(uM*s)^-1]
    #define k2 9.915              // G-protein activation rate per bound receptor complex, [s^-1]
    #define kI 0.7037             // IX concentration needed to exert a half-maximal
			     // inhibitory effect (IC_50), [0.7037uM]
    #define kinh 0.3901           // aCaMK concentration needed for half-maximal 
			     // inhibition (IC_50) of cAMP production, [0.3901uM]
    #define kinhcng 0.8951        // CaCaM concentration needed for half-maximal
			     // inhibition of the CNG channel, [0.8951uM]
    #define n1 1.639              // Hill coeff. of the CNG ch. activation function
    #define n2 2.276              // Hill coeff. of the Cl(Ca) ch. activation function
    #define nI 3.705              // Steepness of the decreasing sigmoid representing
			     // IX-mediated inhibition
    #define ninh 1.372            // Steepness of the decreasing sigmoid representing
			     // aCaMK-mediated inhibition of cAMP synthesis
    #define ninhcng 1.112         // Steepness of the sigmoid inhcng representing
			     // the fold increase in K_1/2 of the CNG channel
			     // as a function of CaCaM concentration
    #define pd 10.88              // cAMP molecule degradation rate, [s^-1]
    #define r1 6.911              // odorant unbinding rate from receptor, [s^-1]
    #define r2 4.055              // G-protein deactivation rate, [s^-1]
    #define Rtot 1                // total number of receptor, [part]
    #define smax 91.09            // maximal cAMP production rate by adenylyl cyclase 
			     // per aG, [91.09uM/s]
    #define vcl -11.16            // ClCa channel reversal potential, [11.16mV]
    #define vcng 0.002655         // CNG channel reversal potential, [0.002655mV]
    #define vl -69.67             // effective leak reversal potential, [69.67mV]

    // state variables: bLR,aG,cAMP,Ca,CaCaM,aCaMK,IX,inhcng,I_cng,I_cl,I_ncx,I_leak, V
    __global__ void dougherty_transduction({{type}} dt, {{type}}* Ostim, {{type}} *binding_rate, {{type}} (*state)[13], {{type}} *I)
    {
	int tid = blockIdx.x * blockDim.x + threadIdx.x;
	if(tid < NUM_OF_NEURON){
	    float a1 = 0;
	    float a2 = 0;
	    float a3 = 0;
	    float a4 = 0;
	    float a5 = 0;
	    float a6 = 0;
	    float a7 = 0;
	
	    float b1 = 0;
	    float b2 = 0;
	    float b3 = 0;
	    float b4 = 0;
	    float b5 = 0;
	    float b6 = 0;
	    float b7 = 0;
	
	    float D1 = 0;
	    float D2 = 0;
	    float D3 = 0;
	    float D4 = 0;
	    float D5 = 0;
	    float D6 = 0;
	    float D7 = 0;
	
	    float bLR = state[tid][0];
	    float aG = state[tid][1];
	    float cAMP = state[tid][2];
	    float Ca = state[tid][3];
	    float CaCaM = state[tid][4];
	    float aCaMK = state[tid][5];
	    float IX = state[tid][6];
	    float inhcng = state[tid][7];
	    float I_cng = state[tid][8];
	    float I_cl = state[tid][9];
	    float I_ncx = state[tid][10];
	    float I_leak = state[tid][11];
	    //float I = 0;
	    float V = state[tid][12];
	
	    
	    
	    //numeric method
	    
	    //CaCaM = 1e-5;
	    //inhcng = 1;
	
	    a1 = k1*binding_rate[tid]*Ostim[tid] * Rtot;
	    a2 = k2*bLR * Gtot;
	    a3 = aG*smax / (1 + powf((aCaMK/kinh),ninh));
	    a4 = inf*I_cng + cc2*CaCaM;
	    a5 = cc1lin*Ca;
	    a6 = ck1lin*CaCaM;   
	    a7 = cx1lin * Ca;
	       
	    b1 = k1*binding_rate[tid]*Ostim[tid] + r1;
	    b2 = k2*bLR + r2;    
	    b3 = pd;
	    b4 = cc1lin + ef/(1 + powf((IX/kI),nI));
	    b5 = cc2;
	    b6 = ck2;
	    b7 = cx2; 
	    
	    D1 = exp(-b1*dt);
	    D2 = exp(-b2*dt);
	    D3 = exp(-b3*dt);
	    D4 = exp(-b4*dt);      
	    D5 = exp(-b5*dt);
	    D6 = exp(-b6*dt);
	    D7 = exp(-b7*dt);
	    
	       
	    // feedback model
	    state[tid][0] = bLR*D1 + (a1/b1)*(1-D1);
	    state[tid][1] = aG*D2 + (a2/b2)*(1-D2);
	    
	    
	    // Kurahashi and Menini Experiment
	    // Don't forget to comment out a3, b3, D3, cAMP
	    //     if ( i< 40001 || i>50001 )
	    //         a3 = aG[:,i-1]*smax / (1 + (aCaMK[:,i-1]/kinh)**ninh);
	    //         b3 = pd;
	    //         D3 = np.math.exp(-b3*dt);
	    //         cAMP[:,i] = cAMP[:,i-1]*D3 + (a3/b3)*(1-D3);
	    //     
	    //     else
	    //         cAMP[:,i] = 1.5;
	    //     end
	    
	    
	    
	    state[tid][2] = cAMP*D3 + (a3/b3)*(1-D3);
	    
	    state[tid][3] = Ca*D4 + (a4/b4)*(1-D4);    
	    state[tid][4] = CaCaM*D5 + (a5/b5)*(1-D5);
	    state[tid][5] = aCaMK*D6 + (a6/b6)*(1-D6);
	    state[tid][6] = IX*D7 + (a7/b7)*(1-D7);
	    
	    
	    // calculate cell currents for the feedback model
	    state[tid][8] = cnmax * powf(cAMP,n1) /( powf(cAMP,n1)+powf((inhcng*hmc1),n1) ) * ( vcng - V );    
	    state[tid][9] = clmax * powf(Ca,n2) / (powf(Ca,n2) + powf(hmc2,n2)) * (vcl - V);
	    state[tid][10] = F * Cvol * ef*Ca/( 1+powf((IX/kI),nI) );    
	    state[tid][11] = gl * (vl - V);
	    
	    
	    // get the membrane voltage
	    state[tid][12] = V + 1/cap*dt*( I[tid] + I_leak );       
	    I[tid] = (I_cng + I_cl + I_ncx);
	    // calculate the CNG channel inhibition factor
	    state[tid][7] = 1 + (inhmax-1)*powf(CaCaM,ninhcng) /(powf(CaCaM,ninhcng) + powf(kinhcng,ninhcng));
	    
	    
	
	}
    }
    """)
        dtype = np.double
        scalartype = dtype.type if dtype.__class__ is np.dtype else dtype
        #mod = SourceModule( template2.render(num_of_neuron=self.num_neuron), options=["--ptxas-options=-v"])
        #mod = SourceModule( template, options=["--ptxas-options=-v"])
        self.block = (128, 1, 1)
        self.grid = ((self.num_neurons - 1) / 128 + 1, 1)
        mod = SourceModule(template.render(type=dtype_to_ctype(dtype),
                                           num_neurons=self.num_neurons),
                           options=["--ptxas-options=-v"])

        func = mod.get_function("dougherty_transduction")
        func.prepare([
            scalartype,  #dt
            np.intp,  #Ostim/I
            np.intp,  #binding_rate
            np.intp,  #state
            np.intp
        ])  #I_drive
        return func
Example #50
0
        "url": url,
        "method": api_data["method"],
        "api_name": url.replace("/", "_")[1:],
        "args": args,
        "comment": api_data["title"]
    })
    return api_info


items = []
for id in getAPIList(projectID=projectID, apiID=apiID, tag=tag):
    items.append(getAPIInfo(id))

template = '''
{% for item in items %}
@request(url='{{item['url']}}', method='{{item['method']}}')
def {{item['api_name']}}(self{% for a in item['args']%}, {{a}}{% endfor %}):
    """{{item['comment']}}"""
    json = {
        {% for args in item['args'] -%}
        "{{args}}": {{ args }},
        {% endfor -%}
        }
    return {'json': json, 'headers': self.headers}
{% endfor -%}
'''

# 创建api文档
print(Template(template).render(items=items))
print("总共生成{}个接口".format(len(items)))
Example #51
0
    sys.exit(1)

# TODO: Generate Volfile based on Volinfo stored in Config map
# For now, Generated Volfile is used in configmap
data = {}
with open(os.path.join(info_dir, "%s.info" % volname)) as f:
    data = json.load(f)

content = ""
template_file = os.path.join(
    templates_dir,
    "%s.brick%s.vol.j2" % (data["type"], os.environ["BRICK_INDEX"]))
with open(template_file) as f:
    content = f.read()

tmpl = Template(content)

volfile_id = "%s.%s.%s" % (volname, nodename, brick_path_name)
volfile_path = os.path.join(volfiles_dir, "%s.vol" % volfile_id)
tmpl.stream(**data).dump(volfile_path)

# Start glusterfsd process
server_role = os.environ.get("KADALU_SERVER_ROLE", "glusterfsd")

if server_role == "glusterfsd":
    os.execv(
        "/usr/sbin/glusterfsd",
        [
            "/usr/sbin/glusterfsd",
            "-N",
            "--volfile-id",
Example #52
0
            match = re.match(r"(.+)Dialect", name)
            if not match:
                continue
            name = match.group(1)
            data = parse(Dialect.__doc__)
            format = {"name": name, "options": []}
            for param in data.params:
                if param.arg_name.startswith("descriptor"):
                    continue
                type = param.type_name
                text = param.description.capitalize()
                name = stringcase.titlecase(param.arg_name.replace("?", ""))
                format["options"].append({"name": name, "text": text, "type": type})
            formats.append(format)
    return formats


# Main


source = os.path.join(docs.SOURCE_DIR, "formats-reference.md")
target_dir = os.path.join(docs.TARGET_DIR, "formats-reference")
target_md = os.path.join(target_dir, "README.md")
os.makedirs(target_dir, exist_ok=True)
formats = get_formats()
with open(source) as file:
    template = Template(file.read())
    document = template.render(formats=formats)
with open(target_md, "wt") as file:
    file.write(document)
Example #53
0
class Popup(Element):
    """Create a Popup instance that can be linked to a Layer.

    Parameters
    ----------
    html: string or Element
        Content of the Popup.
    parse_html: bool, default False
        True if the popup is a template that needs to the rendered first.
    max_width: int, default 300
        The maximal width of the popup.
    show: bool, default False
        True renders the popup open on page load.
    sticky: bool, default False
        True prevents map and other popup clicks from closing.
    """
    _template = Template(u"""
            var {{this.get_name()}} = L.popup({maxWidth: '{{this.max_width}}'
            {% if this.show or this.sticky %}, autoClose: false{% endif %}
            {% if this.sticky %}, closeOnClick: false{% endif %}});

            {% for name, element in this.html._children.items() %}
                var {{name}} = $('{{element.render(**kwargs).replace('\\n',' ')}}')[0];
                {{this.get_name()}}.setContent({{name}});
            {% endfor %}

            {{this._parent.get_name()}}.bindPopup({{this.get_name()}})
            {% if this.show %}.openPopup(){% endif %};

            {% for name, element in this.script._children.items() %}
                {{element.render()}}
            {% endfor %}
        """)  # noqa

    def __init__(self, html=None, parse_html=False, max_width=300, show=False, sticky=False):
        super(Popup, self).__init__()
        self._name = 'Popup'
        self.header = Element()
        self.html = Element()
        self.script = Element()

        self.header._parent = self
        self.html._parent = self
        self.script._parent = self

        script = not parse_html

        if isinstance(html, Element):
            self.html.add_child(html)
        elif isinstance(html, text_type) or isinstance(html, binary_type):
            self.html.add_child(Html(text_type(html), script=script))

        self.max_width = max_width
        self.show = show
        self.sticky = sticky

    def render(self, **kwargs):
        """Renders the HTML representation of the element."""
        for name, child in self._children.items():
            child.render(**kwargs)

        figure = self.get_root()
        assert isinstance(figure, Figure), ('You cannot render this Element '
                                            'if it is not in a Figure.')

        figure.script.add_child(Element(
            self._template.render(this=self, kwargs=kwargs)),
            name=self.get_name())
    def get_hhn_kernel(self):
        template = Template("""
	#define g_Na 120.0
	#define g_K  36.0
	#define g_L  0.3
	#define E_K  (-12.0)
	#define E_Na 115.0
	#define E_L  10.613

	__global__ void
	hhn_model(int *spk, int num_neurons, {{type}} dt, {{type}}* I_pre,  \
		  {{type}}* X_1, {{type}}* X_2, {{type}}* X_3, {{type}}* g_V, {{type}}* V_prev)
	{
	    int cart_id = blockIdx.x * blockDim.x + threadIdx.x;

	    if(cart_id < num_neurons)
	    {
		{{type}} V = g_V[cart_id];
		{{type}} bias = 10;
		spk[cart_id] = 0;

		{{type}} a[3];

		a[0] = (10-V)/(100*(exp((10-V)/10)-1));
		X_1[cart_id] = a[0]*dt - X_1[cart_id]*(dt*(a[0] + exp(-V/80)/8) - 1);
	       
		a[1] = (25-V)/(10*(exp((25-V)/10)-1));
		X_2[cart_id] = a[1]*dt - X_2[cart_id]*(dt*(a[1] + 4*exp(-V/18)) - 1);
	       
		a[2] = 0.07*exp(-V/20);
		X_3[cart_id] = a[2]*dt - X_3[cart_id]*(dt*(a[2] + 1/(exp((30-V)/10)+1)) - 1);

		V = V + dt * (I_pre[cart_id]+bias - \
		   (g_K * pow(X_1[cart_id], 4) * (V - E_K) + \
		    g_Na * pow(X_2[cart_id], 3) * X_3[cart_id] * (V - E_Na) + \
		    g_L * (V - E_L)));

		if(V_prev[cart_id] <= g_V[cart_id] && g_V[cart_id] > V) {
		    spk[cart_id] = 1;
		}
		
		V_prev[cart_id] = g_V[cart_id];
		g_V[cart_id] = V;
	    }
	}
	""")

        dtype = np.double
        scalartype = dtype.type if dtype.__class__ is np.dtype else dtype
        #hhn_update_block = (128,1,1)
        #hhn_update_grid = ((num_neurons - 1) / 128 + 1, 1)
        self.block = (128, 1, 1)
        self.grid = ((self.num_neurons - 1) / 128 + 1, 1)
        mod = SourceModule(template.render(type=dtype_to_ctype(dtype)),
                           options=["--ptxas-options=-v"])
        func = mod.get_function("hhn_model")

        func.prepare([
            np.intp,  # spk
            np.int32,  # num_neurons
            scalartype,  # dt
            np.intp,  # I_pre
            np.intp,  # X1
            np.intp,  # X2
            np.intp,  # X3
            np.intp,  # g_V                 
            np.intp
        ])  # V_pre

        return func
# Read in the command_sets.yml file
# with open("provision/command_sets_16_9.yml") as f:
#    commands = yaml.load(f.read())["commands"]
#    initial_config = commands["initial_config"]
#    nat_vpg_config = commands["nat_config"]

# Read in the entire YAML file for use in various templates
print("Reading in Device Device Details")
with open("provision/device_details.yml") as f:
    device_details = yaml.load(f.read())

# Create the NETCONF template for creating interfaces
print("Setting Up NETCONF Templates")
with open("provision/templates/netconf_interface_template.j2") as f:
    interface_template = Template(f.read())

# Create the NETCONF template for configuring NAT
# The 'netconf_configs' directory must exist prior to running the script
with open("provision/templates/netconf_nat_template.j2") as f:
    nat_template = Template(f.read())

# Create the NETCONF template for configuring hostname
# The 'netconf_configs' directory must exist prior to running the script
with open("provision/templates/netconf_hostname_template.j2") as f:
    hostname_template = Template(f.read())

# Create the NETCONF template for configuring acl
# The 'netconf_configs' directory must exist prior to running the script
with open("provision/templates/netconf_acl_template.j2") as f:
    acl_template = Template(f.read())
Example #56
0
class LayerControl(MacroElement):
    """
    Creates a LayerControl object to be added on a folium map.

    Parameters
    ----------
    position : str
          The position of the control (one of the map corners), can be
          'topleft', 'topright', 'bottomleft' or 'bottomright'
          default: 'topright'
    collapsed : boolean
          If true the control will be collapsed into an icon and expanded on
          mouse hover or touch.
          default: True
    autoZIndex : boolean
          If true the control assigns zIndexes in increasing order to all of
          its layers so that the order is preserved when switching them on/off.
          default: True
    """
    _template = Template("""
        {% macro script(this,kwargs) %}
            var {{this.get_name()}} = {
                base_layers : { {% for key,val in this.base_layers.items() %}"{{key}}" : {{val}},{% endfor %} },
                overlays : { {% for key,val in this.overlays.items() %}"{{key}}" : {{val}},{% endfor %} }
                };
            L.control.layers(
                {{this.get_name()}}.base_layers,
                {{this.get_name()}}.overlays,
                {position: '{{this.position}}',
                 collapsed: {{this.collapsed}},
                 autoZIndex: {{this.autoZIndex}}
                }).addTo({{this._parent.get_name()}});
            {% for val in this.layers_untoggle %}
                {{ val }}.remove();{% endfor %}
        {% endmacro %}
        """)  # noqa

    def __init__(self, position='topright', collapsed=True, autoZIndex=True):
        super(LayerControl, self).__init__()
        self._name = 'LayerControl'
        self.position = position
        self.collapsed = str(collapsed).lower()
        self.autoZIndex = str(autoZIndex).lower()
        self.base_layers = OrderedDict()
        self.overlays = OrderedDict()
        self.layers_untoggle = []

    def render(self, **kwargs):
        """Renders the HTML representation of the element."""
        self.base_layers = OrderedDict(
            [(val.layer_name, val.get_name()) for key, val in
             self._parent._children.items() if isinstance(val, Layer)
             and not val.overlay and val.control])
        self.overlays = OrderedDict(
            [(val.layer_name, val.get_name()) for key, val in
             self._parent._children.items() if isinstance(val, Layer)
             and val.overlay and val.control])
        self.layers_untoggle = [
            val.get_name() for val in
            self._parent._children.values() if isinstance(val, Layer)
            and val.overlay and val.control and not val.show]
        for additional_base_layer in list(self.base_layers.values())[1:]:
            self.layers_untoggle.append(additional_base_layer)
        super(LayerControl, self).render()
Example #57
0
    def make_template_stream(self):
        template_obj = self.env.get_template("overview.templ")

        ads = serialize.AnalysisDeserializer(
            self.database_connection._original_connection,
            analysis_id=self.analysis_id)

        self.glycan_chromatograms = gcs = ads.load_glycan_composition_chromatograms(
        )
        # und = ads.load_unidentified_chromatograms()
        self.unidentified_chromatograms = und = ChromatogramFilter(
            ads.query(serialize.UnidentifiedChromatogram).filter(
                serialize.UnidentifiedChromatogram.analysis_id ==
                self.analysis_id).all())

        if len(gcs) == 0:
            self.log(
                "No glycan compositions were identified. Skipping report building"
            )
            templ = Template('''
                <html>
                <style>
                body {
                    font-family: sans-serif;
                }
                </style>
                <body>
                    <h3>No glycan compositions were identified</h3>
                </body>
                </html>
                ''')
            return templ.stream()

        summary_plot = summaries.GlycanChromatographySummaryGraphBuilder(
            filter(lambda x: x.score > self.threshold, gcs + und))
        lcms_plot, composition_abundance_plot = summary_plot.draw(min_score=5)

        try:
            lcms_plot.ax.legend_.set_visible(False)
        except AttributeError:
            # The legend may not have been created
            pass
        lcms_plot.ax.set_title("Glycan Composition\nLC-MS Aggregated EICs",
                               fontsize=24)

        fig = lcms_plot.ax.figure
        fig.set_figwidth(fig.get_figwidth() * 2.)
        fig.set_figheight(fig.get_figheight() * 2.)

        composition_abundance_plot.ax.set_title(
            "Glycan Composition\nTotal Abundances", fontsize=24)
        composition_abundance_plot.ax.set_xlabel(
            composition_abundance_plot.ax.get_xlabel(), fontsize=14)

        def resolve_key(key):
            match = gcs.find_key(key)
            if match is None:
                match = und.find_key(key)
            return match

        template_stream = (template_obj.stream(
            analysis=ads.analysis,
            lcms_plot=svguri_plot(lcms_plot.ax,
                                  bbox_inches='tight',
                                  patchless=True,
                                  svg_width="100%"),
            composition_abundance_plot=svguri_plot(
                composition_abundance_plot.ax,
                bbox_inches='tight',
                patchless=True,
                svg_width="100%"),
            glycan_chromatograms=gcs,
            unidentified_chromatograms=und,
            resolve_key=resolve_key))
        return template_stream
Example #58
0
def _render_with_context(
    template_str: str, template: jinja2.Template, **kwargs: Any
) -> str:
    """Store template being rendered in a ContextVar to aid error handling."""
    template_cv.set(template_str)
    return template.render(**kwargs)
Example #59
0
def intplotter(data, isodata, nodata, y, hetclassintdf):
    linewidth = 1.5
    source = ColumnDataSource(data)
    s2 = ColumnDataSource(data=dict(mz=data["mz"],
                                    Error=data["Error"],
                                    RA=data["RA"],
                                    Formula=data["Formula"],
                                    HeteroClass=data["HeteroClass"]))
    isosource = ColumnDataSource(isodata)
    nosource = ColumnDataSource(nodata)
    url = "http://www.chemspider.com/Search.aspx?q=@Formula"
    TOOLS = "crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave,box_select,poly_select,lasso_select,hover"

    figdims = (900, 500)  #pixel dimensions for the normal figures
    msxlim = [200, 700]  #x limits in m/z for the mass spectra

    vkxlim = [0, 1]
    vkylim = [0, 2]
    p1 = figure(tools=TOOLS,
                title=y[:-9] + " - Van Krevelen",
                width=figdims[0],
                height=figdims[1],
                x_axis_label='O/C',
                y_axis_label='H/C',
                webgl=True,
                x_range=vkxlim,
                y_range=vkylim)
    color_mapper = LinearColorMapper(palette=glocmap,
                                     low=msxlim[0],
                                     high=msxlim[1])
    p1.scatter(x='OC',
               y='HC',
               source=source,
               size='VKsize',
               fill_color={
                   'field': 'mz',
                   'transform': color_mapper
               },
               fill_alpha=0.75,
               line_color=None)  #use size not radius.
    hover = p1.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('Formula', "@Formula"),
                                  ('Mass', "@mz{1.11111}"),
                                  ('Error (ppm)', "@Error{1.11}")])
    taptool = p1.select(type=TapTool)
    taptool.callback = OpenURL(url=url)

    color_bar = ColorBar(color_mapper=color_mapper,
                         title="m/z",
                         border_line_color=None,
                         location=(0, 0),
                         scale_alpha=0.7)
    #orientation='horizontal',location='top_left', scale_alpha=0.7)#,ticker=FixedTicker(ticks=[2,6,10,14,18]))
    p1.add_layout(color_bar, "right")

    dbexlim = [0, 45]
    dbeylim = [0, 40]
    cmax = max(data["Ono"])
    cmax = int(5 * round(float(cmax) / 5))
    p2 = figure(tools=TOOLS,
                title=y[:-9] + " - DBE vs C# Plot",
                width=figdims[0],
                height=figdims[1],
                x_axis_label='C#',
                y_axis_label='DBE',
                webgl=True,
                x_range=dbexlim,
                y_range=dbeylim)
    color_mapper2 = LinearColorMapper(palette=glocmap2, low=0, high=cmax)
    p2.scatter(x='Cno',
               y='DBE',
               source=source,
               size='VKsize',
               fill_color={
                   'field': 'Ono',
                   'transform': color_mapper2
               },
               fill_alpha=0.75,
               line_color=None)
    hover = p2.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('Formula', "@Formula"),
                                  ('Mass', "@mz{1.11111}"),
                                  ('Error (ppm)', "@Error{1.11}")])
    taptool = p2.select(type=TapTool)
    taptool.callback = OpenURL(url=url)

    color_bar2 = ColorBar(
        color_mapper=color_mapper2,
        title="O#",
        border_line_color=None,
        location=(0, 0),
        scale_alpha=0.7,
        ticker=FixedTicker(
            ticks=[0, int(cmax / 4),
                   int(cmax / 2),
                   int(3 * cmax / 4), cmax]))
    p2.add_layout(color_bar2, "right")

    aixlim = [0, 45]
    aiylim = [0, 1]

    p3 = figure(tools=TOOLS,
                title=y[:-9] + " - AI(mod) vs C# Plot",
                width=figdims[0],
                height=figdims[1],
                x_axis_label='C#',
                y_axis_label='AI(mod)',
                webgl=True,
                x_range=aixlim,
                y_range=aiylim)
    color_mapper3 = LinearColorMapper(palette=glocmap2, low=0, high=cmax)
    p3.scatter(x='Cno',
               y='AImod',
               source=source,
               size='VKsize',
               fill_color={
                   'field': 'Ono',
                   'transform': color_mapper3
               },
               fill_alpha=0.75,
               line_color=None)
    hover = p3.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('Formula', "@Formula"),
                                  ('Mass', "@mz{1.11111}"),
                                  ('Error (ppm)', "@Error{1.11}")])
    taptool = p3.select(type=TapTool)
    taptool.callback = OpenURL(url=url)
    color_bar3 = ColorBar(
        color_mapper=color_mapper3,
        title="O#",
        border_line_color=None,
        location=(0, 0),
        scale_alpha=0.7,
        ticker=FixedTicker(
            ticks=[0, int(cmax / 4),
                   int(cmax / 2),
                   int(3 * cmax / 4), cmax]))
    #orientation='horizontal',location='top_left', scale_alpha=0.7)#,ticker=FixedTicker(ticks=[2,6,10,14,18]))
    p3.add_layout(color_bar3, "right")

    p4 = figure(tools=TOOLS,
                title=y[:-9] + " - Assigned Centroid MS",
                width=figdims[0],
                height=figdims[1],
                x_axis_label='m/z',
                y_axis_label='Abundance',
                y_range=[min(data["RA"]), max(data["RA"])],
                x_range=msxlim)
    p4.segment(x0=0, x1=800, y0=0, y1=0, line_width=1, line_color="black")
    p4.segment(x0='mz',
               y0=0,
               x1='mz',
               y1='RA',
               source=source,
               line_width=linewidth,
               line_color="black")
    p4.scatter(x='mz',
               y='RA',
               source=source,
               fill_color='black',
               line_color=None)
    hover = p4.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('Formula', "@Formula"),
                                  ('Mass', "@mz{1.11111}"),
                                  ('Error (ppm)', "@Error{1.11}")])
    taptool = p4.select(type=TapTool)
    taptool.callback = OpenURL(url=url)
    p4.yaxis[0].formatter = PrintfTickFormatter(format="%4.1e")
    """
    #this is me trying to plot a barplot of heteroatomic class distributions...
    
    p7 = figure(tools=TOOLS, title=y[:-9]+"",width=800, height=600, x_axis_label='HeteroClass',y_axis_label='Count',webgl=True)
    p7.quad(left="HetClassInts",y=hetclassdf[0],source=source,width=5,height=)
    
    t7 = layouts.Column(hist)
    tab7 = Panel(child=t7,title="test")
    """
    stretch = msxlim[0] * 0.1
    p5 = figure(tools=TOOLS,
                title=y[:-9] + " - Assigned Centroid MS",
                width=1400,
                height=600,
                x_axis_label='m/z',
                y_axis_label='Abundance',
                y_range=[min(data["RA"]), max(data["RA"])],
                x_range=(msxlim[0] - stretch, msxlim[1] + stretch))
    p5.segment(x0=0, x1=800, y0=0, y1=0, line_width=1, line_color="black")
    no1 = p5.segment(x0='mz',
                     y0=0,
                     x1='mz',
                     y1='RA',
                     source=nosource,
                     line_width=linewidth,
                     line_color="red")
    no2 = p5.scatter(x='mz',
                     y='RA',
                     source=nosource,
                     fill_color='red',
                     line_color=None,
                     legend="Unassigned Peaks")
    p5.scatter(x='mz',
               y='RA',
               source=source,
               fill_color='black',
               line_color=None,
               legend="Assigned Peaks")
    p5.segment(x0='mz',
               y0=0,
               x1='mz',
               y1='RA',
               source=source,
               line_width=linewidth,
               line_color="black")
    iso1 = p5.segment(x0='mz',
                      y0=0,
                      x1='mz',
                      y1='RA',
                      source=isosource,
                      line_width=linewidth,
                      line_color="green")
    iso2 = p5.scatter(x='mz',
                      y='RA',
                      source=isosource,
                      fill_color='green',
                      line_color=None,
                      legend="Isotologue Peaks")

    hover = p5.select(dict(type=HoverTool))
    hover.tooltips = OrderedDict([('Formula', "@Formula"),
                                  ('Mass', "@mz{1.11111}"),
                                  ('Error (ppm)', "@Error{1.11}")])
    taptool = p5.select(type=TapTool)
    taptool.callback = OpenURL(url=url)
    p5.yaxis[0].formatter = PrintfTickFormatter(format="%4.1e")

    js_code1 = "iso1.glyph.visible = false; iso2.glyph.visible = false; no1.glyph.visible = false; no2.glyph.visible = false;"
    cb1 = CustomJS(code=js_code1,
                   args=dict(iso1=iso1, iso2=iso2, no1=no1, no2=no2))
    js_code2 = "iso1.glyph.visible = true; iso2.glyph.visible = true; no1.glyph.visible = true; no2.glyph.visible = true;"
    cb2 = CustomJS(code=js_code2,
                   args=dict(iso1=iso1, iso2=iso2, no1=no1, no2=no2))

    toggleOn = Button(label="Hide", button_type="success", callback=cb1)
    toggleOff = Button(label="Show", button_type="success", callback=cb2)

    top = layouts.Row(toggleOn, toggleOff)
    t3 = layouts.Column(top, p5)
    tab3 = Panel(child=t3, title="Centroid MS with Isotopomers and No Hits")

    downloadbutton = Button(label="Download", button_type="success")
    downloadbutton.callback = CustomJS(args=dict(s2=s2),
                                       code="""
		var data = s2.get('data');
		var filetext = 'mz,Error,RA,Formula,HeteroClass\\n';
		for (i=0; i < data['mz'].length; i++) {
		var currRow = [data['mz'][i].toString(),
                             data['Error'][i].toString(),
        				data['RA'][i].toString(),
        				data['Formula'][i].toString(),
        				data['HeteroClass'][i].toString().concat('\\n')];
		var joined = currRow.join();
		filetext = filetext.concat(joined);
		}
		var filename = 'data_result.csv';
		var blob = new Blob([filetext], { type: 'text/csv;charset=utf-8;' });

		//addresses IE
		if (navigator.msSaveBlob) {
			navigator.msSaveBlob(blob, filename);
		}

		else {
			var link = document.createElement("a");
			link = document.createElement('a');
			link.href = URL.createObjectURL(blob);
			link.download = filename;
			link.target = "_blank";
			link.style.visibility = 'hidden';
			link.dispatchEvent(new MouseEvent('click'))
		}
	""")

    columns = [
        TableColumn(field="mz",
                    title="m/z",
                    formatter=NumberFormatter(format="0.00000")),
        TableColumn(field="Error",
                    title="Error (ppm)",
                    formatter=NumberFormatter(format="0.00")),
        TableColumn(field="RA", title="Abundance"),
        TableColumn(field="Formula", title="Formula"),
        TableColumn(field="HeteroClass", title="Heteroatomic Class")
    ]
    data_table = DataTable(source=s2,
                           columns=columns,
                           width=1400,
                           row_headers=False,
                           fit_columns=True)
    t4 = layouts.Column(data_table, downloadbutton)
    tab4 = Panel(child=t4, title="Selected Data Table")

    source.callback = CustomJS(args=dict(s2=s2, dt=data_table),
                               code="""
        var inds = cb_obj.get('selected')['1d'].indices;
        var d1 = cb_obj.get('data');
        var d2 = s2.get('data');
        if (inds.length == 0) {
            d2['mz'] = d1['mz']
            d2['Error'] = d1['Error']
            d2['RA'] = d1['RA']
            d2['Formula'] = d1['Formula']
            d2['HeteroClass'] = d1['HeteroClass']
        }
        else if (inds.length != 0) {
            d2['mz'] = []
            d2['Error'] = []
            d2['RA'] = []
            d2['Formula'] = []
            d2['HeteroClass'] = []
            for (i = 0; i < inds.length; i++) {
                d2['mz'].push(d1['mz'][inds[i]])
                d2['Error'].push(d1['Error'][inds[i]])
                d2['RA'].push(d1['RA'][inds[i]])
                d2['Formula'].push(d1['Formula'][inds[i]])
                d2['HeteroClass'].push(d1['HeteroClass'][inds[i]])
            }
        }
        s2.trigger('change');
        dt.trigger('change');
    """)
    """
    hetclasslist = hetclassintdf["HetClass"].tolist()
    hetclasslistnew = []
    for x in hetclasslist:
        hetclasslistnew.append([x,x])
    dropdown = Dropdown(label="Dropdown button", button_type="warning", menu=hetclasslistnew)
    """

    t1 = layouts.Row(p1, p4)
    t2 = layouts.Row(p2, p3)
    t12 = layouts.Column(t1, t2)
    tab1 = Panel(child=t12, title="Main")
    tabs = Tabs(tabs=[tab1, tab3, tab4])

    for figs in [p1, p2, p3, p4, p5]:
        figs.xaxis.axis_label_text_font_size = "14pt"
        figs.xaxis.major_label_text_font_size = "14pt"
        figs.yaxis.axis_label_text_font_size = "14pt"
        figs.yaxis.major_label_text_font_size = "14pt"
        figs.title.text_font_size = "14pt"
        figs.toolbar_location = "above"

    with open(outputpath + 'templates/index.html', 'r') as f:
        template = Template(f.read())

    #js_resources = JSResources(mode='inline')
    html = file_html(tabs, (CDN, CDN),
                     "Interactive Van Krevelen Diagrams",
                     template=template)
    output_file2 = outputpath + y[:-9] + '-plot.html'
    with open(output_file2, 'w') as f:
        f.write(html)
    view(output_file2)
class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
    name = "cmake_find_package_multi"

    config_template = textwrap.dedent("""
        {macros_and_functions}

        # Requires CMake > 3.0
        if(${{CMAKE_VERSION}} VERSION_LESS "3.0")
            message(FATAL_ERROR "The 'cmake_find_package_multi' generator only works with CMake > 3.0")
        endif()

        include(${{CMAKE_CURRENT_LIST_DIR}}/{filename}Targets.cmake)

        {target_props_block}
        {build_modules_block}
        {find_dependencies_block}
        """)

    targets_template = textwrap.dedent("""
        if(NOT TARGET {name}::{name})
            add_library({name}::{name} INTERFACE IMPORTED)
        endif()

        # Load the debug and release library finders
        get_filename_component(_DIR "${{CMAKE_CURRENT_LIST_FILE}}" PATH)
        file(GLOB CONFIG_FILES "${{_DIR}}/{filename}Target-*.cmake")

        foreach(f ${{CONFIG_FILES}})
            include(${{f}})
        endforeach()
        """)

    # This template takes the "name" of the target name::name and configs = ["Release", "Debug"..]
    target_properties = Template("""
# Assign target properties
set_property(TARGET {{name}}::{{name}}
             PROPERTY INTERFACE_LINK_LIBRARIES
             {%- for config in configs %}
             $<$<CONFIG:{{config}}>:${{'{'}}{{name}}_LIBRARIES_TARGETS_{{config.upper()}}}
                                    ${{'{'}}{{name}}_LINKER_FLAGS_{{config.upper()}}_LIST}>
             {%- endfor %})
set_property(TARGET {{name}}::{{name}}
             PROPERTY INTERFACE_INCLUDE_DIRECTORIES
             {%- for config in configs %}
             $<$<CONFIG:{{config}}>:${{'{'}}{{name}}_INCLUDE_DIRS_{{config.upper()}}}>
             {%- endfor %})
set_property(TARGET {{name}}::{{name}}
             PROPERTY INTERFACE_COMPILE_DEFINITIONS
             {%- for config in configs %}
             $<$<CONFIG:{{config}}>:${{'{'}}{{name}}_COMPILE_DEFINITIONS_{{config.upper()}}}>
             {%- endfor %})
set_property(TARGET {{name}}::{{name}}
             PROPERTY INTERFACE_COMPILE_OPTIONS
             {%- for config in configs %}
             $<$<CONFIG:{{config}}>:${{'{'}}{{name}}_COMPILE_OPTIONS_{{config.upper()}}_LIST}>
             {%- endfor %})
    """)

    build_modules = Template("""
# Build modules
{%- for config in configs %}
foreach(_BUILD_MODULE_PATH {{ '${'+name+'_BUILD_MODULES_PATHS_'+config.upper()+'}' }})
    include(${_BUILD_MODULE_PATH})
endforeach()
{%- endfor %}
    """)

    # https://gitlab.kitware.com/cmake/cmake/blob/master/Modules/BasicConfigVersion-SameMajorVersion.cmake.in
    config_version_template = textwrap.dedent("""
        set(PACKAGE_VERSION "{version}")

        if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
            set(PACKAGE_VERSION_COMPATIBLE FALSE)
        else()
            if("{version}" MATCHES "^([0-9]+)\\\\.")
                set(CVF_VERSION_MAJOR "${{CMAKE_MATCH_1}}")
            else()
                set(CVF_VERSION_MAJOR "{version}")
            endif()

            if(PACKAGE_FIND_VERSION_MAJOR STREQUAL CVF_VERSION_MAJOR)
                set(PACKAGE_VERSION_COMPATIBLE TRUE)
            else()
                set(PACKAGE_VERSION_COMPATIBLE FALSE)
            endif()

            if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
                set(PACKAGE_VERSION_EXACT TRUE)
            endif()
        endif()
        """)

    components_target_build_type_tpl = Template(textwrap.dedent("""\
        ########## MACROS ###########################################################################
        #############################################################################################
        {{ conan_message }}
        {{ conan_find_apple_frameworks }}
        {{ conan_package_library_targets }}

        ########### VARIABLES #######################################################################
        #############################################################################################

        {{ global_target_variables }}
        set({{ pkg_name }}_COMPONENTS_{{ build_type }} {{ pkg_components }})

        {%- for comp_name, comp in components %}

        ########### COMPONENT {{ comp_name }} VARIABLES #############################################

        set({{ pkg_name }}_{{ comp_name }}_INCLUDE_DIRS_{{ build_type }} {{ comp.include_paths }})
        set({{ pkg_name }}_{{ comp_name }}_INCLUDE_DIR_{{ build_type }} {{ comp.include_path }})
        set({{ pkg_name }}_{{ comp_name }}_INCLUDES_{{ build_type }} {{ comp.include_paths }})
        set({{ pkg_name }}_{{ comp_name }}_LIB_DIRS_{{ build_type }} {{ comp.lib_paths }})
        set({{ pkg_name }}_{{ comp_name }}_RES_DIRS_{{ build_type }} {{ comp.res_paths }})
        set({{ pkg_name }}_{{ comp_name }}_DEFINITIONS_{{ build_type }} {{ comp.defines }})
        set({{ pkg_name }}_{{ comp_name }}_COMPILE_DEFINITIONS_{{ build_type }} {{ comp.compile_definitions }})
        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_C_{{ build_type }} "{{ comp.cflags_list }}")
        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_CXX_{{ build_type }} "{{ comp.cxxflags_list }}")
        set({{ pkg_name }}_{{ comp_name }}_LIBS_{{ build_type }} {{ comp.libs }})
        set({{ pkg_name }}_{{ comp_name }}_SYSTEM_LIBS_{{ build_type }} {{ comp.system_libs }})
        set({{ pkg_name }}_{{ comp_name }}_FRAMEWORK_DIRS_{{ build_type }} {{ comp.framework_paths }})
        set({{ pkg_name }}_{{ comp_name }}_FRAMEWORKS_{{ build_type }} {{ comp.frameworks }})
        set({{ pkg_name }}_{{ comp_name }}_BUILD_MODULES_PATHS_{{ build_type }} {{ comp.build_modules_paths }})
        set({{ pkg_name }}_{{ comp_name }}_DEPENDENCIES_{{ build_type }} {{ comp.public_deps }})
        set({{ pkg_name }}_{{ comp_name }}_LINKER_FLAGS_LIST_{{ build_type }}
                $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:{{ comp.sharedlinkflags_list }}>
                $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:{{ comp.sharedlinkflags_list }}>
                $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:{{ comp.exelinkflags_list }}>
        )

        ########## COMPONENT {{ comp_name }} FIND LIBRARIES & FRAMEWORKS / DYNAMIC VARS #############

        set({{ pkg_name }}_{{ comp_name }}_FRAMEWORKS_FOUND_{{ build_type }} "")
        conan_find_apple_frameworks({{ pkg_name }}_{{ comp_name }}_FRAMEWORKS_FOUND_{{ build_type }} "{{ '${'+pkg_name+'_'+comp_name+'_FRAMEWORKS_'+build_type+'}' }}" "{{ '${'+pkg_name+'_'+comp_name+'_FRAMEWORK_DIRS_'+build_type+'}' }}")

        set({{ pkg_name }}_{{ comp_name }}_LIB_TARGETS_{{ build_type }} "")
        set({{ pkg_name }}_{{ comp_name }}_NOT_USED_{{ build_type }} "")
        set({{ pkg_name }}_{{ comp_name }}_LIBS_FRAMEWORKS_DEPS_{{ build_type }} {{ '${'+pkg_name+'_'+comp_name+'_FRAMEWORKS_FOUND_'+build_type+'}' }} {{ '${'+pkg_name+'_'+comp_name+'_SYSTEM_LIBS_'+build_type+'}' }} {{ '${'+pkg_name+'_'+comp_name+'_DEPENDENCIES_'+build_type+'}' }})
        conan_package_library_targets("{{ '${'+pkg_name+'_'+comp_name+'_LIBS_'+build_type+'}' }}"
                                      "{{ '${'+pkg_name+'_'+comp_name+'_LIB_DIRS_'+build_type+'}' }}"
                                      "{{ '${'+pkg_name+'_'+comp_name+'_LIBS_FRAMEWORKS_DEPS_'+build_type+'}' }}"
                                      {{ pkg_name }}_{{ comp_name }}_NOT_USED_{{ build_type }}
                                      {{ pkg_name }}_{{ comp_name }}_LIB_TARGETS_{{ build_type }}
                                      "{{ build_type }}"
                                      "{{ pkg_name }}_{{ comp_name }}")

        set({{ pkg_name }}_{{ comp_name }}_LINK_LIBS_{{ build_type }} {{ '${'+pkg_name+'_'+comp_name+'_LIB_TARGETS_'+build_type+'}' }} {{ '${'+pkg_name+'_'+comp_name+'_LIBS_FRAMEWORKS_DEPS_'+build_type+'}' }})

        {%- endfor %}
        """))

    components_targets_tpl = Template(textwrap.dedent("""\
        {%- for comp_name, comp in components %}

        if(NOT TARGET {{ pkg_name }}::{{ comp_name }})
            add_library({{ pkg_name }}::{{ comp_name }} INTERFACE IMPORTED)
        endif()

        {%- endfor %}

        if(NOT TARGET {{ pkg_name }}::{{ pkg_name }})
            add_library({{ pkg_name }}::{{ pkg_name }} INTERFACE IMPORTED)
        endif()

        # Load the debug and release library finders
        get_filename_component(_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
        file(GLOB CONFIG_FILES "${_DIR}/{{ pkg_filename }}Target-*.cmake")

        foreach(f ${CONFIG_FILES})
            include(${f})
        endforeach()

        if({{ pkg_name }}_FIND_COMPONENTS)
            foreach(_FIND_COMPONENT {{ '${'+pkg_name+'_FIND_COMPONENTS}' }})
                list(FIND {{ pkg_name }}_COMPONENTS_{{ build_type }} "{{ pkg_name }}::${_FIND_COMPONENT}" _index)
                if(${_index} EQUAL -1)
                    conan_message(FATAL_ERROR "Conan: Component '${_FIND_COMPONENT}' NOT found in package '{{ pkg_name }}'")
                else()
                    conan_message(STATUS "Conan: Component '${_FIND_COMPONENT}' found in package '{{ pkg_name }}'")
                endif()
            endforeach()
        endif()
        """))

    components_config_tpl = Template(textwrap.dedent("""\
        ########## MACROS ###########################################################################
        #############################################################################################
        {{ conan_message }}

        # Requires CMake > 3.0
        if(${CMAKE_VERSION} VERSION_LESS "3.0")
            message(FATAL_ERROR "The 'cmake_find_package_multi' generator only works with CMake > 3.0")
        endif()

        include(${CMAKE_CURRENT_LIST_DIR}/{{ pkg_filename }}Targets.cmake)

        ########## FIND PACKAGE DEPENDENCY ##########################################################
        #############################################################################################

        include(CMakeFindDependencyMacro)

        {%- for public_dep in pkg_public_deps %}

        if(NOT {{ public_dep }}_FOUND)
            if(${CMAKE_VERSION} VERSION_LESS "3.9.0")
                find_package({{ public_dep }} REQUIRED NO_MODULE)
            else()
                find_dependency({{ public_dep }} REQUIRED NO_MODULE)
            endif()
        else()
            message(STATUS "Dependency {{ public_dep }} already found")
        endif()

        {%- endfor %}

        ########## TARGETS PROPERTIES ###############################################################
        #############################################################################################
        {%- macro tvalue(pkg_name, comp_name, var, config) -%}
        {{'${'+pkg_name+'_'+comp_name+'_'+var+'_'+config.upper()+'}'}}
        {%- endmacro -%}

        {%- for comp_name, comp in components %}

        ########## COMPONENT {{ comp_name }} TARGET PROPERTIES ######################################

        set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_LINK_LIBRARIES
                     {%- for config in configs %}
                     $<$<CONFIG:{{config}}>:{{tvalue(pkg_name, comp_name, 'LINK_LIBS', config)}}
                        {{tvalue(pkg_name, comp_name, 'LINKER_FLAGS_LIST', config)}}>
                     {%- endfor %})
        set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_INCLUDE_DIRECTORIES
                     {%- for config in configs %}
                     $<$<CONFIG:{{config}}>:{{tvalue(pkg_name, comp_name, 'INCLUDE_DIRS', config)}}>
                     {%- endfor %})
        set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_DEFINITIONS
                     {%- for config in configs %}
                     $<$<CONFIG:{{config}}>:{{tvalue(pkg_name, comp_name, 'COMPILE_DEFINITIONS', config)}}>
                     {%- endfor %})
        set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_OPTIONS
                     {%- for config in configs %}
                     $<$<CONFIG:{{config}}>:
                         {{tvalue(pkg_name, comp_name, 'COMPILE_OPTIONS_C', config)}}
                         {{tvalue(pkg_name, comp_name, 'COMPILE_OPTIONS_CXX', config)}}>
                     {%- endfor %})
        set({{ pkg_name }}_{{ comp_name }}_TARGET_PROPERTIES TRUE)

        {%- endfor %}

        ########## GLOBAL TARGET PROPERTIES #########################################################

        if(NOT {{ pkg_name }}_{{ pkg_name }}_TARGET_PROPERTIES)
            set_property(TARGET {{ pkg_name }}::{{ pkg_name }} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
                         {%- for config in configs %}
                         $<$<CONFIG:{{config}}>:{{ '${'+pkg_name+'_COMPONENTS_'+config.upper()+'}'}}>
                         {%- endfor %})
        endif()

        ########## BUILD MODULES ####################################################################
        #############################################################################################

        {%- for comp_name, comp in components %}

        ########## COMPONENT {{ comp_name }} BUILD MODULES ##########################################

        {%- for config in configs %}

        foreach(_BUILD_MODULE_PATH {{ '${'+pkg_name+'_'+comp_name+'_BUILD_MODULES_PATHS_'+config.upper()+'}' }})
            include(${_BUILD_MODULE_PATH})
        endforeach()
        {%- endfor %}

        {%- endfor %}
        """))

    def __init__(self, conanfile):
        super(CMakeFindPackageMultiGenerator, self).__init__(conanfile)
        self.configuration = str(self.conanfile.settings.build_type)
        self.configurations = [v for v in conanfile.settings.build_type.values_range if v != "None"]
        # FIXME: Ugly way to define the output path
        self.output_path = os.getcwd()

    def generate(self):
        generator_files = self.content
        for generator_file, content in generator_files.items():
            generator_file = os.path.join(self.output_path, generator_file)
            save(generator_file, content)

    @property
    def filename(self):
        return None

    @property
    def content(self):
        ret = {}
        build_type = str(self.conanfile.settings.build_type).upper()
        build_type_suffix = "_{}".format(self.configuration.upper()) if self.configuration else ""
        for pkg_name, cpp_info in self.deps_build_info.dependencies:
            self._validate_components(cpp_info)
            pkg_filename = self._get_filename(cpp_info)
            pkg_findname = self._get_name(cpp_info)
            pkg_version = cpp_info.version

            public_deps = self.get_public_deps(cpp_info)
            deps_names = []
            for it in public_deps:
                name = "{}::{}".format(*self._get_require_name(*it))
                if name not in deps_names:
                    deps_names.append(name)
            deps_names = ';'.join(deps_names)
            pkg_public_deps_filenames = [self._get_filename(self.deps_build_info[it[0]]) for it in
                                         public_deps]
            config_version = self.config_version_template.format(version=pkg_version)
            ret[self._config_version_filename(pkg_filename)] = config_version
            if not cpp_info.components:
                ret[self._config_filename(pkg_filename)] = self._config(
                    filename=pkg_filename,
                    name=pkg_findname,
                    version=cpp_info.version,
                    public_deps_names=pkg_public_deps_filenames
                )
                ret["{}Targets.cmake".format(pkg_filename)] = self.targets_template.format(
                    filename=pkg_filename, name=pkg_findname)

                # If any config matches the build_type one, add it to the cpp_info
                dep_cpp_info = extend(cpp_info, build_type.lower())
                deps = DepsCppCmake(dep_cpp_info, self.name)
                find_lib = target_template.format(name=pkg_findname, deps=deps,
                                                  build_type_suffix=build_type_suffix,
                                                  deps_names=deps_names)
                ret["{}Target-{}.cmake".format(pkg_filename, self.configuration.lower())] = find_lib
            else:
                cpp_info = extend(cpp_info, build_type.lower())
                pkg_info = DepsCppCmake(cpp_info, self.name)
                components = self._get_components(pkg_name, cpp_info)
                # Note these are in reversed order, from more dependent to less dependent
                pkg_components = " ".join(["{p}::{c}".format(p=pkg_findname, c=comp_findname) for
                                           comp_findname, _ in reversed(components)])
                global_target_variables = target_template.format(name=pkg_findname, deps=pkg_info,
                                                                 build_type_suffix=build_type_suffix,
                                                                 deps_names=deps_names)
                variables = self.components_target_build_type_tpl.render(
                    pkg_name=pkg_findname,
                    global_target_variables=global_target_variables,
                    pkg_components=pkg_components,
                    build_type=build_type,
                    components=components,
                    conan_find_apple_frameworks=CMakeFindPackageCommonMacros.apple_frameworks_macro,
                    conan_package_library_targets=CMakeFindPackageCommonMacros.conan_package_library_targets
                )
                ret["{}Target-{}.cmake".format(pkg_filename, build_type.lower())] = variables
                targets = self.components_targets_tpl.render(
                    pkg_name=pkg_findname,
                    pkg_filename=pkg_filename,
                    components=components,
                    build_type=build_type
                )
                ret["{}Targets.cmake".format(pkg_filename)] = targets
                target_config = self.components_config_tpl.render(
                    pkg_name=pkg_findname,
                    pkg_filename=pkg_filename,
                    components=components,
                    pkg_public_deps=pkg_public_deps_filenames,
                    conan_message=CMakeFindPackageCommonMacros.conan_message,
                    configs=self.configurations
                )
                ret[self._config_filename(pkg_filename)] = target_config
        return ret

    def _config_filename(self, pkg_filename):
        if pkg_filename == pkg_filename.lower():
            return "{}-config.cmake".format(pkg_filename)
        else:
            return "{}Config.cmake".format(pkg_filename)

    def _config_version_filename(self, pkg_filename):
        if pkg_filename == pkg_filename.lower():
            return "{}-config-version.cmake".format(pkg_filename)
        else:
            return "{}ConfigVersion.cmake".format(pkg_filename)

    def _config(self, filename, name, version, public_deps_names):
        # Builds the XXXConfig.cmake file for one package

        # The common macros
        macros_and_functions = "\n".join([
            CMakeFindPackageCommonMacros.conan_message,
            CMakeFindPackageCommonMacros.apple_frameworks_macro,
            CMakeFindPackageCommonMacros.conan_package_library_targets,
        ])

        # Define the targets properties
        targets_props = self.target_properties.render(name=name, configs=self.configurations)
        # Add build modules
        build_modules_block = self.build_modules.render(name=name, configs=self.configurations)
        # The find_dependencies_block
        find_dependencies_block = ""
        if public_deps_names:
            # Here we are generating only Config files, so do not search for FindXXX modules
            find_dependencies_block = find_transitive_dependencies(public_deps_names,
                                                                   find_modules=False)

        tmp = self.config_template.format(name=name, version=version,
                                          filename=filename,
                                          target_props_block=targets_props,
                                          build_modules_block=build_modules_block,
                                          find_dependencies_block=find_dependencies_block,
                                          macros_and_functions=macros_and_functions)
        return tmp