Home | Trees | Indices | Help |
---|
|
Miscellaneous utilities for use in writing and using commands.
Often we will want to name or create a file based on from pre-existing file paths:
Thus, here we provide a set of functions for extracting components from a file path and extrapolating new names from the same. For consistency, here is the terminology used:
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|
|||
INTERP_RE = re.compile(r'\{
|
|||
SUBSTEM_RE = re.compile(r'.
|
|||
__package__ =
|
|
Return the directory and file name of a path. For example: >>> dir_base ('bar.foo') ('', 'bar.foo') >>> dir_base ('/bar.foo') ('/', 'bar.foo') >>> dir_base ('baz/bar.foo') ('baz/', 'bar.foo') >>> dir_base ('/quux/baz/bar.foo') ('/quux/baz/', 'bar.foo') We use here the Python convention of referring to the file name (as opposed to path) as the "base". Also, there's an apparent oddity in the native splitting of paths, to do with the parent directory: >>> path.split ('bar.foo') ('', 'bar.foo') >>> path.split ('/bar.foo') ('/', 'bar.foo') >>> path.split ('baz/bar.foo') ('baz', 'bar.foo') That is, the dividing filesep only appears if there is nothing else in the directory component. This makes things difficult if you are trying to pick apart a file path and reassemble it - do I need a file separator? was there one there before? Fortunately path.join is reasonably clever about fusing paths, but this is still an inconsistency and so is fixed here.
|
Return the directory and file name of a path. For example: >>> dir_base ('bar.foo') ('', 'bar.foo') >>> dir_base ('/bar.foo') ('/', 'bar.foo') >>> dir_base ('baz/bar.foo') ('baz/', 'bar.foo') >>> dir_base ('/quux/baz/bar.foo') ('/quux/baz/', 'bar.foo') We use here the Python convention of referring to the file name (as opposed to path) as the "base". Also, there's an apparent oddity in the native splitting of paths, to do with the parent directory: >>> path.split ('bar.foo') ('', 'bar.foo') >>> path.split ('/bar.foo') ('/', 'bar.foo') >>> path.split ('baz/bar.foo') ('baz', 'bar.foo') That is, the dividing filesep only appears if there is nothing else in the directory component. This makes things difficult if you are trying to pick apart a file path and reassemble it - do I need a file separator? was there one there before? Fortunately path.join is reasonably clever about fusing paths, but this is still an inconsistency and so is fixed here.
|
Return the directory, stem of the file name and extension from a path. For example: >>> dir_stem_ext ('bar.foo') ('', 'bar', '.foo') >>> dir_stem_ext ('bar.') ('', 'bar', '.') >>> dir_stem_ext ('bar') ('', 'bar', '') >>> dir_stem_ext ('baz/bar.foo') ('baz/', 'bar', '.foo') >>> dir_stem_ext ('/quux/baz/bar.foo') ('/quux/baz/', 'bar', '.foo') Note this uses the file separator convention of dir_base.
|
Return the directory, stem of the file name and extension from a path. For example: >>> dir_stem_ext ('bar.foo') ('', 'bar', '.foo') >>> dir_stem_ext ('bar.') ('', 'bar', '.') >>> dir_stem_ext ('bar') ('', 'bar', '') >>> dir_stem_ext ('baz/bar.foo') ('baz/', 'bar', '.foo') >>> dir_stem_ext ('/quux/baz/bar.foo') ('/quux/baz/', 'bar', '.foo') Note this uses the file separator convention of dir_base.
|
Split the 'stem' of a file name into a core name and a trailing modifier. Many file names are structured as "foo-mod.ext" or "foo.mod.ext" where "mod" is some qualifier, e.g. "report-2.doc", "programme.20101201.txt". This sniffs out the usual forms of these endings and if found, splits and returns the name there. For example: >>> substem_mod ('report-2') ('report', '-2') >>> substem_mod ('programme.20101201') ('programme', '.20101201') >>> substem_mod ('report 2') ('report 2', '') >>> substem_mod ('programme_20101201') ('programme_20101201', '')
|
Interpolate the bracketed sections of the passed string as keyed substrings. The intent for this is as a very simple templating or substitution system to be used in configurations and the like. For example: >>> d = {'foo': '123', 'bar': '456'} >>> interpolate ('abcdef', d) 'abcdef' >>> interpolate ('abc{foo}def', d) 'abc123def' >>> interpolate ('ab{foo}cd{bar}ef', d) 'ab123cd456ef'
|
Interpolate using qualities of a file path. This allows a new file name or path (or actually any string) to be generated by interpolation from a file path. This allows construction of paths to files in the same directory, files with the same name but different extension, files with the same name except for a suffix, etc. The substitution keywords are:
For example: >>> pth = '/foo/bar.baz' >>> interpolate_from_path (pth, '{stem}.new{ext}') 'bar.new.baz' >>> d = {'prefix': 'PRFX', 'ext': '.txt'} >>> interpolate_from_path (pth, '{prefix}{stem}.new{ext}', d) 'PRFXbar.new.txt'
|
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Mon Aug 1 17:55:29 2011 | http://epydoc.sourceforge.net |