createcomponent.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:pattern-library 作者: springload 项目源码 文件源码
def _copy_template(
        self,
        source_path,
        source_dirname,
        target_path,
        target_dirname,
        context,
        component_name=None
    ):

        prefix_length = len(TEMPLATES_PATH) + 1

        # Walk through all the file in source path
        for root, dirs, files in os.walk(source_path):
                path_rest = root[prefix_length:]

                # Rename folder
                relative_dir = path_rest.replace(source_dirname, target_dirname)
                target_dir = os.path.join(target_path, relative_dir)
                if not os.path.exists(target_dir):
                    os.mkdir(target_dir)

                # Copy file
                for filename in files:
                    if filename.endswith(IGNORED_FILE_EXTENSIONS) or filename.startswith('.'):
                        continue

                    old_filename = new_filename = filename
                    if component_name:
                        new_filename = filename.replace('COMP_NAME', component_name)

                    old_path = os.path.join(root, old_filename)
                    new_path = os.path.join(target_dir, new_filename)

                    # Read template
                    with open(old_path, 'rb') as template_file:
                        content = template_file.read()

                    # Parse template
                    content = content.decode('utf-8')
                    template = Engine().from_string(content)
                    content = template.render(context)
                    content = content.encode('utf-8')

                    # Save file
                    with open(new_path, 'wb') as new_file:
                        new_file.write(content)

                    rel_path = new_path.replace(settings.BASE_DIR, '')
                    print '[Created] {file}'.format(file=rel_path)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号