disambigufile package

Submodules

disambigufile.disambigufile module

Class with file-like interface to a file found in provided search path

See top level package docstring for documentation

exception disambigufile.disambigufile.AmbiguousMatchError(found)[source]

Bases: disambigufile.disambigufile.Error

class disambigufile.disambigufile.DisFile(pattern: str, expand: bool = True, path: Optional[str] = None, subpattern: Optional[str] = None, pathopt: str = 'path', pathlist: List[str] = NOTHING)[source]

Bases: object

Class with file-like interface to a file found in provided search path

  • To get filename of disambiguated file, evaluate in string context

  • Supports with context statements

  • Raises exceptions if file is ambiguous
    • All module exceptions inherit from disambigufile.Error

See help(disambigufile) for examples

patternstr

Regular expression describing desired match

expandbool, default=True

Expand ~ and environment variables in path components

pathstr, default=None

Directories to search (colon-separated)

subpatternstr, default=None

Regular expression describing secondary match

pathoptstr, default=’path’

Option name when using configuration data

NoMatchError AmbiguousMatchError

expand: bool
hit()[source]

return filename of disambiguated file

open(mode='r')[source]

open disambiguated file and return file-like object

path: str
pathlist: List[str]
pathopt: str
pattern: str
subpattern: str
exception disambigufile.disambigufile.Error[source]

Bases: Exception

exception disambigufile.disambigufile.NoMatchError[source]

Bases: disambigufile.disambigufile.Error

Module contents

Class with file-like interface to a file found in provided search path

Features

  • Search a path for a file that matches a pattern

  • Search a path for a file inside directories that match a pattern

  • Basic file-like interfaces:
    • DisFile(…).open()

    • with DisFile(…) as f: …

  • Get path from config file (if optini module installed)

Examples

Simple usage:

from disambigufile import DisFile
path = '/bin:/usr/bin:/usr/local/bin'
try:
    print(DisFile('^ls', path=path))
except Exception as e:
    print(f"unable to disambiguate file; exception: {e}")

Using a with statement to open the disambiguated file:

from disambigufile import DisFile
path = 'path1:path2'
try:
    with DisFile(r'^asdf', path=path) as f:
        print(f.read())
except Exception as e:
    print(f"unable to disambiguate file; exception: {e}")

With more specific exception handling:

from disambigufile import DisFile
import disambigufile
path = '/bin:/usr/bin:/usr/local/bin'
try:
    print(DisFile('^ls', path=path))
except disambigufile.Error as e:
    # will only catch module-specific exceptions
    print(f"unable to disambiguate file; exception: {e}")

Match a file inside of a matched directory:

from disambigufile import DisFile
# search for unique file matching ~/Datasets/*2019-08-19*/data*
path='~/Datasets'
try:
    hit = DisFile(
        pattern='2019-08-19',
        path=path,
        subpattern='^data',
    )
    print(hit)
except disambigufile.Error as e:
    print(f"unable to disambiguate file; exception: {e}")

License

  • Free software: MIT license