當前位置: 首頁>>代碼示例>>Python>>正文


Python Template.endswith方法代碼示例

本文整理匯總了Python中jinja2.Template.endswith方法的典型用法代碼示例。如果您正苦於以下問題:Python Template.endswith方法的具體用法?Python Template.endswith怎麽用?Python Template.endswith使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在jinja2.Template的用法示例。


在下文中一共展示了Template.endswith方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_path

# 需要導入模塊: from jinja2 import Template [as 別名]
# 或者: from jinja2.Template import endswith [as 別名]
 def get_path(src_root_, remaining_path, dst_root_, context_, prefix_len_):
     """return relative source path (to template dir) and destination path
     destination path is the absolute destination path
     """
     src_path_ = (src_root_ + '/' + remaining_path)[prefix_len_:]
     dst_path_ = src_path_
     if os.sep != '/':
         dst_path_ = dst_path_.replace('/', os.sep)
     dst_path_ = Template(dst_path_).render(**context_)
     if dst_path_.endswith('_tpl'):
         dst_path_ = dst_path_[:-4]
     return src_path_, os.path.join(dst_root_, dst_path_)
開發者ID:pombredanne,項目名稱:PersiFlan,代碼行數:14,代碼來源:utils.py

示例2: copy_file_structure

# 需要導入模塊: from jinja2 import Template [as 別名]
# 或者: from jinja2.Template import endswith [as 別名]
def copy_file_structure(rootdir, path, replace=None, files_to_ignore=None,
                        partial=None, zipf=None, template_root=None,
                        **kwargs):
    '''Walks through the file structure and copy all directories and files.

    Args:
        rootdir (str): the root directory where the files will be copied to
        path (str): the path to walk through and reproduce

    Keyword args:
        replace (dict, default=None): dictionary for file name replacement.
            Keys are old file names and values are new file names.
        files_to_ignore (list, default=None): list of file names to ignore.
        partial (list, default=None): list of paths for a partial build.
            Only the paths in the lists will be created.
        zipf (zipfile.ZipFile, default=None)
        template_root (str, default=None): the path to the project template
        directory
        **kwargs: dictionary containing the variables for templating
    '''
    if not os.path.exists(rootdir) and zipf is None:
        raise IOError('Root directory not found: "'+rootdir+'"')

    if not files_to_ignore:
        files_to_ignore = get_config_param('files_to_ignore', [])

    if template_root is None:
        template_root = path

    for fname in os.listdir(path):
        src = os.path.join(path, fname)
        relpath = os.path.relpath(src, start=template_root)
        if partial and src not in partial:
            # Partial build: only files and dirs in the partial list
            # will be created
            continue
        ignoring = False
        for pat in files_to_ignore:
            if (fnmatch.fnmatch(fname, pat) or
                    fnmatch.fnmatch(relpath, pat)):
                ignoring = True
                break
        if ignoring:
            # file path is in the list of files to ignore
            six.print_('File ignored: `{0}`'.format(src))
            continue
        if replace and fname in replace:
            fname = Template(replace[fname]).render(**kwargs)
        elif fname.endswith('_py.template'):
            fname = fname[:-12]+'.py'

        dst = os.path.join(rootdir, fname)
        if os.path.isdir(src):
            if not os.path.exists(dst) and zipf is None:
                os.makedirs(dst)
            copy_file_structure(dst, src, replace=replace, partial=partial,
                                files_to_ignore=files_to_ignore, zipf=zipf,
                                template_root=template_root,
                                **kwargs)
        elif os.path.isfile(src):
            file_content = render_template(src, **kwargs)
            create_general_file(dst, file_content, zipf=zipf)
開發者ID:frenetic-be,項目名稱:pyproject,代碼行數:64,代碼來源:utils.py

示例3: write_files

# 需要導入模塊: from jinja2 import Template [as 別名]
# 或者: from jinja2.Template import endswith [as 別名]
    def write_files(self, pypackage_name, dirname, context, destination_root, dry_run=False):
        """
        write a full directory of template or plain files. Filenames can be templated too.
        pypackage_name is the Python package containing templates
        dirname is the directory name inside the Python package
        context is the Jinja2 context
        destination_root is the destination directory
        """

        def get_path(src_root_, remaining_path, dst_root_, context_, prefix_len_):
            """return relative source path (to template dir) and destination path
            destination path is the absolute destination path
            """
            src_path_ = (src_root_ + '/' + remaining_path)[prefix_len_:]
            dst_path_ = src_path_
            if os.sep != '/':
                dst_path_ = dst_path_.replace('/', os.sep)
            dst_path_ = Template(dst_path_).render(**context_)
            if dst_path_.endswith('_tpl'):
                dst_path_ = dst_path_[:-4]
            return src_path_, os.path.join(dst_root_, dst_path_)

        def makedirs(path):
            if not dry_run:
                os.makedirs(path)
            valid(_('Directory %(f)s created.'), f=path)

        loader = PackageLoader(pypackage_name, dirname)
        env = Environment(loader=loader)
        env.filters['lower_first_letter'] = lower_first_letter
        destination_root = Template(destination_root).render(**context)
        if destination_root.endswith('_tpl'):
            destination_root = destination_root[:-4]
        if not os.path.isdir(destination_root):
            makedirs(destination_root)
        prefix_len = len(dirname) + 1
        for root, dirnames, filenames in walk(pypackage_name, dirname):
            new_dirnames = [dirname for dirname in dirnames]
            for dirname in new_dirnames:
                if dirname in ('.svn', '.git', '.hg', 'CVS'):
                    dirnames.remove(dirname)
                    continue
                src_path, dst_path = get_path(root, dirname, destination_root, context, prefix_len)
                if not os.path.isdir(dst_path):
                    makedirs(dst_path)
            for filename in filenames:
                if filename[-4:] == '_inc':
                    continue
                src_path, dst_path = get_path(root, filename, destination_root, context, prefix_len)
                if self.dry_run:
                    f_out = six.BytesIO()
                else:
                    if not os.path.isdir(os.path.dirname(dst_path)):
                        continue
                    if os.path.isdir(dst_path):
                        shutil.rmtree(dst_path)
                    elif os.path.exists(dst_path):
                        os.remove(dst_path)
                    f_out = open(dst_path, 'wb')
                if filename[-4:] == '_tpl':
                    template = env.get_template(src_path)
                    f_out.write(template.render(**context).encode('utf-8'))
                else:
                    f_in = pkg_resources.resource_stream(pypackage_name, root + '/' + filename)
                    data = f_in.read(10240)
                    while data:
                        f_out.write(data)
                        data = f_in.read(10240)
                    f_in.close()
                f_out.close()
                valid(_('File %(f)s written.'), f=dst_path)
開發者ID:pombredanne,項目名稱:PersiFlan,代碼行數:73,代碼來源:utils.py


注:本文中的jinja2.Template.endswith方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。