Skip to main content

A Python wrapper around pathlib providing an OS-independent path representation.

Project description

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')

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unified-path-1.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

unified_path-1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file unified-path-1.0.tar.gz.

File metadata

  • Download URL: unified-path-1.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for unified-path-1.0.tar.gz
Algorithm Hash digest
SHA256 79faa6575acdbd36242eb7e04f20cb6a18514269cd33e29a18a5d069b979adb6
MD5 57ac8c0b08566aed754bfc94582303fa
BLAKE2b-256 22b64e8c2dcceb9e3168b842b5da9f269f060ffaebad6719c341142683a2dac7

See more details on using hashes here.

File details

Details for the file unified_path-1.0-py3-none-any.whl.

File metadata

  • Download URL: unified_path-1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for unified_path-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5a5770e4744d8824631b1a458f88e0e190403e71cd9026f0795a5b57190b39c
MD5 6a8f6817a82fc0135db127309a050379
BLAKE2b-256 6df3281e0db55dba39850203a0c162f8519d99130649cc981fcea3c47f8c0823

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page