Example #1
0
def minted(key, value, format, meta):
    ''' Use minted for code in LaTeX.

    Args:
        key     type of pandoc object
        value   contents of pandoc object
        format  target output format
        meta    document metadata
    '''
    if format != 'latex':
        return

    # Determine what kind of code object this is.
    if key == 'CodeBlock':
        template = Template(
            '\\begin{minted}[$attributes]{$language}\n$contents\n\end{minted}'
        )
        Element = RawBlock
    elif key == 'Code':
        template = Template('\\mintinline[$attributes]{$language}{$contents}')
        Element = RawInline
    else:
        return

    settings = unpack_metadata(meta)

    code = unpack_code(value, settings['language'])

    return [Element(format, template.substitute(code))]
Example #2
0
  def GenerateBridgeConstructor(self):
    if (self._bridge_params_declare != ''):
      template = Template("""\
    public ${NAME}(${PARAMS}, Object wrapper) {
        super(${PARAMS_PASSING});

        this.wrapper = wrapper;
        reflectionInit();
    }

    """)
      value = {'NAME': self._class_java_data.bridge_name,
               'PARAMS': self._bridge_params_declare,
               'PARAMS_PASSING': self._bridge_params_pass_to_super}
      return template.substitute(value)
    else:
      template = Template("""\
    public ${NAME}(Object wrapper) {
        super();

        this.wrapper = wrapper;
        reflectionInit();
    }

    """)
      value = {'NAME': self._class_java_data.bridge_name,
               'PARAMS': self._bridge_params_declare,
               'PARAMS_PASSING': self._bridge_params_pass_to_super}
      return template.substitute(value)
Example #3
0
def writeProperties(c):
    properties = properties_begin
    for m in c["properties"]:
        properties += property.safe_substitute(propertyname=m["name"], comment="")
    properties += methods_end
    properties = Template(properties)
    print(properties.substitute(classname=c["name"]))
Example #4
0
 def _create_report(self):
     context = aq_inner(self.context)
     date = datetime.datetime.now()
     token = django_random.get_random_string(length=24)
     item = api.content.create(
         type='xpose.seodash.report',
         id=token,
         title='Report {0} {1}'.format(date.strftime("%B"),
                                       date.strftime("%Y")),
         container=context,
         safe_id=True
     )
     uuid = api.content.get_uuid(obj=item)
     template_file = os.path.join(os.path.dirname(__file__),
                                  'report.json')
     template = Template(open(template_file).read())
     template_vars = {
         'id': uuid_tool.uuid4(),
         'uid': uuid,
         'timestamp': int(time.time()),
         'created': datetime.datetime.now(),
         'dashboard': api.content.get_uuid(obj=context),
     }
     report = template.substitute(template_vars)
     setattr(item, 'report', report)
     modified(item)
     item.reindexObject(idxs='modified')
     return uuid
Example #5
0
def pattern_to_file(input_l):
    # 入力List[test_so,test_si,pi,pi,po]をテストパターンのリストに置き換える
    # 入力List[{test_so='aaaa',..},{..}]
    output = []
    template = Template('   Ann {* fast_sequential *}\n' + \
                        '   "pattern ${num}": Call "load_unload" { \n' + \
                        '      "test_so"=${test_so}; ' + \
                        '"test_si"=${test_si}; }\n' + \
                        '   Call "${launch}" { \n' + \
                        '      "_pi"=${pi_1}; }\n' + \
                        '   Call "${capture}" { \n' + \
                        '      "_pi"=${pi_2}; "_po"=${po}; }\n')
    template_first = Template('   Ann {* fast_sequential *}\n' + \
                        '   "pattern ${num}": Call "load_unload" { \n' + \
                        '      "test_si"=${test_si}; }\n' + \
                        '   Call "${launch}" { \n' + \
                        '      "_pi"=${pi_1}; }\n' + \
                        '   Call "${capture}" { \n' + \
                        '      "_pi"=${pi_2}; "_po"=${po}; }\n')

    for i, l in enumerate(input_l):
        l['num'] = i
        if i == 0:
            line = template_first.substitute(l)
            output.append(line)
        else:
            #l['num'] = i + 1
            line = template.substitute(l)
            output.append(line)
    return(output)
Example #6
0
File: utils.py Project: fwkz/polon
def create_resource(name, content=(), python_package=False):
    """
    Creates resource directory in current working directory.
    :param name: Name of the root directory of the resource
    :param content: Content of the resource in form of dictionary ex. {"resource_name.py": template_path, "/test": None}
    :return:
    """
    root_path = os.path.join(os.getcwd(), name)
    mkdir_p(root_path)
    print("\nDirectory {path} sucessfully created.".format(path=root_path))

    if python_package:
        open(os.path.join(root_path, "__init__.py"), "a").close()
        print("    __init__.py successfully created.")

    for name, template_path, context in content:
        if os.path.splitext(name)[-1] == "":  # Checking if resource has extension if not it's directory
            os.mkdir(os.path.join(root_path, name))
            print("    Sub-directory /{name} successfully created.".format(name=name))
        else:
            try:
                with open(template_path, "rb") as template_file:
                    template = Template(template_file.read())
            except (IOError, TypeError):
                template = Template("")
            finally:
                with open(os.path.join(root_path, name), "wb") as target_file:
                    target_file.write(template.substitute(**context))
                    print("    {file} successfully created.".format(file=name))
Example #7
0
def writeMembers(c):
    members = members_begin
    for m in c["members"]:
        members += member.safe_substitute(membername=m["name"], membertype=m["type"], comment=m["comment"])
    members += members_end
    members = Template(members)
    print(members.substitute(classname=c["name"]))
Example #8
0
 def renderTemplate(self, file, **kwds):
   f = open(curdir + sep + file)
   template = Template(f.read())
   html = template.substitute(**kwds)
   self.genericHeader(200, 'text/html')
   self.wfile.write(html)
   f.close()
def renderTemplates(inAndOutFilePaths, options, relPathToSdk, absPathToSdk, renderTarget, jobsAndDescs=[]):
    for inFile, outFile in inAndOutFilePaths:
        console.log("Patching file '%s'" % outFile)

        #config = MyTemplate(open(inFile).read())
        config = Template(open(inFile).read())
        out = open(outFile, "w")

        context = {
          "Name": options.name,
          "Namespace": options.namespace,
          "NamespacePath" : (options.namespace).replace('.', '/'),
          "REL_QOOXDOO_PATH": relPathToSdk,
          "ABS_QOOXDOO_PATH": absPathToSdk,
          "QOOXDOO_VERSION": QOOXDOO_VERSION,
          "Cache" : options.cache,
        }

        if renderTarget == TARGET.GRUNT:
            context["JOBS_AND_DESCS"] = jobsAndDescs
            for k, v in context.iteritems():
                if isinstance(v, (str, unicode)):
                    context[k] = gruntifyMacros(v);

        out.write(config.substitute(context).encode('utf-8'))
        out.close()
        os.remove(inFile)
Example #10
0
def _getSystemChannelNameFormatter(service):
    peripheryID = connectionManager.peripheryID
    chanTemplate = Template(service['format'])
    if chanTemplate:
        return chanTemplate.safe_substitute(peripheryID=peripheryID, userString=service['userString'], hostname=service['hostname'], type=service['type'])
    else:
        return None
Example #11
0
    def filter(self, ):
        if (self.filters and len(self.filters) > 0):
            self.filter_params = self.stage_dir + 'filters.txt'
            try:
                with open(self.filter_params, 'w') as f:
                    f.write('--keep="{0}"\n'.format(' or '.join(self.filters)))
            except IOError as e:
                logger.error('Error saving filter params file', e)
                # can't filter so return the raw data
                shutil.copy(self.raw_osm, self.filtered_osm)
                return self.filtered_osm

            # convert to om5 for faster processing
            om5 = self._convert_om5()

            # filter om5 data
            filter_tmpl = Template(
                'osmfilter $om5 --parameter-file=$params --out-osm >$filtered_osm'
            )
            filter_cmd = filter_tmpl.safe_substitute({'om5': om5,
                                                      'params': self.filter_params,
                                                      'filtered_osm': self.filtered_osm})
            proc = subprocess.Popen(filter_cmd, shell=True, executable='/bin/bash',
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            (stdout, stderr) = proc.communicate()
            returncode = proc.wait()
            if (returncode != 0):
                logger.error('%s', stderr)
                raise Exception, "osmfilter process failed with returncode {0}".format(returncode)
            return self.filtered_osm

        else:
            logger.error('No filters found. Returning raw osm data.')
            shutil.copy(self.raw_osm, self.filtered_osm)
            return self.filtered_osm
Example #12
0
 def GenerateWrapperConstructor(self):
   # TODO(wang16): Currently, only support pre/post wrapper lines for
   # Constructors.
   template = Template(
       '${DOC}\n' +
       '    public ${CLASS_NAME}(${PARAMS}) {\n' +
       '${PRE_WRAP_LINES}\n' +
       '        bridge = ReflectionHelper.createInstance(' +
       '\"${CONSTRUCTOR_DECLARE_NAME}\"${PARAMS_PASSING}, this);\n' +
       '        try {\n' +
       '            reflectionInit();\n' +
       '        } catch(Exception e) {\n' +
       '            ReflectionHelper.handleException(e);\n' +
       '        }\n' +
       '${POST_WRAP_LINES}\n' +
       '    }\n\n')
   value = {'CLASS_NAME': self._class_java_data.wrapper_name,
            'DOC': self.GenerateDoc(self.method_doc),
            'PARAMS': self._wrapper_params_declare,
            'PARAMS_PASSING': self._wrapper_params_pass_to_bridge,
            'CONSTRUCTOR_DECLARE_NAME': self._method_declare_name,
            'PRE_WRAP_LINES': self._method_annotations.get(
                self.ANNOTATION_PRE_WRAPLINE, ''),
            'POST_WRAP_LINES': self._method_annotations.get(
                self.ANNOTATION_POST_WRAPLINE, '')}
   return template.substitute(value)
Example #13
0
 def GenerateWrapperStaticMethod(self):
   no_return_value = self._method_return == 'void'
   template = Template(
       '    public static ${RETURN_TYPE} ${NAME}(${PARAMS}) {\n' +
       '       Class<?> clazz = ReflectionHelper.loadClass(' +
       '\"${FULL_BRIDGE_NAME}\");\n' +
       '       Method method = ReflectionHelper.loadMethod(clazz, ' +
       '\"${NAME}\"${PARAMS_DECLARE_FOR_BRIDGE});\n' +
       '       ${RETURN}ReflectionHelper.invokeMethod(method, null' +
       '${PARAMS_PASSING});\n' +
       '    }\n\n')
   if no_return_value:
     return_state = ''
   else:
     return_state = 'return (%s)' % ConvertPrimitiveTypeToObject(
         self.method_return)
   value = {'RETURN_TYPE': self.method_return,
            'NAME': self.method_name,
            'FULL_BRIDGE_NAME': self._class_java_data.GetFullBridgeName(),
            'PARAMS_DECLARE_FOR_BRIDGE':
                self._wrapper_params_declare_for_bridge,
            'PARAMS_PASSING': self._wrapper_params_pass_to_bridge,
            'PARAMS': self._wrapper_params_declare,
            'RETURN': return_state}
   return template.substitute(value)
Example #14
0
def get_init_colors(*inks):
    process_color_value = '{0} {1} {2} {3}'
    init_process_colors_tpl = Template(\
    '/setcmykcolor where {pop\n  $value setcmykcolor\n  \
100 100 moveto 101 101 lineto stroke\n} if')
    
    custom_color_value = '{0} {1} {2} {3} ({name})'
    init_custom_colors_tpl = Template(\
    '/findcmykcustomcolor where {pop\n  $value\n  \
findcmykcustomcolor 1 setcustomcolor\n  100 100 moveto 101 101 \
lineto stroke\n} if')
    
    process_colors_init = []
    custom_colors_init  = []
    
    for ink in inks:
        
        if is_process_color(ink):
            value = process_color_value.format(*process_colors[ink.name])
            process_colors_init.append(
                init_process_colors_tpl.substitute({'value': value}))
        else:
            
            value = custom_color_value.format(*ink.cmyk, name=escape_string(ink.name))
            custom_colors_init.append(
                init_custom_colors_tpl.substitute({'value': value}))
    
    return init_colors.substitute({
        'initProcessColors': '\n'.join(process_colors_init),
        'initCustomColors' : '\n'.join(custom_colors_init)
    })
Example #15
0
  def GenerateBridgeWrapperMethod(self):
    no_return_value = self._method_return == 'void'
    return_is_internal = self.IsInternalClass(self._method_return)
    if return_is_internal:
      return_type_java_data = self.LoadJavaClass(self._method_return)

    template = Template(
        '    public ${RETURN_TYPE} ${NAME}(${PARAMS}) {\n' +
        '        ${RETURN}ReflectionHelper.invokeMethod(\n' +
        '            ${METHOD_DECLARE_NAME}, wrapper${PARAMS_PASSING});\n' +
        '    }\n\n')
    if no_return_value:
      return_statement = ''
    elif return_is_internal:
      return_statement = 'return (%s)' % return_type_java_data.bridge_name
    else:
      return_statement = 'return (%s)' % (
          ConvertPrimitiveTypeToObject(self.method_return))
    value = {'RETURN_TYPE': self.method_return,
             'NAME': self.method_name,
             'METHOD_DECLARE_NAME': self._method_declare_name,
             'PARAMS': self._bridge_params_declare,
             'RETURN': return_statement,
             'PARAMS_PASSING': self._bridge_params_pass_to_wrapper}
    return template.substitute(value)
Example #16
0
def comment(environ, start_response):
    if environ['REQUEST_METHOD'] == 'POST':
        try:
            request_body_size = int(environ['CONTENT_LENGTH'])
            request_body = environ['wsgi.input'].read(request_body_size).split('\n')
        except (TypeError, ValueError):
            request_body = []
        else:
            post_values = dict(item.split('=') for item in request_body)
            post_values['comment'] = urllib.unquote_plus(post_values['comment'])

            row = CONNECTION.cursor().execute('''
                INSERT INTO todo(comment) VALUES("%s");
            ''' % post_values['comment']
            )
        return index(environ, start_response, saved=True)
    else:
        try:
            with open('templates/comment.html') as template_file:
                template = Template(template_file.read())
        except IOError:
            return template_not_found(environ, start_response)

        start_response('200 OK', [('Content-Type', 'text/html')])
        return [template.substitute({})]
Example #17
0
  def GetMethodIDImpl(self, called_by_native):
    """Returns the implementation of GetMethodID."""
    template = Template("""\
  base::android::MethodID::LazyGet<
      base::android::MethodID::TYPE_${STATIC}>(
      env, ${JAVA_CLASS}_clazz(env),
      "${JNI_NAME}",
      ${JNI_SIGNATURE},
      &g_${JAVA_CLASS}_${METHOD_ID_VAR_NAME});
""")
    jni_name = called_by_native.name
    jni_return_type = called_by_native.return_type
    if called_by_native.is_constructor:
      jni_name = '<init>'
      jni_return_type = 'void'
    if called_by_native.signature:
      signature = called_by_native.signature
    else:
      signature = self.jni_params.Signature(called_by_native.params,
                                            jni_return_type, True)
    java_class = self.fully_qualified_class
    if called_by_native.java_class_name:
      java_class += '$' + called_by_native.java_class_name
    values = {
        'JAVA_CLASS': GetBinaryClassName(java_class),
        'JNI_NAME': jni_name,
        'METHOD_ID_VAR_NAME': called_by_native.method_id_var_name,
        'STATIC': 'STATIC' if called_by_native.static else 'INSTANCE',
        'JNI_SIGNATURE': signature,
    }
    return template.substitute(values)
Example #18
0
def getRubyClass(pyClassDict):
    rubyCode = Template("""  class $rubyClassName < MO
    @class_name = '$objectName'
    @ruby_class = '$rubyClassName'
    @prefix = '$prefix'
    @prefixes = [$prefixes]
    @rn_format = '$rnFormat'
    @containers = $containers
    @props = $props
    @child_classes = $children
    @label = '$label'
    @naming_props = $namingProps
    @read_only = $readOnly

    def rn
      $rn
    end
  end
""")
    vals = dict(rubyClassName=rubyClassName(pyClassDict['_name']),
                objectName=getClassName(pyClassDict),
                prefix=getRnPrefix(pyClassDict),
                prefixes=getRnPrefixes(pyClassDict),
                rnFormat=getRnFormat(pyClassDict),
                containers=getContainers(pyClassDict),
                props=getProps(pyClassDict),
                children=getChildren(pyClassDict),
                rn=getRnFunc(pyClassDict),
                label=getLabel(pyClassDict),
                namingProps=getNamingProps(pyClassDict),
                classNameShort=getClassName(pyClassDict).replace('.', ''),
                readOnly=getReadOnly(pyClassDict),
                )
    return rubyCode.substitute(vals)
def submit_emmy_experiment(nt, N, tgs, is_dp, procs, ts, kernel, outfile, target_dir):
    import os
    import subprocess
    from string import Template
    from utils import ensure_dir

    job_template = Template(
        """export OMP_NUM_THREADS=10; export I_MPI_PIN_DOMAIN=socket; export KMP_AFFINITY=granularity=fine,scatter; mpirun_rrze -np $procs -npernode 2 -- $exec_path --n-tests 2 --disable-source-point --npx 1 --npy $procs --npz 1 --nx $N --ny $N --nz $N  --verbose 1 --halo-concatenate 1 --target-ts $ts --wavefront 1 --nt $nt --target-kernel $kernel --thread-group-size $tgs | tee $outfile"""
    )

    target_dir = os.path.join(os.path.abspath("."), target_dir)
    ensure_dir(target_dir)
    outpath = os.path.join(target_dir, outfile)

    if is_dp == 1:
        exec_path = os.path.join(os.path.abspath("."), "build_dp/mwd_kernel")
    else:
        exec_path = os.path.join(os.path.abspath("."), "build/mwd_kernel")

    job_cmd = job_template.substitute(
        nt=nt,
        N=N,
        tgs=tgs,
        procs=procs,
        ts=ts,
        kernel=kernel,
        outfile=outpath,
        exec_path=exec_path,
        target_dir=target_dir,
    )

    print job_cmd
    sts = subprocess.call(job_cmd, shell=True)
Example #20
0
    def get_html(self):
        """Return the HTML content"""
        StaticFile = Pool().get('nereid.static.file')

        # XXX: Use read, as all fields are not required
        banner = self.read(
            [self], [
                'type', 'click_url', 'file', 'file.mimetype',
                'custom_code', 'height', 'width',
                'alternative_text', 'click_url'
            ]
        )[0]

        if banner['type'] == 'media':
            if not (banner['file.mimetype'] and
                    banner['file.mimetype'].startswith('image')):
                return "<!-- no html defined for mime type '%s' -->" % \
                    banner['file.mimetype']
            # replace the `file` in the dictionary with the complete url
            # that is required to render the image based on static file
            file = StaticFile(banner['file'])
            banner['file'] = file.url
            image = Template(
                u'<a href="$click_url">'
                u'<img src="$file" alt="$alternative_text"'
                u' width="$width" height="$height"/>'
                u'</a>'
            )
            return image.substitute(**banner)
        elif banner['type'] == 'custom_code':
            return banner['custom_code']
Example #21
0
    def do_pastebin_json(self, s):
        """Upload to pastebin via json interface."""

        url = urljoin(self.config.pastebin_url, '/json/new')
        payload = {
            'code': s,
            'lexer': 'pycon',
            'expiry': self.config.pastebin_expiry
        }

        self.interact.notify(_('Posting data to pastebin...'))
        try:
            response = requests.post(url, data=payload, verify=True)
            response.raise_for_status()
        except requests.exceptions.RequestException as exc:
          self.interact.notify(_('Upload failed: %s') % (str(exc), ))
          return

        self.prev_pastebin_content = s
        data = response.json()

        paste_url_template = Template(self.config.pastebin_show_url)
        paste_id = urlquote(data['paste_id'])
        paste_url = paste_url_template.safe_substitute(paste_id=paste_id)

        removal_url_template = Template(self.config.pastebin_removal_url)
        removal_id = urlquote(data['removal_id'])
        removal_url = removal_url_template.safe_substitute(removal_id=removal_id)

        self.prev_pastebin_url = paste_url
        self.interact.notify(_('Pastebin URL: %s - Removal URL: %s') %
                             (paste_url, removal_url))

        return paste_url
Example #22
0
 def do_GET(self):
     if self.path == '/':
         self.send_response(301)
         self.send_header('Location', '/index.html')
         self.end_headers()
         return
     elif self.path == '/jsmpg.js':
         content_type = 'application/javascript'
         content = self.server.jsmpg_content
     elif self.path == '/index.html':
         content_type = 'text/html; charset=utf-8'
         tpl = Template(self.server.index_template)
         content = tpl.safe_substitute(dict(
             ADDRESS='%s:%d' % (self.request.getsockname()[0], WS_PORT),
             WIDTH=WIDTH, HEIGHT=HEIGHT, COLOR=COLOR, BGCOLOR=BGCOLOR, ROTATION = ROTATION))
     else:
         self.send_error(404, 'File not found')
         return
     content = content.encode('utf-8')
     self.send_response(200)
     self.send_header('Content-Type', content_type)
     self.send_header('Content-Length', len(content))
     self.send_header('Last-Modified', self.date_time_string(time()))
     self.end_headers()
     if self.command == 'GET':
         self.wfile.write(content)
Example #23
0
def _download_and_format_issues():
    try:
        from robot.utils import HtmlWriter, html_format
    except ImportError:
        sys.exit('creating release requires Robot Framework to be installed.')
    URL = Template('http://code.google.com/p/robotframework-ride/issues/csv?'
                   'sort=priority+type&colspec=ID%20Type%20Priority%20Summary'
                   '&q=target%3A${version}&can=1')
    reader = csv.reader(urlopen(URL.substitute({'version': VERSION})))
    total_issues = 0
    writer = HtmlWriter(StringIO())
    writer.element('h2', 'Release notes for %s' % VERSION)
    writer.start('table', attrs={'border': '1'})
    for row in reader:
        if not row or row[1] == 'Task':
            continue
        row = row[:4]
        writer.start('tr')
        if reader.line_num == 1:
            row = [ '*%s*' % cell for cell in row ]
        else:
            row[0] = '<a href="http://code.google.com/p/robotframework-ride/'\
                     'issues/detail?id=%s">Issue %s</a>' % (row[0], row[0])
            total_issues += 1
        for cell in row:
            if reader.line_num == 1:
                cell = html_format(cell)
            writer.element('td', cell, escape=False)
        writer.end('tr')
    writer.end('table')
    writer.element('p', 'Altogether %d issues.' % total_issues)
    return writer.output.getvalue()
Example #24
0
def call_realign_single(java, gatk, ref_seq, blocks, input_list, output_map, target_intervals, known_vcfs, mem="8g"):
    known_str = " ".join(list("-known %s" % (a) for a in known_vcfs))

    template = Template("""
${JAVA}
-Xmx${MEM} -XX:ParallelGCThreads=2 -jar ${GATK}
-T IndelRealigner
-R ${REF_SEQ}
-disable_auto_index_creation_and_locking_when_reading_rods
-I ${INPUT_LIST}
-targetIntervals ${TARGET_INTERVALS}
${KNOWN_STR}
-nWayOut ${OUTPUT_MAP}
""".replace("\n", " "))

    #for i, block in enumerate(fai_chunk( ref_seq + ".fai", block_size ) ):
    #for i, block in enumerate(blocks):
    cmd = template.substitute(
        dict(
            JAVA=java,
            REF_SEQ=ref_seq,
            GATK=gatk,
            #BLOCK_NUM=i,
            #INTERVAL="%s:%s-%s" % (block[0], block[1], block[2]),
            #INTERVAL="%s" % (block),
            INPUT_LIST=input_list,
            #OUTPUT_BASE=output_base,
            OUTPUT_MAP=output_map,
            KNOWN_STR=known_str,
            TARGET_INTERVALS=target_intervals,
            MEM=mem
        )
    )
    return cmd
Example #25
0
def call_scan(java, gatk, ncpus, ref_seq, input_list, known_vcfs, intervals, mem="8g"):

    known_str = " ".join(list("-known %s" % (a) for a in known_vcfs))
    #out = "%s.intervals" % (output_base)
    out = intervals
    template = Template("""
${JAVA}
-Xmx${MEM} -XX:ParallelGCThreads=2 -jar ${GATK}
-T RealignerTargetCreator
-disable_auto_index_creation_and_locking_when_reading_rods
-nt ${NCPUS}
-R ${REF_SEQ}
-I ${INPUT_LIST}
${KNOWN_STR}
-o ${OUT}
""".replace("\n", " "))
#output_base
    cmd = template.substitute(
        dict(
            JAVA=java,
            REF_SEQ=ref_seq,
            GATK=gatk,
            INPUT_LIST=input_list,
        #    OUTPUT_BASE=output_base,
            KNOWN_STR=known_str,
            NCPUS=ncpus,
            OUT=out,
            MEM=mem
    ))
    return cmd #, out
Example #26
0
def gen_qsub_script(script, name='qsub_magic', nodes=1, ppn=1, walltime='01:00:00',
                    mailto='*****@*****.**', path=os.getcwd(), pre=[], post=[], logfile='',
                    isdonefile=''):
    ''' Generate a template to be runned using qsub.'''
    tplate = Template('\n'.join(
        ['#!/bin/sh',
         '#PBS -S /bin/sh',
         '#PBS -N $name',
         '#PBS -j oe',
         '#PBS -l nodes=$nodes:ppn=$ppn,walltime=$walltime',
         '#PBS -M $mailto',
         '#PBS -m abe',
         'cd $path',
         'ulimit -s unlimited'] +
         pre +
        [ 'python $script > $logfile &&',
          '    echo 0 > $isdonefile ||',
          '    echo 1 > $isdonefile'] +
        post +
        ['return 0']))
    qsub_script = tplate.substitute(name=name, nodes=nodes, ppn=ppn,
                                    walltime=walltime, mailto=mailto,
                                    path=path, script=script, logfile=logfile,
                                    isdonefile=isdonefile)

    return qsub_script
Example #27
0
def addressOrIntersection(cross_street0, cross_street1) :
    place = None
    
    street_address = Template("$street_0")
    intersection = Template("$street_0 & $street_1")

    if cross_street0 :
        # Sometimes street addresses are written out, particularly
        # 'One' and sometimes 'Two.' Right now we are only catching
        # addresses that start with a sequence of numbers a space and
        # something that is not a number or whitespace (ideally an
        # letter.
        if re.compile('\d+ [\D\W]').match(cross_street0) :
            place = street_address.substitute(street_0 = cross_street0)
        else :
            if cross_street1 and cross_street1 != cross_street0 :
                # Likely fertile place for improvement
                xstreet_splitter = re.compile('(.+)( and |/)(.+)')
                xstreet_split = xstreet_splitter.findall(cross_street1)
                if xstreet_split :
                    xstreet0 = xstreet_split[0][0].strip()
                    xstreet1 = xstreet_split[0][2].strip()
                else :
                    xstreet0 = cross_street0
                    xstreet1 = cross_street1
                xstreet0 = xstreet0.title()
                xstreet1 = xstreet1.title()
                place = intersection.substitute(street_0 = xstreet0,
                                                street_1 = xstreet1)
                
    return place
    def send_mail(self, correlations, time, script):
        """Simple convenience function which sends an email \
        from the configured sender to receivers.
        :param correlations: number representing the combined \
          number of threats to be reported.
        :type correlations: :mod:`int`
        :param time: the time it took for the calling program \
            to execute and finish successfully.
        :type time: :mod:`string`
        :param script: the script which was invoked such that a \
            detailed job description can be provided to correlation notifications.
        :type time: :mod:`string`
        """

        description = self.get_description(script)
        message = Template("""
        <br><img src="cid:image1" width="200"><br>
        <p>You are receiving this email because you are subscribed to <b>Assurant's Threat Intelligence notification service</b>.</p>
        <p><b>$corr threat correlation(s) have been identified</b> whilst running one of our threat correlation scripts.</p>
        <p>Identified correlations relate to: <b>$desc</b>.</p>
        <p>To view correlation(s) please visit the Kibana dashboard.</p>
        <p>Time taken to identify correlations was <b>$dur seconds</b>.</p>
        <p><i>To unsubscribe from this service please contact $sender</i>.</p>
        """)
        fullMessage = message.substitute(corr=correlations, dur=time, sender=sender, desc=description)
        # Create the root message and fill in the from, to, and subject headers
        msgRoot = MIMEMultipart('related')
        msgRoot['Subject'] = 'Assurant Threatelligence Update'
        msgRoot['From'] = sender
        msgRoot['To'] = receivers
        msgRoot.preamble = 'This is a multi-part message in MIME format.'
        
        # Encapsulate the plain and HTML versions of the message body in an
        # 'alternative' part, so message agents can decide which they want to display.
        msgAlternative = MIMEMultipart('alternative')
        msgRoot.attach(msgAlternative)

        msgText = MIMEText('This is the alternative plain text message.')
        msgAlternative.attach(msgText)
        
        # We reference the image in the IMG SRC attribute by the ID we give it below
        #msgRoot = MIMEText()
        msgText = MIMEText(fullMessage, 'html')
        msgAlternative.attach(msgText)

        # This example assumes the image is in the current directory
        fp = open('assurant-logo.png', 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()

        # Define the image's ID as referenced above
        msgImage.add_header('Content-ID', '<image1>')
        msgRoot.attach(msgImage)

        smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
        smtpObj.ehlo()
        smtpObj.starttls()
        smtpObj.login(sender, '')
        smtpObj.sendmail(sender, receivers, msgRoot.as_string())
        smtpObj.quit()
def replace_template(infile, vars, outfile):
    with open(infile, "r") as template_file:
        temp = Template(template_file.read())
        result = temp.substitute(vars)

    with open(outfile, "w") as out:
        out.write(result)
Example #30
0
    def __str__(self):
        bset_spec = []
        for el, bset in self.basis_set.items():
            bset_spec.append(" {} library \"{}\"".format(el, bset))

        theory_spec = []
        if self.theory_directives:
            theory_spec.append("{}".format(self.theory))
            for k, v in self.theory_directives.items():
                theory_spec.append(" {} {}".format(k, v))
            theory_spec.append("end")

        t = Template("""title "$title"
charge $charge
basis
$bset_spec
end
$theory_spec
task $theory $operation""")

        return t.substitute(
            title=self.title, charge=self.charge,
            bset_spec="\n".join(bset_spec),
            theory_spec="\n".join(theory_spec),
            theory=self.theory, operation=self.operation)
Example #31
0
#py program to demon, thhe working of the string template
from string import Template

#list student stores the name and marks of three students
student = [('Aryan', 90), ('ryan', 80), ('Yan', 70)]

#create a basic structure to print name and amrks of the student
t = Template('Hey $name, you got $marks marks')

for i in student:
    print(t.substitute(name=i[0], marks=i[1]))
Example #32
0
_PAGE_TEMPLATE = Template("""
<html>
    <head>
        <title>
            $title
        </title>
        <style>
            $css
        </style>
    </head>
    <body>
        <div class="pane-container">
            <div class="pane model">
                <div class="pane__left model__input">
                    <div class="model__content">
                        <h2><span>$title</span></h2>
                        <div class="model__content">
                            <input type="file" class="inputfile"  id="file" name="file" onchange="previewFile()"><br>
                            <label for="file" class="custom-file-upload">Choose a file</label>
                        </div>
                    </div>
                </div>
                <div class="pane__right model__output model__output--empty">
                    <div class="pane__thumb"></div>
                    <div class="model__content">
                        <div id="output" class="output">
                            <div class="placeholder">
                                <div class="placeholder__content">
                                    <canvas id="outputCanvas" width="512" height="512" style="border:1px solid #d3d3d3;">
                                    </canvas>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    

    <script>
        $process_prediction
        function previewFile(){
           //var preview = document.querySelector('img'); //selects the query named img
           var file    = document.querySelector('input[type=file]').files[0]; //sames as here
           var reader  = new FileReader();

           reader.onloadend = function () {
                var c = document.getElementById("outputCanvas");
                var ctx = c.getContext("2d");
                ctx.clearRect(0, 0, c.width, c.height);
                var base_image = new Image();
                base_image.src = reader.result;
                base_image.onload = function(){
                    ctx.drawImage(base_image, 0, 0, 512, 512);
                };
               console.log(typeof(reader.result));
               //console.log(reader.result);
               var xhr = new XMLHttpRequest();
               xhr.open('POST', '/predict');
               //xhr.responseType = 'blob';
               xhr.setRequestHeader('Content-Type', 'image/jpeg');
               xhr.onload = function() {
                var prediction = JSON.parse(xhr.responseText)
                processPrediction(prediction);
               };
               xhr.send(reader.result);
    
           }

           if (file) {
               reader.readAsDataURL(file); //reads the data as a URL
           } else {
               //preview.src = "";
           }
      };
      previewFile();
    </script>
</html>
""")
Example #33
0
def create_tcl_script(files):
    runsim_files = []
    for eachFile in files:
        eachFile = os.path.abspath(eachFile)
        pDir = os.path.dirname(eachFile)
        os.chdir(pDir)

        config = ConfigParser()
        config.read(eachFile)
        config = config["SIMULATION_DECK"]

        # Resolve project Modelsim project path
        args.modelsim_run_dir = os.path.dirname(os.path.abspath(eachFile))
        modelsim_proj_dir = os.path.join(args.modelsim_run_dir, "MMSIM2")
        logger.info(f"Modelsim project dir not provide " +
                    f"using default {modelsim_proj_dir} directory")

        modelsim_proj_dir = os.path.abspath(modelsim_proj_dir)
        config["MODELSIM_PROJ_DIR"] = modelsim_proj_dir
        if not os.path.exists(modelsim_proj_dir):
            os.makedirs(modelsim_proj_dir)

        # Resolve Modelsim Project name
        args.modelsim_proj_name = config["BENCHMARK"] + "_MMSIM"
        logger.info(f"Modelsim project name not provide " +
                    f"using default {args.modelsim_proj_name} directory")

        config["MODELSIM_PROJ_NAME"] = args.modelsim_proj_name
        config["MODELSIM_INI"] = args.modelsim_ini
        config["VERILOG_PATH"] = os.path.join(os.getcwd(),
                                              config["VERILOG_PATH"])
        IncludeFile = os.path.join(os.getcwd(), config["VERILOG_PATH"],
                                   config["VERILOG_FILE2"])
        IncludeFileResolved = os.path.join(
            os.getcwd(), config["VERILOG_PATH"],
            config["VERILOG_FILE2"].replace(".v", "_resolved.v"))
        with open(IncludeFileResolved, "w") as fpw:
            with open(IncludeFile, "r") as fp:
                for eachline in fp.readlines():
                    eachline = eachline.replace("\"./", "\"../../../")
                    fpw.write(eachline)
        # Modify the variables in config file here
        config["TOP_TB"] = os.path.splitext(config["TOP_TB"])[0]

        # Write final template file
        # Write runsim file
        tmpl = Template(
            open(args.modelsim_runsim_tmpl, encoding='utf-8').read())
        runsim_filename = os.path.join(modelsim_proj_dir,
                                       "%s_runsim.tcl" % config['BENCHMARK'])
        logger.info(f"Creating tcl script at : {runsim_filename}")
        with open(runsim_filename, 'w', encoding='utf-8') as tclout:
            tclout.write(tmpl.substitute(config))

        # Write proc file
        proc_filename = os.path.join(
            modelsim_proj_dir, "%s_autocheck_proc.tcl" % config['BENCHMARK'])
        logger.info(f"Creating tcl script at : {proc_filename}")
        with open(proc_filename, 'w', encoding='utf-8') as tclout:
            tclout.write(
                open(args.modelsim_proc_tmpl, encoding='utf-8').read())
        runsim_files.append({
            "ini_file": eachFile,
            "modelsim_run_dir": args.modelsim_run_dir,
            "runsim_filename": runsim_filename,
            "run_complete": False,
            "status": False,
            "finished": True,
            "starttime": 0,
            "endtime": 0,
            "Errors": 0,
            "Warnings": 0
        })
    # Execute modelsim
    if args.run_sim:
        thread_sema = threading.Semaphore(args.maxthreads)
        logger.info("Launching %d parallel threads" % args.maxthreads)
        thread_list = []
        for thread_no, eachjob in enumerate(runsim_files):
            t = threading.Thread(target=run_modelsim_thread,
                                 name=f"Thread_{thread_no:d}",
                                 args=(thread_sema, eachjob, runsim_files))
            t.start()
            thread_list.append(t)
        for eachthread in thread_list:
            eachthread.join()
        return runsim_files
    else:
        logger.info("Created runsim and proc files")
        logger.info(f"runsim_filename {runsim_filename}")
        logger.info(f"proc_filename {proc_filename}")
        from pprint import pprint
        pprint(runsim_files)
Example #34
0
    def query(
        self,
        query: Optional[str],
        filters: Optional[Dict[str, List[str]]] = None,
        top_k: int = 10,
        custom_query: Optional[str] = None,
        index: Optional[str] = None,
    ) -> List[Document]:

        if index is None:
            index = self.index

        # Naive retrieval without BM25, only filtering
        if query is None:
            body = {
                "query": {
                    "bool": {
                        "must": {
                            "match_all": {}
                        }
                    }
                }
            }  # type: Dict[str, Any]
            if filters:
                filter_clause = []
                for key, values in filters.items():
                    filter_clause.append({"terms": {key: values}})
                body["query"]["bool"]["filter"] = filter_clause

        # Retrieval via custom query
        elif custom_query:  # substitute placeholder for question and filters for the custom_query template string
            template = Template(custom_query)
            # replace all "${question}" placeholder(s) with query
            substitutions = {"question": query}
            # For each filter we got passed, we'll try to find & replace the corresponding placeholder in the template
            # Example: filters={"years":[2018]} => replaces {$years} in custom_query with '[2018]'
            if filters:
                for key, values in filters.items():
                    values_str = json.dumps(values)
                    substitutions[key] = values_str
            custom_query_json = template.substitute(**substitutions)
            body = json.loads(custom_query_json)
            # add top_k
            body["size"] = str(top_k)

        # Default Retrieval via BM25 using the user query on `self.search_fields`
        else:
            body = {
                "size": str(top_k),
                "query": {
                    "bool": {
                        "should": [{
                            "multi_match": {
                                "query": query,
                                "type": "most_fields",
                                "fields": self.search_fields
                            }
                        }]
                    }
                },
            }

            if filters:
                filter_clause = []
                for key, values in filters.items():
                    if type(values) != list:
                        raise ValueError(
                            f'Wrong filter format for key "{key}": Please provide a list of allowed values for each key. '
                            'Example: {"name": ["some", "more"], "category": ["only_one"]} '
                        )
                    filter_clause.append({"terms": {key: values}})
                body["query"]["bool"]["filter"] = filter_clause

        if self.excluded_meta_data:
            body["_source"] = {"excludes": self.excluded_meta_data}

        logger.debug(f"Retriever query: {body}")
        result = self.client.search(index=index, body=body)["hits"]["hits"]

        documents = [self._convert_es_hit_to_document(hit) for hit in result]
        return documents
Example #35
0
class XGIrisMulticlass(XGBaseCase):
    python_template = Template("""
import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import xgboost as xgb

X, y = datasets.load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

xg_train = xgb.DMatrix(X_train, label=y_train)
xg_test = xgb.DMatrix(X_test, label=y_test)
params = {
    'objective': 'multi:softmax',
    'num_class': 3,
}
n_estimators = 20
clf = xgb.train(params, xg_train, n_estimators)
y_pred = clf.predict(xg_test, output_margin=True)
# save the model in binary format
clf.save_model('$model_filename')
np.savetxt('$true_predictions_filename', y_pred, delimiter='\t')
datasets.dump_svmlight_file(X_test, y_test, '$data_filename')
""")

    go_template = Template("""
package main

import (
    "github.com/ucfunnel/leaves"
    "github.com/ucfunnel/leaves/mat"
)

func main() {
	test, err := mat.CSRMatFromLibsvmFile("$data_filename", 0, true)
	if err != nil {
		panic(err)
	}

	model, err := leaves.XGEnsembleFromFile("$model_filename", false)
	if err != nil {
		panic(err)
	}

    predictions := mat.DenseMatZero(test.Rows(), model.NOutputGroups())
	err = model.PredictCSR(test.RowHeaders, test.ColIndexes, test.Values, predictions.Values, 0, 1)
    if err != nil {
        panic(err)
    }

    err = predictions.ToCsvFile("$predictions_filename", "\t")
    if err != nil {
        panic(err)
    }
}
""")

    def compare(self):
        self.compare_matrices(
            matrix1_filename=self.files['true_predictions_filename'],
            matrix2_filename=self.files['predictions_filename'],
            tolerance=1e-6,
            max_number_of_mismatches_ratio=0.0)
Example #36
0
class LGIrisRandomForest(LGBaseCase):
    python_template = Template("""
import numpy as np
import pickle
from sklearn import datasets
import lightgbm as lgb
from sklearn.model_selection import train_test_split


data = datasets.load_iris()
X = data['data']
y = data['target']
y[y > 0] = 1
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

n_estimators = 30
d_train = lgb.Dataset(X_train, label=y_train)
params = {
    'boosting_type': 'rf',
    'objective': 'binary',
    'bagging_fraction': 0.8,
    'feature_fraction': 0.8,
    'bagging_freq': 1,
}

clf = lgb.train(params, d_train, n_estimators)

y_pred = clf.predict(X_test)

model_filename = 'lg_rf_iris.model'
pred_filename = 'lg_rf_iris_true_predictions.txt'
# test_filename = 'iris_test.libsvm'

clf.save_model('$model_filename')
np.savetxt('$true_predictions_filename', y_pred)
datasets.dump_svmlight_file(X_test, y_test, '$data_filename')
""")

    go_template = Template("""
package main

import (
    "github.com/ucfunnel/leaves"
    "github.com/ucfunnel/leaves/mat"
)

func main() {
	test, err := mat.CSRMatFromLibsvmFile("$data_filename", 0, true)
	if err != nil {
		panic(err)
	}

	model, err := leaves.LGEnsembleFromFile("$model_filename", false)
	if err != nil {
		panic(err)
	}

    predictions := mat.DenseMatZero(test.Rows(), model.NOutputGroups())
	err = model.PredictCSR(test.RowHeaders, test.ColIndexes, test.Values, predictions.Values, 0, 1)
    if err != nil {
        panic(err)
    }

    err = predictions.ToCsvFile("$predictions_filename", "\t")
    if err != nil {
        panic(err)
    }
}
""")
Example #37
0
from string import Template

with open('/document-store-worker/tests/data/document.v2.json', 'rt') as f:
    DOCUMENT_V2_TEMPLATE = Template(f.read())

with open('/document-store-worker/tests/data/document.v3.json', 'rt') as f:
    DOCUMENT_V3_TEMPLATE = Template(f.read())
Example #38
0
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
"""
import os
from string import Template

from qgis.PyQt import uic
from qgis.PyQt.QtWidgets import QDialog

# replace version from metada file
with open(os.path.join(os.path.dirname(__file__), 'ui_about.ui'), 'r', encoding="utf-8") as about, \
     open(os.path.join(os.path.dirname(__file__), '..', '..', 'metadata.txt'), 'r', encoding="utf-8") as meta, \
     open(os.path.join(os.path.dirname(__file__), 'ui_about_.ui'), 'w') as filledUi:
    t = Template(about.read())
    for line in meta.readlines():
        if line.strip().startswith("version="):
            version = line.split("=")[1].strip()
            break
    t = t.safe_substitute(version=version)
    filledUi.write(t)

FORM_CLASS, _ = uic.loadUiType(
    os.path.join(os.path.dirname(__file__), 'ui_about_.ui'))


class AboutDialog(QDialog, FORM_CLASS):
    def __init__(self, parent=None):
        """
        Constructor
Example #39
0
print(s4)  # Here a is 99

# Format placeholders are evaluated once. They are compiled to Python
# bytecode before executing the program, so syntax errors are revealed
# before the script starts running. Uncomment to see this happen.

# print(f"a is now {a:!4&^#}")

a = 42
print(s4)  # a is still 99 inside the string

# And yet another way to compose strings from smaller pieces. Very
# few examples of people actually using this scheme have been found.
# The mystery deepens.

tt = Template("$who likes $food.")

# Python's flexibility to allow arbitrary keyword arguments in functions
# really displays its power and usefulness here.
print(tt.substitute(who="Bob", food="sushi"))
print(tt.substitute(who="Jack", food="steak"))

# The old-timey string interpolation operator % behaves like in C.

s2 = "a is now %3d, b is now %.4f." % (a, b)
print(s2)

# Strings are iterable, and can be processed as iterables.

it = iter(s1)
print(next(it))  # H
Example #40
0
    def to_out(self, outname, templatefile, convunicode=True, clean=False, sort=False):
        ## output to [outname] using the template given

        ## open the output file
        if isinstance(outname, io.StringIO):
            outfile = outname
            outfile.seek(0)
        else:
            outfile = open(outname, "w", encoding='utf-8')

        ## no unicode output for .tex files
        if('tex' in outname):
            encodeunicode = False
        else:
            encodeunicode = True

        ## check if we want to clean or sort
        if(clean):
            recs = self.cleanup(sort=sort)
            self.records = [reci[0] for reci in recs]


        ## get the template string -- we will replace this for 
        ## each entry
        if isinstance(templatefile, io.StringIO):
            templatelines = templatefile.readlines()
        else:
            templatelines = open(templatefile, 'r').readlines()

        templates = []
        for linei in templatelines:
            if(linei[0] == '@'):
                temptype = re.findall(r'@([A-Za-z]*?):', linei)[0]
                templates.append([temptype.lower(), linei.replace('@%s:'%temptype, '')])
            else:
                templates.append(['all', linei])
        nalls = 0
        for template in templates:
            if(template[0] == 'all'):
                generictempstring = template[1]
                nalls += 1

        if(nalls > 1):
            print("Error! Can only have one general template")
            return

        ## loop through all the records
        for record in self.records:
            rectype = record.rec_type

            templatestring = generictempstring
            for template in templates:
                if(rectype.lower() == template[0]):
                    templatestring = template[1]

            ## first, find the author template because this is going to 
            ## be common to all
            authtemplate   = re.findall(r'auth([sf])([0-9a]?)', templatestring);
            try:
                if(len(authtemplate) > 1):
                    raise ValueError("Error! Only one entry for author is allowed!")
                # authstring     = "auth%s%s"%(authtemplate[0][0],authtemplate[0][1])
                authstyle = authtemplate[0][0]
            except IndexError as e:
                raise IndexError("Please enter an author template")

            ## find if the author list is short or long
            if(authstyle not in ['s', 'f']):
                raise ValueError("Author style must be s=>short or f=>full")
            
            if(authstyle == 's'):
                try:
                    if(authtemplate[0][1] != 'a'):
                        authnum = int(authtemplate[0][1])
                    else:
                        authnum = int(1e10)
                except Exception as e:
                    raise ValueError("Please use $authsa for all authors or enter a number after $auths")
            else:
                authnum   = 1

            ## then find the groups 
            groups = re.findall(r"(?<!\\)([\{]{1}.*?(?<!\\)[\}]{1})", templatestring)

            ## then find all the templates
            kws = re.findall(r'(\$[a-z0-9]+)', templatestring)
            ## copy the template string to replace later
            tempstring = templatestring

            ## this will hold all the attribute values
            tempdict   = {}
            for tempi in kws:

                ## get the variable name
                tempstr = tempi.replace('$','')

                ## special case -- authors
                if("auth" in tempi):
                    authtext = ''
                    if(authstyle=='s'): ## short author style (lastname firstinitials)
                        if(len(record.authors) > authnum):
                            author = record.authors[0]
                            if(encodeunicode):
                                short_name = tex2unicode(author.short_name())
                            else:
                                short_name = author.short_name()

                            authtext = authtext + short_name + " et al."
                        else:
                            nauths = min([len(record.authors), authnum])
                            for authi in range(nauths):
                                author0 = record.authors[authi]
                                if(encodeunicode):
                                    short_name = tex2unicode(author0.short_name())
                                else:
                                    short_name = author0.short_name()

                                authtext = authtext + short_name
                                if(authi==nauths-2):
                                    authtext = authtext + " and "
                                elif(authi==nauths-1):
                                    authtext = authtext + " "
                                else:
                                    authtext = authtext + ", "
                    elif(authstyle=='f'):
                        for i, author in enumerate(record.authors):
                            if(encodeunicode):
                                long_name = tex2unicode(author.long_name())
                            else:
                                long_name = author.long_name()

                            if(i != len(record.authors)-1):
                                authtext = authtext + long_name + ", "
                            else:
                                authtext = authtext + long_name
                    ## save the author string to the output dictionary
                    tempdict[tempstr] = authtext
                else:  ## for everything else
                    if(hasattr(record, tempstr)):
                        if(encodeunicode):
                            try:
                                tempdict[tempstr] = tex2unicode(record.__getattribute__(tempstr))
                            except AttributeError:
                                tempdict[tempstr] = record.__getattribute__(tempstr)
                        else:
                            tempdict[tempstr] = record.__getattribute__(tempstr)

                    else:
                        ## if the record does not have this attribute
                        ## find the group that this attribute is in
                        remgroup = None
                        for groupi in groups:
                            if tempstr in groupi:
                                remgroup = groupi

                        if remgroup is None:
                            raise KeyError(f"entry '{tempstr}' is not available in record {record.entry_name}")

                        ## remove the group
                        tempstring = tempstring.replace(remgroup, '')
                        tempdict[tempstr] = ''

            ## finally, remove all the group {}'s and replace the string
            for groupi in groups:
                repgroup = re.sub(r'{(.*?)}',r'\1', groupi)
                tempstring = tempstring.replace(groupi, repgroup)

            ## and also replace all the actual escaped \{ to {
            tempstring = tempstring.replace('\\{', '{')
            tempstring = tempstring.replace('\\}', '}')
            ## and then substitute
            newtemplate = Template(tempstring)
            outfile.write(newtemplate.safe_substitute(tempdict))
clConfigHeaderFileTemplate = Template("""
/*******************************************************************************
*
* This headerfile is auto-generated by OpenClovis IDE
*
* clCompConfig.h for ${cpmName}
*
********************************************************************************/

#ifndef CL_COMP_CFG
#define CL_COMP_CFG

#ifdef __cplusplus
extern "C" {
#endif

#include <clCommon.h>   
#include <clEoApi.h>                                                                                                                          
                                                                                                                             
#define COMP_NAME       "${cpmName}"
#define COMP_EO_NAME    "${eoName}"

#define HAS_EO_SERVICES ${hasEOServices}

#define COMP_EO_THREAD_PRIORITY ${eoThreadPriority}
#define COMP_EO_NUM_THREAD ${eoNumThread}
#define COMP_IOC_PORT ${iocPort}
#define COMP_EO_USER_CLIENT_ID (CL_EO_USER_CLIENT_ID_START + ${maxNoClients})
#define COMP_EO_USE_THREAD_MODEL ${threadPolicy}
                                                                                                                             
/* Component EO Basic Libraries */
#define COMP_EO_BASICLIB_OSAL   CL_TRUE
#define COMP_EO_BASICLIB_TIMER   CL_TRUE
#define COMP_EO_BASICLIB_BUFFER   CL_TRUE
#define COMP_EO_BASICLIB_IOC   CL_TRUE
#define COMP_EO_BASICLIB_RMD   CL_TRUE
#define COMP_EO_BASICLIB_EO   CL_TRUE
#define COMP_EO_BASICLIB_OM   ${omLibEnable}
#define COMP_EO_BASICLIB_HAL  ${halLibEnable}
#define COMP_EO_BASICLIB_DBAL  ${dbalLibEnable}
                                                                                                                             
/* Component EO Client Libraries */                                                                                                                            

#define COMP_EO_CLIENTLIB_COR   ${corClLib}
#define COMP_EO_CLIENTLIB_CM    ${cmClLib}                  
#define COMP_EO_CLIENTLIB_NAME    ${nameClLib}                  
#define COMP_EO_CLIENTLIB_LOG    ${logClLib}                  
#define COMP_EO_CLIENTLIB_TRACE    CL_FALSE                 
#define COMP_EO_CLIENTLIB_DIAG    CL_FALSE
#define COMP_EO_CLIENTLIB_TXN    ${txnClLib}
#define COMP_EO_CLIENTLIB_MSO    ${msoLib}
#define COMP_EO_CLIENTLIB_PROV    ${provLib}
#define COMP_EO_CLIENTLIB_ALARM    ${alarmClLib}
#define COMP_EO_CLIENTLIB_DEBUG    ${debugClLib}
#define COMP_EO_CLIENTLIB_GMS    CL_FALSE
#define COMP_EO_CLIENTLIB_PM    ${pmLib}

#ifdef __cplusplus
}
#endif                                                                                                                             

#endif

""")
_PAGE_TEMPLATE = Template("""
<html>
    <head>
        <title>
            $title
        </title>
        <style>
            $css
        </style>
    </head>
    <body>
        <div class="pane-container">
            <div class="pane model">
                <div class="pane__left model__input">
                    <div class="model__content">
                        <h2><span>$title</span></h2>
                        <div class="model__content">
                            $inputs
                            <div class="form__field form__field--btn">
                                <button type="button" class="btn btn--icon-disclosure" onclick="predict()">Predict</button>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="pane__right model__output model__output--empty">
                    <div class="pane__thumb"></div>
                    <div class="model__content">
                        <div id="output" class="output">
                            <div class="placeholder">
                                <div class="placeholder__content">
                                    <p>Run model to view results</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <script>
    function escapeHtml(unsafe) {
    return unsafe
         .replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
 }
    function predict() {
        var quotedFieldList = $qfl;
        var data = {};
        quotedFieldList.forEach(function(fieldName) {
            data[fieldName] = document.getElementById("input-" + fieldName).value;
        })

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/predict');
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onload = function() {
            if (xhr.status == 200) {
                // If you want a more impressive visualization than just
                // outputting the raw JSON, change this part of the code.
                var escaped = escapeHtml(JSON.stringify(JSON.parse(xhr.responseText), null, 2));
                var htmlResults = "<pre>" + escaped + "</pre>";

                document.getElementById("output").innerHTML = htmlResults;
            }
        };
        xhr.send(JSON.stringify(data));
    }
    </script>
</html>
""")
Example #43
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
from string import Template
from textwrap import dedent

wsgi_template = Template(
    dedent("""\
    # -*- coding: utf-8 -*-
    from ${app_name} import create_app

    app = create_app()

    """))

procfile_template = Template(dedent("""\
    web: gunicorn wsgi:app

    """))

tests_template = Template(
    dedent("""\
    import unittest

    # write your tests here
    """))

app_init_template = Template(
    dedent("""\
    import logging, os
    from logging.handlers import SMTPHandler, RotatingFileHandler
    from flask import Flask
Example #44
0
# -*- coding: utf-8 -*-
"""
opennms_client.templates
~~~~~~~~~~~~~~~~~~~~~~~~
This file contains xml templates needed by client

"""
from string import Template

template_services = Template(
    """<?xml version="1.0" encoding="UTF-8" standalone="yes"?><service source="P" status="A"><applications/><notify>Y</notify><serviceType id="$id"><name>$name</name></serviceType></service>"""
)
# TODO: remove end of lines for a proper representation
# template_services = Template("""
# <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
# <service source="P" status="A">
#   <applications/>
#   <notify>Y</notify>
#   <serviceType id="$id">
#     <name>$name</name>
#   </serviceType>
# </service>""")

templates = {'services': template_services}
Example #45
0
about_text = Template('''
<div class="content is-size-5 about">
    <h2 class="title is-4">The modeling behind this tool</h2>
    <p>Here we summarize  outputs from ALFRESCO (ALaska FRame-based EcoSystem COde), a spatially explicit landscape-scale wildfire model. We use ALFRESCO to simulate wildfire and vegetation dynamics in response to transient changes in climate, across a wide range of future conditions.</p>

    <h2 class="title is-4">What this tool does</h2>
    <ul>
        <li>Addresses the natural variability of fire by visualizing broad trends and patterns</li>
        <li>Displays future projections using the average of 5 top performing GCMs, plus 10-year summaries</li>
        <li>Provides more exploration opportunities by including 3 different climate scenarios, or RCPs, as well as &ldquo;treatments&rdquo;.</li>
        <li>Provides the ability to subset the analysis by Fire Management Zones, Ecoregions, or look at statewide results.</li>
    </ul>

    <h2 class="title is-4">Treatment options TX0, TX1, TX2</h2>
    <p>ALFRESCO model runs include 3 experimental &ldquo;treatments&rdquo; that simulate alteration of the fire management options: Critical, Full, Modified, and Limited.</p>

    <ul>
        <li><strong>TX0&mdash;Baseline:</strong> Current fire management options are continued into the future, with the exception being all Modified areas are switched to Limited. Otherwise, this scenario assumes minimal changes to current management practices.</li>
        <li><strong>TX1&mdash;More Full Suppression:</strong> Starting with the TX0 treatment, Full suppression areas were then extended by a 10km buffer and were applied to future simulations. This represents an overall increase in suppression efforts in the affected areas.</li>
        <li><strong>TX2&mdash;No Full Suppression:</strong> Starting with the TX0 treatment, Full and Modified suppression areas were re-assigned to Limited status for the purpose of future simulations. Critical areas remained unchanged. This represents an overall decrease in suppression efforts.</li>
    </ul>

    <h2 class="title is-4">General Circulation Models (GCMs) and how they fit into this work</h2>
    <p>GCMs are used to depict how temperature and precipitation respond to changing levels of various gases in the atmosphere. GCMs use future compositions of gases in the atmosphere to make projections of future climate. For this work, GCMs provide projections of future precipitation that drive wildfire activity in the model.</p>
    <p>More info: <a href="http://www.ipcc-data.org/guidelines/pages/gcm_guide.html">Intergovernmental Panel on Climate Change GCM guide</a></p>

    <h2 class="title is-4">Representative Concentration Pathways (RCPs) and how they fit into this work</h2>
    <p>RCPs are used to characterize the consequences of different assumptions about human population growth and economic development. In particular, economic development associated with energy use—energy amounts and sources—is an important driver of future climate. We consider 3 RCPs here (4.5, 6.0, and 8.5).</p>
    <ul>
        <li>RCP 4.5 represents an aggressive reduction in the emission of greenhouse gases like CO2 and methane.</li>
        <li>RCP 8.5 represents increases in the population and a continuation of the use of energy sources that emit large quantities of greenhouse gases.</li>
        <li>RCP 6.0 lies somewhere in between.</li>
    </ul>

    <p>More info: <a href="https://www.ipcc-data.org/guidelines/pages/glossary/glossary_r.html">Intergovernmental Panel on Climate Change RCP Definition</a></p>

    <h2 class="title is-4">What are Fire Management Zones?</h2>
<p>These regions are the current Fire Management Zones for Alaska. For more information, please see <a href="https://afs.ak.blm.gov/fire-management/zones-alaska-zone-coverage-maps.php">the zone coverage</a> maps created by the US Bureau of Land Management / Alaska Fire Service.</p>

<img src="assets/zones.png" alt="Current fire management zones for Alaska"/>

<h2 class="title is-4">What are Ecoregions?</h2>
<p>Ecoregions are areas where ecosystems (and the type, quality, and quantity of environmental resources) are generally similar (Omernik 1987).</p>

<img src="assets/ecoregions.jpg" alt="Ecoregions of Alaska"/>

''')
Example #46
0
 def _generate_output_filename(self, filename, template_vars):
     return Template(filename).substitute(template_vars).replace('.j2', '')
Example #47
0
from string import Template

greeting = 'Salutations'
name = 'Abdullah'
imgSrc = 'http://www.google.com/intl/en_ALL/images/logo.gif'
link = 'http://feihonghsu.blogspot.com'

html = """
<div>
    <h1>
        ${greeting}, ${name}!
    </h1>
    <p>
        This is an image <img src="${imgSrc}" />
    </p>
    <p>
        Try clicking on the <a href="${link}">link</a> right now.
    </p>
</div>"""

"""def locals()
Return a dictionary containing the current scope's local variables.
NOTE: Whether or not updates to this dictionary will affect name lookups in the local 
scope and vice-versa is *implementation dependent* and not covered by 
any backwards compatibility guarantees."""
local_variables = locals()
html = Template(html).substitute(**local_variables)
print(html)
Example #48
0
#!/usr/bin/env python
# Jonas Schnelli, 2013
# make sure the Easysend-Qt.app contains the right plist (including the right version)
# fix made because of serval bugs in Qt mac deployment (https://bugreports.qt-project.org/browse/QTBUG-21267)

from string import Template
from datetime import date

bitcoinDir = "./";

inFile     = bitcoinDir+"/share/qt/Info.plist"
outFile    = "Easysend-Qt.app/Contents/Info.plist"
version    = "unknown";

fileForGrabbingVersion = bitcoinDir+"easysend-qt.pro"
for line in open(fileForGrabbingVersion):
	lineArr = line.replace(" ", "").split("=");
	if lineArr[0].startswith("VERSION"):
		version = lineArr[1].replace("\n", "");

fIn = open(inFile, "r")
fileContent = fIn.read()
s = Template(fileContent)
newFileContent = s.substitute(VERSION=version,YEAR=date.today().year)

fOut = open(outFile, "w");
fOut.write(newFileContent);

print "Info.plist fresh created"
Example #49
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os, util
from string import Template

import jni_gen


def is_manually_generated(f_name, plugin_name):
    return f_name in {'control_ping_reply'}


class_reference_template = Template("""jclass ${ref_name}Class;
""")

find_class_invocation_template = Template("""
    ${ref_name}Class = (jclass)(*env)->NewGlobalRef(env, (*env)->FindClass(env, "io/fd/vpp/jvpp/${plugin_name}/dto/${class_name}"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }""")

find_class_template = Template("""
    ${ref_name}Class = (jclass)(*env)->NewGlobalRef(env, (*env)->FindClass(env, "${class_name}"));
    if ((*env)->ExceptionCheck(env)) {
        (*env)->ExceptionDescribe(env);
        return JNI_ERR;
    }""")
Example #50
0
def read_file_template(filename):
    with open(filename, mode='r', encoding='utf-8') as template_file:
        template_file_content = template_file.read()
    return Template(template_file_content)
Example #51
0
DEFAULT_LOG_CONFIG = Template("""
version: 1

formatters:
    precise:
        format: '%(asctime)s - %(name)s - %(lineno)d - %(levelname)s - \
%(request)s - %(message)s'

filters:
    context:
        (): synapse.logging.context.LoggingContextFilter
        request: ""

handlers:
    file:
        class: logging.handlers.RotatingFileHandler
        formatter: precise
        filename: ${log_file}
        maxBytes: 104857600
        backupCount: 10
        filters: [context]
        encoding: utf8
    console:
        class: logging.StreamHandler
        formatter: precise
        filters: [context]

loggers:
    synapse:
        level: INFO

    synapse.storage.SQL:
        # beware: increasing this to DEBUG will make synapse log sensitive
        # information such as access tokens.
        level: INFO

root:
    level: INFO
    handlers: [file, console]
""")
Example #52
0
                    "_ENCRYPTED" not in x))
        ]:
            col_camel = camel_case(field)
            get_set_arr.append(
                """update{pojo}.set{col_camel}({pojo_camel}.get{col_camel})""".
                format(pojo=tup.pojo,
                       pojo_camel=pojo_camel,
                       col_camel=col_camel))
        get_set_script = "\n".join(get_set_arr)
        mone_arr = []
        if tup.manyToOne != "":
            for mone in tup.manyToOne.split(","):
                repo_text = Template(
                    """\toverride def findBy$moneClass($mone : $moneClass):List[$pojo] = {
                $pojo_camel Repository.findBy$moneClass($mone).asScala.toList
                }""").substitute(pojo=tup.pojo,
                                 mone=mone,
                                 moneClass=camel_case(mone),
                                 pojo_camel=pojo_camel).replace(
                                     "{} ".format(pojo_camel), pojo_camel)
                mone_arr.append(repo_text)
        code_text = "\n".join(mone_arr)

        out_text = replace_string("source/PojoServiceImpl.scala", tup,
                                  code_text, get_set_script)
        out_file.write(out_text)

    with open(
            target_path +
            "/controller/rest/Rest{0}Controller.scala".format(tup.pojo),
            "w") as out_file:
Example #53
0
def include_header(the_title):
    with open('templates/header.html') as headf:
        head_text = headf.read()
    header = Template(head_text)
    return header.substitute(title=the_title)
def mnist_tf_volume(
        docker_repo_training='seldonio/deepmnistclassifier_trainer',
        docker_tag_training='0.3',
        docker_repo_serving='seldonio/deepmnistclassifier_runtime',
        docker_tag_serving='0.3'):

    #use volume for storing model
    #here model is saved and mounted into pre-defined image for serving
    #alternatively model can be baked into image - for that see mabdeploy-seldon.py
    #requires seldon v0.3.0 or higher
    modelvolop = dsl.VolumeOp(name="modelpvc",
                              resource_name="modelpvc",
                              size="50Mi",
                              modes=dsl.VOLUME_MODE_RWO)

    tfjobjson_template = Template("""
{
	"apiVersion": "kubeflow.org/v1beta1",
	"kind": "TFJob",
	"metadata": {
		"name": "mnist-train-{{workflow.uid}}",
		"ownerReferences": [
		{
			"apiVersion": "argoproj.io/v1alpha1",
			"kind": "Workflow",
			"controller": true,
			"name": "{{workflow.name}}",
			"uid": "{{workflow.uid}}"
		}
	    ]
	},
	"spec": {
		"tfReplicaSpecs": {
			"Worker": {
				"replicas": 1,
				"template": {
					"spec": {
						"containers": [
							{
								"image": "$dockerrepotraining:$dockertagtraining",
								"name": "tensorflow",
								"volumeMounts": [
									{
										"mountPath": "/data",
										"name": "persistent-storage"
									}
								]
							}
						],
						"restartPolicy": "OnFailure",
						"volumes": [
							{
								"name": "persistent-storage",
								"persistentVolumeClaim": {
									"claimName": "$modelpvc"
								}
							}
						]
					}
				},
				"tfReplicaType": "MASTER"
			}
		}
	}
}
""")

    tfjobjson = tfjobjson_template.substitute({
        'dockerrepotraining':
        str(docker_repo_training),
        'dockertagtraining':
        str(docker_tag_training),
        'modelpvc':
        modelvolop.outputs["name"]
    })

    tfjob = json.loads(tfjobjson)

    train = dsl.ResourceOp(
        name="train",
        k8s_resource=tfjob,
        success_condition='status.replicaStatuses.Worker.succeeded == 1')

    seldon_serving_json_template = Template("""
{
	"apiVersion": "machinelearning.seldon.io/v1alpha2",
	"kind": "SeldonDeployment",
	"metadata": {
		"labels": {
			"app": "seldon"
		},
		"name": "mnist-classifier"
	},
	"spec": {
		"annotations": {
			"deployment_version": "v1",
			"project_name": "MNIST Example"
		},
		"name": "mnist-classifier",
		"predictors": [
			{
				"annotations": {
					"predictor_version": "v1"
				},
				"componentSpecs": [
					{
						"spec": {
							"containers": [
								{
									"image": "$dockerreposerving:$dockertagserving",
									"imagePullPolicy": "Always",
									"name": "mnist-classifier",
									"volumeMounts": [
										{
											"mountPath": "/data",
											"name": "persistent-storage"
										}
									]
								}
							],
							"terminationGracePeriodSeconds": 1,
							"volumes": [
								{
									"name": "persistent-storage",
									"persistentVolumeClaim": {
											"claimName": "$modelpvc"
									}
								}
							]
						}
					}
				],
				"graph": {
					"children": [],
					"endpoint": {
						"type": "REST"
					},
					"name": "mnist-classifier",
					"type": "MODEL"
				},
				"name": "mnist-classifier",
				"replicas": 1
			}
		]
	}
}    
""")
    seldon_serving_json = seldon_serving_json_template.substitute({
        'dockerreposerving':
        str(docker_repo_serving),
        'dockertagserving':
        str(docker_tag_serving),
        'modelpvc':
        modelvolop.outputs["name"]
    })

    seldon_deployment = json.loads(seldon_serving_json)

    serve = dsl.ResourceOp(
        name='serve',
        k8s_resource=seldon_deployment,
        success_condition='status.state == Available').after(train)
Example #55
0
            urllib.unquote(url).encode('ascii', 'ignore').decode('utf8'))
        string2 = "</a><div class='container'>%s</div></div>" % cgi.escape(
            endpoint[0])
        string2 = string2.replace(
            cgi.escape(endpoint[1]),
            "<span style='background-color:yellow'>%s</span>" %
            cgi.escape(endpoint[1]))

        html += string + string2

if args.output != 'cli':
    hide = os.dup(1)
    os.close(1)
    os.open(os.devnull, os.O_RDWR)
    try:
        s = Template(open('%s/template.html' % sys.path[0], 'r').read())

        text_file = open(args.output, "wb")
        text_file.write(s.substitute(content=html).encode('utf-8'))
        text_file.close()

        print("URL to access output: file://%s" % os.path.abspath(args.output))
        file = "file://%s" % os.path.abspath(args.output)
        if sys.platform == 'linux' or sys.platform == 'linux2':
            subprocess.call(["xdg-open", file])
        else:
            webbrowser.open(file)
    except Exception as e:
        print("Output can't be saved in %s \
            due to exception: %s" % (args.output, e))
    finally:
{
    viewer {
      login
      id
    }
  }
"""

createContributedRepoQuery = Template("""
query {
    user(login: "******") {
      repositoriesContributedTo(last: 100, includeUserRepositories: true) {
        nodes {
          isFork
          name
          owner {
            login
          }
        }
      }
    }
  }
""")

createCommittedDateQuery = Template("""
query {
    repository(owner: "$owner", name: "$name") {
      ref(qualifiedName: "master") {
        target {
          ... on Commit {
            history(first: 100, author: { id: "$id" }) {
Example #57
0
def WriteMacros(file, functions):
    template = Template(
        '#define $name gpu::GetVulkanFunctionPointers()->${name}Fn\n')
    WriteFunctions(file, functions, template)
Example #58
0
 def yt(self, irc, msg, args, query):
     """<search term>
     Search for YouTube videos
     """
     apikey = self.registryValue("developerKey")
     if not apikey:
         irc.reply("Error: You need to set an API key to use this plugin.")
         return
     template = self.registryValue("template", msg.channel)
     template = template.replace("{{", "$").replace("}}", "")
     template = Template(template)
     response = None
     title = None
     video_id = self.dosearch(query, msg.channel)
     if video_id:
         log.debug("YouTube: got video id: %s" % video_id)
         opts = {
             "part": "snippet,statistics,contentDetails",
             "maxResults": 1,
             "key": apikey,
             "id": video_id,
         }
         opts = utils.web.urlencode(opts)
         api_url = "https://www.googleapis.com/youtube/v3/videos?%s" % (opts)
         log.debug("YouTube: requesting %s" % (api_url))
         request = utils.web.getUrl(api_url).decode()
         response = json.loads(request)
         try:
             if response["pageInfo"]["totalResults"] > 0:
                 items = response["items"]
                 video = items[0]
                 snippet = video["snippet"]
                 statistics = video["statistics"]
                 view_count = 0
                 like_count = 0
                 dislike_count = 0
                 comment_count = 0
                 favorite_count = 0
                 if "viewCount" in statistics:
                     view_count = "{:,}".format(int(statistics["viewCount"]))
                 if "likeCount" in statistics:
                     like_count = "{:,}".format(int(statistics["likeCount"]))
                 if "dislikeCount" in statistics:
                     dislike_count = "{:,}".format(int(statistics["dislikeCount"]))
                 if "favoriteCount" in statistics:
                     favorite_count = "{:,}".format(int(statistics["favoriteCount"]))
                 if "commentCount" in statistics:
                     comment_count = "{:,}".format(int(statistics["commentCount"]))
                 channel_title = snippet["channelTitle"]
                 video_duration = video["contentDetails"]["duration"]
                 duration_seconds = self.get_total_seconds_from_duration(
                     video_duration
                 )
                 if duration_seconds > 0:
                     duration = self.get_duration_from_seconds(duration_seconds)
                 else:
                     duration = "LIVE"
                 results = {
                     "title": snippet["title"],
                     "duration": duration,
                     "views": view_count,
                     "likes": like_count,
                     "dislikes": dislike_count,
                     "comments": comment_count,
                     "favorites": favorite_count,
                     "uploader": channel_title,
                     "link": "https://youtu.be/%s" % (video_id),
                     "published": snippet["publishedAt"].split("T")[0],
                     "logo": self.get_youtube_logo(msg.channel),
                 }
                 title = template.safe_substitute(results)
             else:
                 log.debug("YouTube: video appears to be private; no results!")
         except:
             log.error(
                 "YouTube: Error parsing Youtube API JSON response: %s"
                 % (str(response))
             )
     else:
         irc.reply("No results found for: %s" % query)
         return
     if title:
         use_bold = self.registryValue("useBold", msg.channel)
         if use_bold:
             title = ircutils.bold(title)
         irc.reply(title, prefixNick=False)
Example #59
0
def WriteFunctionDeclarations(file, functions):
    template = Template('  VulkanFunction<PFN_${name}> ${name}Fn;\n')
    WriteFunctions(file, functions, template)
Example #60
0
babylon_unpacker_body_template = Template('''
<body>
   <canvas id="renderCanvas"></canvas>
   <script type="text/javascript">
      // Get the canvas element from our HTML below
      var canvas = document.querySelector("#renderCanvas");
      // Load the BABYLON 3D engine
      var engine = new BABYLON.Engine(canvas, true);

      var babylon_data = $babylon_data;
      var max_length = babylon_data['max_length'];

      // -------------------------------------------------------------
      // Here begins a function that we will 'call' just after it's built
      var createScene = function () {
        // This creates a basic Babylon Scene object (non-mesh)
        var scene = new BABYLON.Scene(engine);
        scene.useRightHandedSystem = true;
        scene.clearColor = new BABYLON.Color4(.9, .9, .9, .9);
      	var camera = new BABYLON.ArcRotateCamera("ArcRotateCamera",
                                              0, 0, 2*babylon_data['max_length'],
                                              new BABYLON.Vector3(babylon_data['center'][0],
                                                                  babylon_data['center'][1],
                                                                  babylon_data['center'][2]), scene);
      	camera.wheelPrecision=100./babylon_data['max_length']
      	camera.pinchPrecision=100./babylon_data['max_length']
      	camera.panningSensibility=800./babylon_data['max_length'];
      	camera.minZ=0.01*babylon_data['max_length'];
      	camera.attachControl(canvas);
      	camera.inertia = 0;
      	camera.panningInertia = 0;
      	// cam.mode = BABYLON.Camera.ORTHOGRAPHIC_CAMERA;
      	camera.upVector = new BABYLON.Vector3(0, 0, 1);
      	camera.lowerBetaLimit = null;
        camera.upperBetaLimit = null;
        camera.checkCollisions = false;
        camera.lowerRadiusLimit = 0.01*babylon_data['max_length'];
        scene.lastEdgewidthUpdate = Date.now();


        camera.onViewMatrixChangedObservable.add(() => {
            if ((Date.now() - scene.lastEdgewidthUpdate) > 1000){
                scene.lastEdgewidthUpdate = Date.now();
                for (mesh of scene.meshes){
                    var dist = BABYLON.Vector3.Distance(camera.position, mesh.position);
                    mesh.edgesWidth = dist*0.1;
                }
            }
         })

      	var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(-1, -1, -1), scene);
      	light1.intensity=0.5;
      	light1.specular = new BABYLON.Color3(0, 0, 0);

      	// var light2 = new BABYLON.SpotLight("Spot0", new BABYLON.Vector3(0, 30, -10), new BABYLON.Vector3(0, -1, 0), 0.8, 2, scene);
      	// light2.diffuse = new BABYLON.Color3(1, 1, 1);
      	// light2.specular = new BABYLON.Color3(1, 1, 1);
        var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 0, 0), scene);
        light2.specular = new BABYLON.Color3(0, 0, 0);
        light2.intensity = 0.3;
        light2.parent = camera;

        var light3 = new BABYLON.HemisphericLight("light3", new BABYLON.Vector3(1, 1, 1), scene);
        light3.specular = new BABYLON.Color3(0, 0, 0);
        light3.intensity = 0.50;


        var showAxis = function (size) {
            var makeTextPlane = function (text, color, size) {
                var dynamicTexture = new BABYLON.DynamicTexture("DynamicTexture", 50, scene, true);
                dynamicTexture.hasAlpha = true;
                dynamicTexture.drawText(text, 5, 40, "bold 36px Arial", color, "transparent", true);
                var plane = new BABYLON.Mesh.CreatePlane("TextPlane", size, scene, true);
                plane.material = new BABYLON.StandardMaterial("TextPlaneMaterial", scene);
                plane.material.backFaceCulling = false;
                plane.material.specularColor = new BABYLON.Color3(0, 0, 0);
                plane.material.diffuseTexture = dynamicTexture;
                return plane;
            };
            var axisX = BABYLON.Mesh.CreateLines("axisX", [
                new BABYLON.Vector3.Zero(), new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, 0.05 * size, 0),
                new BABYLON.Vector3(size, 0, 0), new BABYLON.Vector3(size * 0.95, -0.05 * size, 0)
            ], scene);
            axisX.color = new BABYLON.Color3(1, 0, 0);
            var xChar = makeTextPlane("X", "red", size / 10);
            xChar.position = new BABYLON.Vector3(0.9 * size, -0.05 * size, 0);
            var axisY = BABYLON.Mesh.CreateLines("axisY", [
                new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3(-0.05 * size, size * 0.95, 0),
                new BABYLON.Vector3(0, size, 0), new BABYLON.Vector3(0.05 * size, size * 0.95, 0)
            ], scene);
            axisY.color = new BABYLON.Color3(0, 1, 0);
            var yChar = makeTextPlane("Y", "green", size / 10);
            yChar.position = new BABYLON.Vector3(0, 0.9 * size, -0.05 * size);
            var axisZ = BABYLON.Mesh.CreateLines("axisZ", [
                new BABYLON.Vector3.Zero(), new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3(0, -0.05 * size, size * 0.95),
                new BABYLON.Vector3(0, 0, size), new BABYLON.Vector3(0, 0.05 * size, size * 0.95)
            ], scene);
            axisZ.color = new BABYLON.Color3(0, 0, 1);
            var zChar = makeTextPlane("Z", "blue", size / 10);
            zChar.position = new BABYLON.Vector3(0, 0.05 * size, 0.9 * size);
        };

        showAxis(1);


        var meshes = []
        for (let mesh_data of babylon_data['meshes']){
          var mesh = new BABYLON.Mesh(mesh_data['name'], scene);
          meshes.push(mesh);

          var normals = [];
          var vertexData = new BABYLON.VertexData();
          BABYLON.VertexData.ComputeNormals(mesh_data['positions'], mesh_data['indices'], normals);

          vertexData.positions = mesh_data['positions'];
          vertexData.indices = mesh_data['indices'];
          vertexData.normals = normals;
          vertexData.applyToMesh(mesh);
          mesh.enableEdgesRendering(0.9);
          mesh.edgesWidth = max_length*0.1;
          mesh.edgesColor = new BABYLON.Color4(0, 0, 0, 0.6);
          var mat = new BABYLON.StandardMaterial("material", scene);
          // mat.diffuseColor = BABYLON.Color3.Green();
          // mat.specularColor = new BABYLON.Color3(0.5, 0.6, 0.87);
          // mat.emissiveColor = new BABYLON.Color3(1, 1, 1);
          // mat.ambientColor = new BABYLON.Color3(0.23, 0.98, 0.53);
          mat.backFaceCulling = false;
          mesh.material = mat;
          mat.diffuseColor = new BABYLON.Color3(mesh_data['color'][0],
                                                mesh_data['color'][1],
                                                mesh_data['color'][2]);
          mat.alpha = mesh_data['alpha'];

        }


        if (babylon_data['steps']){


          var n_primitives = babylon_data['meshes'].length;
          var n_steps = babylon_data['steps'].length;

          var advancedTexture = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("UI");
          var animation_stopped = false;

          var buttonHeightInPixels = 50;
          var buttonWidthInPixels = 150;

          // Set container and its size and position
          let heightInPixels = buttonHeightInPixels*7;
          let widthInPixels = buttonWidthInPixels;

          let topInPixels = -canvas.height/2 + heightInPixels/2;
          let leftInPixels = -canvas.width/2 + widthInPixels/2;

          var buttonsContainer = new BABYLON.GUI.StackPanel("buttons_panel");
          buttonsContainer.background = "#263238";
          buttonsContainer.color = "white";
          buttonsContainer.height = ""+heightInPixels+"px";
          buttonsContainer.width = ""+widthInPixels+"px";
          buttonsContainer.top = ""+topInPixels+"px";
          buttonsContainer.left = ""+leftInPixels+"px";

          var step_number_label = new BABYLON.GUI.TextBlock();
          step_number_label.text = "Step n°1/"+n_steps;
          step_number_label.width = ""+buttonWidthInPixels+"px";
          step_number_label.height = ""+buttonHeightInPixels+"px";
          buttonsContainer.addControl(step_number_label);


          var start_stop_button = BABYLON.GUI.Button.CreateSimpleButton("animation", "Stop/Resume animation");
          start_stop_button.width = ""+buttonWidthInPixels+"px";
          start_stop_button.height = ""+buttonHeightInPixels+"px";
          start_stop_button.onPointerUpObservable.add(function(){animation_stopped = !animation_stopped});
          buttonsContainer.addControl(start_stop_button);

          var first_step_button = BABYLON.GUI.Button.CreateSimpleButton("animation", "First step");
          first_step_button.width = ""+buttonWidthInPixels+"px";
          first_step_button.height = ""+buttonHeightInPixels+"px";
          first_step_button.onPointerUpObservable.add(function(){animation_stopped=true; iframe=0; showStep(Math.floor(0))});
          buttonsContainer.addControl(first_step_button);

          var previous_step_button = BABYLON.GUI.Button.CreateSimpleButton("animation", "Previous step");
          previous_step_button.width = ""+buttonWidthInPixels+"px";
          previous_step_button.height = ""+buttonHeightInPixels+"px";
          previous_step_button.onPointerUpObservable.add(function(){animation_stopped=true; iframe-=10; showStep(Math.floor(iframe/10))});
          buttonsContainer.addControl(previous_step_button);

          var next_step_button = BABYLON.GUI.Button.CreateSimpleButton("animation", "Next step");
          next_step_button.width = ""+buttonWidthInPixels+"px";
          next_step_button.height = ""+buttonHeightInPixels+"px";
          next_step_button.onPointerUpObservable.add(function(){animation_stopped=true; iframe+=10; showStep(Math.floor(iframe/10))});
          buttonsContainer.addControl(next_step_button);

          var last_step_button = BABYLON.GUI.Button.CreateSimpleButton("animation", "Last step");
          last_step_button.width = ""+buttonWidthInPixels+"px";
          last_step_button.height = ""+buttonHeightInPixels+"px";
          last_step_button.onPointerUpObservable.add(function(){animation_stopped=true; iframe=10*(n_steps-1); showStep(Math.floor(iframe/10))});
          buttonsContainer.addControl(last_step_button);

          var step_label = new BABYLON.GUI.TextBlock();
          step_label.text = "Label";
          step_label.width = ""+buttonWidthInPixels+"px";
          step_label.height = ""+buttonHeightInPixels+"px";
          buttonsContainer.addControl(step_label);

          advancedTexture.addControl(buttonsContainer);


          var showStep = function (istep){
            // Parts position update
            step_number_label.text = "Step n°"+(istep+1)+"/"+n_steps;
            for(let iprimitive=0; iprimitive<n_primitives; iprimitive++){
                mesh = meshes[iprimitive];
                mesh.position = new BABYLON.Vector3(babylon_data['steps'][istep][iprimitive]['position'][0],
                                                    babylon_data['steps'][istep][iprimitive]['position'][1],
                                                    babylon_data['steps'][istep][iprimitive]['position'][2]);


                mesh.rotation = BABYLON.Vector3.RotationFromAxis(
                  new BABYLON.Vector3(babylon_data['steps'][istep][iprimitive]['orientations'][0][0],
                                      babylon_data['steps'][istep][iprimitive]['orientations'][0][1],
                                      babylon_data['steps'][istep][iprimitive]['orientations'][0][2]),
                  new BABYLON.Vector3(babylon_data['steps'][istep][iprimitive]['orientations'][1][0],
                                      babylon_data['steps'][istep][iprimitive]['orientations'][1][1],
                                      babylon_data['steps'][istep][iprimitive]['orientations'][1][2]),
                  new BABYLON.Vector3(babylon_data['steps'][istep][iprimitive]['orientations'][2][0],
                                      babylon_data['steps'][istep][iprimitive]['orientations'][2][1],
                                      babylon_data['steps'][istep][iprimitive]['orientations'][2][2]));
            }

            if ('label' in babylon_data['steps'][istep]){
              step_label.text = babylon_data['steps'][istep]['label'];
            }
            else{
              step_label.text = "";
            }

          }

        var iframe = 0;

        scene.registerBeforeRender(function () {
                  if (!animation_stopped){
                    if (iframe % 10 == 0){
                      var istep = iframe / 10;

                      showStep(istep);


                    }
                    iframe++;
                    iframe = iframe % (n_steps*10);
                  }

          });
          }


    	return scene;	  };

      var scene = createScene();

      // Register a render loop to repeatedly render the scene
      engine.runRenderLoop(function () {
         scene.render();
      });
      // Watch for browser/canvas resize events
      window.addEventListener("resize", function () {
         engine.resize();
      });

      //scene.debugLayer.show();

   </script>
</body>

</html>

''')