Package: aiopathy
AioPathy: an asynchronous Path interface for local and cloud bucket storageAioPathy is a python package (with type annotations) for working with Cloud Bucket storage providers using a pathlib interface. It provides an easy-to-use API bundled with a CLI app for basic file operations between local files and remote buckets. It enables a smooth developer experience by letting developers work against the local file system during development and only switch over to live APIs for deployment. It also makes converting bucket blobs into local files a snap with optional local file caching.🚀 QuickstartYou can installpathyfrom pip:pipinstallpathyThe package exports thePathyclass and utilities for configuring the bucket storage provider to use.frompathyimportPathy,use_fs# Use the local file-system for quicker developmentuse_fs()# Create a bucketPathy("gs://my_bucket").mkdir(exist_ok=True)# An excellent blobgreeting=Pathy(f"gs://my_bucket/greeting.txt")# But it doesn't exist yetassertnotgreeting.exists()# Create it by writing some textgreeting.write_text("Hello World!")# Now it existsassertgreeting.exists()# Delete itgreeting.unlink()# Now it doesn'tassertnotgreeting.exists()Supported CloudsThe table below details the supported cloud provider APIs.Cloud ServiceSupportInstall ExtrasGoogle Cloud Storage✅pip install pathy[gcs]Amazon S3✅pip install pathy[s3]Azure✅pip install pathy[azure]Google Cloud StorageGoogle recommends using a JSON credentials file, which you can specify by path:fromgoogle.oauth2importservice_accountfrompathyimportset_client_paramscredentials=service_account.Credentials.from_service_account_file("./my-creds.json")set_client_params("gs",credentials=credentials)Amazon S3S3 uses a JSON credentials file, which you can specify by path:frompathyimportset_client_paramsset_client_params("s3",key_id="YOUR_ACCESS_KEY_ID",key_secret="YOUR_ACCESS_SECRET")AzureAzure blob storage can be passed aconnection_string:frompathyimportset_client_paramsset_client_params("azure",connection_string="YOUR_CONNECTION_STRING")or aBlobServiceClientinstance:fromazure.storage.blobimportBlobServiceClientfrompathyimportset_client_paramsservice:BlobServiceClient=BlobServiceClient.from_connection_string("YOUR_CONNECTION_STRING")set_client_params("azure",service=service)Semantic VersioningBefore Pathy reaches v1.0 the project is not guaranteed to have a consistent API, which means that types and classes may move around or be removed. That said, we try to be predictable when it comes to breaking changes, so the project uses semantic versioning to help users avoid breakage.Specifically, new releases increase thepatchsemver component for new features and fixes, and theminorcomponent when there are breaking changes. If you don't know much about semver strings, they're usually formatted{major}.{minor}.{patch}so increasing thepatchcomponent means incrementing the last number.Consider a few examples:From VersionTo VersionChanges are Breaking0.2.00.2.1No0.3.20.3.6No0.3.10.3.17No0.2.20.3.0YesIf you are concerned about breaking changes, you can pin the version in your requirements so that it does not go beyond the current semverminorcomponent, for example if the current version was0.1.37:pathy>=0.1.37,<0.2.0🎛 APIPathyclassPathy(self,args,kwargs)Subclass ofpathlib.Paththat works with bucket APIs.existsmethodPathy.exists(self)->boolReturns True if the path points to an existing bucket, blob, or prefix.fluidclassmethodPathy.fluid(path_candidate:Union[str,Pathy,BasePath],)->Union[Pathy,BasePath]Infer either a Pathy or pathlib.Path from an input path or string.The returned type is a union of the potentialFluidPathtypes and will
type-check correctly against the minimum overlapping APIs of all the input
types.If you need to use specific implementation details of a type, "narrow" the
return of this function to the desired type, e.g.frompathyimportFluidPath,Pathyfluid_path:FluidPath=Pathy.fluid("gs://my_bucket/foo.txt")# Narrow the type to a specific classassertisinstance(fluid_path,Pathy),"must be Pathy"# Use a member specific to that classassertfluid_path.prefix=="foo.txt/"from_bucketclassmethodPathy.from_bucket(bucket_name:str,scheme:str='gs')->'Pathy'Initialize a Pathy from a bucket name. This helper adds a trailing slash and
the appropriate prefix.frompathyimportPathyassertstr(Pathy.from_bucket("one"))=="gs://one/"assertstr(Pathy.from_bucket("two"))=="gs://two/"globmethodPathy.glob(self:'Pathy',pattern:str,)->Generator[Pathy,NoneType,NoneType]Perform a glob match relative to this Pathy instance, yielding all matched
blobs.is_dirmethodPathy.is_dir(self:'Pathy')->boolDetermine if the path points to a bucket or a prefix of a given blob
in the bucket.Returns True if the path points to a bucket or a blob prefix.
Returns False if it points to a blob or the path doesn't exist.is_filemethodPathy.is_file(self:'Pathy')->boolDetermine if the path points to a blob in the bucket.Returns True if the path points to a blob.
Returns False if it points to a bucket or blob prefix, or if the path doesn’t
exist.iterdirmethodPathy.iterdir(self:'Pathy',)->Generator[Pathy,NoneType,NoneType]Iterate over the blobs found in the given bucket or blob prefix path.lsmethodPathy.ls(self:'Pathy')->Generator[BlobStat,NoneType,NoneType]List blob names with stat information under the given path.This is considerably faster than using iterdir if you also need
the stat information for the enumerated blobs.Yields BlobStat objects for each found blob.mkdirmethodPathy.mkdir(self,mode:int=511,parents:bool=False,exist_ok:bool=False,)->NoneCreate a bucket from the given path. Since bucket APIs only have implicit
folder structures (determined by the existence of a blob with an overlapping
prefix) this does nothing other than create buckets.If parents is False, the bucket will only be created if the path points to
exactly the bucket and nothing else. If parents is true the bucket will be
created even if the path points to a specific blob.The mode param is ignored.Raises FileExistsError if exist_ok is false and the bucket already exists.openmethodPathy.open(self:'Pathy',mode:str='r',buffering:int=8192,encoding:Optional[str]=None,errors:Optional[str]=None,newline:Optional[str]=None,)->IO[Any]Open the given blob for streaming. This delegates to thesmart_openlibrary that handles large file streaming for a number of bucket API
providers.ownermethodPathy.owner(self:'Pathy')->Optional[str]Returns the name of the user that owns the bucket or blob
this path points to. Returns None if the owner is unknown or
not supported by the bucket API provider.renamemethodPathy.rename(self:'Pathy',target:Union[str,pathlib.PurePath])->'Pathy'Rename this path to the given target.If the target exists and is a file, it will be replaced silently if the user
has permission.If path is a blob prefix, it will replace all the blobs with the same prefix
to match the target prefix.replacemethodPathy.replace(self:'Pathy',target:Union[str,pathlib.PurePath])->'Pathy'Renames this path to the given target.If target points to an existing path, it will be replaced.resolvemethodPathy.resolve(self,strict:bool=False)->'Pathy'Resolve the given path to remove any relative path specifiers.frompathyimportPathypath=Pathy("gs://my_bucket/folder/../blob")assertpath.resolve()==Pathy("gs://my_bucket/blob")rglobmethodPathy.rglob(self:'Pathy',pattern:str,)->Generator[Pathy,NoneType,NoneType]Perform a recursive glob match relative to this Pathy instance, yielding
all matched blobs. Imagine adding "**/" before a call to glob.rmdirmethodPathy.rmdir(self:'Pathy')->NoneRemoves this bucket or blob prefix. It must be empty.samefilemethodPathy.samefile(self:'Pathy',other_path:Union[str,bytes,int,pathlib.Path],)->boolDetermine if this path points to the same location as other_path.statmethodPathy.stat(self:'Pathy')->pathy.BlobStatReturns information about this bucket path.to_localclassmethodPathy.to_local(blob_path:Union[Pathy,str],recurse:bool=True,)->pathlib.PathDownload and cache either a blob or a set of blobs matching a prefix.The cache is sensitive to the file updated time, and downloads new blobs
as their updated timestamps change.touchmethodPathy.touch(self:'Pathy',mode:int=438,exist_ok:bool=True)->NoneCreate a blob at this path.If the blob already exists, the function succeeds if exist_ok is true
(and its modification time is updated to the current time), otherwise
FileExistsError is raised.BlobStatdataclassBlobStat(self,name:str,size:Optional[int],last_modified:Optional[int],)->NoneStat for a bucket itemuse_fsfunctionuse_fs(root:Optional[str,pathlib.Path,bool]=None,)->Optional[pathy.BucketClientFS]Use a path in the local file-system to store blobs and buckets.This is useful for development and testing situations, and for embedded
applications.get_fs_clientfunctionget_fs_client()->Optional[pathy.BucketClientFS]Get the file-system client (or None)use_fs_cachefunctionuse_fs_cache(root:Optional[str,pathlib.Path,bool]=None,)->Optional[pathlib.Path]Use a path in the local file-system to cache blobs and buckets.This is useful for when you want to avoid fetching large blobs multiple
times, or need to pass a local file path to a third-party library.get_fs_cachefunctionget_fs_cache()->Optional[pathlib.Path]Get the folder that holds file-system cached blobs and timestamps.set_client_paramsfunctionset_client_params(scheme:str,kwargs:Any)->NoneSpecify args to pass when instantiating a service-specific Client
object. This allows for passing credentials in whatever way your underlying
client library prefers.CLIPathy command line interface. (v0.5.2)Usage:$[OPTIONS]COMMAND[ARGS]...Options:--install-completion: Install completion for the current shell.--show-completion: Show completion for the current shell, to copy it or customize the installation.--help: Show this message and exit.Commands:cp: Copy a blob or folder of blobs from one...ls: List the blobs that exist at a given...mv: Move a blob or folder of blobs from one path...rm: Remove a blob or folder of blobs from a given...cpCopy a blob or folder of blobs from one bucket to another.Usage:$cp[OPTIONS]FROM_LOCATIONTO_LOCATIONArguments:FROM_LOCATION: [required]TO_LOCATION: [required]Options:--help: Show this message and exit.lsList the blobs that exist at a given location.Usage:$ls[OPTIONS]LOCATIONArguments:LOCATION: [required]Options:-l, --long: Print long style entries with updated time and size shown. [default: False]--help: Show this message and exit.mvMove a blob or folder of blobs from one path to another.Usage:$mv[OPTIONS]FROM_LOCATIONTO_LOCATIONArguments:FROM_LOCATION: [required]TO_LOCATION: [required]Options:--help: Show this message and exit.rmRemove a blob or folder of blobs from a given location.Usage:$rm[OPTIONS]LOCATIONArguments:LOCATION: [required]Options:-r, --recursive: Recursively remove files and folders. [default: False]-v, --verbose: Print removed files and folders. [default: False]--help: Show this message and exit.CreditsPathy is originally based on theS3Pathproject, which provides a Path interface for S3 buckets.