本文整理汇总了Python中em.Interpreter方法的典型用法代码示例。如果您正苦于以下问题:Python em.Interpreter方法的具体用法?Python em.Interpreter怎么用?Python em.Interpreter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类em
的用法示例。
在下文中一共展示了em.Interpreter方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_by_template
# 需要导入模块: import em [as 别名]
# 或者: from em import Interpreter [as 别名]
def generate_by_template(output_file, template_file, em_globals):
"""
Invokes empy intepreter to geneate output_file by the
given template_file and predefined em_globals dict
"""
# check if folder exists:
folder_name = os.path.dirname(output_file)
if not os.path.exists(folder_name):
os.makedirs(folder_name)
ofile = open(output_file, 'w')
# todo, reuse interpreter
interpreter = em.Interpreter(output=ofile, globals=em_globals, options={
em.RAW_OPT: True, em.BUFFERED_OPT: True})
try:
interpreter.file(open(template_file))
except OSError as e:
ofile.close()
os.remove(output_file)
raise
interpreter.shutdown()
ofile.close()
return True
示例2: empy
# 需要导入模块: import em [as 别名]
# 或者: from em import Interpreter [as 别名]
def empy(template_name, data, options=None):
template_path = os.path.join(
os.path.dirname(__file__), 'templates', template_name)
output = StringIO()
try:
interpreter = Interpreter(output=output, options=options)
with open(template_path, 'r') as h:
content = h.read()
interpreter.string(content, template_path, locals=data)
value = output.getvalue()
return value
except Exception as e:
print("%s processing template '%s'" %
(e.__class__.__name__, template_name), file=sys.stderr)
raise
finally:
interpreter.shutdown()
示例3: _expand_template
# 需要导入模块: import em [as 别名]
# 或者: from em import Interpreter [as 别名]
def _expand_template(template_file, data, output_file):
output = StringIO()
interpreter = em.Interpreter(
output=output,
options={
em.BUFFERED_OPT: True,
em.RAW_OPT: True,
},
globals=data,
)
with open(template_file, 'r') as h:
try:
interpreter.file(h)
content = output.getvalue()
except Exception as e:
if os.path.exists(output_file):
os.remove(output_file)
print("Exception when expanding '%s' into '%s': %s" %
(template_file, output_file, e), file=sys.stderr)
raise
finally:
interpreter.shutdown()
if os.path.exists(output_file):
with open(output_file, 'r') as h:
if h.read() == content:
return
else:
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, 'w') as h:
h.write(content)
示例4: evaluate_template
# 需要导入模块: import em [as 别名]
# 或者: from em import Interpreter [as 别名]
def evaluate_template(template_name, data):
global _interpreter
# create copy before manipulating
data = dict(data)
data['TEMPLATE'] = _evaluate_template
template_path = os.path.join(os.path.dirname(__file__), template_name)
output = StringIO()
try:
_interpreter = em.Interpreter(
output=output,
options={
em.BUFFERED_OPT: True,
em.RAW_OPT: True,
})
with open(template_path, 'r') as h:
content = h.read()
_interpreter.invoke(
'beforeFile', name=template_name, file=h, locals=data)
_interpreter.string(content, template_path, locals=data)
_interpreter.invoke('afterFile')
return output.getvalue()
except Exception as e: # noqa: F841
print(
f"{e.__class__.__name__} processing template '{template_name}'",
file=sys.stderr)
raise
finally:
_interpreter.shutdown()
_interpreter = None
示例5: expand_template
# 需要导入模块: import em [as 别名]
# 或者: from em import Interpreter [as 别名]
def expand_template(template_path, destination_path, data):
"""
Expand an EmPy template.
The directory of the destination path is created if necessary.
:param template_path: The patch of the template file
:param destination_path: The path of the generated expanded file
:param dict data: The data used for expanding the template
:raises: Any exception which `em.Interpreter.string` might raise
"""
output = StringIO()
try:
# disable OVERRIDE_OPT to avoid saving / restoring stdout
interpreter = CachingInterpreter(
output=output, options={OVERRIDE_OPT: False})
with template_path.open('r') as h:
content = h.read()
interpreter.string(content, str(template_path), locals=data)
output = output.getvalue()
except Exception as e: # noqa: F841
logger.error(
"{e.__class__.__name__} processing template '{template_path}'"
.format_map(locals()))
raise
else:
os.makedirs(str(destination_path.parent), exist_ok=True)
# if the destination_path is a symlink remove the symlink
# to avoid writing to the symlink destination
if destination_path.is_symlink():
destination_path.unlink()
with destination_path.open('w') as h:
h.write(output)
finally:
interpreter.shutdown()
示例6: generate_install
# 需要导入模块: import em [as 别名]
# 或者: from em import Interpreter [as 别名]
def generate_install(self, job_start_file):
# Default for Upstart is /etc/ros/DISTRO/JOBNAME.d
self._set_job_path()
# User-specified launch files.
self._add_job_files()
# This is optional to support the old --augment flag where a "job" only adds
# launch files to an existing configuration.
if (self.job.generate_system_files):
# Share a single instance of the empy interpreter.
self.interpreter = em.Interpreter(globals=self.job.__dict__.copy())
self.installation_files[os.path.join(self.root, "etc/init", self.job.name + ".conf")] = {
"content": self._fill_template("templates/job.conf.em"), "mode": 0o644}
self.installation_files[os.path.join(self.root, "usr/sbin", self.job.name + "-start")] = {
"content": self._fill_template("templates/%s"%job_start_file), "mode": 0o755}
self.installation_files[os.path.join(self.root, "usr/sbin", self.job.name + "-stop")] = {
"content": self._fill_template("templates/job-stop.em"), "mode": 0o755}
self.interpreter.shutdown()
# Add an annotation file listing what has been installed. This is a union of what's being
# installed now with what has been installed previously, so that an uninstall should remove
# all of it. A more sophisticated future implementation could track contents or hashes and
# thereby warn users when a new installation is stomping a change they have made.
self._load_installed_files_set()
self.installed_files_set.update(self.installation_files.keys())
# Remove the job directory. This will fail if it is not empty, and notify the user.
self.installed_files_set.add(self.job.job_path)
# Remove the annotation file itself.
self.installed_files_set.add(self.installed_files_set_location)
self.installation_files[self.installed_files_set_location] = {
"content": "\n".join(self.installed_files_set)}
return self.installation_files