def _proceed(self):
		d_conf = self._agent_data.get_all_conf()
		env = Environment(loader=FileSystemLoader('./templates'), extensions=['jinja2.ext.i18n'])
		env.list_templates(extensions=['tpl'])
		OTemplate = env.get_template('im_agent_conf_freeradius.tpl')
		OTemplate.render(d_conf)
		fd = open(conf_file_dest, 'w')
		fd.write(OTemplate.render(d_conf))
		raise StopApplication('Proceed conf file creation')
Ejemplo n.º 2
0
def main(argv):
    env = Environment(
        keep_trailing_newline=True,
        loader=FileSystemLoader(TEMPLATE_FOLDER),
        trim_blocks=True,
        lstrip_blocks=True,
    )
    templates = env.list_templates()
    for module in MODULES:
        for template_file in templates:
            template = env.get_template(template_file)
            if template_file.endswith(".tf.tmpl"):
                template_file = template_file.replace(".tf.tmpl", ".tf")
            rendered = template.render(
                module.template_options(BASE_TEMPLATE_OPTIONS))
            with open(os.path.join(module.path, template_file), "w") as f:
                f.write(rendered)
                if template_file.endswith(".sh"):
                    os.chmod(os.path.join(module.path, template_file), 0o755)
        # Call terraform fmt for module folder
        print("terraform fmt %s" % module.path)
        subprocess.call(["terraform", "fmt", "-write=true", module.path],
                        stdout=DEVNULL_FILE,
                        stderr=subprocess.STDOUT)

    DEVNULL_FILE.close()
Ejemplo n.º 3
0
class StaticPagesRenderer:
    """
        static ファイル群をレンダリングする。
    """
    def __init__(
        self,
        fileStorage: storage.SupportStorage,
        basedir: str,
    ):
        self.jinja2_env = Environment(
            loader=PackageLoader('chalicelib', 'templates'),
            autoescape=select_autoescape(['html']),
        )
        self.fileStorage = fileStorage
        self.basedir = basedir

    def render_all(self):
        templates = self.jinja2_env.list_templates()
        static_templates = [t for t in templates if t.startswith('static/')]
        basepath = self.fileStorage.path_object(self.basedir)

        for template_path in static_templates:
            template = self.jinja2_env.get_template(template_path)
            html = template.render()
            filename = Path(template_path).stem + '.html'
            outputpath = str(basepath / filename)
            logger.info('generating a static file "%s"', outputpath)
            stream = self.fileStorage.get_output_stream(outputpath)
            stream.write(html.encode('UTF-8'))
            self.fileStorage.close_output_stream(stream)
Ejemplo n.º 4
0
 def generate(self, module):
     logging.info('Generating project...')
     env = Environment(loader=PackageLoader(module))
     for template_name in env.list_templates():
         template = env.get_template(template_name)
         with open(template_name.replace('.jinja', ''), 'w') as f:
             f.write(template.render(self.__config))
Ejemplo n.º 5
0
def main(argv):
    modules = json.loads(argv[1])
    for module in modules:
        template_folder = module["template_folder"]
        module = Module(module["path"], module["options"])
        env = Environment(
            keep_trailing_newline=True,
            loader=FileSystemLoader(template_folder),
            trim_blocks=True,
            lstrip_blocks=True,
        )
        templates = env.list_templates()

        for template_file in templates:
            template = env.get_template(template_file)
            if template_file.endswith(".tf.tmpl"):
                template_file = template_file.replace(".tf.tmpl", ".tf")
            rendered = template.render(
                module.template_options({
                    'autogeneration_note':
                    AUTOGEN_NOTE.format(folder=template_folder)
                }))
            with open(os.path.join(module.path, template_file), "w") as f:
                f.write(rendered)
                if template_file.endswith(".tf"):
                    subprocess.call([
                        "terraform", "fmt", "-write=true",
                        os.path.join(module.path, template_file)
                    ],
                                    stdout=DEVNULL_FILE,
                                    stderr=subprocess.STDOUT)
                if template_file.endswith(".sh"):
                    os.chmod(os.path.join(module.path, template_file), 0o755)
    DEVNULL_FILE.close()
Ejemplo n.º 6
0
def render_modules(template_folder, modules_list):
    env = Environment(
        keep_trailing_newline=True,
        loader=FileSystemLoader(template_folder),
        trim_blocks=True,
        lstrip_blocks=True,
    )
    templates = env.list_templates()
    for module in modules_list:
        for template_file in templates:
            template = env.get_template(template_file)
            if template_file.endswith(".tf.tmpl"):
                template_file = template_file.replace(".tf.tmpl", ".tf")
            rendered = template.render(
                module.template_options(
                    {'autogeneration_note': AUTOGEN_NOTE + template_folder}
                )
            )
            with open(os.path.join(module.path, template_file), "w") as f:
                f.write(rendered)
                if template_file.endswith(".tf"):
                    subprocess.call(
                        [
                            "terraform",
                            "fmt",
                            "-write=true",
                            os.path.join(module.path, template_file)
                        ],
                        stdout=DEVNULL_FILE,
                        stderr=subprocess.STDOUT
                    )
                if template_file.endswith(".sh"):
                    os.chmod(os.path.join(module.path, template_file), 0o755)
Ejemplo n.º 7
0
    def k8s_create_manifest(self):
        '''
        This function creates the k8s manifests, it takes in object requirements and writes them into templated files under a new directory

        :self.doc: object requirements in yaml format
        :return: None
        '''

        # Create output directory
        directory = os.getcwd() + '/' + self.doc['name'] + '/k8s/'

        try:
            os.makedirs(directory)
        except OSError:
            print (f"Creation of the directory {directory} failed")
        else:
            print (f"Successfully created the directory {directory}")

        # Render files
        file_loader = PackageLoader('creator', 'templates/')
        env = Environment(loader=file_loader)

        for template_name in env.list_templates():
            template = env.get_template(template_name)

            rendered_output = template.render(name=self.doc['name'], hostname=self.doc['hostname'], ingress=self.doc['ingress'], registry=self.doc['registry'])

            output = directory + '/' + template_name
            with open(output, 'w') as f:
                f.write(rendered_output)
Ejemplo n.º 8
0
def generate_html(sitedir, conf):
    """
    Compile Jinja2 templates in `sourcedir` to html files saved in
    `targetdir` preserving the directory structure.
    """
    sourcedir, targetdir = conf['sourcedir'], conf['targetdir']

    def templates_filter(filename):
        """
        Function for filtering the templates as per the 
        `skip_patterns` specified in the config
        """
        return filename.endswith('.html') and \
            not reduce(lambda x, y: x or fnmatch.fnmatch(filename, y),
                       conf['skip_templates'],
                       False)

    env = Environment(loader=FileSystemLoader(conf['sourcedir']))    
    templates = env.list_templates(filter_func=templates_filter)
    for t in templates:
        filepath = os.path.join(targetdir,
                                *tuple(t.split(os.path.sep)))
        dirname = os.path.dirname(filepath)
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        with open(filepath, 'w') as f:
            f.writelines(env.get_template(t).generate())
            logger.info("Generated %s" % filepath)
Ejemplo n.º 9
0
    def run_command(self, args):
        # Parsing input arguments
        if args.output_dir is None:
            args.output_dir = getcwd()
        pipeline = dict()
        pipeline['title'] = args.name
        pipeline['module_name'] = pipeline['title'].replace(' ', '_').lower()
        pipeline['class_name'] = pipeline['title'].replace(' ', '')
        pipeline['command_name'] = pipeline['title'].replace(' ', '-').lower()
        pipeline['dir'] = join(args.output_dir, pipeline['module_name'])

        from jinja2 import Environment, PackageLoader, select_autoescape
        env = Environment(loader=PackageLoader(
            'clinica', 'resources/templates/pipeline_template'),
                          autoescape=select_autoescape(['py']))

        if not isdir(pipeline['dir']):
            mkdir(pipeline['dir'])

        for template_file in env.list_templates(extensions='j2'):
            template = env.get_template(template_file)
            rendered_file, template_ext = splitext(template_file)
            rendered_filename, rendered_file_ext = splitext(rendered_file)
            if rendered_file_ext == '.py' and rendered_filename != '__init__':
                path_to_write = join(
                    pipeline['dir'],
                    pipeline['module_name'] + '_' + rendered_file)
            else:
                path_to_write = join(pipeline['dir'], rendered_file)
            with open(path_to_write, 'w+') as file_to_write:
                print("Generating template %s" % file_to_write.name)
                file_to_write.write(template.render(pipeline=pipeline))
Ejemplo n.º 10
0
    def run_command(self, args):
        if args.output_dir is None:
            args.output_dir = getcwd()
        pipeline = dict()
        pipeline["title"] = args.name
        pipeline["module_name"] = pipeline["title"].replace(" ", "_").lower()
        pipeline["class_name"] = pipeline["title"].replace(" ", "")
        pipeline["command_name"] = pipeline["title"].replace(" ", "-").lower()
        pipeline["dir"] = join(args.output_dir, pipeline["module_name"])

        from jinja2 import Environment, PackageLoader, select_autoescape

        env = Environment(
            loader=PackageLoader("clinica",
                                 "resources/templates/pipeline_template"),
            autoescape=select_autoescape(["py"]),
        )

        if not isdir(pipeline["dir"]):
            mkdir(pipeline["dir"])

        for template_file in env.list_templates(extensions="j2"):
            template = env.get_template(template_file)
            rendered_file, template_ext = splitext(template_file)
            rendered_filename, rendered_file_ext = splitext(rendered_file)
            if rendered_file_ext == ".py" and rendered_filename != "__init__":
                path_to_write = join(
                    pipeline["dir"],
                    pipeline["module_name"] + "_" + rendered_file)
            else:
                path_to_write = join(pipeline["dir"], rendered_file)
            with open(path_to_write, "w+") as file_to_write:
                print(f"Generating template {file_to_write.name}")
                file_to_write.write(template.render(pipeline=pipeline))
Ejemplo n.º 11
0
def build(args):
    print('.')

    working_dir = os.path.dirname(os.path.realpath(__file__))

    env = Environment(loader=FileSystemLoader('templates'),
                      autoescape=select_autoescape(['html']))
    for filepath in env.list_templates():
        try:
            template = env.get_template(filepath)

            try:
                Path(f'build/{"/".join(filepath.split("/")[:-1])}').mkdir(
                    parents=True, exist_ok=True)
            except:
                pass

            template.stream(name='foo').dump(f'build/{filepath}')

        except TemplateSyntaxError as e:
            print(f'Could not build {filepath}:\n{e}')

        except UnicodeDecodeError:
            try:
                Path(f'build/{"/".join(filepath.split("/")[:-1])}').mkdir(
                    parents=True, exist_ok=True)
            except:
                pass

            os.system(
                f'cp {working_dir}/templates/{filepath} {working_dir}/build/{filepath}'
            )
Ejemplo n.º 12
0
    def render_item(self, target, resource, item, **kwargs):
        if resource not in self.resources:
            raise RenderError("No resource {0}".format(resource))

        templates_path = os.path.join(self.resources_path, resource)
        J2Env = Environment(loader=FileSystemLoader(templates_path))

        target_params = self.param_tree.get_target_params(target=target, )
        item_params = self.param_tree.get_item_params(
            target=target,
            resource=resource,
            item=item,
        )

        item_params.update(self.system_params)

        rendered_data = []

        for template in J2Env.list_templates(extensions=self.TMPL_EXT):
            output_filename = os.path.join(
                resource,
                os.path.splitext(item)[0],
                os.path.splitext(template)[0],
            )
            output_data = J2Env.get_template(template).render(
                target=target_params, item=item_params, **kwargs)

            rendered_data.append((output_filename, output_data))

        return rendered_data
Ejemplo n.º 13
0
def get_env(templates_dir):
    # https://stackoverflow.com/questions/9767585/insert-static-files-literally-into-jinja-templates-without-parsing-them
    def include_file(path):
        path = os.path.join(templates_dir, '../', path)
        output = []
        for file_path in sorted(glob.glob(path)):
            with open(file_path, 'r') as f:
                output.append(f.read())
        return '\n'.join(output)

    def list_files(path):
        path = os.path.join(templates_dir, '../', path)
        if os.path.isdir(path):
            files = [
                os.path.join(path, f) for f in os.listdir(path)
                if os.path.isfile(os.path.join(path, f))
            ]
        else:
            files = glob.glob(path)
        return sorted(files)

    env = Environment(undefined=StrictUndefined,
                      loader=FileSystemLoader([templates_dir]))

    env.filters['b64decode'] = b64decode
    env.filters['b64encode'] = b64encode
    env.filters['hash_sha256'] = hash_sha256
    env.filters['to_yaml'] = to_yaml
    env.globals['include_file'] = include_file
    env.globals['list_files'] = list_files

    log.debug('Available templates in path {}: {}'.format(
        templates_dir, env.list_templates()))
    return env
Ejemplo n.º 14
0
def get_report_template(report_version: str,
                        default_template: str = "index.md"):
    """
    Retrieve and return the most appropriate template based on the
    chaostoolkit-lib version used when running the experiment.
    """
    env = Environment(loader=PackageLoader('chaosreport', 'template'))
    env.filters["pretty_date"] = lambda d: str(
        maya.MayaDT.from_datetime(dateparser.parse(d)))
    env.globals["pretty_duration"] = lambda d0, d1: date.delta(
        dateparser.parse(d0), dateparser.parse(d1), words=False)[0]

    if not report_version:
        return env.get_template(default_template)

    templates = []
    for name in env.list_templates(["md"]):
        if name in ["index.md", "header.md"]:
            continue

        _, _, v = name.split('_')
        v, _ = v.rsplit('.md', 1)
        templates.append((semver.parse_version_info(v), name))

    templates = sorted(templates, key=lambda vinfo: vinfo[0])

    report_version = report_version.replace('rc1', '-rc1')
    for (vinfo, name) in templates:
        if semver.match(
                report_version,
                "<={v}".format(v=semver.format_version(**vinfo._asdict()))):
            return env.get_template(name)

    # none of the old versions matched, we can use the latest template
    return env.get_template(default_template)
Ejemplo n.º 15
0
def creator(project_type, project_path):
    click.echo(project_type)
    click.echo(project_path)
    module_path = os.path.join(os.path.dirname(flask_init.__file__),
                               project_type)

    # Initialize the jinja2 environment variable
    environment = Environment(loader=PackageLoader('flask_init', project_type))

    for temp in environment.list_templates():

        template = environment.get_template(temp)

        # Apply context
        parsed_template = template.render()

        # Remove tpl in the name
        file_name = template.name.split('-tpl')[0]
        distination_file = os.path.join(project_path, file_name)

        # Create folder if no folder in that distination folder
        # https://stackoverflow.com/a/12517490/3762142
        if not os.path.exists(os.path.dirname(distination_file)):
            os.makedirs(os.path.dirname(distination_file))

        with open(distination_file, "w") as f:
            f.write(parsed_template + '\n')
Ejemplo n.º 16
0
def main(argv):
    try:
        config_file = argv[1]
    except IndexError as e:
        config_file = os.path.join(ROOT_PATH, 'examples', 'config.yaml')

    with open(config_file) as io:
        config_file = yaml.load(io)

    env = Environment(loader=FileSystemLoader(os.path.join(
        ROOT_PATH, 'templates'),
                                              encoding='utf8'), )
    target_path = config_file['target_dir']
    target_path = os.path.expanduser(target_path)

    for t in env.list_templates():
        try:
            template = env.get_template(t)
            content = template.render(config_file)
            target_dir = os.path.join(target_path, os.path.dirname(t))
            target_file = os.path.join(target_path, t)
            print(target_dir)
            print(target_file)
            pathlib.Path(target_dir).mkdir(parents=True, exist_ok=True)
            with open(target_file, 'w') as io:
                io.write(content)
        except UnicodeDecodeError as e:
            print("Load template error: %s" % t)
Ejemplo n.º 17
0
def build_packaging(package_name: str, output_folder: str, build_conf: bool = False) -> None:
    _LOGGER.info("Building template %s", package_name)
    package_folder = Path(output_folder) / Path(package_name)

    if build_conf:
        build_default_conf(package_folder, package_name)

    conf = read_conf(package_folder)
    if not conf:
        raise ValueError("Create a {} file before calling this script".format(package_folder / CONF_NAME))

    env = Environment(
        loader=PackageLoader('packaging_tools', 'templates'),
        keep_trailing_newline=True
    )
    conf = build_config(conf)

    for template_name in env.list_templates():
        future_filepath = Path(output_folder) / package_name / template_name

        # Might decide to make it more generic one day
        if template_name == "HISTORY.rst" and future_filepath.exists():
            _LOGGER.info("Skipping HISTORY.txt template, since a previous one was found")
            # Never overwirte the ChangeLog
            continue

        template = env.get_template(template_name)
        result = template.render(**conf)

        with open(Path(output_folder) / package_name / template_name, "w") as fd:
            fd.write(result)

    _LOGGER.info("Template done %s", package_name)
Ejemplo n.º 18
0
class testBearcart(object):
    """Test Bearcart Chart class"""

    @classmethod
    def setup_class(self):
        """Bearcart test data and template setup"""
        import pandas.io.data as web

        all_data = {}
        for ticker in ["AAPL", "GOOG"]:
            all_data[ticker] = web.get_data_yahoo(ticker, "4/1/2013", "5/1/2013")
        self.price = pd.DataFrame({tic: data["Adj Close"] for tic, data in all_data.iteritems()})
        self.templates = Environment(loader=FileSystemLoader("templates"))

    def test_init(self):
        """Test bearcart chart creation"""

        # Test defaults
        chart = bearcart.Chart()
        assert chart.height == 400
        assert chart.width == 750
        assert chart.renderer == "line"
        temps = {x.split(".")[0]: x.split(".")[1] for x in self.templates.list_templates()}
        for key, value in chart.template_vars.iteritems():
            template = self.templates.get_template(".".join([key, temps[key]]))
            assert value == template.render()

    def test_params(self):
        """Test bearcart chart parameters and component removal"""

        # Test params
        chart = bearcart.Chart(width=500, height=1000, plt_type="area")
        assert chart.height == 1000
        assert chart.width == 500
        assert chart.renderer == "area"

        # Test component removal
        chart = bearcart.Chart(x_axis=False, legend=False)
        nt.assert_list_equal(chart.template_vars.keys(), ["y_axis", "hover"])

    def test_data(self):
        """Test bearcart data import and transformation"""

        # Test data
        series = self.price["AAPL"]
        chart = bearcart.Chart(series)
        assert_almost_equal(chart.raw_data, series)
        assert chart.json_data[0]["name"] == "AAPL"
        assert len(series) == len(chart.json_data[0]["data"])
        nt.eq_(time.mktime(series.index[0].timetuple()), chart.json_data[0]["data"][0]["x"])

    def test_non_timeseries(self):
        """Test non timeseries data"""
        tabular_data = [random.randint(10, 100) for x in range(0, 25, 1)]
        df = pd.DataFrame({"Data 1": tabular_data})

        chart = bearcart.Chart(df, x_time=False)
        nt.assert_in("x_axis_num", chart.template_vars)
        nt.assert_in("xFormatter: function(x)" "{return Math.floor(x / 10) * 10}\n}", chart.template_vars["hover"])
Ejemplo n.º 19
0
def build(path):
    """Main function of the module. This is what `ginpar build` calls.

    Parameters
    ----------
    build_path : str
        Path of the build.
    """

    _SITE_FILE = "config.yaml"
    _SITE = read_config(_SITE_FILE)

    _THEME = _SITE["theme"].split("/")[1]
    _THEME_PATH = os.path.join("themes", _THEME)

    _TEMPLATES_PATH = os.path.join(_THEME_PATH, "templates")
    _SKETCHES_PATH = _SITE["content_path"]

    _jinja_env = Environment(loader=FileSystemLoader(_TEMPLATES_PATH), trim_blocks=True)

    _jinja_env.filters["unkebab"] = unkebab
    _jinja_env.filters["getattrs"] = dict_to_attrs

    if not os.path.isdir(_THEME_PATH):
        clone_repo(_SITE["theme"], _THEME_PATH)
        delete_git_files(_THEME_PATH)

    input_templates = list(
        map(
            lambda t: _jinja_env.get_template(t),
            filter(
                lambda t: t.startswith(os.path.join("form", "inputs")),
                _jinja_env.list_templates(),
            ),
        )
    )

    create_publishing_directory(path)
    echo(f"Building in `{os.path.abspath(path)}`")

    copy_theme(path, _THEME)
    echo(f"Building using theme `{_THEME}`")

    ## Create the sketches list
    sketches = list(get_sketches(_SKETCHES_PATH))
    echo(f"Found {len(sketches)} sketch(es)")
    sketches.sort(key=lambda a: a["data"]["date"], reverse=True)

    render_index(path, sketches, _SITE, _jinja_env.get_template("index.html"))
    echo("Building main page")

    echo("Building sketches:")
    for sketch in sketches:
        echo(f"  Building {sketch['name']}")
        render_sketch_page(
            path, sketch, _SITE, _jinja_env.get_template("sketch.html"), input_templates
        )

    success("Success.")
Ejemplo n.º 20
0
class PlwTemplate(object):
    # INIT
    def __init__(self, tem, sta):
        self.nbtemplates = 0
        self.templates_path = tem
        self.load_templates()
        logger.info("template_path : %s, load %d templates " %
                    (self.templates_path, self.nbtemplates))
        logger.debug("templates defined : " +
                     str(self.templates_env.list_templates()))
        self.set_staticpath(sta)

    # SET STATIC PATH
    def set_staticpath(self, sta):
        self.static_path = sta
        if not self.static_path.endswith('\\'):
            self.static_path = self.static_path + '\\'
        logger.debug("#")
        logger.debug("# static_path : " + self.static_path)

    # IS_VALID
    def is_valid(self):
        if not self.nbtemplates:
            logger.critical(
                "ERROR : Please put JINJA templates files in directory : " +
                self.templates_path)
            return False
        return True

    # LOAD_TEMPLATES
    def load_templates(self):
        #print("load_templates")
        #print(self.templates_path)

        self.templates_env = Environment(
            loader=FileSystemLoader(self.templates_path),
            autoescape=select_autoescape(['html', 'xml']),
            trim_blocks=True,
            lstrip_blocks=True)
        if not self.templates_env.list_templates():
            return False

        self.nbtemplates = len(self.templates_env.list_templates())
        return True
def build_package(**kwargs) -> None:
    # prepare template render parameters
    output_folder = kwargs.get("output_folder")
    package_name = kwargs.get("package_name")
    swagger_readme_output = '../' + package_name.replace('-', '/')
    kwargs['swagger_readme_output'] = swagger_readme_output
    namespace = package_name.replace('-', '.')
    kwargs['namespace'] = namespace
    kwargs['date'] = time.strftime('%Y-%m-%d', time.localtime())
    folder_list = package_name.split('-')
    kwargs['folder_first'] = folder_list[0]
    kwargs['folder_second'] = folder_list[1]
    kwargs['folder_parent'] = Path(output_folder).parts[-2]
    kwargs['test_prefix'] = folder_list[-1]

    _LOGGER.info("Build start: %s", package_name)
    check_parameters(output_folder)

    # generate swagger readme
    env = Environment(loader=FileSystemLoader(_TEMPLATE),
                      keep_trailing_newline=True)
    swagger_readme = generate_swagger_readme(output_folder, env, **kwargs)

    # generate code with autorest and swagger readme
    _LOGGER.info("generate SDK code with autorest")
    check_call(
        f'autorest --version=3.7.2 --python --use=@autorest/[email protected] --use=@autorest/[email protected]'
        f'  --python-mode=update {swagger_readme}',
        shell=True)

    # generate necessary file(setup.py, CHANGELOG.md, etc)
    work_path = Path(output_folder)
    for template_name in env.list_templates():
        _LOGGER.info(f"generate necessary file: {template_name}")
        template = env.get_template(template_name)
        result = template.render(**kwargs)
        # __init__.py is a weird one
        if template_name == "__init__.py":
            split_package_name = package_name.split("-")[:-1]
            for i in range(len(split_package_name)):
                init_path = work_path.joinpath(*split_package_name[:i + 1],
                                               template_name)
                with open(init_path, "w") as fd:
                    fd.write(result)
        elif template_name != 'swagger_README.md':
            with open(work_path / template_name, "w") as fd:
                fd.write(result)

    # generate test framework
    generate_test_sample(_TEMPLATE_TESTS, work_path / Path('tests'), **kwargs)

    # generate sample framework
    generate_test_sample(_TEMPLATE_SAMPLES, work_path / Path('samples'),
                         **kwargs)

    _LOGGER.info("Build complete: %s", package_name)
def generate_test_sample(template_path: Path, target_path: Path, **kwargs: Any) -> None:
    if not os.path.exists(target_path):
        os.makedirs(target_path)
    env = Environment(loader=FileSystemLoader(template_path), keep_trailing_newline=True)
    for template_name in env.list_templates():
        _LOGGER.info(f"generate file: {template_name}")
        template = env.get_template(template_name)
        result = template.render(**kwargs)
        with open(target_path / template_name, "w") as fd:
            fd.write(result)
Ejemplo n.º 23
0
def build_packaging_by_package_name(package_name: str, output_folder: str, build_conf: bool = False) -> None:
    _LOGGER.info("Building template %s", package_name)
    package_folder = Path(output_folder) / Path(package_name)

    if build_conf:
        build_default_conf(package_folder, package_name)

    conf = read_conf(package_folder)
    if not conf:
        raise ValueError("Create a {} file before calling this script".format(package_folder / CONF_NAME))

    if not conf.get("auto_update", True):
        _LOGGER.info(f"Package {package_name} has no auto-packaging update enabled")
        return

    env = Environment(
        loader=PackageLoader('packaging_tools', 'templates'),
        keep_trailing_newline=True
    )
    conf = build_config(conf)

    for template_name in env.list_templates():
        future_filepath = Path(output_folder) / package_name / template_name

        # Might decide to make it more generic one day
        if template_name == "HISTORY.rst" and future_filepath.exists():
            _LOGGER.info("Skipping HISTORY.txt template, since a previous one was found")
            # Never overwirte the ChangeLog
            continue

        template = env.get_template(template_name)
        result = template.render(**conf)

        # __init__.py is a weird one
        if template_name == "__init__.py":
            split_package_name = package_name.split("-")[:-1]
            for i in range(len(split_package_name)):
                init_path = Path(output_folder).joinpath(
                    package_name,
                    *split_package_name[:i+1],
                    template_name
                )
                with open(init_path, "w") as fd:
                    fd.write(result)

            continue

        with open(future_filepath, "w") as fd:
            fd.write(result)
    # azure_bdist_wheel had been removed, but need to delete it manually
    with suppress(FileNotFoundError):
        (Path(output_folder) / package_name / "azure_bdist_wheel.py").unlink()


    _LOGGER.info("Template done %s", package_name)
Ejemplo n.º 24
0
class testBearcart(object):
    '''Test Bearcart Chart class'''

    @classmethod
    def setup_class(self):
        '''Bearcart test data and template setup'''
        import pandas.io.data as web

        all_data = {}
        for ticker in ['AAPL', 'GOOG']:
            all_data[ticker] = web.get_data_yahoo(ticker,
                                                  '4/1/2013', '5/1/2013')
        self.price = pd.DataFrame({tic: data['Adj Close']
                                  for tic, data in all_data.iteritems()})
        self.templates = Environment(loader=FileSystemLoader('templates'))

    def test_init(self):
        '''Test bearcart chart creation'''

        #Test defaults
        chart = bearcart.Chart()
        assert chart.height == 400
        assert chart.width == 750
        assert chart.renderer == 'line'
        temps = {x.split('.')[0]: x.split('.')[1]
                 for x in self.templates.list_templates()}
        for key, value in chart.template_vars.iteritems():
            template = self.templates.get_template('.'.join([key, temps[key]]))
            assert value == template.render()

    def test_params(self):
        '''Test bearcart chart parameters and component removal'''

        #Test params
        chart = bearcart.Chart(width=500, height=1000, type='area')
        assert chart.height == 1000
        assert chart.width == 500
        assert chart.renderer == 'area'

        #Test component removal
        chart = bearcart.Chart(x_axis=False, legend=False)
        nt.assert_list_equal(chart.template_vars.keys(),
                             ['y_axis', 'hover'])

    def test_data(self):
        '''Test bearcart data import and transformation'''

        #Test data
        series = self.price['AAPL']
        chart = bearcart.Chart(series)
        assert_almost_equal(chart.raw_data, series)
        assert chart.json_data[0]['name'] == 'AAPL'
        assert len(series) == len(chart.json_data[0]['data'])
        nt.eq_(time.mktime(series.index[0].timetuple()),
               chart.json_data[0]['data'][0]['x'])
Ejemplo n.º 25
0
def build_packaging_by_package_name(package_name: str, output_folder: str, build_conf: bool = False) -> None:
    _LOGGER.info("Building template %s", package_name)
    package_folder = Path(output_folder) / Path(package_name)

    if build_conf:
        build_default_conf(package_folder, package_name)

    conf = read_conf(package_folder)
    if not conf:
        raise ValueError("Create a {} file before calling this script".format(package_folder / CONF_NAME))

    if not conf.get("auto_update", True):
        _LOGGER.info(f"Package {package_name} has no auto-packaging update enabled")
        return

    env = Environment(
        loader=PackageLoader('packaging_tools', 'templates'),
        keep_trailing_newline=True
    )
    conf = build_config(conf)

    for template_name in env.list_templates():
        future_filepath = Path(output_folder) / package_name / template_name

        # Might decide to make it more generic one day
        if template_name == "HISTORY.rst" and future_filepath.exists():
            _LOGGER.info("Skipping HISTORY.txt template, since a previous one was found")
            # Never overwirte the ChangeLog
            continue

        template = env.get_template(template_name)
        result = template.render(**conf)

        # __init__.py is a weird one
        if template_name == "__init__.py":
            split_package_name = package_name.split("-")[:-1]
            for i in range(len(split_package_name)):
                init_path = Path(output_folder).joinpath(
                    package_name,
                    *split_package_name[:i+1],
                    template_name
                )
                with open(init_path, "w") as fd:
                    fd.write(result)

            continue

        with open(future_filepath, "w") as fd:
            fd.write(result)
    # azure_bdist_wheel had been removed, but need to delete it manually
    with suppress(FileNotFoundError):
        (Path(output_folder) / package_name / "azure_bdist_wheel.py").unlink()


    _LOGGER.info("Template done %s", package_name)
Ejemplo n.º 26
0
def main(args):
    here = os.path.dirname(os.path.abspath(__file__))
    tdir = os.path.join(here, 'templates', args['type'])
    target = os.path.join(os.path.dirname(here), 'plugins', 'plugin-' + args['name'])
    
    if os.path.exists(target):
        print("The folder %s shouldn't exit" % target)
        sys.exit(1)

    exec_name = args['name'].replace('-', '_')
    # we copy the needed files
    shutil.copytree(tdir, target, symlinks=True)
    
    shutil.move(os.path.join(target, 'check_foo'),
                os.path.join(target, exec_name))
    
    shutil.move(os.path.join(target, 'test_check_foo.py'),
                os.path.join(target, 'test_' + exec_name + '.py'))

    shutil.move(os.path.join(target, 'doc', 'plugin-check_foo.rst'),
                os.path.join(target, 'doc', 'plugin-' + exec_name + '.rst'))
    
    os.symlink(exec_name,
               os.path.join(target, exec_name + '.py'))

    # and feed them to jinja2
    loader = FileSystemLoader(target)
    env = Environment(loader=loader)

    # template variables
    tvars = dict(args)

    tvars['exec_name'] = args['name'].replace('-', '_')
    now = datetime.now()
    tvars['year'] = now.year
    tvars['date_long'] = '%s.%s.%s.%s.%s' % (now.year, now.month, now.day, now.hour, now.minute)
    tvars['date_rfc2822'] = email.utils.formatdate(time.mktime(now.timetuple()))
    
    
    for template in env.list_templates():
        output = env.get_template(template).render(tvars)
        with open(os.path.join(target, template), 'w') as f:
            f.write(output)

    print('')
    print('Your plugin is ready!')
    print('You can now edit the files in plugin-%s' % args['name'])
    print('')
    print('main python executable: %s' % exec_name)
    print('tests suite:            test_%s.py' % exec_name)
    print('requirements:           requirements.txt, debian/control')
    print('')
    print('Good luck, and thanks for playing!')
Ejemplo n.º 27
0
class Templater(object):
    '''
    Class to load and return all the templates a `CodeObject` defines.

    Parameters
    ----------

    package_name : str, tuple of str
        The package where the templates are saved. If this is a tuple then each template will be searched in order
        starting from the first package in the tuple until the template is found. This allows for derived templates
        to be used. See also `~Templater.derive`.
    env_globals : dict (optional)
        A dictionary of global values accessible by the templates. Can be used for providing utility functions.
        In all cases, the filter 'autoindent' is available (see existing templates for example usage).

    Notes
    -----

    Templates are accessed using ``templater.template_base_name`` (the base name is without the file extension).
    This returns a `CodeObjectTemplate`.
    '''
    def __init__(self, package_name, env_globals=None):
        if isinstance(package_name, basestring):
            package_name = (package_name,)
        loader = ChoiceLoader([PackageLoader(name, 'templates') for name in package_name])
        self.env = Environment(loader=loader, trim_blocks=True,
                               lstrip_blocks=True, undefined=StrictUndefined)
        self.env.globals['autoindent'] = autoindent
        self.env.filters['autoindent'] = autoindent
        if env_globals is not None:
            self.env.globals.update(env_globals)
        else:
            env_globals = {}
        self.env_globals = env_globals
        self.package_names = package_name
        for name in self.env.list_templates():
            template = CodeObjectTemplate(self.env.get_template(name),
                                          self.env.loader.get_source(self.env,
                                                                     name)[0])
            setattr(self, os.path.splitext(name)[0], template)

    def derive(self, package_name, env_globals=None):
        '''
        Return a new Templater derived from this one, where the new package name and globals overwrite the old.
        '''
        if isinstance(package_name, basestring):
            package_name = (package_name,)
        if env_globals is None:
            env_globals = {}
        package_name = package_name+self.package_names
        new_env_globals = self.env_globals.copy()
        new_env_globals.update(**env_globals)
        return Templater(package_name, env_globals=new_env_globals)
Ejemplo n.º 28
0
class Templater(object):
    '''
    Class to load and return all the templates a `CodeObject` defines.

    Parameters
    ----------

    package_name : str, tuple of str
        The package where the templates are saved. If this is a tuple then each template will be searched in order
        starting from the first package in the tuple until the template is found. This allows for derived templates
        to be used. See also `~Templater.derive`.
    env_globals : dict (optional)
        A dictionary of global values accessible by the templates. Can be used for providing utility functions.
        In all cases, the filter 'autoindent' is available (see existing templates for example usage).

    Notes
    -----

    Templates are accessed using ``templater.template_base_name`` (the base name is without the file extension).
    This returns a `CodeObjectTemplate`.
    '''
    def __init__(self, package_name, env_globals=None):
        if isinstance(package_name, basestring):
            package_name = (package_name,)
        loader = ChoiceLoader([PackageLoader(name, 'templates') for name in package_name])
        self.env = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
        self.env.globals['autoindent'] = autoindent
        self.env.filters['autoindent'] = autoindent
        if env_globals is not None:
            self.env.globals.update(env_globals)
        else:
            env_globals = {}
        self.env_globals = env_globals
        self.package_names = package_name
        for name in self.env.list_templates():
            template = CodeObjectTemplate(self.env.get_template(name),
                                          self.env.loader.get_source(self.env,
                                                                     name)[0])
            setattr(self, os.path.splitext(name)[0], template)

    def derive(self, package_name, env_globals=None):
        '''
        Return a new Templater derived from this one, where the new package name and globals overwrite the old.
        '''
        if isinstance(package_name, basestring):
            package_name = (package_name,)
        if env_globals is None:
            env_globals = {}
        package_name = package_name+self.package_names
        new_env_globals = self.env_globals.copy()
        new_env_globals.update(**env_globals)
        return Templater(package_name, env_globals=new_env_globals)
Ejemplo n.º 29
0
class Templater(object):
    '''
    Class to load and return all the templates a `CodeObject` defines.
    '''
    def __init__(self, basedir):
        self.basedir = basedir
        self.env = Environment(loader=FileSystemLoader(basedir),
                               trim_blocks=True,
                               lstrip_blocks=True,
                               )
        for name in self.env.list_templates():
            template = CodeObjectTemplate(self.env.get_template(name))
            setattr(self, os.path.splitext(name)[0], template)
Ejemplo n.º 30
0
def main(folder='templates'):

    floader = FileSystemLoader(folder)
    env = Environment(loader=floader)
    for template in env.list_templates(extensions=['html']):

        rendered = env.get_template(template).render()
        equ = template == "index.html"
        with open(
                f"html/{template}"
                if template != "index.html" else "./index.html", 'w') as f:

            f.write(rendered)
Ejemplo n.º 31
0
class ConcourseTemplating:
    def __init__(self, attributes, pipelines):
        self.attributes = attributes
        self.pipelines = pipelines
        self._environment = Environment(
            loader=FileSystemLoader(self.pipelines.root_dir))

    def list_templates(self):
        return [ self._environment.get_template(template) \
            for template in self._environment.list_templates() ]

    def apply_attributes(self):
        for template in self.list_templates():
            print template.render(self.attributes.attribute_map)
Ejemplo n.º 32
0
class Templater(object):
    '''
    Class to load and return all the templates a `CodeObject` defines.
    '''
    def __init__(self, basedir):
        self.basedir = basedir
        self.env = Environment(
            loader=FileSystemLoader(basedir),
            trim_blocks=True,
            lstrip_blocks=True,
        )
        for name in self.env.list_templates():
            template = CodeObjectTemplate(self.env.get_template(name))
            setattr(self, os.path.splitext(name)[0], template)
Ejemplo n.º 33
0
def parse_templates(project_name, secret_key, dest_dir):
    """ Perform variable replacement in templates """

    env = Environment(loader=FileSystemLoader(dest_dir))
    includes = ['.gitignore', '.py']
    
    valid_templates = validate_templates(env.list_templates(), includes)

    for template_file in valid_templates:
        template = env.get_template(template_file)
        outfile = open(join(dest_dir, template_file), 'w')
        rendered_output = template.render(project_name=project_name, secret_key=generate_secret_key())
        outfile.write(rendered_output)
        outfile.close()
Ejemplo n.º 34
0
def build_templates(project: Project):
    env = Environment(
        loader=FileSystemLoader(project.template_path),
        autoescape=select_autoescape(["html", "xml"]),
    )
    for template_name in env.list_templates():
        if not is_page_template(template_name):
            continue
        html_path = project.docs_path / template_name
        html_path.parent.mkdir(exist_ok=True, parents=True)

        template = env.get_template(template_name)

        with html_path.open(mode="w") as f:
            template.stream().dump(f)
Ejemplo n.º 35
0
def render(input_dir: str, output_dir: str):
    print("Loading...")
    env = Environment(loader=FileSystemLoader(input_dir))
    input_templates = env.list_templates(
        filter_func=lambda t: t.startswith(PAGES_DIR))
    print(f"Loaded {len(input_templates)} template(s)")
    print("Rendering...")
    output = render_templates(env, input_templates)
    print("Processing...")
    output = process_html(output)
    print("Removing old files...")
    clear_dir(output_dir)
    print("Saving...")
    save_templates(output_dir, output)
    print("Saved templates")
Ejemplo n.º 36
0
def render(config):
    """Render simulation directory."""
    target_dir = config['meta']['path'].with_suffix('')
    env = Environment(loader=FileSystemLoader(str(template_dir)))
    env.keep_trailing_newline = True
    for template_name in env.list_templates():
        # Render template
        template = env.get_template(template_name)
        rendered = template.render(**config, Path=Path)
        # Write target
        target = target_dir / template_name
        target.parent.mkdir(parents=True, exist_ok=True)
        target.write_text(rendered)
    print('Simulation directory is initialized at',
          target_dir.relative_to(os.getcwd()))
Ejemplo n.º 37
0
def render_html(indir, outdir):
    now = datetime.datetime.now()

    indir = os.path.abspath(indir)
    outdir = os.path.abspath(outdir)
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    env = Environment(loader=FileSystemLoader(indir))
    for t in env.list_templates():
        if t == 'common.html' or t == 'navbar.html':
            continue
        temp = env.get_template(t)
        html = temp.render({'year': now.year})
        with open(os.path.join(outdir, t), 'w') as f:
            f.write(html)
Ejemplo n.º 38
0
def render_html(indir, outdir):
    now = datetime.datetime.now()

    indir = os.path.abspath(indir)
    outdir = os.path.abspath(outdir)
    if not os.path.exists(outdir):
        os.makedirs(outdir)

    env = Environment(loader=FileSystemLoader(indir))
    for t in env.list_templates():
        if t == 'common.html' or t == 'navbar.html':
            continue
        temp = env.get_template(t)
        html = temp.render({'year': now.year})
        with open(os.path.join(outdir, t), 'w') as f:
            f.write(html)
Ejemplo n.º 39
0
    def _get_nereid_template_messages(cls):
        """
        Extract localizable strings from the templates of installed modules.

        For every string found this function yields a
        `(module, template, lineno, function, message)` tuple, where:

        * module is the name of the module in which the template is found
        * template is the name of the template in which message was found
        * lineno is the number of the line on which the string was found,
        * function is the name of the gettext function used (if the string
          was extracted from embedded Python code), and
        * message is the string itself (a unicode object, or a tuple of
          unicode objects for functions with multiple string arguments).
        * comments List of Translation comments if any. Comments in the code
          should have a prefix `trans:`. Example::

              {{ _(Welcome) }} {# trans: In the top banner #}
        """
        extract_options = cls._get_nereid_template_extract_options()
        logger = logging.getLogger('nereid.translation')

        for module, directory in cls._get_installed_module_directories():
            template_dir = os.path.join(directory, 'templates')
            if not os.path.isdir(template_dir):
                # The template directory does not exist. Just continue
                continue

            logger.info(
                'Found template directory for module %s at %s' % (
                    module, template_dir
                )
            )
            # now that there is a template directory, load the templates
            # using a simple filesystem loader and load all the
            # translations from it.
            loader = FileSystemLoader(template_dir)
            env = Environment(loader=loader)
            extensions = '.html,.jinja'
            for template in env.list_templates(extensions=extensions):
                logger.info('Loading from: %s:%s' % (module, template))
                file_obj = open(loader.get_source({}, template)[1])
                for message_tuple in babel_extract(
                        file_obj, GETTEXT_FUNCTIONS,
                        ['trans:'], extract_options):
                    yield (module, template) + message_tuple
Ejemplo n.º 40
0
    def __generate_target_files(self, template_path, target_path, name,
                                capitalized_name):
        try:
            os.mkdir(target_path)
        except FileExistsError:
            print(f'Module {name}, already exists.')
            sys.exit(1)

        # Create the jinja2 environment.
        j2_env = Environment(loader=FileSystemLoader(template_path),
                             trim_blocks=True)

        for template in j2_env.list_templates():
            j2_env.get_template(template).stream(
                module=name, module_capitalized=capitalized_name,
                new_line='').dump(
                    os.path.join(target_path, '.'.join(template.split('-'))))
Ejemplo n.º 41
0
def main(sdkdir, tpldir, outdir, ignore, excludes):
    oldfuncs = {}
    oldcallbacks = {}
    sdkinclude = os.path.join(sdkdir, "include")

    if not ignore:
        fpath = os.path.join(outdir, "ts3lib.cpp")
        if os.path.exists(fpath):
            log.info("Loading old functions")
            oldfuncs = parsing.getSymbols(fpath, [sdkinclude], False)[3]

        fpath = os.path.join(outdir, "ts3plugin.cpp")
        if os.path.exists(fpath):
            log.info("Loading old callbacks")
            oldcallbacks = parsing.getSymbols(fpath, [sdkinclude], False)[2]

        wrappers.closeFiles()

    consts = {}
    enums = {}
    log.info("Loading defines")
    for paths in DEFINES_SOURCES:
        cs, ens, cbs, fs = parsing.getSymbols(os.path.join(sdkdir, *paths), [sdkinclude], True)
        consts.update(cs)
        enums.update(ens)

    log.info("Loading callbacks")
    callbacks = functionMerge(oldcallbacks, parsing.getSymbols(os.path.join(sdkdir, "src", "plugin.c"), [sdkinclude], True)[2])
    callbacks = {key: callbacks[key] for key in callbacks if callbacks[key].name not in PYPLUGIN_CALLBACK_BLACKLIST}

    log.info("Loading functions")
    functions = functionMerge(oldfuncs, parsing.getSymbols(os.path.join(sdkdir, "include", "ts3_functions.h"), [sdkinclude], True)[3])
    functions = {key: functions[key] for key in functions if key not in MODULE_FUNCTION_BLACKLIST}

    env = Environment(trim_blocks=True, lstrip_blocks=True, loader=FileSystemLoader(tpldir), undefined=StrictUndefined)

    for tmp in env.list_templates(extensions=['tpl']):
        if excludes and tmp in excludes:
            log.debug("Ignoring template %s" % tmp)
            continue

        log.debug("Writing template %s" % tmp)
        t = env.get_template(tmp)
        with open(os.path.join(outdir, tmp[:-4]), "w") as f:
            f.write(t.render(constants=consts, enums=enums, callbacks=callbacks, functions=functions))
Ejemplo n.º 42
0
class Templater(object):
    '''
    Class to load and return all the templates a `CodeObject` defines.
    '''
    def __init__(self, package_name, env_globals=None):
        self.env = Environment(loader=PackageLoader(package_name, 'templates'),
                               trim_blocks=True,
                               lstrip_blocks=True,
                               )
        self.env.globals['autoindent'] = autoindent
        self.env.filters['autoindent'] = autoindent
        if env_globals is not None:
            self.env.globals.update(env_globals)
        for name in self.env.list_templates():
            template = CodeObjectTemplate(self.env.get_template(name),
                                          self.env.loader.get_source(self.env,
                                                                     name)[0])
            setattr(self, os.path.splitext(name)[0], template)
def make_services(deploy_tag, web_tag):
    import shutil
    from jinja2 import FileSystemLoader, Environment

    # delete and re-create dest folder
    shutil.rmtree(SERVICES_DEST_DIR, ignore_errors=True)
    os.makedirs(SERVICES_DEST_DIR)

    tmpl_env = Environment(
        loader=FileSystemLoader(searchpath=SERVICES_SOURCE_DIR)
    )

    services = []
    version = get_version(deploy_tag, web_tag)
    for tmpl_name in tmpl_env.list_templates():
        tmpl_filename, tmpl_ext = os.path.splitext(tmpl_name)
        if tmpl_ext != '.jinja':
            shutil.copy(os.path.join(SERVICES_SOURCE_DIR, tmpl_name), SERVICES_DEST_DIR)
            services.append(tmpl_filename)
            continue

        # generate service name:
        #   <file-name>.<deploy-tag>-<web-tag>[@]
        filename_parts = tmpl_filename.split('@')
        filename_parts[0] = '%s%s' % (filename_parts[0], version)
        output_filename = '@'.join(filename_parts)

        output_path = os.path.join(SERVICES_DEST_DIR, '%s.service' % output_filename)

        tmpl = tmpl_env.get_template(tmpl_name)

        # write the processed file to dest folder
        with open(output_path, "wb") as f:
            f.write(tmpl.render({
                'version': version,
                'deploy_tag': deploy_tag,
                'web_tag': web_tag
            }))
        services.append(output_filename)

    return services
Ejemplo n.º 44
0
def render(extensions=[], stylesheets=None, target='development'):
    env = Environment(
        loader=FileSystemLoader('./views'),
        extensions=extensions,
        keep_trailing_newline=True,
    )
    env.assets_environment = assets_env

    javascripts = [os.path.basename(path) for path in assets_env['app'].urls()]

    for t in env.list_templates(filter_func=is_template):
        template = env.get_template(t)
        if template.name == 'index.html':
            filepath = 'index.html'
        else:
            filepath = os.path.join(os.path.splitext(template.name)[0], 'index.html')

        mkdir_p(os.path.join('./dist', os.path.dirname(filepath)))

        template.stream({
            'data': data,
            'stylesheets': stylesheets,
            'javascripts': javascripts
        }).dump(os.path.join('./dist', filepath), "utf-8")
Ejemplo n.º 45
0
    def mk_pages(self):
        env = Environment(loader=FileSystemLoader(searchpath=self.source_path))
        templatenames = sorted(env.list_templates("md"))
        vars_dict = {}
        try:
            with open(os.path.join(self.source_path, "vars.json")) as vars_json:
                vars_dict = json.load(vars_json)
        except Exception as e:
            import sys
            sys.exit(e)
            pass

        self.pagesdata = []
        for templatename in templatenames:
            template = env.get_template(templatename)
            md = template.render(vars_dict)
            body = pypandoc.convert(md, "mediawiki", format="md")
            title = templatename.split("-", maxsplit=1)[-1]  # rm "01-" ...
            title = title.rsplit(".", maxsplit=1)[0]  # rm filetype
            p = {
                'title': title,
                'body': body
            }
            self.pagesdata.append(p)
Ejemplo n.º 46
0
class Generator(object):
    """Baseclass generator"""

    def __init__(self, *args, **kwargs):
        for idx, item in enumerate(('context', 'settings', 'path', 'theme',
                'output_path', 'markup')):
            setattr(self, item, args[idx])

        for arg, value in kwargs.items():
            setattr(self, arg, value)

        # templates cache
        self._templates = {}
        self._templates_path = os.path.expanduser(os.path.join(self.theme, 'templates'))
        simple_loader = FileSystemLoader(os.path.join(os.path.dirname(os.path.abspath(__file__)), "themes", "simple", "templates"))
        self._env = Environment(
            loader=ChoiceLoader([
                FileSystemLoader(self._templates_path),
                simple_loader,                              # implicit inheritance
                PrefixLoader({'!simple' : simple_loader})   # explicit inheritance
            ]),
            extensions=self.settings.get('JINJA_EXTENSIONS', []),
        )
        debug('self._env.list_templates(): {0}'.format(self._env.list_templates()))

        # get custom Jinja filters from user settings
        custom_filters = self.settings.get('JINJA_FILTERS', {})
        self._env.filters.update(custom_filters)

    def get_template(self, name):
        """Return the template by name.
        Use self.theme to get the templates to use, and return a list of
        templates ready to use with Jinja2.
        """
        if name not in self._templates:
            try:
                self._templates[name] = self._env.get_template(name + '.html')
            except TemplateNotFound:
                raise Exception('[templates] unable to load %s.html from %s' % (
                    name, self._templates_path))
        return self._templates[name]

    def get_files(self, path, exclude=[], extensions=None):
        """Return a list of files to use, based on rules

        :param path: the path to search the file on
        :param exclude: the list of path to exclude
        """
        if not extensions:
            extensions = self.markup

        files = []

        try:
            iter = os.walk(path, followlinks=True)
        except TypeError: # python 2.5 does not support followlinks
            iter = os.walk(path)

        for root, dirs, temp_files in iter:
            for e in exclude:
                if e in dirs:
                    dirs.remove(e)
            files.extend([os.sep.join((root, f)) for f in temp_files
                if True in [f.endswith(ext) for ext in extensions]])
        return files

    def _update_context(self, items):
        """Update the context with the given items from the currrent
        processor.
        """
        for item in items:
            value = getattr(self, item)
            if hasattr(value, 'items'):
                value = value.items()
            self.context[item] = value
Ejemplo n.º 47
0
class PackageTree(object):
    """Package directory tree class."""

    #: Jinja2 template name
    template_name = 'bootstrap_py'
    #: the suffix name of working directory for generating
    suffix = '-bootstrap-py'
    #: init filename
    init = '__init__.py'
    #: default permission
    exec_perm = 0o755
    #: include directories to packages
    pkg_dirs = ['{module_name}', '{module_name}/tests']

    def __init__(self, pkg_data):
        """Initialize."""
        self.cwd = os.getcwd()
        self.name = pkg_data.name
        self.outdir = os.path.abspath(pkg_data.outdir)
        self.tmpdir = tempfile.mkdtemp(suffix=self.suffix)
        self.templates = Environment(loader=PackageLoader(self.template_name))
        self.pkg_data = pkg_data

    def _init_py(self, dir_path):
        return os.path.join(self.tmpdir,
                            dir_path.format(**self.pkg_data.to_dict()),
                            self.init)

    def _sample_py(self, file_path):
        return os.path.join(self.tmpdir,
                            self.pkg_data.module_name,
                            os.path.splitext(file_path)[0])

    def _tmpl_path(self, file_path):
        return os.path.join(self.tmpdir, os.path.splitext(file_path)[0])

    def _generate_dirs(self):
        dirs = [os.path.dirname(tmpl)
                for tmpl in self.templates.list_templates()
                if tmpl.find('/') > -1] + self.pkg_dirs
        for dir_path in dirs:
            if not os.path.isdir(
                    os.path.join(self.tmpdir,
                                 dir_path.format(**self.pkg_data.to_dict()))):
                os.makedirs(os.path.join(self.tmpdir,
                                         dir_path.format(
                                             **self.pkg_data.to_dict())),
                            self.exec_perm)

    def _generate_docs(self):
        docs_path = os.path.join(self.tmpdir, 'docs')
        os.makedirs(docs_path)
        build_sphinx(self.pkg_data, docs_path)

    def _list_module_dirs(self):
        return [dir_path for dir_path in self.pkg_dirs
                if dir_path.find('{module_name}') == 0]

    def _generate_init(self):
        tmpl = self.templates.get_template('__init__.py.j2')

        for dir_path in self._list_module_dirs():
            if not os.path.isfile(self._init_py(dir_path)):
                with open(self._init_py(dir_path), 'w') as fobj:
                    # pylint: disable=no-member
                    fobj.write(
                        tmpl.render(**self.pkg_data.to_dict()) + '\n')
        return True

    def _generate_file(self, file_path):
        tmpl = self.templates.get_template(file_path)
        with open(self._tmpl_path(file_path), 'w') as fobj:
            fobj.write(
                tmpl.render(**self.pkg_data.to_dict()) + '\n')
        return True

    def _generate_exec_file(self, file_path):
        self._generate_file(file_path)
        os.chmod(self._tmpl_path(file_path), self.exec_perm)
        return True

    def _generate_samples(self, file_path):
        if not self.pkg_data.with_samples:
            return
        tmpl = self.templates.get_template(file_path)
        if file_path == 'sample.py.j2':
            with open(self._sample_py(file_path), 'w') as fobj:
                fobj.write(
                    tmpl.render(
                        **self.pkg_data.to_dict()) + '\n')
        elif file_path == 'test_sample.py.j2':
            with open(self._sample_py(os.path.join('tests',
                                                   file_path)), 'w') as fobj:
                fobj.write(
                    tmpl.render(
                        **self.pkg_data.to_dict()) + '\n')
        return True

    def _generate_files(self):
        generator = (lambda f: guard(
            g(self._generate_init, f == '__init__.py.j2'),
            g(self._generate_exec_file, f == 'utils/pre-commit.j2', (f,)),
            g(self._generate_samples, f.endswith('sample.py.j2'), (f,)),
            g(self._generate_file, params=(f,))))
        for file_path in self.templates.list_templates():
            generator(file_path)
        os.chdir(self.tmpdir)
        os.symlink('../../README.rst', 'docs/source/README.rst')
        os.chdir(self.cwd)

    def move(self):
        """Move directory from working directory to output directory."""
        if not os.path.isdir(self.outdir):
            os.makedirs(self.outdir)
        shutil.move(self.tmpdir, os.path.join(self.outdir, self.name))

    def clean(self):
        """Clean up working directory."""
        shutil.rmtree(self.tmpdir)

    def generate(self):
        """Generate package directory tree."""
        self._generate_docs()
        self._generate_dirs()
        self._generate_files()

    def vcs_init(self):
        """Initialize VCS repository."""
        VCS(os.path.join(self.outdir, self.name), self.pkg_data)
Ejemplo n.º 48
0
class Generator(object):

    def __init__(self, base):
        self.question = 0
        self.jinja_env = Environment(
            loader=PackageLoader('lektor', 'quickstart-templates/%s' % base),
            line_statement_prefix='%%',
            line_comment_prefix='##',
            variable_start_string='${',
            variable_end_string='}',
            block_start_string='<%',
            block_end_string='%>',
            comment_start_string='/**',
            comment_end_string='**/',
        )
        self.options = {}
        self.term_width = min(click.get_terminal_size()[0], 78)
        self.e = click.secho
        self.w = partial(click.wrap_text, width=self.term_width)

    def abort(self, message):
        click.echo('Error: %s' % message, err=True)
        raise click.Abort()

    def prompt(self, text, default=None, info=None):
        self.question += 1
        self.e('')
        self.e('Step %d:' % self.question, fg='yellow')
        if info is not None:
            self.e(click.wrap_text(info, self.term_width - 2, '| ', '| '))
        text = '> ' + click.style(text, fg='green')

        if default is True or default is False:
            rv = click.confirm(text, default=default)
        else:
            rv = click.prompt(text, default=default, show_default=True)
        return rv

    def title(self, title):
        self.e(title, fg='cyan')
        self.e('=' * len(title), fg='cyan')
        self.e('')

    def text(self, text):
        self.e(self.w(text))

    def confirm(self, prompt):
        self.e('')
        click.confirm(prompt, default=True, abort=True, prompt_suffix=' ')

    @contextmanager
    def make_target_directory(self, path):
        path = os.path.abspath(path)
        try:
            os.makedirs(path)
        except OSError as e:
            self.abort('Could not create target folder: %s' % e)

        if os.path.isdir(path):
            try:
                if len(os.listdir(path)) != 0:
                    raise OSError('Directory not empty')
            except OSError as e:
                self.abort('Bad target folder: %s' % e)

        scratch = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex)
        os.makedirs(scratch)
        try:
            yield scratch
        except:
            shutil.rmtree(scratch)
            raise
        else:
            # Use shutil.move here in case we move across a file system
            # boundary.
            for filename in os.listdir(scratch):
                if isinstance(path, unicode):
                    filename = filename.decode(fs_enc)
                shutil.move(os.path.join(scratch, filename),
                            os.path.join(path, filename))
            os.rmdir(scratch)

    def expand_filename(self, base, ctx, template_filename):
        def _repl(match):
            return ctx[match.group(1)]
        return os.path.join(base, _var_re.sub(_repl, template_filename))[:-3]

    def run(self, ctx, path):
        with self.make_target_directory(path) as scratch:
            for template in self.jinja_env.list_templates():
                if not template.endswith('.in'):
                    continue
                fn = self.expand_filename(scratch, ctx, template)
                directory = os.path.dirname(fn)
                try:
                    os.makedirs(directory)
                except OSError:
                    pass
                tmpl = self.jinja_env.get_template(template)
                rv = tmpl.render(ctx).strip('\r\n')
                if rv:
                    with open(fn, 'w') as f:
                        f.write(rv.encode('utf-8') + '\n')
Ejemplo n.º 49
0
class Generator(object):
    """Baseclass generator"""

    def __init__(self, context, settings, path, theme, output_path, **kwargs):
        self.context = context
        self.settings = settings
        self.path = path
        self.theme = theme
        self.output_path = output_path

        for arg, value in kwargs.items():
            setattr(self, arg, value)

        self.readers = Readers(self.settings)

        # templates cache
        self._templates = {}
        self._templates_path = []
        self._templates_path.append(os.path.expanduser(
            os.path.join(self.theme, 'templates')))
        self._templates_path += self.settings['EXTRA_TEMPLATES_PATHS']

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

        simple_loader = FileSystemLoader(os.path.join(theme_path,
                                         "themes", "simple", "templates"))
        self.env = Environment(
            trim_blocks=True,
            lstrip_blocks=True,
            loader=ChoiceLoader([
                FileSystemLoader(self._templates_path),
                simple_loader,  # implicit inheritance
                PrefixLoader({'!simple': simple_loader})  # explicit one
            ]),
            extensions=self.settings['JINJA_EXTENSIONS'],
        )

        logger.debug('template list: {0}'.format(self.env.list_templates()))

        # provide utils.strftime as a jinja filter
        self.env.filters.update({'strftime': DateFormatter()})

        # get custom Jinja filters from user settings
        custom_filters = self.settings['JINJA_FILTERS']
        self.env.filters.update(custom_filters)

        signals.generator_init.send(self)

    def get_template(self, name):
        """Return the template by name.
        Use self.theme to get the templates to use, and return a list of
        templates ready to use with Jinja2.
        """
        if name not in self._templates:
            try:
                self._templates[name] = self.env.get_template(name + '.html')
            except TemplateNotFound:
                raise Exception('[templates] unable to load %s.html from %s'
                                % (name, self._templates_path))
        return self._templates[name]

    def _include_path(self, path, extensions=None):
        """Inclusion logic for .get_files(), returns True/False

        :param path: the path which might be including
        :param extensions: the list of allowed extensions (if False, all
            extensions are allowed)
        """
        if extensions is None:
            extensions = tuple(self.readers.extensions)
        basename = os.path.basename(path)
        if extensions is False or basename.endswith(extensions):
            return True
        return False

    def get_files(self, path, exclude=[], extensions=None):
        """Return a list of files to use, based on rules

        :param path: the path to search (relative to self.path)
        :param exclude: the list of path to exclude
        :param extensions: the list of allowed extensions (if False, all
            extensions are allowed)
        """
        files = []
        root = os.path.join(self.path, path)

        if os.path.isdir(root):
            for dirpath, dirs, temp_files in os.walk(root, followlinks=True):
                for e in exclude:
                    if e in dirs:
                        dirs.remove(e)
                reldir = os.path.relpath(dirpath, self.path)
                for f in temp_files:
                    fp = os.path.join(reldir, f)
                    if self._include_path(fp, extensions):
                        files.append(fp)
        elif os.path.exists(root) and self._include_path(path, extensions):
            files.append(path)  # can't walk non-directories
        return files

    def add_source_path(self, content):
        location = content.get_relative_source_path()
        self.context['filenames'][location] = content

    def _update_context(self, items):
        """Update the context with the given items from the currrent
        processor.
        """
        for item in items:
            value = getattr(self, item)
            if hasattr(value, 'items'):
                value = list(value.items())  # py3k safeguard for iterators
            self.context[item] = value
Ejemplo n.º 50
0
def parse_shared_memory_header(args):
    # set up jinja2 environment for template parsing
    templates_dir = os.path.join(os.path.dirname(__file__), '../carseour/templates/')
    jinja2_env = Environment(loader=FileSystemLoader(templates_dir))
    templates = {}

    # add a check to see if the field is an array definition
    def is_array_definition(field):
        return 'array_size' in field

    jinja2_env.tests['array_definition'] = is_array_definition

    # retrieve and parse template definitions we know
    ext = '.template'
    ext_len = len(ext)

    for template_name in jinja2_env.list_templates():
        templates[template_name[:-ext_len]] = jinja2_env.get_template(template_name)

    # we'll find anonymous enums, which will generate warnings.
    # this doesn't work .. for some reason. but should. :(
    CppHeaderParser.print_warnings = 0

    print("Generating new file..\nAny \"[1103] WARN-enum: nameless enum\" warning can be IGNORED.\n\n")

    # parse the header file
    header_defs = CppHeaderParser.CppHeader(args.input_file)

    output_file = os.path.join(os.getcwd(), args.output_file)
    dst = open(output_file, "w")

    enums = []

    for header_enum in header_defs.enums:
        enum = {
            'values': []
        }

        for value in header_enum['values']:
            # replace "wrong" format for shifts from the parser
            if isinstance(value['value'], str):
                value['value'] = value['value'].replace("< <", "<< ")

            enum['values'].append(value)

        enums.append(enum)

    dst.write(templates['header'].render(enums=enums))

    for class_name in header_defs.classes:
        template = templates[class_name] if class_name in templates else templates['class']
        fields = header_defs.classes[class_name]['properties']['public']

        # we rename SharedMemory to GameInstance to better represent what it is
        if class_name == 'SharedMemory':
            class_name = 'GameInstance'

        dst.write(template.render(fields=fields, class_name=class_name))

    dst.close()

    print("\nWrote " + output_file)
Ejemplo n.º 51
0
class Gallery:

    def __init__(self, config, root_path):
        self.config = config
        self.root_path = root_path
        self.source_image_path = os.path.join(root_path, "source_images")
        self.source_video_path = os.path.join(root_path, "source_videos")
        self.theme_name = config.get("gallery", "theme")
        self.themes_path = os.path.join(root_path, "themes")
        self.theme_path = os.path.join(self.themes_path, self.theme_name)

        if not os.path.exists(self.theme_path):
            raise Exception("theme '%s' not found " % self.theme_name)

        themeconfigpath = os.path.join(self.theme_path, "config.txt")

        if not os.path.exists(themeconfigpath):
            raise Exception("couldn't find theme config.txt")

        self.config.read(themeconfigpath)

        self.templates_path = os.path.join(self.theme_path, "templates")

        self.template_path = os.path.join(root_path, "templates")
        self.output_path = os.path.join(root_path, "output")

        self.small_size = self.config.get("gallery",
                                          "small_image_size").split(",")
        self.large_size = self.config.get("gallery",
                                          "large_image_size").split(",")

        self.video_size = self.config.get("gallery",
                                          "video_max_size").split(",")

        self.configure_templates()
        self.discover_plugins()

    def discover_plugins(self):
        self.plugin_manager = PluginManager()
        self.plugin_manager.discover_plugins(
            os.path.join(self.root_path, "plugins"))

    def configure_templates(self):
        self.tempEnv = Environment(
            loader=FileSystemLoader(self.templates_path))

    def get_template(self, templateName):
        return self.tempEnv.get_template(templateName)

    def generate(self):
        images = self.discover_images()

        videos = self.process_videos()

        print videos

        images = self.generate_images(images)

        # plugin call point - pre page generation, with images as
        # arguments (to provide extra context for pages)
        extra_context = self.plugin_manager.prePageGeneration(
            self.config,
            self.source_image_path,
            self.source_video_path,
            images,
            videos)

        self.generate_pages(images, videos, extra_context)
        self.copy_static_content()

    def upload(self):
        # plugin call point, generation complete - upload
        self.plugin_manager.upload(self.config, self.output_path)

        # plugin call point, generation complete - notify
        self.plugin_manager.notify(self.config, self.output_path)

    def copy_static_content(self):
        static_path = os.path.join(self.theme_path, "static")
        static_output_path = os.path.join(self.output_path, "static")
        if os.path.exists(static_output_path):
            shutil.rmtree(static_output_path)
        shutil.copytree(static_path, static_output_path)

    def discover_images(self):
        images = []
        for filename in os.listdir(self.source_image_path):
            if filename.lower().find(".jpg") != -1:
                image = MediaObject(
                    source_path=os.path.join(
                        self.source_image_path,
                        filename),
                    filename=filename,
                    output_path=self.output_path)
                image.target_small_size = self.small_size
                image.target_large_size = self.large_size
                images.append(image)

        print "%s images found" % len(images)
        return images

    def process_videos(self):
        videos = []
        vp = videoprocessing.VideoProcessor()

        for filename in os.listdir(self.source_video_path):
            video = MediaObject(type=MediaObject.TYPE_VIDEO,
                                filename=filename,
                                source_path=os.path.join(
                                    self.source_video_path, filename),
                                output_path=self.output_path)

            videos.append(video)

            if video.transcoded:
                # copy already transcoded video
                target_path = os.path.join(self.output_path, video.filename)
                print "copying m4v video: %s" % video.source_path
                shutil.copy(video.source_path, target_path)
            else:
                # transcode the others
                target_filename = video.filename[
                    :video.filename.find(".")] + ".m4v"
                video.filename = target_filename
                target_path = os.path.join(self.output_path, target_filename)

                print "transcoding %s to %s" % (video.source_path, target_path)
                params = vp.getSizeAndDuration(video.source_path)
                vp.trancodeRawVideo(
                    video.source_path, target_path, params, self.video_size)

            video.thumb_path = vp.getScreencap(
                video.source_path, self.output_path)
            video.thumb_filename = os.path.split(video.thumb_path)[1]

            # get dimensions and duration
            params = vp.getSizeAndDuration(
                os.path.join(self.output_path, video.filename))
            video.width = params.get('width', None)
            video.height = params.get('height', None)
            video.hours = params.get('hours', None)
            video.minutes = params.get('minutes', None)
            video.seconds = params.get('seconds', None)
            video.title = filename

        return videos

    def generate_pages(self, images, videos, extra_context):
        try:
            themeMode = self.config.get("theme", "THEME_MODE")
        except ConfigParser.NoOptionError:
            themeMode = 'static'

        # merge video and photo records
        media = []
        media.extend(videos)
        media.extend(images)
        for mediaobject in media:
            if mediaobject.type == MediaObject.TYPE_PHOTO:
                mediaobject.page = "view_photo_%s.html" % mediaobject.id
            elif mediaobject.type == MediaObject.TYPE_VIDEO:
                mediaobject.page = "view_video_%s.html" % mediaobject.id

        if themeMode == 'ajax':
            return self.generate_ajax_pages(media, extra_context)
        elif themeMode == 'static':
            return self.generate_plain_pages(media, extra_context)
        else:
            raise Exception("unknown mode in theme")

    def generate_ajax_pages(self, media, extra_context):

        page_context = {
            'root_url': self.config.get("gallery", "ROOT_URL"),
            'imagecount': len(media),
            'media': media,
            'gallerytitle': self.config.get("gallery", "title"),
        }

        page_context.update(extra_context)

        self.render_page("index.html", "index.html", page_context)

        # create video embed pages
        for mediaitem in media:
            if mediaitem.type == MediaObject.TYPE_VIDEO:
                local_page_context = {
                    'video': mediaitem,
                    'root_url': self.config.get("gallery", "ROOT_URL"),
                }
                self.render_page("embedvideo.html", "embed_%s.html" %
                                mediaitem.filename, local_page_context)

        self.render_static_pages(page_context)

    def generate_plain_pages(self, media, extra_context):
        indexIsAlbumPage = self.config.getboolean(
            "theme", "INDEX_IS_ALBUM_PAGE")
        imagesPerPage = int(self.config.get("theme", "IMAGES_PER_PAGE"))

        # set up previous and next links
        for i in range(len(media)):
            prevlink = None
            nextlink = None
            if i > 0:
                prevlink = media[i - 1].page
            if i < (len(media) - 1):
                nextlink = media[i + 1].page
            media[i].next_link = nextlink
            media[i].prev_link = prevlink

        pages = int(math.ceil((len(media) / float(imagesPerPage))))

        page_context = {
            'root_url': self.config.get("gallery", "ROOT_URL"),
            'images_per_page': imagesPerPage,
            'pages': pages,
            'imagecount': len(media),
            'gallerytitle': self.config.get("gallery", "title"),
        }

        page_context.update(extra_context)

        pagelinks = []
        for page in range(pages):
            if page == 0 and indexIsAlbumPage:
                pagelinks.append(
                    {'title': (int(page) + 1), 'link': "index.html"})
            else:
                pagelinks.append(
                    {'title': (int(page) + 1), 'link': "page%s.html" % (int(page) + 1)})
        page_context['pagelinks'] = pagelinks

        # generate album pages
        if indexIsAlbumPage:
            currPageName = "index.html"
        else:
            currPageName = "page1.html"
        for page in range(pages):
            pageno = page + 1
            print "generating page %s" % pageno
            page_media = media[page * imagesPerPage:pageno * imagesPerPage]

            # set the owner page for the media items
            for mediaitem in page_media:
                mediaitem.home_page = currPageName

            page_context['media'] = page_media
            page_context['pageno'] = pageno

            prevlink = None
            if page > 0:
                prevlink = "page%s.html" % page
            nextlink = None
            if pageno < pages:
                nextlink = "page%s.html" % (int(pageno) + 1)

            page_context['prevlink'] = prevlink
            page_context['nextlink'] = nextlink

            self.render_page("albumpage.html", currPageName, page_context)

            currPageName = "page%s.html" % (pageno + 1)

        # generate image and video view pages
        for mediaitem in media:
            if mediaitem.type == MediaObject.TYPE_PHOTO:
                local_page_context = {
                    'img': mediaitem,
                    'root_url': self.config.get("gallery", "ROOT_URL"),
                }
                self.render_page(
                    "viewimage.html", mediaitem.page, local_page_context)

            if mediaitem.type == MediaObject.TYPE_VIDEO:
                local_page_context = {
                    'video': mediaitem,
                    'root_url': self.config.get("gallery", "ROOT_URL"),
                }
                self.render_page(
                    "viewvideo.html", mediaitem.page, local_page_context)

        self.render_static_pages(page_context)

    def render_static_pages(self, context):
        indexIsAlbumPage = self.config.getboolean(
            "theme", "INDEX_IS_ALBUM_PAGE")

        if not indexIsAlbumPage:
            self.render_page("index.html", "index.html", context)

        # render any other template not in the list of reserved names
        for template in self.tempEnv.list_templates():
            if template not in RESERVED_TEMPLATES:
                print "rendering static page - %s" % template
                self.render_page(template, template, context)

    def render_page(self, templateName, outputName, context):
        page_template = self.get_template(templateName)

        html = page_template.render(context)
        outfile = open(os.path.join(self.output_path, outputName), "w")
        outfile.write(html)
        outfile.close()

    def generate_images(self, images):
        p = Pool()

        images = p.map(process_image, [[image] for image in images])

        print images

        return images
Ejemplo n.º 52
0
	css_file = open(css_file_path, 'w')
	css_file.write(css_file_contents)
	css_file.close()

print 'Done!'

# find and compile html templates
print '---- Compiling HTML Templates ----'
templates_file_paths = _match_files_recursive(PATH_DIRECTORY_OUTPUT, '*.html')
templates_dir_paths = list(set(map(lambda x: os.path.split(x)[0], templates_file_paths)))
template_dict = {}
for template_file_path in templates_file_paths:
	template_dict[os.path.split(template_file_path)[1]] = template_file_path

loader = FileSystemLoader(templates_dir_paths)
environment = Environment(loader=loader)
example_base = environment.get_template('example_base.html')

for template_name in environment.list_templates():
	if template_name == 'example_base.html' or not ('.html' in template_name):
		continue

	example = environment.get_template(template_name)
	example_rendered = example.render(static_url=PATH_EXAMPLE_STATIC_URL, example_base=example_base)
	
	with open(template_dict[template_name], 'w') as f:
		f.write(example_rendered)
		f.close()

print 'Done!'
Ejemplo n.º 53
0
Archivo: at.py Proyecto: q3k/at
    hwaddr, name = updater.get_device(request.remote_addr)
    return { 'hwaddr': hwaddr, 'name': name, 
        'login': request.session['login'] }

@dispatcher.match('/claim', 'POST')
@restrict_to_hs
@login_required
@render('post_claim.html')
def claim(request):
    hwaddr, lease_name = updater.get_device(request.remote_addr)
    if not hwaddr:
        return { 'error': 'Invalid device.' }
    userid = request.session['userid']
    try:
        conn.execute('insert into devices (hwaddr, name, owner, ignored)\
            values (?, ?, ?, ?)', [hwaddr, request['name'], userid, False])
        return {}
    except sqlite3.Error as e:
        return { 'error': 'Could not add device! Perhaps someone claimed it?' }

port = 8080
if __name__ == '__main__':
    print env.list_templates()
    logger.setLevel(logging.DEBUG)
    logger.addHandler(logging.StreamHandler())
    conn = setup_db()
    updater = DnsmasqUpdater(config.lease_file, config.lease_offset, config.timeout)
    updater.start()
    server = simple_server.make_server('', port, app)
    server.serve_forever()
Ejemplo n.º 54
0
class Builder:
  def __init__(self):
    self.templatedir = 'templates'
    self.builddir = 'build'
    self.env = Environment(loader=PackageLoader(__name__, self.templatedir))
    
  def render(self):
    """
      Render all templates and static resources
    """
    for filepath in self.env.list_templates(filter_func=self.filter_ignored_files):
      if self.is_template(filepath):
        self.render_template(filepath)
      else:
        self.copy_static(filepath)

  def is_template(self, filepath):
    """
      Return True when the file is a Jinja template
    """
    (_, ext) = os.path.splitext(filepath)
    return (ext == '.jinja')

  def render_template(self, filepath):
    assert (self.is_template(filepath))
    self.prepare_subdirectories(filepath)
    dst = self.get_template_destination(filepath)
    logging.info("Rendering %s to %s" % (filepath, dst))
    with open(dst, 'w') as f:
      t = self.env.get_template(filepath)
      f.write(t.render().encode('utf8'))

  def copy_static(self, filepath):
    """
      Copy a static file to the build directory
    """
    assert (not self.is_template(filepath))
    self.prepare_subdirectories(filepath)
    src = os.path.join(self.templatedir, filepath)
    dst = self.get_destination(filepath)
    logging.info("Copying %s to %s" % (src, dst))
    shutil.copyfile(src, dst)

  def prepare_subdirectories(self, filepath):
      """
        Create sub-directories in build directory
      """
      path = os.path.dirname(filepath)
      dst_path = os.path.join(self.builddir, path)
      try:
        os.makedirs(dst_path)
      except:
        pass

  def get_template_destination(self, filepath):
    """
      Compute the destination file path of a template
    """
    assert (self.is_template(filepath))
    (root, _) = os.path.splitext(filepath)
    return (self.get_destination(root))

  def get_destination(self, filepath):
    """
      Compute the destination file path of a static file
    """
    return (os.path.join(self.builddir, filepath))

  def filter_ignored_files(self, filepath):
    """
      Filter function to ignore files beginning by '_' (useful for layouts)
    """
    filename = os.path.basename(filepath)
    return (filename[0] != '_')
Ejemplo n.º 55
0
class Tachikoma():
  ''' build the actual site '''

  def __init__(self, directory):
    ''' Perform the intial setup with the directory''' 
    self.error_msg(self.set_working_dir(directory))
    self.site_url = "http://www.section9.co.uk"

  def read_layouts(self):
    ''' Scan for and read in layouts - memory intensive? probably not '''
    layout_dict = {}
    for fn in os.listdir(self.layout_dir):
      f = open(self.layout_dir + "/" + fn)
      name, extension = os.path.splitext(fn)
      layout_dict[name] = f.read()
      f.close()
    # Load the layouts into the environment so jinja can find them
    self.jinja_env = Environment(extensions=['jinja2.ext.loopcontrols'], 
                                 loader=DictLoader(layout_dict));

  def parse_item(self, path):
    # Start with the YAML front matter - build a dictionary on metadata
    if not os.path.isfile(path):
      return (False, "Ignoring directory " + path)
    f = open(path)

    try:
      lines = f.readlines()
    except:
      return (False, "File is not an item: " + path)

    first = False
    found = False

    yaml_raw = ""
    item_body = ""

    item = Item()
    item.path, item.ext = os.path.splitext(path)
    item.name = os.path.basename(item.path)
    item.id = self.post_id
    self.post_id += 1

    if item.ext != ".md" and item.ext != ".markdown" and item.ext != ".html" and item.ext != ".htm":
      return (False, "Can only work with Markdown or HTML containing a YAML header")

    for line in lines:
      if "---" in line: # this works but is a bit lame
        if not first:
          first = True
          continue          
        elif not found: # don't parse markdown horizontal rules '---'
          found = True
          continue

      if first and not found:
        yaml_raw += line
      else:
        item_body += line

    if not found:
      return (False, "Incorrect YAML front matter in file: " + path)
    
    item.metadata = yaml.safe_load(yaml_raw)

    f.close()

    # Attempt to find the date and title
    self.parse_file_name(item)

    # Test for REQUIRED metadata
    if not item.metadata["layout"]:
      return (False, "ERROR: no layout directive found in " + path)

    if item.metadata["layout"] not in self.jinja_env.list_templates():
      return(False , "ERROR: no matching layout " + item.metadata["layout"] + " found in " + self.layout_dir )
        
    # If markdown, set a raw step
    if item.ext == ".md" or item.ext == ".markdown":
      item.raw = item_body
    else:
      item.content = item_body
      
    # Programmatically add all the metadata
    for key in item.metadata.keys():
      setattr(item, key, item.metadata[key])

    # Now set the URL which matches the filename
    item.url = "/posts/" + item.name + ".html"
   
    return(True,item)


  def parse_file_name(self, item):
    '''
    Look at the file name to determine the date
    Format is year-month-day_title or year_month_day_title
    at the beginning of the file
    '''

    tokens = item.name.split("-")
    
    if len(tokens) < 4:
      tokens = item.name.split("_")

    if len(tokens) < 4:
      return
  
    item.date = datetime.strptime(tokens[0] + " " + tokens[1] + " " + tokens[2], "%Y %m %d") 
    item.title = " ".join(tokens[3:])



  def parse_items(self):

    ''' Scan the Item directory for pages '''

    self.post_id = 0

    # List level posts
    for fn in os.listdir(self.post_dir):
      result, item = self.parse_item(self.post_dir + "/" + fn)
      if not result:
        print(item)
        continue
     
      item.tpath = self.site_post_dir
      self.posts.append(item)

    # Find all the related pages by tag and set these in the dictionary
    # this is a nasty nasty loop
    for post in self.posts:
      post.related = []
      if "tags" in post.metadata:
        for tag in post.metadata["tags"]:
          for testpost in self.posts:
            if "tags" in testpost.metadata:
              for tag in testpost.metadata["tags"]:
                if not (testpost.name, testpost.title) in post.related and testpost.name != post.name:
                  post.related.append( (testpost.name, testpost.title) )


    # Top level pages
    for fn in os.listdir(self.dir):
      result, item = self.parse_item(self.dir + "/" + fn)
      if not result:
        print(item)
        continue
    
      item.tpath = self.site_dir
      self.pages.append(item)

    return (True,"Success")



  def build_items(self):
    ''' Perform the markdown and jinja2 steps on the raw Items and write to files '''

    def write_out(self,item):
      """Make the item into a jinja template, render it and write the output"""
      template = self.jinja_env.from_string(item.content)
      item.rendered = template.render(page=item, site=self.site)
      print ("Creating: " + item.tpath + "/" + item.name + ".html")
      f = open(item.tpath + "/" + item.name + ".html", "w")
      f.write(item.rendered)
      f.close()
    
    for item in self.posts:
      
      if item.ext == ".md" or item.ext == ".markdown":
        
        # this section tests for mdx-outline plugin for markdown, which section9 uses to create its masonry bricks
        # its useful if you want sections but you can simply comment this out and use markdown straight if you prefer
        #try:
        #  import mdx_outline
        #  foo_loaded = True
        #except ImportError:
        foo_loaded = False 
        
        if foo_loaded:
          item.content = markdown.markdown(item.raw, ['outline(wrapper_tag=div,omit_head=True, wrapper_cls=s%(LEVEL)d box)'])
        else:
          item.content = markdown.markdown(item.raw)

        # Since this is markdown, we cant add an extends statement so we set the whole thing as a content block
        item.content = "{% block content %}\n" + item.content + "{% endblock %}"
        item.content = "{{% extends '{0}' %}}\n".format(item.metadata["layout"]) + item.content
  
      write_out(self,item) 
    
     
    for item in self.pages:
      item.content = "{{% extends '{0}' %}}\n".format(item.metadata["layout"]) + item.content
      write_out(self,item)


  def set_working_dir(self, directory):
    if directory[0]== "/":
      self.dir = directory
    else:
      self.dir = os.getcwd() + "/" + directory
    self.post_dir = self.dir + "/_posts"
    self.site_dir = self.dir + "/_site"
    self.site_post_dir = self.dir + "/_site/posts"
    self.layout_dir = self.dir + "/_layouts"

    self.layouts = {}

    # Test directories exist

    if not os.path.exists(self.post_dir):
      return (False, "No Item Directory")
    if not os.path.exists(self.layout_dir ):
      return (False,"No Layout directory")
    
    self.read_layouts()

    return (True,"Success")
         


  def build(self):
    ''' Build the site from the given directory '''

    self.pages = []
    self.posts = []
    self.site = Item()
    
    # TODO Establish a method to set this in a global file
    # TODO Setup the site metadata first for the Items and such
    self.site.title = "Section9 dot co dot uk ltd"

    self.error_msg( self.parse_items() )

    # Sort Items by date
    self.posts.sort(key= lambda x: x.date, reverse=True)

    self.site.posts = self.posts
    self.build_items()
                    
    return (True,"Finished Building")

  def clean(self):
    ''' Remove the site and re-copy. Not called when the server is running '''

    if os.path.exists(self.site_dir):
      shutil.rmtree(self.site_dir)
    
    os.mkdir(self.site_dir)
    os.mkdir(self.site_post_dir)
    
    self.copydirs()


  def _recursive_overwrite(self, src, dest, ignore=None):
    if os.path.isdir(src):
      if not os.path.isdir(dest):
        os.makedirs(dest)
      files = os.listdir(src)
      
      if ignore is not None:
        ignored = ignore(src, files)
      else:
        ignored = set()
      for f in files:
        if f not in ignored:
          self._recursive_overwrite(os.path.join(src, f), os.path.join(dest, f), ignore)
    else:
      shutil.copyfile(src, dest)
  
  def copydirs(self):
    ''' copy any directories to the _site dir that arent special dirs '''
    for fn in os.listdir(self.dir):
      if os.path.isdir(self.dir + "/" + fn) and fn[0] != "_" and fn[0] != ".":
        print("Copying directory " + fn + " to " + self.site_dir)
        #dir_util.copy_tree(self.dir + "/" + fn, self.site_dir  + "/" + fn)
        self._recursive_overwrite(self.dir + "/" + fn, self.site_dir  + "/" + fn, ignore = shutil.ignore_patterns('.*', 'tmp*'))

  def error_msg(self,value):
    result, message = value
    if not result:
      print(message)
      quit()
Ejemplo n.º 56
0
    params.append(Parameter(**p))

  consts = []
  for c in yaml_input['constants']:
    c['name'].lower()
    consts.append(Constant(**c))

  cl_khr_fp64 = \
    len([ p for p in params if p.type == 'double' ]) > 0 or \
    len([ c for c in consts if c.type == 'double' ]) > 0

  # process all templates in templates/ directory
  # (ignoring temporary files and anything in the macros directory)
  env = Environment(loader=PackageLoader('pairgen', 'templates'))
  istemplate = lambda x: not x.startswith('.') and \
                         not x.startswith('macros') and \
                         not x.startswith('bases')
  for t in env.list_templates(filter_func=istemplate):
    output = name + '_' + t
    template = env.get_template(t)
    template.stream(
      cwd=os.getcwd(),
      cl_khr_fp64=cl_khr_fp64,
      maxdim=max([p.dim for p in params]),
      name=name,
      headername=name.upper(),
      classname=name.capitalize(),
      params=params,
      consts=consts).dump(output)
   #print t, "->", output
Ejemplo n.º 57
0
class Generator(object):
    """Baseclass generator"""

    def __init__(self, context, settings, path, theme, output_path,
                 readers_cache_name='', **kwargs):
        self.context = context
        self.settings = settings
        self.path = path
        self.theme = theme
        self.output_path = output_path

        for arg, value in kwargs.items():
            setattr(self, arg, value)

        self.readers = Readers(self.settings, readers_cache_name)

        # templates cache
        self._templates = {}
        self._templates_path = []
        self._templates_path.append(os.path.expanduser(
            os.path.join(self.theme, 'templates')))
        self._templates_path += self.settings['EXTRA_TEMPLATES_PATHS']

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

        simple_loader = FileSystemLoader(os.path.join(theme_path,
                                         "themes", "simple", "templates"))
        self.env = Environment(
            trim_blocks=True,
            lstrip_blocks=True,
            loader=ChoiceLoader([
                FileSystemLoader(self._templates_path),
                simple_loader,  # implicit inheritance
                PrefixLoader({'!simple': simple_loader})  # explicit one
            ]),
            extensions=self.settings['JINJA_EXTENSIONS'],
        )

        logger.debug('Template list: %s', self.env.list_templates())

        # provide utils.strftime as a jinja filter
        self.env.filters.update({'strftime': DateFormatter()})

        # get custom Jinja filters from user settings
        custom_filters = self.settings['JINJA_FILTERS']
        self.env.filters.update(custom_filters)

        signals.generator_init.send(self)

    def get_template(self, name):
        """Return the template by name.
        Use self.theme to get the templates to use, and return a list of
        templates ready to use with Jinja2.
        """
        if name not in self._templates:
            try:
                self._templates[name] = self.env.get_template(name + '.html')
            except TemplateNotFound:
                raise Exception('[templates] unable to load %s.html from %s'
                                % (name, self._templates_path))
        return self._templates[name]

    def _include_path(self, path, extensions=None):
        """Inclusion logic for .get_files(), returns True/False

        :param path: the path which might be including
        :param extensions: the list of allowed extensions (if False, all
            extensions are allowed)
        """
        if extensions is None:
            extensions = tuple(self.readers.extensions)
        basename = os.path.basename(path)

        #check IGNORE_FILES
        ignores = self.settings['IGNORE_FILES']
        if any(fnmatch.fnmatch(basename, ignore) for ignore in ignores):
            return False

        if extensions is False or basename.endswith(extensions):
            return True
        return False

    def get_files(self, paths, exclude=[], extensions=None):
        """Return a list of files to use, based on rules

        :param paths: the list pf paths to search (relative to self.path)
        :param exclude: the list of path to exclude
        :param extensions: the list of allowed extensions (if False, all
            extensions are allowed)
        """
        if isinstance(paths, six.string_types):
            paths = [paths] # backward compatibility for older generators

        # group the exclude dir names by parent path, for use with os.walk()
        exclusions_by_dirpath = {}
        for e in exclude:
            parent_path, subdir = os.path.split(os.path.join(self.path, e))
            exclusions_by_dirpath.setdefault(parent_path, set()).add(subdir)

        files = []
        for path in paths:
            # careful: os.path.join() will add a slash when path == ''.
            root = os.path.join(self.path, path) if path else self.path

            if os.path.isdir(root):
                for dirpath, dirs, temp_files in os.walk(root, followlinks=True):
                    for e in exclusions_by_dirpath.get(dirpath, ()):
                        if e in dirs:
                            dirs.remove(e)
                    reldir = os.path.relpath(dirpath, self.path)
                    for f in temp_files:
                        fp = os.path.join(reldir, f)
                        if self._include_path(fp, extensions):
                            files.append(fp)
            elif os.path.exists(root) and self._include_path(path, extensions):
                files.append(path)  # can't walk non-directories
        return files

    def add_source_path(self, content):
        """Record a source file path that a Generator found and processed.
        Store a reference to its Content object, for url lookups later.
        """
        location = content.get_relative_source_path()
        self.context['filenames'][location] = content

    def _add_failed_source_path(self, path):
        """Record a source file path that a Generator failed to process.
        (For example, one that was missing mandatory metadata.)
        The path argument is expected to be relative to self.path.
        """
        self.context['filenames'][os.path.normpath(path)] = None

    def _is_potential_source_path(self, path):
        """Return True if path was supposed to be used as a source file.
        (This includes all source files that have been found by generators
        before this method is called, even if they failed to process.)
        The path argument is expected to be relative to self.path.
        """
        return os.path.normpath(path) in self.context['filenames']

    def _update_context(self, items):
        """Update the context with the given items from the currrent
        processor.
        """
        for item in items:
            value = getattr(self, item)
            if hasattr(value, 'items'):
                value = list(value.items())  # py3k safeguard for iterators
            self.context[item] = value

    def __str__(self):
        # return the name of the class for logging purposes
        return self.__class__.__name__
Ejemplo n.º 58
0
class Renderer(object):
    """The renderer object.

    :param template_folder: the directory containing the templates. Defaults to
                            ``'templates'``
    :param extensions: list of extensions to add to the Environment
    :param contexts: list of regex-function pairs. the function should return a
                     context for that template. the regex, if matched against
                     a filename, will cause the context to be used.
    :param rules: used to override template compilation. The value of rules
                  should be a list of `regex, function` pairs where `function`
                  takes a jinja2 Environment, the filename, and the context and
                  renders the template, and `regex` is a regex that if matched
                  against a filename will cause `function` to be used instead
                  of the default.
    :param encoding: the encoding of templates to use. Defaults to 'utf8'
    """
    debug_log_format = None

    def __init__(self,
                 template_folder="templates",
                 outpath=".",
                 extensions=None,
                 contexts=None,
                 rules=None,
                 encoding="utf8"):

        if os.path.isabs(template_folder):
            template_path = template_folder
        else:
            # TODO: Remove this
            calling_module = inspect.getmodule(inspect.stack()[-1][0])
            # Absolute path to project
            project_path = os.path.realpath(os.path.dirname(
                calling_module.__file__))
            # Absolute path to templates
            template_path = os.path.join(project_path, template_folder)

        self.template_folder = template_path
        self.outpath = outpath
        self.extensions = extensions or []
        self.contexts = contexts or []
        self.rules = rules or []
        self.encoding = encoding

        loader = FileSystemLoader(searchpath=template_folder,
                                  encoding=encoding)
        self._env = Environment(loader=loader, extensions=self.extensions)

        self.logger_name = __name__
        self.logger = logging.create_logger(self)

        self.debug = False

    @property
    def template_names(self):
        return self._env.list_templates(filter_func=self.filter_func)

    def get_template(self, template_name):
        """Get a Template object from the environment.

        :param template_name: the name of the template
        """
        return self._env.get_template(template_name)

    def get_context(self, template_name):
        """Get the context for a template.

        By default, this function will return an empty context.

        If a matching context_generator can be found, it will be passed the
        template and executed. The context generator should return a dictionary
        representing the context.

        :param template_name: the name of the template
        """
        for regex, context_generator in self.contexts:
            if re.match(regex, template_name):
                try:
                    return context_generator(self.get_template(template_name))
                except TypeError:
                    return context_generator()
        return {}

    def filter_func(self, filename):
        """Check if the file should be rendered.

        Hidden files will not be rendered.

        Files prefixed with an underscore are assumed to be partials and will
        not be rendered.

        :param filename: the name of the file to check
        """
        _, tail = os.path.split(filename)
        return not (tail.startswith('_') or tail.startswith("."))

    def _ensure_dir(self, template_name):
        """Ensure the output directory for a template exists."""
        head, tail = os.path.split(template_name)
        if head:
            file_dirpath = os.path.join(self.outpath, head)
            if not os.path.exists(file_dirpath):
                os.makedirs(file_dirpath)

    def render_template(self, template_name):
        """Render a template.

        If a matching Rule can be found, rendering will be delegated to the
        rule.

        :param template_name: the name of the template
        """
        self.logger.info("Rendering %s..." % template_name)

        template = self.get_template(template_name)
        context = self.get_context(template_name)
        for regex, render_func in self.rules:
            if re.match(regex, template.name):
                render_func(self, template, **context)
                break
        else:
            self._ensure_dir(template.name)
            fp = os.path.join(self.outpath, template.name)
            template.stream(**context).dump(fp, self.encoding)

    def render_templates(self):
        """Render each of the templates."""
        for template_name in self.template_names:
            self.render_template(template_name)

    def _watch(self):
        """Watch and reload templates."""
        import easywatch

        self.logger.info("Watching '%s' for changes..." %
                          self.template_folder)
        self.logger.info("Press Ctrl+C to stop.")

        def handler(event_type, src_path):
            template_name = os.path.relpath(src_path, self.template_folder)
            if event_type == "modified":
                if src_path.startswith(self.template_folder):
                    self.render_template(template_name)
        easywatch.watch(self.template_folder, handler)

    def run(self, debug=False, use_reloader=False):
        """Run the renderer.

        :param debug: if given, then display logs
        :param reload: if given, reload templates on modification
        """
        if debug:
            self.debug = True

        self.render_templates()

        if use_reloader:
            self._watch()
"""
Main file.
"""

import os
import argparse
from conf import conf
from jinja2 import Environment, FileSystemLoader

basedir = os.path.dirname(os.path.realpath(__file__))
templatedir = os.path.join(basedir, 'templates')
outputdir = os.path.join(basedir, 'output')

parser = argparse.ArgumentParser(description='A simple dotfile manager')
parser.add_argument('-d', '--directory', default=templatedir,
                    help='Directory from which to read templates')
parser.add_argument('-o', '--output', default=outputdir,
                    help='Output directory')
args = parser.parse_args()

env = Environment(loader=FileSystemLoader(args.directory, followlinks=True))
for template in env.list_templates():
    filepath = os.path.join(args.output, template)
    if not os.path.exists(filepath):
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
    with open(filepath, 'w') as f:
        f.write(env.get_template(template).render(**conf))
print('All templates written')