本文整理汇总了Python中importlib.resources.path方法的典型用法代码示例。如果您正苦于以下问题:Python resources.path方法的具体用法?Python resources.path怎么用?Python resources.path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类importlib.resources
的用法示例。
在下文中一共展示了resources.path方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: norm_and_check
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def norm_and_check(source_tree, requested):
"""Normalise and check a backend path.
Ensure that the requested backend path is specified as a relative path,
and resolves to a location under the given source tree.
Return an absolute version of the requested path.
"""
if os.path.isabs(requested):
raise ValueError("paths must be relative")
abs_source = os.path.abspath(source_tree)
abs_requested = os.path.normpath(os.path.join(abs_source, requested))
# We have to use commonprefix for Python 2.7 compatibility. So we
# normalise case to avoid problems because commonprefix is a character
# based comparison :-(
norm_source = os.path.normcase(abs_source)
norm_requested = os.path.normcase(abs_requested)
if os.path.commonprefix([norm_source, norm_requested]) != norm_source:
raise ValueError("paths must be inside source tree")
return abs_requested
示例2: _in_proc_script_path
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def _in_proc_script_path():
return resources.path(__package__, '_in_process.py')
示例3: _expand_template
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [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: _create_folder
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def _create_folder(folder_name, base_directory, exist_ok=True):
folder_path = os.path.join(base_directory, folder_name)
print('creating folder', folder_path)
os.makedirs(folder_path, exist_ok=exist_ok)
return folder_path
示例5: _create_template_file
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def _create_template_file(
template_subdir, template_file_name, output_directory, output_file_name, template_config
):
full_package = 'ros2pkg.resource.' + template_subdir
with importlib_resources.path(full_package, template_file_name) as path:
template_path = str(path)
if not os.path.exists(template_path):
raise FileNotFoundError('template not found:', template_path)
output_file_path = os.path.join(output_directory, output_file_name)
print('creating', output_file_path)
_expand_template(template_path, template_config, output_file_path)
示例6: genBuildGradle
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def genBuildGradle(projectType, team):
with resources.path(__name__, "templates") as path:
with open(
os.path.join(path, projectType, "build.gradle.mako"), "r"
) as template:
return Template(template.read()).render(team=team)
示例7: get_policy_default
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def get_policy_default(name):
with importlib_resources.path('sros2.policy.defaults', name) as path:
return str(path)
示例8: get_policy_schema
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def get_policy_schema(name):
with importlib_resources.path('sros2.policy.schemas', name) as path:
return str(path)
示例9: get_policy_template
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def get_policy_template(name):
with importlib_resources.path('sros2.policy.templates', name) as path:
return str(path)
示例10: get_transport_default
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def get_transport_default(transport, name):
module = 'sros2.policy.defaults.' + transport
with importlib_resources.path(module, name) as path:
return str(path)
示例11: get_transport_schema
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def get_transport_schema(transport, name):
module = 'sros2.policy.schemas.' + transport
with importlib_resources.path(module, name) as path:
return str(path)
示例12: load_policy
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def load_policy(policy_file_path):
if not os.path.isfile(policy_file_path):
raise FileNotFoundError("policy file '%s' does not exist" % policy_file_path)
policy = etree.parse(policy_file_path)
policy.xinclude()
try:
policy_xsd_path = get_policy_schema('policy.xsd')
policy_xsd = etree.XMLSchema(etree.parse(policy_xsd_path))
policy_xsd.assertValid(policy)
except etree.DocumentInvalid as e:
raise RuntimeError(str(e))
return policy
示例13: cve_jsonvalidation
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def cve_jsonvalidation(json_doc_path, version, mode="public"):
"""
Validate CVE JSON format
:param json_doc_path string, absolute path for the
:returns bool
"""
# fetch from official repository
if version == 4:
if mode == "public":
os.system(
"mkdir -p /tmp/cve; cd /tmp/cve/ ; \
wget https://raw.githubusercontent.com/CVEProject/\
automation-working-group/master/cve_json_schema/CVE_JSON_4.0_min_public.schema"
)
schema_path = "/tmp/cve/CVE_JSON_4.0_min_public.schema"
elif mode == "reject":
os.system(
"mkdir -p /tmp/cve; cd /tmp/cve/ ; \
wget https://raw.githubusercontent.com/CVEProject/\
automation-working-group/master/cve_json_schema/CVE_JSON_4.0_min_reject.schema"
)
schema_path = "/tmp/cve/CVE_JSON_4.0_min_reject.schema"
elif mode == "reserved":
os.system(
"mkdir -p /tmp/cve; cd /tmp/cve/ ; \
wget https://raw.githubusercontent.com/CVEProject/\
automation-working-group/master/cve_json_schema/CVE_JSON_4.0_min_reserved.schema"
)
schema_path = "/tmp/cve/CVE_JSON_4.0_min_reserved.schema"
else:
print("Mode " + mode + "not implemented")
raise NotImplementedError
elif version == 3:
raise NotImplementedError
else:
print("Invalid version")
raise SystemExit
# via a file, e.g.
with open(schema_path, "r") as schema:
schema_doc = json.load(schema)
# Open the file for reading
with open(json_doc_path, "r") as document:
try:
json_doc = json.load(document)
except ValueError as err:
sys.stderr.write("Failed to parse JSON : \n")
sys.stderr.write(" " + str(err) + "\n")
raise SystemExit
try:
validate(json_doc, schema_doc)
green("Record passed validation \n")
except jsonschema.exceptions.ValidationError as incorrect:
validator = Draft4Validator(schema_doc)
errors = sorted(validator.iter_errors(json_doc), key=lambda e: e.path)
for error in errors:
red("Record did not pass: \n")
sys.stderr.write(str(error.message) + "\n")
示例14: genRobotCode
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def genRobotCode(projectType, config):
if projectType == "Simple":
with resources.path(__name__, "templates") as path:
with open(os.path.join(path, "Simple", "Robot.java.mako"), "r") as template:
return Template(template.read()).render(
epr=config["encoderEPR"],
units=config["units"],
ports=config["motorPorts"],
inverted=config["motorsInverted"],
controllers=config["controllerTypes"],
encoderports=config["encoderPorts"],
encoderinv=config["encoderInverted"],
)
elif projectType == "Talon":
with resources.path(__name__, "templates") as path:
with open(os.path.join(path, "Talon", "Robot.java.mako"), "r") as template:
return Template(template.read()).render(
epr=config["encoderEPR"],
units=config["units"],
ports=config["motorPorts"],
inverted=config["motorsInverted"],
controllers=config["controllerTypes"],
encoderinv=config["encoderInverted"],
)
elif projectType == "SparkMax_Brushed":
with resources.path(__name__, "templates") as path:
with open(
os.path.join(path, "SparkMax_Brushed", "Robot.java.mako"), "r"
) as template:
return Template(template.read()).render(
epr=config["encoderEPR"],
units=config["units"],
gearing=config["gearing"],
ports=config["motorPorts"],
inverted=config["motorsInverted"],
encoderinv=config["encoderInverted"],
)
elif projectType == "SparkMax_Brushless":
with resources.path(__name__, "templates") as path:
with open(
os.path.join(path, "SparkMax_Brushless", "Robot.java.mako"), "r"
) as template:
return Template(template.read()).render(
units=config["units"],
gearing=config["gearing"],
ports=config["motorPorts"],
inverted=config["motorsInverted"],
)
示例15: genRobotCode
# 需要导入模块: from importlib import resources [as 别名]
# 或者: from importlib.resources import path [as 别名]
def genRobotCode(projectType, config):
if projectType == "Simple":
with resources.path(__name__, "templates") as path:
with open(os.path.join(path, "Simple", "Robot.java.mako"), "r") as template:
return Template(template.read()).render(
epr=config["encoderEPR"],
ports=config["motorPorts"],
inverted=config["motorsInverted"],
controllers=config["controllerTypes"],
encoderports=config["encoderPorts"],
encoderinv=config["encoderInverted"],
offset=config["offset"],
units=config["units"],
)
elif projectType == "Talon":
with resources.path(__name__, "templates") as path:
with open(os.path.join(path, "Talon", "Robot.java.mako"), "r") as template:
return Template(template.read()).render(
epr=config["encoderEPR"],
ports=config["motorPorts"],
inverted=config["motorsInverted"],
controllers=config["controllerTypes"],
encoderinv=config["encoderInverted"],
offset=config["offset"],
units=config["units"],
)
elif projectType == "SparkMax_Brushed":
with resources.path(__name__, "templates") as path:
with open(
os.path.join(path, "SparkMax_Brushed", "Robot.java.mako"), "r"
) as template:
return Template(template.read()).render(
epr=config["encoderEPR"],
ports=config["motorPorts"],
gearing=config["gearing"],
inverted=config["motorsInverted"],
encoderinv=config["encoderInverted"],
offset=config["offset"],
units=config["units"],
)
elif projectType == "SparkMax_Brushless":
with resources.path(__name__, "templates") as path:
with open(
os.path.join(path, "SparkMax_Brushless", "Robot.java.mako"), "r"
) as template:
return Template(template.read()).render(
ports=config["motorPorts"],
gearing=config["gearing"],
inverted=config["motorsInverted"],
offset=config["offset"],
units=config["units"],
)