Generalised abstract file path that provides path manipulations independent from the local environment
Project description
GPath
GPath is a Python package that provides a robust, generalised abstract file path that allows path manipulations independent from the local environment, maximising cross-platform compatibility.
Install
pip install generic-path
Basic examples
Import GPath:
from gpath import GPath
Create a GPath object and manipulate it:
g = GPath("/usr/bin")
common = GPath.find_common(g, "/usr/local/bin") # GPath("/usr")
relpath = g.relpath_from("/usr/local/bin") # GPath("../../bin")
joined = GPath.join("/usr/local/bin", relpath) # GPath("/usr/bin")
assert g == joined
For function arguments, strings or os.PathLike objects can be used interchangeably with GPaths.
Binary operations are also supported:
g1 = GPath("C:/Windows/System32")
g2 = GPath("../SysWOW64/drivers")
added = g1 + g2 # GPath("C:/Windows/SysWOW64/drivers")
subtracted = g1 - 1 # GPath("C:/Windows")
# Shift the imaginary current working directory in relative paths
shifted_right = g2 >> 1 # GPath("../../SysWOW64/drivers")
shifted_left = g2 << 1 # GPath("SysWOW64/drivers")
The GPath.partition() method is useful when dealing with paths from various different sources:
partitions = GPath.partition("/usr/bin", "/usr/local/bin", "../../doc", "C:/Windows", "C:/Program Files")
assert partitions == {
GPath("/usr") : [GPath("bin"), GPath("local/bin")],
GPath("../../doc") : [GPath("")],
GPath("C:/") : [GPath("Windows"), GPath("Program Files")],
}
Issues
Found a bug? Please report an issue, or, better yet, contribute a bugfix.
Compatibility
The default GPath() interface supports the vast majority of valid file paths on Windows, Linux and macOS (and other POSIX-like operating systems), with some limited caveats.
Linux, macOS and POSIX
If using GPath(),
- any backslashes
\(after parsing escape sequences) in the path will be treated as path separators - if the second character of the path is a colon
x:, the first characterxwill be treated as a drive letter
These issues can be avoided by using GPath.from_posix() instead. This will cause all \ and : to be treated as normal characters in file names.
Windows
- trailing dots
.and spaceswill not be stripped - reserved MS-DOS device names (such as AUX, CLOCK$, COM0 through COM9, CON, LPT0 through LPT9, NUL, PRN) will be treated as normal file names
Changelog
This project follows PEP 440 and Semantic Versioning (SemVer). In addition to the guarantees specified by SemVer, for versions before 1.0, this project guarantees backwards compatibility of the API for patch version updates (0.y.z).
The recommended version specifier is generic-path ~= x.y for version 1.0 and later, and generic-path ~= 0.y.z for versions prior to 1.0.
0.5.1
- Improved documentation
0.5
- Added support for Python 3.12 and 3.13
- Dropped support for end-of-life Python versions 3.7 and 3.8
- Removed methods
Platform.__new__andPlatform.__format__that were unintentionally documented as public
0.4.5
- Improved documentation
0.4.4
- Added the ability to force GPath to interpret a path string as originating from a specific operating system, which prevents problems in edge cases, using the new
platformargument inGPath.__init__(); this also propagates to new GPaths returned by operations- Added the read-only property
g.platformfor this propagating platform parameter - Added static methods
from_posix(),from_windows()(and aliasesfrom_linux()andfrom_macos()) as alternative interfaces to call the constructor for a specific platform - Added submodule
gpath.platformwith aPlatformenum for specifying platforms (either as a string given ingpath.platform.platform_namesor as agpath.Platformobject)
- Added the read-only property
- Added
g.render(), which returns aRenderedPathobject for printing and sorting in a platform-specific manner- Added submodule
gpath.rendercontaining subclasses ofRenderedPaththat define the render behaviour for each platform - GenericRenderedPath resembles the previous behaviour of GPath, and prints with forward slashes
/ - PosixRenderedPath ignores drive names when sorting and printing, and always prints with forward slashes
/ - WindowsRenderedPath always prints with backslashes
\
- Added submodule
- Fixed bug with encoding propagation in the copy constructor
0.4.3
- Fixed support for bytes in older versions of Python
0.4.1, 0.4.2
- Fixed documentation and readme
0.4
Breaking API changes
- Replaced the following instance methods with read-only properties:
g.get_parent_level()→g.parent_levelg.get_parent_parts()→g.parent_partsg.get_device()→g.drive(renameddevicetodrive)g.is_absolute()→g.absoluteg.is_root()→g.root
- Replaced
g.get_parts()andg.from_parts()with the read-only propertiesg.named_partsandg.relative_parts - Replaced
GPath.find_common()withg.common_with()and addedg1 & g2as an alias forg.common_with()with default options - Removed the ability to sort GPaths, and removed the following comparison operators:
g1 < g2g1 <= g2g1 > g2g1 >= g2
- Removed package constants
PATH_SEPARATOR,PATH_CURRENT,PATH_PARENT, and typedefPathLike
Breaking behavioural changes
- Changed
g1 + g2,g1 / g2andg.join()so that appending an absolute path overwrites the left operand; previously the left operand would be returned unchanged - Changed
g1 == g2so that it can return True even when the left operand is GPath-like but not a GPath object
Other changes
- Added
g.as_absolute(),g.as_relative(),g.with_drive(),g.without_drive()for returning modified copies of the path - Added support for drive names in relative paths
- Added support for instantiating a GPath with a bytes-like object,
GPath(byteslike)(or, fixed the constructor that was previously broken for bytes-like objects) - Added an argument to
GPath.__init__()to allow specifying encoding (default'utf-8'), which propagates to new GPaths when performing operations with other bytes-like operands - Added the read-only property
g.encodingfor this propagating encoding - Added abstract base classes for GPath, from
collections.abc - Fixed
g1 / g2 - Fixed small errors in web documentation
0.3
- Renamed
GPath.currenttoGPath.current_dirandGPath.parenttoGPath.parent_dir - Renamed
g.is_root()tog.is_absolute() - Renamed the optional arguments in
g.find_common()andg.partition(), fromcommon_currentandcommon_parenttoallow_currentandallow_parent - Added a new
g.is_root()that checks whether the path is exactly root - Added
g.__div__()as an alias ofg.__add__() - Added web documentation at https://gpath.gnayihs.uy/
0.2.1
- Fixed basic example in README
0.2
- Added support for Python versions 3.7 through 3.9; previously only 3.10 and 3.11 were supported
0.1
- Initial version
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file generic_path-0.5.1.post1.tar.gz.
File metadata
- Download URL: generic_path-0.5.1.post1.tar.gz
- Upload date:
- Size: 137.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a241f0b30cdbaca613b70fed885bc458ea402a0fd149dbb7388c70d1b28e15d7
|
|
| MD5 |
c4abb9d1655d0901904cdd7277d9d4fc
|
|
| BLAKE2b-256 |
270032e732ae8690e1a927071df3bdfa330d96618c01747a16f4a78db194e35d
|
File details
Details for the file generic_path-0.5.1.post1-py3-none-any.whl.
File metadata
- Download URL: generic_path-0.5.1.post1-py3-none-any.whl
- Upload date:
- Size: 24.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83e558b1ba3bb5aa7c226911b24e15e51047bf50cedbf5ba1b92dec44a424c7f
|
|
| MD5 |
919b859dd587624cdf7b35ecf0817071
|
|
| BLAKE2b-256 |
eee55c0c64cba935db3542183de2ea84d1374182f643100704a068eddd92ad7a
|