本文整理汇总了Python中validator.chromemanifest.ChromeManifest.get_triples方法的典型用法代码示例。如果您正苦于以下问题:Python ChromeManifest.get_triples方法的具体用法?Python ChromeManifest.get_triples怎么用?Python ChromeManifest.get_triples使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类validator.chromemanifest.ChromeManifest
的用法示例。
在下文中一共展示了ChromeManifest.get_triples方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_duplicate_subjects
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def test_duplicate_subjects():
"""Test that two triplets with the same subject can be retrieved."""
c = ChromeManifest("""
foo bar abc
foo bar def
foo bam test
oof rab cba
""", "chrome.manifest")
assert len(list(c.get_triples(subject="foo"))) == 3
assert len(list(c.get_triples(subject="foo", predicate="bar"))) == 2
assert len(list(c.get_triples(subject="foo",
predicate="bar",
object_="abc"))) == 1
示例2: test_lines
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def test_lines():
"""Test that the correct line numbers are given in a chrome.manifest."""
c = ChromeManifest("""
zero foo bar
one bar foo
two abc def
#comment
four def abc
""".strip(), "chrome.manifest")
eq_(list(c.get_triples(subject="zero"))[0]["line"], 1)
eq_(list(c.get_triples(subject="one"))[0]["line"], 2)
eq_(list(c.get_triples(subject="two"))[0]["line"], 3)
eq_(list(c.get_triples(subject="four"))[0]["line"], 5)
示例3: test_duplicate_subjects
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def test_duplicate_subjects():
"""Test that two triplets with the same subject can be retrieved."""
c = ChromeManifest("""
foo bar abc
foo bar def
foo bam test
oof rab cba
""", 'chrome.manifest')
assert len(list(c.get_triples(subject='foo'))) == 3
assert len(list(c.get_triples(subject='foo', predicate='bar'))) == 2
assert len(list(c.get_triples(subject='foo',
predicate='bar',
object_='abc'))) == 1
示例4: test_lines
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def test_lines():
"""Test that the correct line numbers are given in a chrome.manifest."""
c = ChromeManifest("""
zero foo bar
one bar foo
two abc def
#comment
four def abc
""".strip(), 'chrome.manifest')
assert list(c.get_triples(subject='zero'))[0]['line'] == 1
assert list(c.get_triples(subject='one'))[0]['line'] == 2
assert list(c.get_triples(subject='two'))[0]['line'] == 3
assert list(c.get_triples(subject='four'))[0]['line'] == 5
示例5: _get_locales
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def _get_locales(err, xpi_package):
"Returns a list of locales from the chrome.manifest file."
# Retrieve the chrome.manifest if it's cached.
if err is not None and \
err.get_resource("chrome.manifest"): # pragma: no cover
chrome = err.get_resource("chrome.manifest")
else:
chrome_data = xpi_package.read("chrome.manifest")
chrome = ChromeManifest(chrome_data)
if err is not None:
err.save_resource("chrome.manifest", chrome)
pack_locales = chrome.get_triples("locale")
locales = {}
# Find all of the locales referenced in the chrome.manifest file.
for locale in pack_locales:
locale_jar = locale["object"].split()
location = locale_jar[-1]
if not location.startswith("jar:"):
continue
full_location = location[4:].split("!")
locale_desc = {"predicate": locale["predicate"],
"path": full_location[0],
"target": full_location[1],
"name": locale_jar[0]}
locale_name = "%s:%s" % (locale["predicate"], locale_jar[0])
if locale_name not in locales:
locales[locale_name] = locale_desc
return locales
示例6: _list_locales
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def _list_locales(err, xpi_package=None):
"Returns a raw list of locales from chrome.manifest"
chrome = None
if xpi_package is not None:
# Handle a reference XPI
chrome = ChromeManifest(xpi_package.read("chrome.manifest"), "chrome.manifest")
else:
# Handle the current XPI
chrome = err.get_resource("chrome.manifest")
if not chrome:
return None
pack_locales = chrome.get_triples("locale")
return list(pack_locales)
示例7: test_open
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def test_open():
"""Open a chrome file and ensure that data can be pulled from it."""
chrome = open("tests/resources/chromemanifest/chrome.manifest")
chrome_data = chrome.read()
manifest = ChromeManifest(chrome_data, "chrome.manifest")
assert manifest is not None
assert manifest.get_value("locale", "basta")["object"] == "resource"
g_obj = list(manifest.get_objects("subject", "predicate"))
assert len(g_obj) == 1
assert g_obj[0] == "object"
obj_resource = list(manifest.get_triples(None, None, "resource"))
assert len(obj_resource) == 2
pred_pred = list(manifest.get_triples(None, "predicate", None))
assert len(pred_pred) == 2
sub_locale = list(manifest.get_triples("locale", None, None))
assert len(sub_locale) == 2
示例8: get_translation_xpi
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def get_translation_xpi(request, project_slug, lang_code):
""" Compile project's XPI in given language
"""
project = get_object_or_404(Project, slug=project_slug)
language = get_object_or_404(Language, code=lang_code)
xpi = get_object_or_404(XpiFile, project=project)
zip_orig = zipfile.ZipFile(os.path.join(settings.XPI_DIR,xpi.filename), "r")
zip_buffer = StringIO()
zip_file = zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED)
# copy all the contents from original file except META-INF,
# to make it unsigned
for item in zip_orig.infolist():
if not (item.filename.startswith('META-INF') or
item.filename == 'chrome.manifest'):
fn = item.filename
data = zip_orig.read(item.filename)
zip_file.writestr(item, data)
# write our localization
for resource in Resource.objects.filter(project=project):
template = _compile_translation_template(resource, language)
zip_file.writestr("tx-locale/%s/%s" % (lang_code, resource.name), template)
chrome_str = zip_orig.read("chrome.manifest")
manifest = ChromeManifest(chrome_str, "manifest")
zip_file.writestr("chrome.manifest", chrome_str +\
"\nlocale %(predicate)s %(code)s tx-locale/%(code)s/\n" % {
'predicate': list(manifest.get_triples("locale"))[0]['predicate'],
'code': lang_code,
})
zip_file.close()
zip_buffer.flush()
zip_contents = zip_buffer.getvalue()
response = HttpResponse(mimetype='application/x-xpinstall')
response['Content-Disposition'] = 'filename=%s.xpi' % project_slug
response.write(zip_contents)
return response
示例9: populate_chrome_manifest
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def populate_chrome_manifest(err, xpi_package):
"Loads the chrome.manifest if it's present"
if "chrome.manifest" in xpi_package:
chrome_data = xpi_package.read("chrome.manifest")
chrome = ChromeManifest(chrome_data, "chrome.manifest")
chrome_recursion_buster = set()
# Handle the case of manifests linked from the manifest.
def get_linked_manifest(path, from_path, from_chrome, from_triple):
if path in chrome_recursion_buster:
err.warning(
err_id=("submain", "populate_chrome_manifest",
"recursion"),
warning="Linked manifest recursion detected.",
description="A chrome registration file links back to "
"itself. This can cause a multitude of "
"issues.",
filename=path)
return
# Make sure the manifest is properly linked
if path not in xpi_package:
err.notice(
err_id=("submain", "populate_chrome_manifest", "linkerr"),
notice="Linked manifest could not be found.",
description=["A linked manifest file could not be found "
"in the package.",
"Path: %s" % path],
filename=from_path,
line=from_triple["line"],
context=from_chrome.context)
return
chrome_recursion_buster.add(path)
manifest = ChromeManifest(xpi_package.read(path), path)
for triple in manifest.triples:
yield triple
if triple["subject"] == "manifest":
for subtriple in get_linked_manifest(
triple["predicate"], path, manifest, triple):
yield subtriple
chrome_recursion_buster.discard(path)
chrome_recursion_buster.add("chrome.manifest")
# Search for linked manifests in the base manifest.
for extra_manifest in chrome.get_triples(subject="manifest"):
# When one is found, add its triples to our own.
for triple in get_linked_manifest(extra_manifest["predicate"],
"chrome.manifest", chrome,
extra_manifest):
chrome.triples.append(triple)
chrome_recursion_buster.discard("chrome.manifest")
# Create a reference so we can get the chrome manifest later, but make
# it pushable so we don't run chrome manifests in JAR files.
err.save_resource("chrome.manifest", chrome, pushable=True)
# Create a non-pushable reference for tests that need to access the
# chrome manifest from within JAR files.
err.save_resource("chrome.manifest_nopush", chrome, pushable=False)
示例10: __init__
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def __init__(self, filename, project=None, release=None, name=None):
"""
Fills in a list of locales from the chrome.manifest file.
"""
Bundle.__init__(self, project, release)
self.xpi = XPIManager(filename, name=name)
# here we will store managers for jarfiles
self.jarfiles = {}
chrome = ChromeManifest(self.xpi.read("chrome.manifest"), "manifest")
locales = list(chrome.get_triples("locale"))
if not locales:
return None
# read the list
for locale in locales:
code, location = locale["object"].split()
# finding out the language of the locale
try:
lang = self._get_lang(code)
except Language.DoesNotExist:
self.log("Locale %s SKIPPED" % code, "font-weight:bold")
continue
# Locales can be bundled in JARs
jarred = location.startswith("jar:")
if jarred:
# We just care about the JAR path
location = location[4:]
split_location = location.split("!", 2)
# Ignore malformed JAR URIs.
if len(split_location) < 2:
continue
jarname, location = split_location
# missing file mentioned
if jarname not in self.xpi:
continue
# may be we have already read this one
if jarname in self.jarfiles:
package = self.jarfiles[jarname]
else:
jar = StringIO(self.xpi.read(jarname))
package = XPIManager(jar, mode="r", name=jarname)
else:
package = self.xpi
# and now we read files from there
location = location.strip('/')
result = {}
for f in package.package_contents():
f = f.strip("/")
if f.startswith(location) and f != location:
result[f.split("/")[-1]] = package.read(f)
# file with same name in different jars can get overwritten
if lang not in self.locales:
self.locales[lang] = result
else:
self.locales[lang].update(result)
示例11: populate_chrome_manifest
# 需要导入模块: from validator.chromemanifest import ChromeManifest [as 别名]
# 或者: from validator.chromemanifest.ChromeManifest import get_triples [as 别名]
def populate_chrome_manifest(err, xpi_package):
"Loads the chrome.manifest if it's present"
if 'chrome.manifest' in xpi_package:
chrome_data = xpi_package.read('chrome.manifest')
chrome = ChromeManifest(chrome_data, 'chrome.manifest')
chrome_recursion_buster = set()
# Handle the case of manifests linked from the manifest.
def get_linked_manifest(path, from_path, from_chrome, from_triple):
if path in chrome_recursion_buster:
err.warning(
err_id=('submain', 'populate_chrome_manifest',
'recursion'),
warning='Linked manifest recursion detected.',
description='A chrome registration file links back to '
'itself. This can cause a multitude of '
'issues.',
filename=path)
return
# Make sure the manifest is properly linked
if path not in xpi_package:
err.notice(
err_id=('submain', 'populate_chrome_manifest', 'linkerr'),
notice='Linked manifest could not be found.',
description=('A linked manifest file could not be found '
'in the package.',
'Path: %s' % path),
filename=from_path,
line=from_triple['line'],
context=from_chrome.context)
return
chrome_recursion_buster.add(path)
manifest = ChromeManifest(xpi_package.read(path), path)
for triple in manifest.triples:
yield triple
if triple['subject'] == 'manifest':
subpath = triple['predicate']
# If the path is relative, make it relative to the current
# file.
if not subpath.startswith('/'):
subpath = '%s/%s' % (
'/'.join(path.split('/')[:-1]), subpath)
subpath = subpath.lstrip('/')
for subtriple in get_linked_manifest(
subpath, path, manifest, triple):
yield subtriple
chrome_recursion_buster.discard(path)
chrome_recursion_buster.add('chrome.manifest')
# Search for linked manifests in the base manifest.
for extra_manifest in chrome.get_triples(subject='manifest'):
# When one is found, add its triples to our own.
for triple in get_linked_manifest(extra_manifest['predicate'],
'chrome.manifest', chrome,
extra_manifest):
chrome.triples.append(triple)
chrome_recursion_buster.discard('chrome.manifest')
# Create a reference so we can get the chrome manifest later, but make
# it pushable so we don't run chrome manifests in JAR files.
err.save_resource('chrome.manifest', chrome, pushable=True)
# Create a non-pushable reference for tests that need to access the
# chrome manifest from within JAR files.
err.save_resource('chrome.manifest_nopush', chrome, pushable=False)