当前位置: 首页>>代码示例>>Python>>正文


Python Template.save方法代码示例

本文整理汇总了Python中template.Template.save方法的典型用法代码示例。如果您正苦于以下问题:Python Template.save方法的具体用法?Python Template.save怎么用?Python Template.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在template.Template的用法示例。


在下文中一共展示了Template.save方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from template import Template [as 别名]
# 或者: from template.Template import save [as 别名]
        class EasyMockGen:
            def __init__(self, args, header):
                self.args = args
                self.header_orig = header
                self.header_file = os.path.basename(header)

                # try to locate the header in -I paths
                if not os.path.isfile(self.header_orig):
                    for x in os.environ.get('EASYMOCK_CFLAGS').split():
                        if x.startswith("-I"):
                            inc_header = "%s/%s" % (x[2:], header)
                            if os.path.isfile(inc_header):
                                self.header_orig = inc_header
                                self.header_file = header
                if not os.path.isfile(self.header_orig):
                    raise Exception("ERROR: Cannot read: %s" % self.header_orig)

                self.header_prep = self.header_orig
                self.header_name = re.sub(r"\.[\w]+$", r"", self.header_file)
                self.header_name = re.sub(r"/", r"_", self.header_name)
                self.filemock_h = ("mock_%s.h") % (self.header_name)
                self.filemock_c = ("mock_%s.c") % (self.header_name)
                self.tempdir = tempfile.mkdtemp(prefix='easymock.')
                self.mockh = None
                self.mockc = None

            def run_system(self, cmd):
                print(cmd)
                rc = os.system(cmd)
                if rc != 0:
                    raise Exception("ERROR: Command failed: %s" % cmd)

            def preprocess(self):
                if self.args.no_preprocess:
                    return
                gcc = os.environ.get('EASYMOCK_GCC')
                cflags = os.environ.get('EASYMOCK_CFLAGS')
                self.header_prep = \
                    "%s/%s" % (self.tempdir, os.path.basename(self.header_file))
                incext = os.path.realpath(os.path.dirname(__file__) +
                                          "/../src/easymock_ext.h")

                print("\n=== Preprocessing header : %s" % self.header_orig)
                print("EASYMOCK_GCC = %s" % gcc)
                print("EASYMOCK_CFLAGS = %s" % cflags)

                self.run_system("%s %s -include %s -E %s -o %s" % \
                                (gcc, cflags, incext,
                                 self.header_orig, self.header_prep))
                print("Preprocessed header : %s" % self.header_prep)

            def generate(self):
                print("=== Generating mocks for %s" % self.header_file)
                cwd_stored = os.getcwd()
                scriptdir = os.path.dirname(os.path.realpath(__file__))

                print("Parsing file...")
                os.chdir(self.tempdir)
                fdv = FuncDeclVisitor(self.args)
                fdv.parse(os.path.realpath(self.header_prep))

                if self.args.print_ast:
                    # pprint(v.funcdecls)
                    fdv.ast.show(attrnames=True, nodenames=True)

                if fdv.funcdecls:
                    HFile = collections.namedtuple(
                        'File', ['name', 'incl', 'funcs'])
                    file = HFile(name=self.header_name,
                                 incl=self.header_file,
                                 funcs=fdv.funcdecls)
                    os.chdir(scriptdir + "/../tmpl")
                    print("Generating %s" % (self.filemock_h))
                    self.mockh = Template("file.h.tmpl")
                    self.mockh.render({'file': file})
                    print("Generating %s" % (self.filemock_c))
                    self.mockc = Template("file.c.tmpl")
                    self.mockc.render({'file': file})
                else:
                    print("No function declarations found in %s" % (self.header_file))

                # restore cwd
                os.chdir(cwd_stored)

            def save(self):
                if self.mockh and self.mockc:
                    mockh_file = "%s/%s" % (self.args.outdir, self.filemock_h)
                    mockc_file = "%s/%s" % (self.args.outdir, self.filemock_c)
                    print("Saving %s" % os.path.realpath(mockh_file))
                    self.mockh.save(mockh_file)
                    print("Saving %s" % os.path.realpath(mockc_file))
                    self.mockc.save(mockc_file)

            def cleanup(self):
                if self.tempdir and not self.args.no_rm:
                    shutil.rmtree(self.tempdir)
开发者ID:alveko,项目名称:easymock,代码行数:98,代码来源:easymockgen.py


注:本文中的template.Template.save方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。