Ejemplo n.º 1
0
def render_template(filename, out_dir, **context_vars):
    assert (filename.endswith('.mako'))
    template_filepath = os.path.join(os.path.dirname(__file__), filename)
    out_filepath = os.path.join(out_dir, os.path.splitext(filename)[0])

    warning = 'DO NOT EDIT! Script {0!r} generated this file from {1!r}'
    warning = warning.format(os.path.basename(__file__), filename)

    fake_alignment = re.compile(r'\.*\n\.+', flags=re.MULTILINE)
    fake_tab = re.compile(r'>-------')

    def fake_whitespace(proto_text):
        if debug:
            print('fake whitespace: before: {0!r}'.format(proto_text))
        text = unicode(proto_text)
        text = fake_alignment.sub('', text)
        text = fake_tab.sub('\t', text)
        if debug:
            print('fake whitespace:  after: {0!r}'.format(text))
        return text

    with open(out_filepath, 'w') as out_file:
        template = mako.template.Template(filename=template_filepath,
                                          strict_undefined=True)
        ctx = mako.runtime.Context(buffer=out_file,
                                   warning=warning,
                                   fake_whitespace=fake_whitespace,
                                   **context_vars)
        template.render_context(ctx)
Ejemplo n.º 2
0
def render_template(filename, out_dir, **context_vars):
    assert(filename.endswith('.mako'))
    template_filepath = os.path.join(os.path.dirname(__file__), filename)
    out_filepath = os.path.join(out_dir, os.path.splitext(filename)[0])

    warning = 'DO NOT EDIT! Script {0!r} generated this file from {1!r}'
    warning = warning.format(os.path.basename(__file__), filename)

    fake_alignment = re.compile(r'\.*\n\.+', flags=re.MULTILINE)
    fake_tab = re.compile(r'>-------')

    def fake_whitespace(proto_text):
        if debug:
            print('fake whitespace: before: {0!r}'.format(proto_text))
        text = unicode(proto_text)
        text = fake_alignment.sub('', text)
        text = fake_tab.sub('\t', text)
        if debug:
            print('fake whitespace:  after: {0!r}'.format(text))
        return text

    with open(out_filepath, 'w') as out_file:
        template = mako.template.Template(
            filename=template_filepath,
            strict_undefined=True)
        ctx = mako.runtime.Context(
            buffer=out_file,
            warning=warning,
            fake_whitespace=fake_whitespace,
            **context_vars)
        template.render_context(ctx)
 def render( environ, start_response ):
     response_write = start_response( self.response.wsgi_status(), self.response.wsgi_headeritems() )
     class StreamBuffer( object ):
         def write( self, d ):
             response_write( d.encode( 'utf-8' ) )
     buffer = StreamBuffer()
     context = mako.runtime.Context( buffer, **data )
     template.render_context( context )
     return []
Ejemplo n.º 4
0
 def render( environ, start_response ):
     response_write = start_response( self.response.wsgi_status(), self.response.wsgi_headeritems() )
     class StreamBuffer( object ):
         def write( self, d ):
             response_write( d.encode( 'utf-8' ) )
     buffer = StreamBuffer()
     context = mako.runtime.Context( buffer, **data )
     template.render_context( context )
     return []
Ejemplo n.º 5
0
def _generate_template(output_dir, filename, lookup, template):
    output = file(os.path.join(output_dir, filename), 'w')
    try:
        context = mako.runtime.Context(output, **lookup)
        template.render_context(context)
    except:
        raise Exception(mako.exceptions.text_error_template().render())
    finally:
        output.close()
Ejemplo n.º 6
0
    def save_data_file(self, out, start_time):
        """
        Save the parsed CSV to TCX
        """
        if self.do_interpolate:
            self.ride.interpolate()

        if self.do_physics:
            self.ride.model_distance(self.physics_mass)

        template = mako.template.Template(
            xml_templates.training_center_database, default_filters=[])
        now = self._format_time(start_time)

        header = dict(id=now,
                      start_time=now,
                      total_time=str(self.ride.header.time),
                      distance_meters=str(float(self.ride.header.distance)),
                      average_heart_rate=str(self.ride.header.average_hr),
                      maximum_heart_rate=str(self.ride.header.max_hr),
                      sport=self.sport,
                      version=version.version)

        secs_per_sample = max(self.ride.delta(), 1)
        points = []

        for i in range(0, self.ride.count()):
            delta_time = start_time + datetime.timedelta(seconds=i *
                                                         int(secs_per_sample))
            power = float(self.ride.power[i]) * self.power_fudge

            point = dict(time=self._format_time(delta_time),
                         bpm=self.ride.hr[i],
                         cadence=self.ride.rpm[i],
                         distance_meters=("%.5f" %
                                          float(self.ride.distance[i])),
                         watts=str(int(power)))

            points.append(point)

        context = mako.runtime.Context(out, points=points, header=header)
        template.render_context(context)