Package: generic-path
GPathGPathis a Python package that provides a robust, generalised abstract file path that allows path manipulations independent from the local environment, maximising cross-platform compatibility.Installpip install generic-pathBasic examplesImport GPath:fromgpathimportGPathCreate 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")assertg==joinedFor function arguments, strings oros.PathLikeobjects 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 pathsshifted_right=g2>>1# GPath("../../SysWOW64/drivers")shifted_left=g2<<1# GPath("SysWOW64/drivers")TheGPath.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")assertpartitions=={GPath("/usr"):[GPath("bin"),GPath("local/bin")],GPath("../../doc"):[GPath("")],GPath("C:/"):[GPath("Windows"),GPath("Program Files")],}IssuesFound a bug? Pleasereport an issue, or, better yet,contribute a bugfix.CompatibilityThe defaultGPath()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 POSIXIf usingGPath(),any backslashes\(after parsing escape sequences) in the path will be treated as path separatorsif the second character of the path is a colonx:, the first characterxwill be treated as a drive letterThese issues can be avoided by usingGPath.from_posix()instead. This will cause all\and:to be treated as normal characters in file names.Windowstrailing dots.and spaceswill not be strippedreserved MS-DOS device names (such as AUX, CLOCK$, COM0 through COM9, CON, LPT0 through LPT9, NUL, PRN) will be treated as normal file namesChangelogThis project followsPEP 440andSemantic 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 isgeneric-path ~=x.yfor version 1.0 and later, andgeneric-path ~=0.y.zfor versions prior to 1.0.0.4.5Improved documentation0.4.4Added 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 newplatformargument inGPath.__init__(); this also propagates to new GPaths returned by operationsAdded the read-only propertyg.platformfor this propagating platform parameterAdded static methodsfrom_posix(),from_windows()(and aliasesfrom_linux()andfrom_macos()) as alternative interfaces to call the constructor for a specific platformAdded submodulegpath.platformwith aPlatformenum for specifying platforms (either as a string given ingpath.platform.platform_namesor as agpath.Platformobject)Addedg.render(), which returns aRenderedPathobject for printing and sorting in a platform-specific mannerAdded submodulegpath.rendercontaining subclasses ofRenderedPaththat define the render behaviour for each platformGenericRenderedPath 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\Fixed bug with encoding propagation in the copy constructor0.4.3Fixed support for bytes in older versions of Python0.4.1, 0.4.2Fixed documentation and readme0.4Breaking API changesReplaced 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.rootReplacedg.get_parts()andg.from_parts()with the read-only propertiesg.named_partsandg.relative_partsReplacedGPath.find_common()withg.common_with()and addedg1&g2as an alias forg.common_with()with default optionsRemoved the ability to sort GPaths, and removed the following comparison operators:g1<g2g1<=g2g1>g2g1>=g2Removed package constantsPATH_SEPARATOR,PATH_CURRENT,PATH_PARENT, and typedefPathLikeBreaking behavioural changesChangedg1+g2,g1/g2andg.join()so that appending an absolute path overwrites the left operand; previously the left operand would be returned unchangedChangedg1==g2so that it can return True even when the left operand is GPath-like but not a GPath objectOther changesAddedg.as_absolute(),g.as_relative(),g.with_drive(),g.without_drive()for returning modified copies of the pathAdded support for drive names in relative pathsAdded 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 toGPath.__init__()to allow specifying encoding (default'utf-8'), which propagates to new GPaths when performing operations with other bytes-like operandsAdded the read-only propertyg.encodingfor this propagating encodingAdded abstract base classes for GPath, fromcollections.abcFixedg1/g2Fixed small errors in web documentation0.3RenamedGPath.currenttoGPath.current_dirandGPath.parenttoGPath.parent_dirRenamedg.is_root()tog.is_absolute()Renamed the optional arguments ing.find_common()andg.partition(), fromcommon_currentandcommon_parenttoallow_currentandallow_parentAdded a newg.is_root()that checks whether the path is exactly rootAddedg.__div__()as an alias ofg.__add__()Added web documentation athttps://gpath.gnayihs.uy/0.2.1Fixed basic example in README0.2Added support for Python versions 3.7 through 3.9; previously only 3.10 and 3.11 were supported0.1Initial version