Beispiel #1
0
 def getResourceStream(di, name):
     try:
         for rn in pkg_resources.ResourceManager().resource_listdir(di, ''):
             fname = PurePath(rn).name
             if (Utils.endsWithString(name, fname, True)):
                 return pkg_resources.ResourceManager().resource_stream(
                     di, fname)
     except:
         return None
Beispiel #2
0
    def test_get_cache_path_race(self, tmpdir):
        # Patch to os.path.isdir to create a race condition
        def patched_isdir(dirname, unpatched_isdir=pkg_resources.isdir):
            patched_isdir.dirnames.append(dirname)

            was_dir = unpatched_isdir(dirname)
            if not was_dir:
                os.makedirs(dirname)
            return was_dir

        patched_isdir.dirnames = []

        # Get a cache path with a "race condition"
        mgr = pkg_resources.ResourceManager()
        mgr.set_extraction_path(str(tmpdir))

        archive_name = os.sep.join(('foo', 'bar', 'baz'))
        with mock.patch.object(pkg_resources, 'isdir', new=patched_isdir):
            mgr.get_cache_path(archive_name)

        # Because this test relies on the implementation details of this
        # function, these assertions are a sentinel to ensure that the
        # test suite will not fail silently if the implementation changes.
        called_dirnames = patched_isdir.dirnames
        assert len(called_dirnames) == 2
        assert called_dirnames[0].split(os.sep)[-2:] == ['foo', 'bar']
        assert called_dirnames[1].split(os.sep)[-1:] == ['foo']
Beispiel #3
0
 def _load_api_spec(self):
     """
     Gets the API specification of the module specified
     """
     return yaml.load(pkg_resources.ResourceManager().resource_stream(
         'mistk.evaluation', '/server/swagger/swagger.yaml'),
                      Loader=yaml.FullLoader)
Beispiel #4
0
 def _load_api_spec(self):
     """
     Gets the API specification of the module specified
     
     :param module: THe name of the module
     """
     return yaml.load(pkg_resources.ResourceManager().resource_stream('mistk', '/model/server/swagger/swagger.yaml'), Loader=yaml.SafeLoader)
Beispiel #5
0
    def _format_stream_predict_details(self, predictions, prediction_details):
        """
        Format stream predict details in the form of NGX Markdown 
        :param predictions: predictions for each streaming image
        :param prediction_details: prediction probabilities for each streaming image
        
        """
        details = "# ATL Logistic Regression Model Results"
        details += "\nThese section contains details about the **ATL Logistic Regression Model Results**.<br/><br/>"
        details += "<br/><br/>"

        metric_image_path = 'metrics-test.png'
        metric_image_data = pkg_resources.ResourceManager().resource_stream(
            __name__, 'metrics-test.png')
        encoded_bytes = base64.b64encode(
            metric_image_data.read()).decode('UTF-8')
        details += "\n## Initial Training Results\n"
        details += f"\n![Test](data:image/png;base64,{encoded_bytes})"
        details += "<br/><br/>"

        details += "\n## Trained Model Streaming Results"
        details += "<br/><br/>"

        for image, preds in prediction_details.items():
            details += f"\n#### For image {image} with prediction of {predictions[image]}"
            details += "\n\n| Class        | Probability |"
            details += "\n| ------------- |:-------------:|"
            for i in range(len(self._regr.classes_)):
                details += f"\n| {self._regr.classes_[i]}        | {round(preds[0][i], 3)} |"
            details += "\n\n"

        return details
Beispiel #6
0
 def getResourcesNames(di, exts):
     ee = Utils.splitString(exts, ';', True)
     res = []
     for rn in pkg_resources.ResourceManager().resource_listdir(di, ''):
         for e in ee:
             if (PurePath(rn).suffix == e):
                 res.append(rn)
                 break
     return res
Beispiel #7
0
def load_movie_trailer_site():
    '''Loads the movie trailer website.'''

    res_man_inst = pkg_resources.ResourceManager()
    data_file = res_man_inst.resource_stream("movie_trailer_site",
                                             "/data/movie_data.csv")
    data_loader = movie_builder.MovieBuilder(data_file)
    data_loader.load_info()
    fresh_tomatoes.open_movies_page(data_loader.get_movie_info())
Beispiel #8
0
 def __init__(self, egg, resource_name):
     self.manager = pkg_resources.ResourceManager()
     self.egg = pkg_resources.get_distribution(egg)
     self.resource_name = resource_name
     self.root_resource_path = self.egg.get_resource_filename(
         self.manager, resource_name)
     self.search_path = [self.root_resource_path]
     self.jinja_loader = jinja2.FileSystemLoader(self.search_path)
     self.jinja_env = jinja2.Environment(loader=self.jinja_loader)
Beispiel #9
0
def load_params(json_path=None):
    """
    Loads parameters .json file.
    """
    if json_path is None:
        json_path = pkgrs.ResourceManager().resource_filename(
            "foramgeochem", "resources/params.json")
    with open(json_path) as f:
        ps = json.load(f)
    return ps
Beispiel #10
0
def filename_resolver(package: str) -> Callable[[str], str]:

    resource_manager = pkg.ResourceManager()

    def func(resource):
        return resource_manager.resource_filename(package, resource)

    func.__doc__ = f"Computes file names for resources located in {package}"

    return func
Beispiel #11
0
def get_launcher(gui):
    """Use the exe files generated via
    https://bitbucket.org/vinay.sajip/simple_launcher"""
    if struct.calcsize('P') == 8:  # 64-bit
        bits = '64'
    else:
        bits = '32'
    name = 'launchers/%s%s.exe' % ('w' if gui else 't', bits)
    data = pkg_resources.ResourceManager().resource_stream('scriptlib', name)
    return data.read()
 def __init__(self):
     '''
     Constructor
     '''
     import pkg_resources
     rsman = pkg_resources.ResourceManager()
     dbname = rsman.resource_filename('lct', 'db/moon.db')
     self.conn = sqlite3.connect(dbname)
     self.features = {}
     self.club_type = set()
     self.feature_type = set()
Beispiel #13
0
def filename_resolver(package=None):
    if package is None:
        module = inspect.getmodule(inspect.stack()[1][0])
        package = module.__name__

    resource_manager = pkg.ResourceManager()

    def func(resource):
        return resource_manager.resource_filename(package, resource)

    func.__doc__ = f"Computes file names for resources located in {package}"

    return func
Beispiel #14
0
 def __create_resources(report_dir):
     """ Create and write the resources. """
     resource_manager = pkg_resources.ResourceManager()
     resource_module = app.__name__
     for resource_type, encoding in (('img', None), ('dist', None), ('html', 'utf-8')):
         resource_dir = (report_dir / resource_type) if resource_type != 'html' else report_dir
         filesystem.create_dir(resource_dir)
         for resource in resource_manager.resource_listdir(resource_module, resource_type):
             filename = resource_dir / resource
             contents = resource_manager.resource_string(resource_module, resource_type + '/' + resource)
             mode = 'w' if encoding else 'wb'
             contents = contents.decode(encoding) if encoding else contents
             filesystem.write_file(contents, filename, mode, encoding)
Beispiel #15
0
def filename_resolver(package=None):
    if package is None:
        caller_package = inspect.getmodule(inspect.stack()[1][0]).__name__
        package = caller_package

    resource_manager = pkg.ResourceManager()

    def func(resource):
        return resource_manager.resource_filename(package, resource)

    # noinspection PyCompatibility
    func.__doc__ = "Computes file names for resources located in {}".format(package)

    return func
Beispiel #16
0
 def __init__(self, egg_or_spec, resource_name, manager=None, root_resource=None):
     if pkg_resources is None:
         raise NotImplementedError("This class requires pkg_resources.")
     if isinstance(egg_or_spec, (str, unicode)):
         self.egg = pkg_resources.get_distribution(egg_or_spec)
     else:
         self.egg = egg_or_spec
     self.resource_name = resource_name
     if manager is None:
         manager = pkg_resources.ResourceManager()
     self.manager = manager
     if root_resource is None:
         root_resource = resource_name
     self.root_resource = os.path.normpath(root_resource)
Beispiel #17
0
def save_params(ps, json_path=None, overwrite=False):
    """
    Saves parameters .json file.
    """
    if json_path is None:
        json_path = pkgrs.ResourceManager().resource_filename(
            "foramgeochem", "resources/params.json")
    if os.path.exists(json_path) and not overwrite:
        raise ValueError(
            f'{json_path} exists. Set overwrite=True if you really want to lose the old one.'
        )
    else:
        with open(json_path, 'w') as f:
            json.dump(ps, f, indent=2)
        print(f'Parameters saved to {json_path}')
    def __init__(self, club_name):
        """Initialize the class.

        Parameters
        ----------
        club_name : str
            The name of the observing club to sort on. Values are Lunar and LunarII.
        """
        rsman = pkg_resources.ResourceManager()
        dbname = rsman.resource_filename('pylunar', 'db/lunar.db')
        self.conn = sqlite3.connect(dbname)
        self.club_name = club_name
        self.features = collections.OrderedDict()
        self.club_type = set()
        self.feature_type = set()
Beispiel #19
0
def _find_app_roots():
    """Return a list of root paths for each installed Django Application."""
    resource_manager = pkg_resources.ResourceManager()
    apps = list(settings.INSTALLED_APPS)
    apps.reverse()
    app_roots = []
    for app in apps:
        provider = pkg_resources.get_provider(app)
        try:
            app_root = provider.get_resource_filename(resource_manager,
                                                      'static')
        except KeyError:
            app_root = None
        if app_root is not None and os.path.isdir(app_root):
            if not app_root.endswith('/'):
                app_root += '/'
            app_roots.append(app_root)
    return app_roots
Beispiel #20
0
def package_open(package_name, filename):
    """
    Provides a context manager for opening a file within a package.
    If successful, the file will be opened in binary reading mode.

    Example:
        with package_open('some_package', 'path/to/file') as f:
            data = f.read()
    """
    provider = pkg_resources.get_provider(package_name)
    if not provider.has_resource(filename):
        raise IOError('No such file or directory [{}]: {}'.format(
            package_name, filename))

    manager = pkg_resources.ResourceManager()

    with contextlib.closing(
            six.BytesIO(provider.get_resource_string(manager, filename))) as f:
        yield f
    def test_resource_filename_rewrites_on_change(self):
        """
		If a previous call to get_resource_filename has saved the file, but
		the file has been subsequently mutated with different file of the
		same size and modification time, it should not be overwritten on a
		subsequent call to get_resource_filename.
		"""
        import mod
        manager = pkg_resources.ResourceManager()
        zp = pkg_resources.ZipProvider(mod)
        filename = zp.get_resource_filename(manager, 'data.dat')
        assert os.stat(filename).st_mtime == 1368379500
        f = open(filename, 'wb')
        f.write('hello, world?')
        f.close()
        os.utime(filename, (1368379500, 1368379500))
        filename = zp.get_resource_filename(manager, 'data.dat')
        f = open(filename)
        assert f.read() == 'hello, world!'
        manager.cleanup_resources()
Beispiel #22
0
def set_loop_type(library):
    global connection_type
    import pkg_resources
    
    # find module file
    manager = pkg_resources.ResourceManager()
    libPath = '%(library)s_net/net_%(library)s.py' % {'library':library}
    if not manager.resource_exists(__name__, libPath):
        raise ValueError('Unknown loop type: %r' % library)
    
    # load the module
    modulePath = manager.resource_filename(__name__, libPath)
    moduleName = 'net_%s' % library
    moduleFile, pathName, desc = imp.find_module(moduleName, [os.path.dirname(modulePath)])
    module = imp.load_module('rethinkdb.' + moduleName, moduleFile, pathName, desc)
    
    # set the connection type
    connection_type = module.Connection
    
    # cleanup
    manager.cleanup_resources()
Beispiel #23
0
def filename_resolver(package: str = None) -> Callable[[str], str]:
    """Create method to find data file name.

    Uses resource manager to handle all the cases of the deployment.

    Args:
        package: the package below which the data is stored.
                 Optional, if not specified, the package of caller will be used.

    Returns:
        callable which appends the argument to the package folder.
    """
    package = _resolve_package(package)
    resource_manager = pkg.ResourceManager()  # type: ignore

    def func(resource: str) -> str:
        return cast(str, resource_manager.resource_filename(package, resource))

    func.__doc__ = f"Computes file names for resources located in {package}"

    return func
Beispiel #24
0
def doctree(defs: [Def], outdir: str):
    env = jinja2.Environment(
        loader=jinja2.PackageLoader('doxyfront'),
        autoescape=jinja2.select_autoescape(['html']),
        trim_blocks=True,
    )
    global template
    template = env.get_template('doctree.html')

    render_jobs = []
    for d in defs:
        _generate_href(d)
        if d.page is not None:
            script = prepare_render(d)
            render_jobs.append((os.path.join(outdir, d.page), script))

    os.makedirs(outdir, exist_ok=True)
    with multiprocessing.Pool() as pool:
        pool.map(render_one, render_jobs)

    manager = pkg_resources.ResourceManager()
    provider = pkg_resources.get_provider('doxyfront')
    _extract_assets(manager, provider, 'assets', outdir)
Beispiel #25
0
    def set_loop_type(self, library=None):
        if library is None:
            self.connection_type = net.DefaultConnection
            return

        # find module file
        manager = pkg_resources.ResourceManager()
        libPath = "%(library)s_net/net_%(library)s.py" % {"library": library}
        if not manager.resource_exists(__name__, libPath):
            raise ValueError("Unknown loop type: %r" % library)

        # load the module
        modulePath = manager.resource_filename(__name__, libPath)
        moduleName = "net_%s" % library
        moduleFile, pathName, desc = imp.find_module(
            moduleName, [os.path.dirname(modulePath)])
        module = imp.load_module("rethinkdb." + moduleName, moduleFile,
                                 pathName, desc)

        # set the connection type
        self.connection_type = module.Connection

        # cleanup
        manager.cleanup_resources()
Beispiel #26
0
def expand_source_filenames(source_filenames):
    """
    Expands django application references in the source_filenames list to a
    list of source files by loading the media.conf file for that django
    application, if it exists.

    """
    resource_manager = pkg_resources.ResourceManager()
    new_source_list = []

    for source in source_filenames:
        if isinstance(source, tuple):
            try:
                new_source = source[0]
                new_group = source[1]
                # TODO: This code should use a variant of get_app_roots
                # so the resource access is better cached. I'd advise
                # altering get_app_roots to return an array of tuples
                # which can then be filtered by app name.
                if new_source in settings.INSTALLED_APPS:
                    try:
                        provider = pkg_resources.get_provider(new_source)
                        media_conf = provider.get_resource_string(
                            resource_manager, 'media.conf')
                        media_data = yaml.load(media_conf)
                        if media_data:
                            group = media_data[new_group]
                            if group:
                                new_source_list.extend(group)
                    except IOError:
                        pass
            except IndexError:
                pass
        else:
            new_source_list.append(source)
    return new_source_list
Beispiel #27
0
 def OnInit(self):
     self.res = xrc.XmlResource.Get()
     self.res.Load(
         pkg_resources.ResourceManager().resource_filename('mydupfilekiller', 'wx_gui.xrc'))
     self.init_frame()
     return True
Beispiel #28
0
    parser.add_argument('--script', dest='scriptfile',  action='store',
                        help="script file to load")
    parser.add_argument('--fdt', dest='dtbfile',  action='store',
                        help="dtb file to load")
    parser.add_argument('--ramfs', dest='ramfsfile',  action='store',
                        help="ramfs file to load")

    args = parser.parse_args()

    return args

if __name__ == '__main__':
    # Try to get boot files from the python package, or from local tree
    try:
        dist = pkg_resources.get_distribution('pyamlboot')
        fpath = dist.get_resource_filename(pkg_resources.ResourceManager(), "files")
    except:
        fpath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "files")
    boards = list_boards(fpath)
    args = parse_cmdline(boards)
    usb = BootUSB(args.board, fpath, args.upath)

    usb.load_uboot()

    if args.imagefile is not None:
        usb.write_file(args.imagefile, usb.UBOOT_IMAGEADDR, 512, True)

    if args.dtbfile is not None:
        usb.write_file(args.dtbfile, usb.UBOOT_DTBADDR)

    if args.scriptfile is not None:
Beispiel #29
0
import xml.etree.ElementTree as etree
from xml.parsers.expat import ExpatError

import tw2.core as twc
import tw2.core.middleware as tmw
import tw2.core.templating as templating
import six

try:
    import formencode as fe
    possible_errors = (twc.ValidationError, fe.Invalid)
except ImportError:
    possible_errors = (twc.ValidationError, )

rm = pk.ResourceManager()

_BOOLEAN_ATTRS = frozenset([
    'selected', 'checked', 'compact', 'declare', 'defer', 'disabled', 'ismap',
    'multiple', 'nohref', 'noresize', 'noshade', 'nowrap'
])


def replace_boolean_attrs(needle):
    """
    makes boolean attributes xml safe.
    """
    for attr in _BOOLEAN_ATTRS:
        eyelet = ' %s ' % attr
        if eyelet in needle:
            needle = needle.replace(eyelet, '%s="%s" ' % (attr, attr))
Beispiel #30
0
"""INI-file basd secrets reading."""
import os
import io

import configparser
from urllib.parse import urlparse

import pkg_resources

_resource_manager = pkg_resources.ResourceManager()


class MissingSecretsEnvironmentVariable(Exception):
    """Thrown when we try to interpolate an environment variable that does not exist."""


def resolve(uri):
    """Resolve secrets location."""

    # Do we look like a relative file (no URL scheme)
    if "://" not in uri:
        uri = "file://" + os.path.abspath(os.path.join(os.getcwd(), uri))

    parts = urlparse(uri)

    assert parts.scheme in (
        "resource",
        "file"), "Only resource: supported ATM, got {} in {}".format(
            include_file, fpname)

    if parts.scheme == "resource":