Python pathlib.PosixPath() Examples

The following are 30 code examples of pathlib.PosixPath(). 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: TensorHiveManager.py    From TensorHive with Apache License 2.0 6 votes vote down vote up
def __init__(self):
        super().__init__()
        self.infrastructure_manager = InfrastructureManager(SSH.AVAILABLE_NODES)

        self.dedicated_ssh_key = ssh.init_ssh_key(PosixPath(SSH.KEY_FILE).expanduser())

        if not SSH.AVAILABLE_NODES:
            log.error('[!] Empty ssh configuration. Please check {}'.format(SSH.HOSTS_CONFIG_FILE))
            raise ConfigurationException

        manager_ssh_key_path = SSH.KEY_FILE
        if SSH.TEST_ON_STARTUP:
            manager_ssh_key_path = self.test_ssh()

        self.connection_manager = SSHConnectionManager(config=SSH.AVAILABLE_NODES, ssh_key_path=manager_ssh_key_path)
        self.service_manager = None 
Example #2
Source File: oss.py    From Det3D with Apache License 2.0 6 votes vote down vote up
def _make_child(self, args: Iterable[str]):

        if not self.bucket:
            bucket, *rest_args = args
            bucket = bucket.lstrip("/")
            bucket, *rest_parts = PosixPath(bucket).parts
            return self.with_bucket(bucket)._make_child(rest_parts + rest_args)

        parts = [p for p in self._key_parts]
        for item in args:
            if not isinstance(item, str):
                raise ValueError("child must be string")
            item = item.lstrip("/")  # remove leading '/'
            if not item:
                raise ValueError("child must not be empty")
            for p in PosixPath(item).parts:
                parts.append(p)

        return self._create(self._client, self.bucket, tuple(parts)) 
Example #3
Source File: oss.py    From Det3D with Apache License 2.0 6 votes vote down vote up
def _parse_s3url(cls, s3url: Optional[str] = None):
        if s3url is None:
            return "", ()

        if not s3url.startswith("s3://"):
            raise ValueError(
                "s3url must be formated as 's3://<bucket_name>/path/to/object'"
            )

        r = urlparse(s3url)
        assert r.scheme == "s3"

        key = r.path.lstrip("/")  # remove the leading /

        parts = PosixPath(key).parts
        return r.netloc, parts 
Example #4
Source File: test_cinder_integration.py    From ceph-lcm with Apache License 2.0 6 votes vote down vote up
def test_prepare_api_response(new_cluster):
    integration = cinder_integration.CinderIntegration.find_one(
        new_cluster.model_id
    )
    integration.config = "config"
    integration.keyrings["images.keyring"] = "[client.images]\n\tkey = 111"
    integration.keyrings["vols.keyring"] = "[client.vols]\n\tkey = 22"

    root_path = pathlib.PosixPath(pytest.faux.gen_alphanumeric())
    response = integration.prepare_api_response(str(root_path))

    assert str(root_path.joinpath("ceph.conf")) in response
    assert str(root_path.joinpath("images.keyring")) in response
    assert str(root_path.joinpath("vols.keyring")) in response
    assert len(response) == 3

    assert response[str(root_path.joinpath("images.keyring"))] == \
        integration.keyrings["images.keyring"]
    assert response[str(root_path.joinpath("vols.keyring"))] == \
        integration.keyrings["vols.keyring"]

    assert "[client.images]" in response[str(root_path.joinpath("ceph.conf"))]
    assert "[client.vols]" in response[str(root_path.joinpath("ceph.conf"))] 
Example #5
Source File: AppServer.py    From TensorHive with Apache License 2.0 6 votes vote down vote up
def _inject_api_endpoint_to_app():
    '''
    Allows for changing API URL that web app is sending requests to.
    Web app expects API URL to be specified in `config.json`.
    The file must not exist, it will be created automatically if needed.
    '''
    try:
        web_app_json_config_path = PosixPath(__file__).parent / 'dist/static/config.json'
        data = {
            'apiPath': 'http://{}:{}/{}'.format(
                API.URL_HOSTNAME,
                API_SERVER.PORT,
                API.URL_PREFIX),
            'version': tensorhive.__version__,
            'apiVersion': API.VERSION
        }
        # Overwrite current file content/create file if it does not exist
        with open(str(web_app_json_config_path), 'w') as json_file:
            json.dump(data, json_file)
    except IOError as e:
        log.error('Could inject API endpoint URL, reason: ' + str(e))
    except Exception as e:
        log.critical('Unknown error: ' + str(e))
    else:
        log.debug('API URL injected successfully: {}'.format(data)) 
Example #6
Source File: py7zr.py    From py7zr with GNU Lesser General Public License v2.1 6 votes vote down vote up
def is_7zfile(file: Union[BinaryIO, str, pathlib.Path]) -> bool:
    """Quickly see if a file is a 7Z file by checking the magic number.
    The file argument may be a filename or file-like object too.
    """
    result = False
    try:
        if isinstance(file, io.IOBase) and hasattr(file, "read"):
            result = SevenZipFile._check_7zfile(file)  # type: ignore  # noqa
        elif isinstance(file, str):
            with open(file, 'rb') as fp:
                result = SevenZipFile._check_7zfile(fp)
        elif isinstance(file, pathlib.Path) or isinstance(file, pathlib.PosixPath) or \
                isinstance(file, pathlib.WindowsPath):
            with file.open(mode='rb') as fp:  # type: ignore  # noqa
                result = SevenZipFile._check_7zfile(fp)
        else:
            raise TypeError('invalid type: file should be str, pathlib.Path or BinaryIO, but {}'.format(type(file)))
    except OSError:
        pass
    return result 
Example #7
Source File: files.py    From ueberzug with GNU General Public License v3.0 6 votes vote down vote up
def lock(path: pathlib.PosixPath):
    """Creates a lock file,
    a file protected from beeing used by other processes.
    (The lock file isn't the same as the file of the passed path.)

    Args:
        path (pathlib.PosixPath): path to the file
    """
    path = path.with_suffix('.lock')

    if not path.exists():
        path.touch()

    with path.open("r+") as lock_file:
        try:
            fcntl.lockf(lock_file.fileno(), fcntl.LOCK_EX)
            yield lock_file
        finally:
            fcntl.lockf(lock_file.fileno(), fcntl.LOCK_UN) 
Example #8
Source File: cmd_parse.py    From bayesmark with Apache License 2.0 6 votes vote down vote up
def infer_settings(opt_root, opt_pattern="**/optimizer.py"):
    opt_root = PosixPath(opt_root)
    assert opt_root.is_dir(), "Opt root directory doesn't exist: %s" % opt_root
    assert opt_root.is_absolute(), "Only absolute path should have even gotten this far."

    # Always sort for reproducibility
    source_files = sorted(opt_root.glob(opt_pattern))
    source_files = [ss.relative_to(opt_root) for ss in source_files]

    settings = {_cleanup(str(ss.parent)): [str(ss), {}] for ss in source_files}

    assert all(joinable(kk) for kk in settings), "Something went wrong in name sanitization."
    assert len(settings) == len(source_files), "Name collision after sanitization of %s" % repr(source_files)
    assert len(set(CONFIG.keys()) & set(settings.keys())) == 0, "Name collision with builtin optimizers."

    return settings 
Example #9
Source File: train.py    From nlp-architect with Apache License 2.0 5 votes vote down vote up
def parse_data(self, data: PathLike or PosixPath, parsed_dir: PathLike or PosixPath):
        _, data_size = parse_docs(self.parser, data, out_dir=parsed_dir)
        if data_size < 1000:
            raise ValueError(
                "The data contains only {0} sentences. A minimum of 1000 "
                "sentences is required for training.".format(data_size)
            )
        return parsed_dir 
Example #10
Source File: UsageLoggingService.py    From TensorHive with Apache License 2.0 5 votes vote down vote up
def __init__(self, path: PosixPath) -> None:
        self.path = path 
Example #11
Source File: UsageLoggingService.py    From TensorHive with Apache License 2.0 5 votes vote down vote up
def save(self, out_path: PosixPath) -> None:
        log_file = JSONLogFile(out_path)

        # If file is empty, initialize it with template
        if not log_file.path.exists():
            log_file.write(self.empty_log_file_format)

        # Overwrite log file with updated content
        updated_log_content = self.updated_log(log_file)
        log_file.write(updated_log_content, default=object_serializer)

        log.debug('Log file has been updated {}'.format(out_path)) 
Example #12
Source File: data_handler.py    From claf with MIT License 5 votes vote down vote up
def __init__(self, cache_path=CachePath.ROOT):
        if type(cache_path) != PosixPath:
            raise ValueError(f"cache_path type is PosixPath (use pathlib.Path). not f{type(cache_path)}")

        self.cache_path = cache_path
        cache_path.mkdir(parents=True, exist_ok=True) 
Example #13
Source File: posix.py    From Pokemon-Terminal with GNU General Public License v3.0 5 votes vote down vote up
def __build_fifo_path(name: str) -> PosixPath:
        return PosixPath(f'/tmp/{name}/{os.getuid()}') 
Example #14
Source File: posix.py    From Pokemon-Terminal with GNU General Public License v3.0 5 votes vote down vote up
def __has_open_file_handles_real(path: PosixPath) -> bool:
        for proc in psutil.process_iter():
            try:
                if proc.pid != os.getpid():
                    for file in proc.open_files():
                        if PosixPath(file.path).samefile(path):
                            return True
            except psutil.Error:
                continue
        return False 
Example #15
Source File: posix.py    From Pokemon-Terminal with GNU General Public License v3.0 5 votes vote down vote up
def __has_open_file_handles(path: PosixPath) -> bool:
        # HACK psutil explicitely filters out FIFOs from open_files()
        # HACK patch the function responsible of it so it does the reverse instead
        # HACK (only enumerate FIFOs in open_files)
        try:
            with patch("psutil._psplatform.isfile_strict", _isfifo_strict):
                return PosixNamedEvent.__has_open_file_handles_real(path)
        except Exception:
            # Something happened(tm), or the platform doesn't uses isfile_strict (ex: BSD).
            # Do a best effort.
            return PosixNamedEvent.__has_open_file_handles_real(path) 
Example #16
Source File: ssh.py    From TensorHive with Apache License 2.0 5 votes vote down vote up
def init_ssh_key(path: PosixPath):
    if path.exists():
        key = RSAKey.from_private_key_file(str(path))
        log.info('[⚙] Using existing SSH key in {}'.format(path))
    else:
        key = generate_cert(path)
        log.info('[⚙] Generated SSH key in {}'.format(path))
    return key 
Example #17
Source File: library.py    From ueberzug with GNU General Public License v3.0 5 votes vote down vote up
def main(options):
    directory = \
        pathlib.PosixPath(os.path.abspath(os.path.dirname(__file__))) / 'lib'
    print((directory / 'lib.sh').as_posix()) 
Example #18
Source File: test_pathlib.py    From android_universal with MIT License 5 votes vote down vote up
def test_concrete_class(self):
        p = self.cls('a')
        self.assertIs(type(p),
            pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath) 
Example #19
Source File: test_pathlib.py    From android_universal with MIT License 5 votes vote down vote up
def test_unsupported_flavour(self):
        if os.name == 'nt':
            self.assertRaises(NotImplementedError, pathlib.PosixPath)
        else:
            self.assertRaises(NotImplementedError, pathlib.WindowsPath) 
Example #20
Source File: cinder_integration.py    From ceph-lcm with Apache License 2.0 5 votes vote down vote up
def prepare_api_response(self, root_path):
        path = pathlib.PosixPath(root_path)
        config = self.config

        response = {}
        for basename, content in self.keyrings.items():
            response[str(path.joinpath(basename))] = content
            config += "\n{0}\nkeyring = {1}\n".format(
                content.split("\n", 1)[0],
                path.joinpath(basename)
            )
        response[str(path.joinpath("ceph.conf"))] = config

        return response 
Example #21
Source File: methplotlib.py    From methplotlib with MIT License 5 votes vote down vote up
def qc_plots(meth_data, window, qcpath=None, outpath=None):

    if qcpath is None and outpath is None:
        outfile = "qc_report_{}.html".format(window.string)
    elif qcpath is None:
        from pathlib import Path, PosixPath
        p = Path(outpath)
        Path.mkdir(p.parent, exist_ok=True, parents=True)
        outfile = str(p.parent / PosixPath("qc_" + p.stem + ".html"))
    else:
        from pathlib import Path
        p = Path(qcpath)
        Path.mkdir(p.parent, exist_ok=True, parents=True)
        outfile = qcpath

    with open(outfile, 'w') as qc_report:
        qc_report.write(qc.num_sites_bar(meth_data))
        if len([m for m in meth_data if m.data_type == "nanopolish_freq"]) > 0:
            data = [m.table.rename({"methylated_frequency": m.name}, axis='columns')
                    for m in meth_data if m.data_type == "nanopolish_freq"]
            full = data[0].join(data[1:]).dropna(how="any", axis="index")
            qc_report.write(qc.modified_fraction_histogram(full))
        if len([m for m in meth_data if m.data_type == "nanopolish_freq"]) > 2:
            qc_report.write(qc.pairwise_correlation_plot(full))
            qc_report.write(qc.pca(full))
            qc_report.write(qc.global_box(data))
        if len([m for m in meth_data
                if m.data_type in ["nanopolish_call", "nanopolish_phased"]]) > 2:
            pass 
Example #22
Source File: filesystem.py    From pyrdp with GNU General Public License v3.0 5 votes vote down vote up
def getFullPath(self, name: str = "") -> str:
        path = PosixPath("/")

        if name != "":
            path /= name

        return str(path) 
Example #23
Source File: test_factory.py    From asu with GNU General Public License v2.0 5 votes vote down vote up
def test_pathlib(app):
    assert isinstance(app.config["STORE_PATH"], PosixPath)
    assert isinstance(app.config["JSON_PATH"], PosixPath)
    assert isinstance(app.config["CACHE_PATH"], PosixPath)
    assert app.config["STORE_PATH"].is_dir()
    assert app.config["JSON_PATH"].is_dir()
    assert app.config["CACHE_PATH"].is_dir() 
Example #24
Source File: cluster.py    From transcoder with GNU General Public License v3.0 5 votes vote down vote up
def converted_path(self, path):
        if self.props.is_windows():
            return str(PureWindowsPath(path))
        else:
            return str(PosixPath(path)) 
Example #25
Source File: test_pathlib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_concrete_class(self):
        p = self.cls('a')
        self.assertIs(type(p),
            pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath) 
Example #26
Source File: test_pathlib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_unsupported_flavour(self):
        if os.name == 'nt':
            self.assertRaises(NotImplementedError, pathlib.PosixPath)
        else:
            self.assertRaises(NotImplementedError, pathlib.WindowsPath) 
Example #27
Source File: test_pathlib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_concrete_class(self):
        p = self.cls('a')
        self.assertIs(type(p),
            pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath) 
Example #28
Source File: test_pathlib.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_unsupported_flavour(self):
        if os.name == 'nt':
            self.assertRaises(NotImplementedError, pathlib.PosixPath)
        else:
            self.assertRaises(NotImplementedError, pathlib.WindowsPath) 
Example #29
Source File: filesystem.py    From pyrdp with GNU General Public License v3.0 5 votes vote down vote up
def getFullPath(self, name: str = "") -> str:
        path = PosixPath(self.name)

        if name != "":
            path /= name

        path = str(path)

        if self.parent is None:
            return path
        else:
            return self.parent.getFullPath(path) 
Example #30
Source File: filesystem.py    From pyrdp with GNU General Public License v3.0 5 votes vote down vote up
def getFullPath(self, name: str = "") -> str:
        path = PosixPath(self.name)

        if name != "":
            path /= name

        path = str(path)

        if self.parent is None:
            return path
        else:
            return self.parent.getFullPath(path)