Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_cache_uptodate = True
aliases = {} # Maps alias Mime types to canonical names
inheritance = defaultdict(set) # Maps to sets of parent mime types.
# Load aliases
for path in BaseDirectory.load_data_paths(os.path.join('mime', 'aliases')):
with open(path, 'r') as f:
for line in f:
alias, canonical = line.strip().split(None, 1)
aliases[alias] = canonical
# Load filename patterns (globs)
globs = GlobDB()
for path in BaseDirectory.load_data_paths(os.path.join('mime', 'globs2')):
globs.merge_file(path)
globs.finalise()
# Load magic sniffing data
magic = MagicDB()
for path in BaseDirectory.load_data_paths(os.path.join('mime', 'magic')):
magic.merge_file(path)
magic.finalise()
# Load subclasses
for path in BaseDirectory.load_data_paths(os.path.join('mime', 'subclasses')):
with open(path, 'r') as f:
for line in f:
sub, parent = line.strip().split(None, 1)
inheritance[sub].add(parent)
def _cache_database():
global globs, magic, aliases, inheritance, _cache_uptodate
_cache_uptodate = True
aliases = {} # Maps alias Mime types to canonical names
inheritance = defaultdict(set) # Maps to sets of parent mime types.
# Load aliases
for path in BaseDirectory.load_data_paths(os.path.join('mime', 'aliases')):
with open(path, 'r') as f:
for line in f:
alias, canonical = line.strip().split(None, 1)
aliases[alias] = canonical
# Load filename patterns (globs)
globs = GlobDB()
for path in BaseDirectory.load_data_paths(os.path.join('mime', 'globs2')):
globs.merge_file(path)
globs.finalise()
# Load magic sniffing data
magic = MagicDB()
for path in BaseDirectory.load_data_paths(os.path.join('mime', 'magic')):
magic.merge_file(path)
magic.finalise()
def get_data_file(filename, package=PACKAGE_NAME):
"""
Return path to @filename if it exists
anywhere in the data paths, else raise ResourceLookupError.
"""
data_paths = []
try:
from . import version_subst
except ImportError:
first_datadir = "./data"
else:
first_datadir = os.path.join(version_subst.DATADIR, package)
data_paths.append(first_datadir)
for data_path in base.load_data_paths(package):
if not data_path in data_paths:
data_paths.append(data_path)
for direc in data_paths:
file_path = os.path.join(direc, filename)
if os.path.exists(file_path):
return file_path
if package == PACKAGE_NAME:
raise ResourceLookupError("Resource %s not found" % filename)
else:
raise ResourceLookupError("Resource %s in package %s not found" %
(filename, package))
def _load(self):
"Loads comment for current language. Use get_comment() instead."
resource = os.path.join('mime', self.media, self.subtype + '.xml')
for path in BaseDirectory.load_data_paths(resource):
doc = minidom.parse(path)
if doc is None:
continue
for comment in doc.documentElement.getElementsByTagNameNS(FREE_NS, 'comment'):
lang = comment.getAttributeNS(XML_NAMESPACE, 'lang') or 'en'
goodness = 1 + (lang in xdg.Locale.langs)
if goodness > self._comment[0]:
self._comment = (goodness, _get_node_data(comment))
if goodness == 2: return
def get_data_paths():
# Add "./data" in workdir for running from builddir
data_paths = []
data_paths.append("./data")
data_paths.extend(base.load_data_paths(PACKAGE_NAME))
return data_paths
def xdg_static_folder():
for path in BaseDirectory.load_data_paths('pbs'):
return path
return 'static'
def _load(self):
"Loads comment for current language. Use get_comment() instead."
resource = os.path.join('mime', self.media, self.subtype + '.xml')
for path in BaseDirectory.load_data_paths(resource):
doc = minidom.parse(path)
if doc is None:
continue
for comment in doc.documentElement.getElementsByTagNameNS(FREE_NS, 'comment'):
lang = comment.getAttributeNS(XML_NAMESPACE, 'lang') or 'en'
goodness = 1 + (lang in xdg.Locale.langs)
if goodness > self._comment[0]:
self._comment = (goodness, _get_node_data(comment))
if goodness == 2: return
def lookup(path):
"""Return location for @path if exists, else none"""
return next(xdg.BaseDirectory.load_data_paths(*path), None)