Unified Path
Unified Path is a Python wrapper around pathlib.PurePath and pathlib.Path, that provides an OS-independent path representation.
It is especially useful in shared environments when you need to write to a path in a configuration file, and this file may be read from different OS.
Installation
Install from pypi using pip:
pip install unified-path
Drive translations
Let's say you have a shared drive containing pictures. This drive is accessible on Windows as P:, and on Linux as /mnt/pictures.
Using this library you can easily handle this situation by storing an unified path to the Pictures drive, that will be readable on both OS.
First you need to register the paths:
import unified_path as up
up.register_drive_translation('P:/', '/mnt/pictures')
Then you can obtain the unified string representation using the as_unified() function:
# In Linux
up.UnifiedPath('/mnt/pictures/folder/img.jpg').as_unified() # P:/folder/img.jpg
# In Windows
up.UnifiedPath('P:\\folder\\img.jpg').as_unified() # P:/folder/img.jpg
You can construct an UnifiedPath instance from either the local representation or either the unified representation.
As UnifiedPath inherits from pathlib.Path, you can use the result as any other Path object.
# In Linux
up.UnifiedPath('P:/folder/img.jpg') # ok
up.UnifiedPath('P:\\folder\\img.jpg') # also ok
# In Windows
up.UnifiedPath('P:/folder/img.jpg') # ok
up.UnifiedPath('/mnt/pictures/folder/img.jpg') # also ok
Virtual drives
It is also possible to assign a virtual drive letter to a dynamic path. This allows to "hide" a part of the path that may change, so that the stored path will always remain valid.
import unified_path as up
git_root = ... # obtain root path of current repository using some git command
up.register_virtual_drive('G:/', git_root)
up.UnifiedPath('G:/data/conf.json') # ok
Usage with argparse
Unified Path can be used as argparse arguments:
# Now you can use -i P:/folder/img.jpg in Linux
parser.add_argument('-i', type=up.UnifiedPath, required=True, help='input file')
The library also comes with a helper that constraints the file extension:
# An error will be emitted if calling -i with a file that is not a .jpg
parser.add_argument('-i', type=up.make_path_validator(['.jpg']), required=True, help='input image')
Metadata
Release files for unified-path 1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| unified-path-1.0.tar.gz | 10.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unified_path-1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.0 kB
Release files / unified-path-1.0.tar.gz
| Download URL | unified-path-1.0.tar.gz |
|---|---|
| Size | 10.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
79faa6575acdbd36242eb7e04f20cb6a18514269cd33e29a18a5d069b979adb6
|
|
BLAKE2b-256 checksum How to use checksums |
22b64e8c2dcceb9e3168b842b5da9f269f060ffaebad6719c341142683a2dac7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / unified_path-1.0-py3-none-any.whl
| Download URL | unified_path-1.0-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a5a5770e4744d8824631b1a458f88e0e190403e71cd9026f0795a5b57190b39c
|
|
BLAKE2b-256 checksum How to use checksums |
6df3281e0db55dba39850203a0c162f8519d99130649cc981fcea3c47f8c0823
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Security for unified-path 1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.