Python pathlib.PurePath() Examples

The following are 30 code examples of pathlib.PurePath(). 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 pathlib , or try the search function .
Example #1
Source File: baseconnector.py    From resolwe with Apache License 2.0 7 votes vote down vote up
def validate_url(wrapped, instance, args, kwargs):
    """Enforces argument named "url" to be relative path.

    Check that it is instance of str or os.PathLike and that it represents
    relative path.
    """
    try:
        # Use -1 since self is not included in the args.
        url = args[getfullargspec(wrapped).args.index("url") - 1]
    except IndexError:
        url = kwargs.get("url")
    if not isinstance(url, (str, PathLike)):
        raise TypeError("Argument 'url' must be a string or path-like object")
    if PurePath(url).is_absolute():
        raise ValueError("Argument 'url' must be a relative path")
    return wrapped(*args, **kwargs) 
Example #2
Source File: backup.py    From maloja with GNU General Public License v3.0 6 votes vote down vote up
def backup(folder,level="full"):

	selected_files = user_files["minimal"] if level == "minimal" else user_files["minimal"] + user_files["full"]
	real_files = []
	for g in selected_files:
		real_files += glob.glob(datadir(g))

	log("Creating backup of " + str(len(real_files)) + " files...")

	now = datetime.utcnow()
	timestr = now.strftime("%Y_%m_%d_%H_%M_%S")
	filename = "maloja_backup_" + timestr + ".tar.gz"
	archivefile = os.path.join(folder,filename)
	assert not os.path.exists(archivefile)
	with tarfile.open(name=archivefile,mode="x:gz") as archive:
		for f in real_files:
			p = PurePath(f)
			r = p.relative_to(datadir())
			archive.add(f,arcname=r)
	log("Backup created!") 
Example #3
Source File: utils.py    From bioconda-utils with MIT License 6 votes vote down vote up
def load_conda_build_config(platform=None, trim_skip=True):
    """
    Load conda build config while considering global pinnings from conda-forge.
    """
    config = api.Config(
        no_download_source=True,
        set_build_id=False)

    # get environment root
    env_root = PurePath(shutil.which("bioconda-utils")).parents[1]
    # set path to pinnings from conda forge package
    config.exclusive_config_files = [
        os.path.join(env_root, "conda_build_config.yaml"),
        os.path.join(
            os.path.dirname(__file__),
            'bioconda_utils-conda_build_config.yaml'),
    ]
    for cfg in chain(config.exclusive_config_files, config.variant_config_files or []):
        assert os.path.exists(cfg), ('error: {0} does not exist'.format(cfg))
    if platform:
        config.platform = platform
    config.trim_skip = trim_skip
    return config 
Example #4
Source File: manager.py    From parsec-cloud with GNU Affero General Public License v3.0 6 votes vote down vote up
def remount_workspace_new_timestamp(
        self, workspace_id: EntryID, original_timestamp: Pendulum, target_timestamp: Pendulum
    ) -> PurePath:
        """
        Mount the workspace at target_timestamp, and unmount the workspace at the original
        timestamp if it is not None. If both timestamps are equals, do nothing.
        """
        # TODO : use different workspaces for temp mount
        if original_timestamp == target_timestamp:
            try:
                return self._mountpoint_tasks[(workspace_id, target_timestamp)].value
            except KeyError:
                pass
        new_workspace = await self._load_workspace_timestamped(workspace_id, target_timestamp)

        runner_task = await self._mount_workspace_helper(new_workspace, target_timestamp)
        if original_timestamp is not None:
            if (workspace_id, original_timestamp) not in self._mountpoint_tasks:
                raise MountpointNotMounted(f"Workspace `{workspace_id}` not mounted.")

            await self._mountpoint_tasks[(workspace_id, original_timestamp)].cancel_and_join()
        return runner_task.value 
Example #5
Source File: utils.py    From pySCENIC with GNU General Public License v3.0 6 votes vote down vote up
def load_exp_matrix(fname: str, transpose: bool = False,
                    return_sparse: bool = False,
                    attribute_name_cell_id: str = ATTRIBUTE_NAME_CELL_IDENTIFIER,
                    attribute_name_gene: str = ATTRIBUTE_NAME_GENE) -> pd.DataFrame:
    """
    Load expression matrix from disk.

    Supported file formats are CSV, TSV and LOOM.

    :param fname: The name of the file that contains the expression matrix.
    :param transpose: Is the expression matrix stored as (rows = genes x columns = cells)?
    :param return_sparse: Returns a sparse matrix when loading from loom
    :return: A 2-dimensional dataframe (rows = cells x columns = genes).
    """
    extension = PurePath(fname).suffixes
    if is_valid_suffix(extension, 'grn'):
        if '.loom' in extension:
            return load_exp_matrix_as_loom(fname, return_sparse, attribute_name_cell_id, attribute_name_gene)
        else:
            df = pd.read_csv(fname, sep=suffixes_to_separator(extension), header=0, index_col=0)
            return df.T if transpose else df
    else:
        raise ValueError("Unknown file format \"{}\".".format(fname)) 
Example #6
Source File: __init__.py    From addon with GNU General Public License v3.0 6 votes vote down vote up
def bread(fd):
    # type: (Union[bytes, str, pathlib.Path, pathlib.PurePath, TextIO, BinaryIO]) -> bytes
    """Return bdecoded data from filename, file, or file-like object.

    if fd is a bytes/string or pathlib.Path-like object, it is opened and
    read, otherwise .read() is used. if read() not available, exception
    raised.
    """
    if isinstance(fd, (bytes, str)):
        with open(fd, 'rb') as fd:
            return bdecode(fd.read())
    elif pathlib is not None and isinstance(fd, (pathlib.Path, pathlib.PurePath)):
        with open(str(fd), 'rb') as fd:
            return bdecode(fd.read())
    else:
        return bdecode(fd.read()) 
Example #7
Source File: __init__.py    From addon with GNU General Public License v3.0 6 votes vote down vote up
def bwrite(data, fd):
    # type: (Union[Tuple, List, OrderedDict, Dict, bool, int, str, bytes], Union[bytes, str, pathlib.Path, pathlib.PurePath, TextIO, BinaryIO]) -> None
    """Write data in bencoded form to filename, file, or file-like object.

    if fd is bytes/string or pathlib.Path-like object, it is opened and
    written to, otherwise .write() is used. if write() is not available,
    exception raised.
    """
    if isinstance(fd, (bytes, str)):
        with open(fd, 'wb') as fd:
            fd.write(bencode(data))
    elif pathlib is not None and isinstance(fd, (pathlib.Path, pathlib.PurePath)):
        with open(str(fd), 'wb') as fd:
            fd.write(bencode(data))
    else:
        fd.write(bencode(data)) 
Example #8
Source File: tdvt.py    From connector-plugin-sdk with MIT License 6 votes vote down vote up
def copy_files_to_zip(self, dst_file_name, src_dir, is_logs):
        dst = os.path.join(os.getcwd(), dst_file_name)
        mode = 'w' if not os.path.isfile(dst) else 'a'
        optional_dir_name = self.test_config.config_file.replace('.', '_')
        if is_logs is True:
            log_dir = os.path.join(src_dir, optional_dir_name)
            glob_path = glob.glob(os.path.join(log_dir, '*.txt'))
            glob_path.extend(glob.glob(os.path.join(log_dir, '*.log')))
            glob_path.extend(glob.glob(os.path.join(log_dir, 'crashdumps/*')))
        else:
            glob_path = glob.glob(os.path.join(src_dir, 'actual.*'))
        with zipfile.ZipFile(dst, mode, zipfile.ZIP_DEFLATED) as myzip:
            for actual in glob_path:
                path = pathlib.PurePath(actual)
                file_to_be_zipped = path.name
                inner_output = os.path.join(optional_dir_name, file_to_be_zipped)
                myzip.write(actual, inner_output) 
Example #9
Source File: baseconnector.py    From resolwe with Apache License 2.0 6 votes vote down vote up
def validate_urls(wrapped, instance, args, kwargs):
    """Enforces argument named "urls" to be a list of relative paths."""
    try:
        # Use -1 since self is not included in the args.
        urls = args[getfullargspec(wrapped).args.index("urls") - 1]
    except IndexError:
        urls = kwargs.get("urls")
    # Check that URLS is really a list of strings.
    if not isinstance(urls, list):
        raise TypeError("Argument urls must be a list of strings or path-like objects")
    if not all(isinstance(url, (str, PathLike)) for url in urls):
        raise TypeError("Argument urls must be a list of strings or path-like objects")
    # Check that all URLS are relative.
    if any(PurePath(url).is_absolute() for url in urls):
        raise ValueError("Paths must be relative.")
    return wrapped(*args, *kwargs) 
Example #10
Source File: utils.py    From pySCENIC with GNU General Public License v3.0 6 votes vote down vote up
def save_matrix(df: pd.DataFrame, fname: str, transpose: bool = False) -> None:
    """
    Save matrix to disk.

    Supported file formats are CSV, TSV and LOOM.

    :param df: A 2-dimensional dataframe (rows = cells x columns = genes).
    :param fname: The name of the file to be written.
    :param transpose: Should the expression matrix be stored as (rows = genes x columns = cells)?
    """
    extension = PurePath(fname).suffixes
    if is_valid_suffix(extension, 'aucell'):
        if '.loom' in extension:
            return save_df_as_loom(df, fname)
        else:
            (df.T if transpose else df).to_csv(fname, sep=suffixes_to_separator(extension))
    else:
        raise ValueError("Unknown file format \"{}\".".format(fname)) 
Example #11
Source File: writer.py    From pipenv with MIT License 6 votes vote down vote up
def _format_value(v):
    if isinstance(v, bool):
        return 'true' if v else 'false'
    if isinstance(v, int) or isinstance(v, long):
        return unicode(v)
    if isinstance(v, float):
        if math.isnan(v) or math.isinf(v):
            raise ValueError("{0} is not a valid TOML value".format(v))
        else:
            return repr(v)
    elif isinstance(v, unicode) or isinstance(v, bytes):
        return _escape_string(v)
    elif isinstance(v, datetime.datetime):
        return format_rfc3339(v)
    elif isinstance(v, list):
        return '[{0}]'.format(', '.join(_format_value(obj) for obj in v))
    elif isinstance(v, dict):
        return '{{{0}}}'.format(', '.join('{} = {}'.format(_escape_id(k), _format_value(obj)) for k, obj in v.items()))
    elif isinstance(v, _path_types):
        return _escape_string(str(v))
    else:
        raise RuntimeError(v) 
Example #12
Source File: utils.py    From pySCENIC with GNU General Public License v3.0 6 votes vote down vote up
def load_modules(fname: str) -> Sequence[Type[GeneSignature]]:
    # Loading from YAML is extremely slow. Therefore this is a potential performance improvement.
    # Potential improvements are switching to JSON or to use a CLoader:
    # https://stackoverflow.com/questions/27743711/can-i-speedup-yaml
    # The alternative for which was opted in the end is binary pickling.
    extension = PurePath(fname).suffixes
    if is_valid_suffix(extension, 'ctx_yaml'):
        return load_from_yaml(fname)
    elif '.dat' in extension:
        with openfile(fname, 'rb') as f:
            return pickle.load(f)
    elif '.gmt' in extension:
        sep = guess_separator(fname)
        return GeneSignature.from_gmt(fname,
                                      field_separator=sep,
                                      gene_separator=sep)
    else:
        raise ValueError("Unknown file format for \"{}\".".format(fname)) 
Example #13
Source File: _container_builder.py    From pipelines with Apache License 2.0 6 votes vote down vote up
def _get_staging_location(self):
    if self._gcs_staging_checked:
      return self._gcs_staging

    # Configure the GCS staging bucket
    if self._gcs_staging is None:
      try:
        gcs_bucket = _get_project_id()
      except:
        raise ValueError('Cannot get the Google Cloud project ID, please specify the gcs_staging argument.')
      self._gcs_staging = 'gs://' + gcs_bucket + '/' + GCS_STAGING_BLOB_DEFAULT_PREFIX
    else:
      from pathlib import PurePath
      path = PurePath(self._gcs_staging).parts
      if len(path) < 2 or not path[0].startswith('gs'):
        raise ValueError('Error: {} should be a GCS path.'.format(self._gcs_staging))
      gcs_bucket = path[1]
    from ._gcs_helper import GCSHelper
    GCSHelper.create_gcs_bucket_if_not_exist(gcs_bucket)
    self._gcs_staging_checked = True
    return self._gcs_staging 
Example #14
Source File: writer.py    From pex with Apache License 2.0 6 votes vote down vote up
def _format_value(v):
    if isinstance(v, bool):
        return 'true' if v else 'false'
    if isinstance(v, int) or isinstance(v, long):
        return unicode(v)
    if isinstance(v, float):
        if math.isnan(v) or math.isinf(v):
            raise ValueError("{0} is not a valid TOML value".format(v))
        else:
            return repr(v)
    elif isinstance(v, unicode) or isinstance(v, bytes):
        return _escape_string(v)
    elif isinstance(v, datetime.datetime):
        return format_rfc3339(v)
    elif isinstance(v, list):
        return '[{0}]'.format(', '.join(_format_value(obj) for obj in v))
    elif isinstance(v, dict):
        return '{{{0}}}'.format(', '.join('{} = {}'.format(_escape_id(k), _format_value(obj)) for k, obj in v.items()))
    elif isinstance(v, _path_types):
        return _escape_string(str(v))
    else:
        raise RuntimeError(v) 
Example #15
Source File: test_pathlib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def _check_str_subclass(self, *args):
        # Issue #21127: it should be possible to construct a PurePath object
        # from an str subclass instance, and it then gets converted to
        # a pure str object.
        class StrSubclass(str):
            pass
        P = self.cls
        p = P(*(StrSubclass(x) for x in args))
        self.assertEqual(p, P(*args))
        for part in p.parts:
            self.assertIs(type(part), str) 
Example #16
Source File: upload.py    From blobxfer with MIT License 5 votes vote down vote up
def create_destination_id(client, container, name):
        # type: (azure.storage.StorageClient, str, str) -> str
        """Create a unique destination id
        :param azure.storage.StorageClient client: storage client
        :param str container: container name
        :param str name: entity name
        :rtype: str
        :return: unique id for the destination
        """
        path = str(pathlib.PurePath(name))
        return ';'.join((client.primary_endpoint, container, path)) 
Example #17
Source File: imagecrawler.py    From nichtparasoup with MIT License 5 votes vote down vote up
def path_is_image(self, uri: _Uri) -> bool:
        return PurePath(
            urlparse(uri).path
        ).suffix in self._IMAGE_SUFFIXES 
Example #18
Source File: common.py    From scylla with Apache License 2.0 5 votes vote down vote up
def fspath(path):
        """
        Return the string representation of the path.
        If str or bytes is passed in, it is returned unchanged.
        This code comes from PEP 519, modified to support earlier versions of
        python.

        This is required for python < 3.6.
        """
        if isinstance(path, (py.builtin.text, py.builtin.bytes)):
            return path

        # Work from the object's type to match method resolution of other magic
        # methods.
        path_type = type(path)
        try:
            return path_type.__fspath__(path)
        except AttributeError:
            if hasattr(path_type, '__fspath__'):
                raise
            try:
                import pathlib
            except ImportError:
                pass
            else:
                if isinstance(path, pathlib.PurePath):
                    return py.builtin.text(path)

            raise TypeError("expected str, bytes or os.PathLike object, not "
                            + path_type.__name__) 
Example #19
Source File: defaults.py    From td-ameritrade-python-api with MIT License 5 votes vote down vote up
def json_settings_path(self):
        """Generates a path to the `.td_python_library/td_state.json` file.

        Returns:
        ----
        {pathlib.PurePath} -- A PurePath object pointing to the
             `.td_python_library/td_state.json` file.
        """
        return self.settings_directory.joinpath(self.credenitals_file_name) 
Example #20
Source File: battery.py    From thinkpad-tools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, name: str = 'BAT0', **kwargs):
        self.name: str = name
        self.path: pathlib.PurePath = BASE_DIR / self.name
        for prop, default_value in PROPERTIES.items():
            if prop in kwargs.keys():
                if type(kwargs[prop]) == type(default_value):
                    self.__dict__[prop] = kwargs[prop]
            self.__dict__[prop] = default_value
        self.battery_health: int = 100 
Example #21
Source File: defaults.py    From td-ameritrade-python-api with MIT License 5 votes vote down vote up
def json_library_path(self):
        """Generates a path to the `td/td_state.json` file.

        Returns:
        ----
        {pathlib.PurePath} -- A PurePath object pointing to the
             `td/td_state.json` file.
        """
        return self.library_directory.joinpath(self.credenitals_file_name) 
Example #22
Source File: plugins.py    From modmail with GNU Affero General Public License v3.0 5 votes vote down vote up
def path(self):
        return PurePath("plugins") / self.user / self.repo / f"{self.name}-{self.branch}" 
Example #23
Source File: defaults.py    From td-ameritrade-python-api with MIT License 5 votes vote down vote up
def path_library(self) -> pathlib.PurePath:
        """Generates the TD Library Path.

        Returns:
        ----
        {pathlib.PurePath} -- A PurePath object pointing to the TD
            library.
        """     
        library_directory = pathlib.Path(__file__).parent
        return library_directory 
Example #24
Source File: defaults.py    From td-ameritrade-python-api with MIT License 5 votes vote down vote up
def settings_directory(self) -> pathlib.PurePath:
        """Returns the `.td_python_library` directory path.

        Returns:
        ----
        {pathlib.PurePath} -- A path object.
        """    
        return self.path_settings() 
Example #25
Source File: defaults.py    From td-ameritrade-python-api with MIT License 5 votes vote down vote up
def library_directory(self) -> pathlib.PurePath:
        """Returns the TD Library directory path.

        Returns:
        ----
        {pathlib.PurePath} -- A path object.
        """    
        return self.path_library() 
Example #26
Source File: defaults.py    From td-ameritrade-python-api with MIT License 5 votes vote down vote up
def home_directory(self) -> pathlib.PurePath:
        """Returns the Home directory path.

        Returns:
        ----
        {pathlib.PurePath} -- A path object.
        """        
        return self.path_home() 
Example #27
Source File: defaults.py    From td-ameritrade-python-api with MIT License 5 votes vote down vote up
def path_home(self) -> pathlib.PurePath:
        """Determines the user's Home Path using Pathlib.

        Returns:
        ----
        {pathlib.PurePath} -- A PurePath object that points to
            the user's home directory.
        """

        home_directory = pathlib.Path.home()
        return home_directory 
Example #28
Source File: test_pathlib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def _check_str_subclass(self, *args):
        # Issue #21127: it should be possible to construct a PurePath object
        # from an str subclass instance, and it then gets converted to
        # a pure str object.
        class StrSubclass(str):
            pass
        P = self.cls
        p = P(*(StrSubclass(x) for x in args))
        self.assertEqual(p, P(*args))
        for part in p.parts:
            self.assertIs(type(part), str) 
Example #29
Source File: plugins.py    From modmail with GNU Affero General Public License v3.0 5 votes vote down vote up
def download_plugin(self, plugin, force=False):
        if plugin.abs_path.exists() and not force:
            return

        plugin.abs_path.mkdir(parents=True, exist_ok=True)

        if plugin.cache_path.exists() and not force:
            plugin_io = plugin.cache_path.open("rb")
            logger.debug("Loading cached %s.", plugin.cache_path)

        else:
            async with self.bot.session.get(plugin.url) as resp:
                logger.debug("Downloading %s.", plugin.url)
                raw = await resp.read()
                plugin_io = io.BytesIO(raw)
                if not plugin.cache_path.parent.exists():
                    plugin.cache_path.parent.mkdir(parents=True)

                with plugin.cache_path.open("wb") as f:
                    f.write(raw)

        with zipfile.ZipFile(plugin_io) as zipf:
            for info in zipf.infolist():
                path = PurePath(info.filename)
                if len(path.parts) >= 3 and path.parts[1] == plugin.name:
                    plugin_path = plugin.abs_path / Path(*path.parts[2:])
                    if info.is_dir():
                        plugin_path.mkdir(parents=True, exist_ok=True)
                    else:
                        plugin_path.parent.mkdir(parents=True, exist_ok=True)
                        with zipf.open(info) as src, plugin_path.open("wb") as dst:
                            shutil.copyfileobj(src, dst)

        plugin_io.close() 
Example #30
Source File: ffmpeg.py    From transcoder with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, ffmpeg_path):
        self.ffmpeg = ffmpeg_path
        self.last_command = ''
        self.monitor_interval = 30
        self.log_path: PurePath = None