Python pkg_resources.resource_exists() Examples

The following are 30 code examples of pkg_resources.resource_exists(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pkg_resources , or try the search function .
Example #1
Source File: classloader.py    From aries-cloudagent-python with Apache License 2.0 6 votes vote down vote up
def scan_subpackages(cls, package: str) -> Sequence[str]:
        """Return a list of sub-packages defined under a named package."""
        # FIXME use importlib.resources in python 3.7
        if "." in package:
            package, sub_pkg = package.split(".", 1)
        else:
            sub_pkg = "."
        if not pkg_resources.resource_isdir(package, sub_pkg):
            raise ModuleLoadError(f"Undefined package {package}")
        found = []
        joiner = "" if sub_pkg == "." else f"{sub_pkg}."
        for sub_path in pkg_resources.resource_listdir(package, sub_pkg):
            if pkg_resources.resource_exists(
                package, f"{sub_pkg}/{sub_path}/__init__.py"
            ):
                found.append(f"{package}.{joiner}{sub_path}")
        return found 
Example #2
Source File: precalculated_text_measurer.py    From pybadges with Apache License 2.0 6 votes vote down vote up
def default(cls) -> 'PrecalculatedTextMeasurer':
        """Returns a reasonable default PrecalculatedTextMeasurer."""
        if cls._default_cache is not None:
            return cls._default_cache

        if pkg_resources.resource_exists(__name__, 'default-widths.json.xz'):
            import lzma
            with pkg_resources.resource_stream(__name__,
                                               'default-widths.json.xz') as f:
                with lzma.open(f, "rt") as g:
                    cls._default_cache = PrecalculatedTextMeasurer.from_json(
                        cast(TextIO, g))
                    return cls._default_cache
        elif pkg_resources.resource_exists(__name__, 'default-widths.json'):
            with pkg_resources.resource_stream(__name__,
                                               'default-widths.json') as f:
                cls._default_cache = PrecalculatedTextMeasurer.from_json(
                    io.TextIOWrapper(f, encoding='utf-8'))
                return cls._default_cache
        else:
            raise ValueError('could not load default-widths.json') 
Example #3
Source File: auxiliar.py    From investpy with MIT License 6 votes vote down vote up
def resource_to_data(path_to_data):
    """
    This is an auxiliar function to read data from a given resource.
    """

    resource_package = 'investpy'
    resource_path = '/'.join(('resources', path_to_data))
    if pkg_resources.resource_exists(resource_package, resource_path):
        data = pd.read_csv(pkg_resources.resource_filename(resource_package, resource_path))
    else:
        raise FileNotFoundError("ERR#0115: data file not found or errored.")

    if data is None:
        raise IOError("ERR#0115: data file not found or errored.")

    return data 
Example #4
Source File: materials.py    From MCEdit-Unified with ISC License 6 votes vote down vote up
def __init__(self, mats, definition_file):
        self._mats = mats
        self.block_map = {}
        self.blockstates = {}

        for b in self._mats:
            if b.ID == 0:
                b.stringID = "air"
            self.block_map[b.ID] = "minecraft:" + b.stringID

        # When running from a bundled app on Linux (and possibly on OSX) pkg_resource can't find the needed files.
        if pkg_resources.resource_exists(__name__, definition_file):
            # We're running from source or on Windows using the executable (<<== Not sure...)
            with pkg_resources.resource_stream(__name__, definition_file) as def_file:
                self.blockstates = json.load(def_file)
        else:
            # In all other cases, retrieve the file directly from the file system.
            with open(os.path.join("pymclevel", definition_file)) as def_file:
                self.blockstates = json.load(def_file)

        self.material_map[self._mats] = self 
Example #5
Source File: install.py    From treadmill with Apache License 2.0 6 votes vote down vote up
def _is_scan_dir(package, src_dir, dst_dir):
    """Check if working on a scan dir.
    """
    if os.path.exists(os.path.join(dst_dir, _CONTROL_DIR_NAME)):
        return True

    package_name = package.__name__
    if pkg_resources.resource_isdir(package_name,
                                    os.path.join(src_dir, _CONTROL_DIR_NAME)):
        return True

    if pkg_resources.resource_exists(package_name,
                                     os.path.join(src_dir, _CONTROL_DIR_FILE)):
        return True

    return False 
Example #6
Source File: certificates_data.py    From investpy with MIT License 5 votes vote down vote up
def certificate_countries_as_list():
    """
    This function retrieves all the available countries to retrieve certificates from, as the listed countries 
    are the ones indexed on Investing.com. The purpose of this function is to list the countries which 
    have available certificates according to Investing.com data, since the country parameter is needed when
    retrieving data from any certificate available.

    Returns:
        :obj:`list` - countries:
            The resulting :obj:`list` contains all the countries listed on Investing.com with available certificates
            to retrieve data from.

            In the case that the file reading of `certificate_countries.csv` which contains the names of the available
            countries with certificates was successfully completed, the resulting :obj:`list` will look like::

                countries = ['france', 'germany', 'italy', 'netherlands', 'sweden']

    Raises:
        FileNotFoundError: raised if `certificate_countries.csv` file was not found.
        IOError: raised when `certificate_countries.csv` file is missing or empty.
    
    """

    resource_package = 'investpy'
    resource_path = '/'.join(('resources', 'certificates', 'certificate_countries.csv'))
    if pkg_resources.resource_exists(resource_package, resource_path):
        countries = pd.read_csv(pkg_resources.resource_filename(resource_package, resource_path))
    else:
        raise FileNotFoundError("ERR#0098: certificate countries file not found or errored.")

    if countries is None:
        raise IOError("ERR#0099: certificate countries not found or unable to retrieve.")

    return countries['country'].tolist() 
Example #7
Source File: commodities_data.py    From investpy with MIT License 5 votes vote down vote up
def commodity_groups_list():
    """
    This function returns a listing with all the available commodity groupsson that a filtering can be applied when
    retrieving data from commodities. The current available commodity groups are metals, agriculture and energy, 
    which include all the raw materials or commodities included in them.

    Returns:
        :obj:`list` - commodity_groups:
            The resulting :obj:`list` contains all the available commodity groups as indexed in Investing.com

    Raises:
        FileNotFoundError: raised when `commodities.csv` file was not found.
        IOError: raised when `commodities.csv` file is missing or empty.

    """

    resource_package = 'investpy'
    resource_path = '/'.join(('resources', 'commodities', 'commodities.csv'))
    if pkg_resources.resource_exists(resource_package, resource_path):
        commodities = pd.read_csv(pkg_resources.resource_filename(resource_package, resource_path))
    else:
        raise FileNotFoundError("ERR#0075: commodities file not found or errored.")

    if commodities is None:
        raise IOError("ERR#0076: commodities not found or unable to retrieve.")
    
    return commodities['group'].unique().tolist() 
Example #8
Source File: stocks_data.py    From investpy with MIT License 5 votes vote down vote up
def stock_countries_as_list():
    """
    This function returns a listing with all the available countries from where stocks can be retrieved, so to
    let the user know which of them are available, since the parameter country is mandatory in every stock retrieval
    function. Also, not just the available countries, but the required name is provided since Investing.com has a
    certain country name standard and countries should be specified the same way they are in Investing.com.

    Returns:
        :obj:`list` - countries:
            The resulting :obj:`list` contains all the available countries with stocks as indexed in Investing.com

    Raises:
        FileNotFoundError: raised if `stock_countries.csv` file was not found.
        IOError: raised when `stock_countries.csv` file is missing or empty.

    """

    resource_package = 'investpy'
    resource_path = '/'.join(('resources', 'stocks', 'stock_countries.csv'))
    if pkg_resources.resource_exists(resource_package, resource_path):
        countries = pd.read_csv(pkg_resources.resource_filename(resource_package, resource_path))
    else:
        raise FileNotFoundError("ERR#0071: stock countries file not found or errored.")

    if countries is None:
        raise IOError("ERR#0036: stock countries list not found or unable to retrieve.")
    else:
        return countries['country'].tolist() 
Example #9
Source File: packaged.py    From yatsm with MIT License 5 votes vote down vote up
def find_packaged_regressor(name):
    """ Find location of a regression method packaged with YATSM

    See :data:`packaged_regressions` for a list of
    available pre-packaged regressors

    Args:
        name (str): name of packaged regression object

    Returns:
        str: path to packaged regression method

    Raises:
        KeyError: raise KeyError if user specifies unknown regressor
        IOError: raise IOError if the packaged regressor cannot be found

    """
    if name not in packaged_regressions:
        raise KeyError('Cannot load unknown packaged regressor %s' % name)

    path = pkg_resources.resource_filename(__name__, 'pickles')
    logger.debug('Checking data files in %s for packaged regressors' % path)
    if not pkg_resources.resource_exists(__name__, 'pickles'):
        raise IOError('Cannot find packaged regressors in %s. Did you install '
                      'YATSM via setuptools?' % path)

    resource = os.path.join('pickles', name + '.pkl')
    if not pkg_resources.resource_exists(__name__, resource):
        raise IOError('Cannot find packaged regression method %s, but package '
                      'directory exists. Check the contents of %s if possible'
                      % (resource, path))

    return pkg_resources.resource_filename(__name__, resource) 
Example #10
Source File: indices_data.py    From investpy with MIT License 5 votes vote down vote up
def index_countries_as_list():
    """
    This function retrieves all the country names indexed in Investing.com with available indices to retrieve data
    from, via reading the `indices.csv` file from the resources directory. So on, this function will display a listing
    containing a set of countries, in order to let the user know which countries are available for indices data retrieval.

    Returns:
        :obj:`list` - countries:
            The resulting :obj:`list` contains all the available countries with indices as indexed in Investing.com

    Raises:
        FileNotFoundError: raised if the `indices.csv` file was not found.
        IOError: raised if the `indices.csv` file is missing or errored.
    
    """

    resource_package = 'investpy'
    resource_path = '/'.join(('resources', 'indices', 'indices.csv'))
    if pkg_resources.resource_exists(resource_package, resource_path):
        indices = pd.read_csv(pkg_resources.resource_filename(resource_package, resource_path))
    else:
        raise FileNotFoundError("ERR#0059: indices file not found or errored.")

    if indices is None:
        raise IOError("ERR#0037: indices not found or unable to retrieve.")
    else:
        return indices['country'].unique().tolist() 
Example #11
Source File: funds_data.py    From investpy with MIT License 5 votes vote down vote up
def fund_countries_as_list():
    """
    This function retrieves all the country names indexed in Investing.com with available funds to retrieve data
    from, via reading the `fund_countries.csv` file from the resources directory. So on, this function will display a
    listing containing a set of countries, in order to let the user know which countries are taken into account and also
    the return listing from this function can be used for country param check if needed.

    Returns:
        :obj:`list` - countries:
            The resulting :obj:`list` contains all the available countries with funds as indexed in Investing.com

    Raises:
        FileNotFoundError: raised when the `fund_countries.csv` file was not found.
        IndexError: raised if `fund_countries.csv` file was unavailable or not found.
    
    """

    resource_package = 'investpy'
    resource_path = '/'.join(('resources', 'funds', 'fund_countries.csv'))
    if pkg_resources.resource_exists(resource_package, resource_path):
        countries = pd.read_csv(pkg_resources.resource_filename(resource_package, resource_path))
    else:
        raise FileNotFoundError("ERR#0072: fund countries file not found or errored.")

    if countries is None:
        raise IOError("ERR#0040: fund countries list not found or unable to retrieve.")

    for index, row in countries.iterrows():
        if row['country'] == 'uk':
            countries.loc[index, 'country'] = 'united kingdom'
        elif row['country'] == 'usa':
            countries.loc[index, 'country'] = 'united states'

    return countries['country'].tolist() 
Example #12
Source File: data.py    From semantic-text-similarity with MIT License 5 votes vote down vote up
def load_sts_b_data():
    """
    Loads the STS-B dataset found here: http://ixa2.si.ehu.es/stswiki/index.php/STSbenchmark

    :return a tuple containing the train, dev, test split given by the data aggregators.
    """

    if not resource_exists('semantic_text_similarity', 'data/sts_b/sts-train.csv'):
        raise FileNotFoundError('Cannot find STS-B dataset')

    train = resource_string('semantic_text_similarity', 'data/sts_b/sts-train.csv').decode('utf-8').strip()
    dev = resource_string('semantic_text_similarity', 'data/sts_b/sts-dev.csv').decode('utf-8').strip()
    test = resource_string('semantic_text_similarity', 'data/sts_b/sts-dev.csv').decode('utf-8').strip()

    def yielder():
        for partition in (train, dev, test):
            data = []
            for idx,line in enumerate(partition.split('\n')):
                line = tuple(line.split("\t"))
                data.append({
                    'index': idx,
                    'sentence_1': line[5],
                    'sentence_2': line[6],
                    'similarity': float(line[4])
                })
            yield data

    return tuple([dataset for dataset in yielder()]) 
Example #13
Source File: _bridgesupport.py    From MIA-Dictionary-Addon with GNU General Public License v3.0 5 votes vote down vote up
def safe_resource_exists(package, resource):
    try:
        return pkg_resources.resource_exists(package, resource)
    except ImportError:
        # resource_exists raises ImportError when it cannot find
        # the first argument.
        return False 
Example #14
Source File: app.py    From allura with Apache License 2.0 5 votes vote down vote up
def has_resource(cls, resource_path):
        """Determine whether this Application has the resource pointed to by
        ``resource_path``.

        If the resource is not found for the immediate class, its parents
        will be searched. The return value is the class that "owns" the
        resource, or None if the resource is not found.

        """
        for klass in [o for o in cls.__mro__ if issubclass(o, Application)]:
            if pkg_resources.resource_exists(klass.__module__, resource_path):
                return klass 
Example #15
Source File: __init__.py    From PerfKitBenchmarker with Apache License 2.0 5 votes vote down vote up
def ResourceExists(self, name):
    return pkg_resources.resource_exists(self.package, name) 
Example #16
Source File: pkgdata.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def resource_exists(package_or_requirement, resource_name):
        return False 
Example #17
Source File: pkgdata.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def getResource(identifier, pkgname=__name__):
    """
    Acquire a readable object for a given package name and identifier.
    An IOError will be raised if the resource can not be found.

    For example:
        mydata = getResource('mypkgdata.jpg').read()

    Note that the package name must be fully qualified, if given, such
    that it would be found in sys.modules.

    In some cases, getResource will return a real file object.  In that
    case, it may be useful to use its name attribute to get the path
    rather than use it as a file-like object.  For example, you may
    be handing data off to a C API.
    """
    if resource_exists(pkgname, identifier):
        return resource_stream(pkgname, identifier)

    mod = sys.modules[pkgname]
    fn = getattr(mod, '__file__', None)
    if fn is None:
        raise IOError("%s has no __file__!" % repr(mod))
    path = os.path.join(os.path.dirname(fn), identifier)
    if sys.version_info < (3, 3):
        loader = getattr(mod, '__loader__', None)
        if loader is not None:
            try:
                data = loader.get_data(path)
            except IOError:
                pass
            else:
                return BytesIO(data)
    return open(os.path.normpath(path), 'rb') 
Example #18
Source File: pkgdata.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def resource_exists(package_or_requirement, resource_name):
        return False 
Example #19
Source File: pkgdata.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def getResource(identifier, pkgname=__name__):
    """
    Acquire a readable object for a given package name and identifier.
    An IOError will be raised if the resource can not be found.

    For example:
        mydata = getResource('mypkgdata.jpg').read()

    Note that the package name must be fully qualified, if given, such
    that it would be found in sys.modules.

    In some cases, getResource will return a real file object.  In that
    case, it may be useful to use its name attribute to get the path
    rather than use it as a file-like object.  For example, you may
    be handing data off to a C API.
    """
    if resource_exists(pkgname, identifier):
        return resource_stream(pkgname, identifier)

    mod = sys.modules[pkgname]
    fn = getattr(mod, '__file__', None)
    if fn is None:
        raise IOError("%s has no __file__!" % repr(mod))
    path = os.path.join(os.path.dirname(fn), identifier)
    if sys.version_info < (3, 3):
        loader = getattr(mod, '__loader__', None)
        if loader is not None:
            try:
                data = loader.get_data(path)
            except IOError:
                pass
            else:
                return BytesIO(data)
    return open(os.path.normpath(path), 'rb') 
Example #20
Source File: pkgdata.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def resource_exists(package_or_requirement, resource_name):
        return False 
Example #21
Source File: pkgdata.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def resource_exists(package_or_requirement, resource_name):
        return False 
Example #22
Source File: pkgdata.py    From fxxkpython with GNU General Public License v3.0 5 votes vote down vote up
def getResource(identifier, pkgname=__name__):
    """
    Acquire a readable object for a given package name and identifier.
    An IOError will be raised if the resource can not be found.

    For example:
        mydata = getResource('mypkgdata.jpg').read()

    Note that the package name must be fully qualified, if given, such
    that it would be found in sys.modules.

    In some cases, getResource will return a real file object.  In that
    case, it may be useful to use its name attribute to get the path
    rather than use it as a file-like object.  For example, you may
    be handing data off to a C API.
    """
    if resource_exists(pkgname, identifier):
        return resource_stream(pkgname, identifier)

    mod = sys.modules[pkgname]
    fn = getattr(mod, '__file__', None)
    if fn is None:
        raise IOError("%s has no __file__!" % repr(mod))
    path = os.path.join(os.path.dirname(fn), identifier)
    if sys.version_info < (3, 3):
        loader = getattr(mod, '__loader__', None)
        if loader is not None:
            try:
                data = loader.get_data(path)
            except IOError:
                pass
            else:
                return BytesIO(data)
    return open(os.path.normpath(path), 'rb') 
Example #23
Source File: __init__.py    From compoundpi with GNU General Public License v2.0 5 votes vote down vote up
def resource_exists(module, name):
        name = os.path.join(UI_DIR, name)
        return os.path.exists(name) and not os.path.isdir(name) 
Example #24
Source File: makeTest.py    From PhiSpy with MIT License 5 votes vote down vote up
def __init__(self, kmers_type):
        # Create a hash of the kmers that points to the index of an array that holds the value
        self._kmers = {}
        self._kmers_phage = []
        self._kmers_all = []
        self._kmers_type = kmers_type
        kmers_file = 'data/phage_kmers_' + self._kmers_type + '_wohost.txt'
        if not pkg_resources.resource_exists:
            log_and_message(f"ERROR: Kmers file {kmers_file} not found", "RED", stderr=True, quiet=self.quiet)
            sys.exit(13)

        for line in pkg_resources.resource_stream('PhiSpyModules', kmers_file):
            line = line.decode().strip()
            self._kmers[line] = '' 
Example #25
Source File: classification.py    From PhiSpy with MIT License 5 votes vote down vote up
def call_randomforest(**kwargs):
    training_file = kwargs['training_set']
    test_data = kwargs['test_data']

    if not pkg_resources.resource_exists('PhiSpyModules', training_file):
        log_and_message(f"FATAL: Can not find data file {training_file}\n", c="RED", stderr=True, loglevel="CRITICAL")
        sys.exit(11)
    strm = pkg_resources.resource_stream('PhiSpyModules', training_file)
    log_and_message(f"Using training set in {training_file}")
    train_data = np.genfromtxt(TextIOWrapper(strm), delimiter="\t", skip_header=1, filling_values=1)

    all_metrics = ['orf_length_med', 'shannon_slope', 'at_skew', 'gc_skew', 'max_direction', 'phmms']
    if kwargs['phmms']:
        kwargs['metrics'].append('phmms')

    if len(kwargs['metrics']) < len(all_metrics):
        log_and_message(f"Using the following metric(s): {', '.join(sorted(kwargs['metrics']))}.")
        skip_metrics = [all_metrics.index(x) for x in set(all_metrics) - set(kwargs['metrics'])]
        train_data = np.delete(train_data, skip_metrics, 1)
        test_data = np.delete(test_data, skip_metrics, 1)
    else:
        log_and_message(f"Using all metrics: {', '.join(all_metrics)}.")

    """
    Przemek's comment
    by default 10 until version 0.22 where default is 100
    number of estimators also implies the precision of probabilities, generally 1/n_estimators
    in R's randomForest it's 500 and the usage note regarding number of trees to grow says:
    "This should not be set to too small a number, to ensure that every input row gets predicted at least a few times."
    """
    log_and_message(f"Running the random forest classifier with {kwargs['randomforest_trees']} trees and {kwargs['threads']} threads")
    clf = RandomForestClassifier(n_estimators=kwargs['randomforest_trees'], n_jobs=kwargs['threads'])
    clf.fit(train_data[:, :-1], train_data[:, -1].astype('int'))
    return clf.predict_proba(test_data)[:,1] 
Example #26
Source File: __init__.py    From MARA_Framework with GNU Lesser General Public License v3.0 5 votes vote down vote up
def preloadFont(cls, font):
        """
        Load font data if exist
        """
        for extension in ('tlf', 'flf'):
            fn = '%s.%s' % (font, extension)
            if pkg_resources.resource_exists('pyfiglet.fonts', fn):
                data = pkg_resources.resource_string('pyfiglet.fonts', fn)
                data = data.decode('UTF-8', 'replace')
                return data
        else:
            raise FontNotFound(font) 
Example #27
Source File: parser.py    From gemBS with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        self.reg = re.compile("[$][{]([^}]+)[}]")
        self.reg1 = re.compile("([^:]+)[:](.*)")
        if pkg_resources.resource_exists("gemBS", "etc/gemBS_configs"):
            self.sys_config_dir = pkg_resources.resource_filename("gemBS", "etc/gemBS_configs") 
Example #28
Source File: utilities.py    From FAI-PEP with Apache License 2.0 5 votes vote down vote up
def getMeta(args, platform):
    meta = None
    if not args.frameworks_dir:
        meta_file = os.path.join(
            "specifications/frameworks", args.framework, platform, "meta.json"
        )
        if "aibench" in sys.modules and pkg_resources.resource_exists(
            "aibench", meta_file
        ):
            meta = json.loads(pkg_resources.resource_string("aibench", meta_file))
            return meta
        else:
            # look for files in the old default place
            old_default = str(
                os.path.dirname(os.path.realpath(__file__))
                + "/../../specifications/frameworks"
            )
            meta_file = os.path.join(old_default, args.framework, platform, "meta.json")
    else:
        meta_file = os.path.join(
            args.frameworks_dir, args.framework, platform, "meta.json"
        )
    if os.path.isfile(meta_file):
        with open(meta_file, "r") as f:
            meta = json.load(f)
    return meta 
Example #29
Source File: build_program.py    From FAI-PEP with Apache License 2.0 5 votes vote down vote up
def _readFromBinary(framework, frameworks_dir, platform, dst):
    script_path = os.path.join("specifications/frameworks",
        framework, platform, "build.sh")
    if not pkg_resources.resource_exists("aibench", script_path):
        raise Exception(
            "cannot find the build script in the binary under {}.".format(script_path))
    raw_build_script = pkg_resources.resource_string("aibench", script_path)
    if not os.path.exists(os.path.dirname(dst)):
        os.makedirs(os.path.dirname(dst))
    with open(os.path.join(os.path.dirname(dst), "build.sh"), "w") as f:
        f.write(raw_build_script.decode("utf-8"))
    build_script = f.name

    return build_script 
Example #30
Source File: run.py    From rgkit with The Unlicense 5 votes vote down vote up
def _make_player(file_name):
        try:
            return game.Player(file_name=file_name)
        except IOError as msg:
            if pkg_resources.resource_exists('rgkit', file_name):
                bot_filename = pkg_resources.resource_filename('rgkit',
                                                               file_name)
                return game.Player(file_name=bot_filename)
            raise IOError(msg)