mirror of https://github.com/sgoudham/Enso-Bot.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
5 years ago
|
from . import abc
|
||
|
|
||
|
from ._compat import Path, ZipPath
|
||
|
|
||
|
|
||
|
class FileReader(abc.TraversableResources):
|
||
|
def __init__(self, loader):
|
||
|
self.path = Path(loader.path).parent
|
||
|
|
||
|
def resource_path(self, resource):
|
||
|
"""
|
||
|
Return the file system path to prevent
|
||
|
`resources.path()` from creating a temporary
|
||
|
copy.
|
||
|
"""
|
||
|
return str(self.path.joinpath(resource))
|
||
|
|
||
|
def files(self):
|
||
|
return self.path
|
||
|
|
||
|
|
||
|
class ZipReader(abc.TraversableResources):
|
||
|
def __init__(self, loader, module):
|
||
|
_, _, name = module.rpartition('.')
|
||
|
prefix = loader.prefix.replace('\\', '/') + name + '/'
|
||
|
self.path = ZipPath(loader.archive, prefix)
|
||
|
|
||
|
def open_resource(self, resource):
|
||
|
try:
|
||
|
return super().open_resource(resource)
|
||
|
except KeyError as exc:
|
||
|
raise FileNotFoundError(exc.args[0])
|
||
|
|
||
|
def is_resource(self, path):
|
||
|
# workaround for `zipfile.Path.is_file` returning true
|
||
|
# for non-existent paths.
|
||
|
target = self.files().joinpath(path)
|
||
|
return target.is_file() and target.exists()
|
||
|
|
||
|
def files(self):
|
||
|
return self.path
|