SF.net SVN: fclient: [122] trunk/fclient/fclient_lib/pyex/namespace.py
Status: Pre-Alpha
Brought to you by:
jurner
From: <ju...@us...> - 2008-02-02 18:30:29
|
Revision: 122 http://fclient.svn.sourceforge.net/fclient/?rev=122&view=rev Author: jurner Date: 2008-02-02 10:30:26 -0800 (Sat, 02 Feb 2008) Log Message: ----------- some new methods Modified Paths: -------------- trunk/fclient/fclient_lib/pyex/namespace.py Modified: trunk/fclient/fclient_lib/pyex/namespace.py =================================================================== --- trunk/fclient/fclient_lib/pyex/namespace.py 2008-02-02 18:29:43 UTC (rev 121) +++ trunk/fclient/fclient_lib/pyex/namespace.py 2008-02-02 18:30:26 UTC (rev 122) @@ -5,15 +5,88 @@ #********************************************************************* # #********************************************************************* -def unique_filename(fpath, prefix='', names=None): - """Creates a filepath with a unique (human readable) filename - @param: fpath: filepath to patch +def split_ext(fpath, extensions=1): + """Splits a filepath into filepath and extension + @param fpath: (str) filepath to split + @param extensions: (int) maximum number of extensions to split (can be a negative integer aswell) + + >>> split_ext('foo.txt') + ('foo', '.txt') + + >>> split_ext('foo.txt.bar', extensions=2) + ('foo', '.txt.bar') + + >>> split_ext('foo.txt.bar', extensions=0) + ('foo.txt.bar', '') + + >>> split_ext('foo.txt', extensions=-2) + ('foo', '.txt') + + """ + if extensions == 0: + return fpath, '' + + if extensions == 1: + head, tail = os.path.splitext(fpath) + return head, tail + + elif extensions < 1: + out = [] + tmp_fpath = fpath + while extensions < 0: + tmp_fpath, ext = os.path.splitext(tmp_fpath) + if not ext: break + out.insert(0, ext) + extensions += 1 + return tmp_fpath, ''.join(out) + + elif extensions > 1: + out = [] + tmp_fpath = fpath + for i in xrange(extensions): + tmp_fpath, ext = os.path.splitext(tmp_fpath) + if not ext: break + out.insert(0, ext) + return tmp_fpath, ''.join(out) + +#********************************************************************* +# +#********************************************************************* +def strip_unique_filename_postfix(fpath, extensions=1, pattern=re.compile('\s\([1-9]\d*\)\Z')): + """Strips a posfix as added by L{unique_filename} from a filepath + @param fpath: (str) filepath to strip + @param extensions: (int) maximum number of extensions to split (may be a negative integer aswell) + @param pattern: regex pattern tu use. The filename part of fpath (without extension) will be split + by this pattern and the first part is used for further processing. + @return: (str) stripped filepath + + >>> strip_unique_filename_postfix('foo (1).txt') + 'foo.txt' + + >>> strip_unique_filename_postfix('foo (0).txt') + 'foo (0).txt' + + """ + filename, ext = split_ext(fpath, extensions=extensions) + filename = pattern.split(filename)[0] + return filename + ext + +#********************************************************************* +# +#********************************************************************* +def unique_filename(fpath, prefix='', names=None, extensions=1, ispostfixed=False): + """Creates a filepath with a unique (human readable) postfix + @param fpath: filepath to postfix @param prefix: prefix to add to the filename or '' @param names: list of filenames to patch agaunst or None to check files in the directory of filepath + @param extensions: (int) maximum number of extensions to split (may be a negative integer aswell) + @param ispostfixed: if True, the filepath is assumed to be already postfixed and the algo tries + to replace former postfixes - @return: a filepath with one of these 'MyFile (1).txt' patched filenames + @return: a filepath with one of these 'MyFile (1).txt' postfixed filenames + >>> names = ['foo.txt', 'foo (1).txt'] >>> unique_filename('foo.txt', names=names) 'foo (2).txt' @@ -28,6 +101,9 @@ >>> result == expected True + + # an explicite prefix may be passed aswell + >>> names = ['foo.txt', ] >>> unique_filename('foo.txt', names=names, prefix="Copy Of") 'Copy Of foo.txt' @@ -36,56 +112,78 @@ >>> unique_filename('foo.txt', names=names, prefix="Copy Of") 'Copy Of foo (1).txt' + + # the algo tries its best to replace already present postfixes if desired + + >>> names = ['foo.txt', 'foo (1).txt'] + >>> unique_filename('foo (1).txt', names=names, ispostfixed=True) + 'foo (2).txt' + + >>> names = ['foo.txt', 'foo (1).txt'] + >>> unique_filename('foo.txt', names=names, ispostfixed=True) + 'foo (2).txt' + + + # the number of extensions to be assumed may be passed explicitely + + >>> names = ['foo.txt', 'foo (1).txt'] + >>> unique_filename('foo.txt.bar', names=names, extensions=2) + 'foo.txt.bar' + + >>> names = ['foo.txt.bar', 'foo (1).txt'] + >>> unique_filename('foo.txt.bar', names=names, extensions=2) + 'foo (1).txt.bar' + """ filename = os.path.basename(fpath) dirname = os.path.dirname(fpath) if not filename: raise ValueError("No filename found: %s" % fpath) - + + if ispostfixed: + filename = strip_unique_filename_postfix(filename, extensions=extensions) + tmp_filename, tmp_ext = split_ext(filename, extensions=extensions) + n = 0 - tmp_filename = filename while True: if names is not None: - if tmp_filename not in names: break + if filename not in names: break else: - if not os.path.exists(os.path.join(dirname, tmp_filename)): break + if not os.path.exists(os.path.join(dirname, filename)): break n += 1 - tmp_filename, tmp_ext = os.path.splitext(filename) if prefix: if n == 1: - tmp_filename = '%s %s' % (prefix, tmp_filename) + filename = '%s %s' % (prefix, tmp_filename) else: - tmp_filename = '%s %s (%s)' % (prefix, tmp_filename, n -1) + filename = '%s %s (%s)' % (prefix, tmp_filename, n -1) else: - tmp_filename = '%s (%s)' % (tmp_filename, n) - tmp_filename = tmp_filename + tmp_ext + filename = '%s (%s)' % (tmp_filename, n) + filename = filename + tmp_ext - return os.path.join(dirname, tmp_filename) + return os.path.join(dirname, filename) - #********************************************************************* # #********************************************************************* -def unquote_uri(uri, slash='-'): +def unquote_uri(uri, slash=None): """Unquotes an uri so it can be used as a name in the filesystem @param uri: (str) uri to unquote - @param slasch: (str) substitution string shlashes to use or None to keep slashes + @param slash: (str) substitution string shlashes to use or None to keep slashes @return: (str) uri - >>> unquote_uri('foo/bar') + >>> unquote_uri('foo/bar', slash='-') 'foo-bar' >>> unquote_uri('foo/b%20ar') - 'foo-b ar' + 'foo/b ar' >>> unquote_uri('foo/b%34ar') - 'foo-b%34ar' + 'foo/b%34ar' >>> unquote_uri('foo/bar', slash='77') 'foo77bar' >>> unquote_uri('foo/bar', slash=None) 'foo/bar' """ - # impossible to handle all the dos and donts on oses. So do minimum # to enshure user readability if slash is not None: @@ -94,48 +192,10 @@ uri = uri.replace('%28', '(') uri = uri.replace('%29', ')') return uri - #********************************************************************* # #********************************************************************* -def split_extensions(fpath): - """Splits all extensions from a filepath - @param fpath: filepath to split extensions from - @return: (tuple) filepath, [extension1, extension(N) ...] - - >>> split_extensions('foo.aaa.bbb') - ('foo', ['.aaa', '.bbb']) - - >>> split_extensions('foo') - ('foo', []) - - >>> split_extensions('.foo.aaa') - ('.foo', ['.aaa']) - - >>> split_extensions('') - ('', []) - - """ - directory, name = os.path.split(fpath) - tmp_name = name - exts = [] - while True: - tmp_name, ext = os.path.splitext(tmp_name) - if not tmp_name: - name = ext - break - if not ext: - name = tmp_name - break - exts.append(ext) - - exts.reverse() - return os.path.join(directory, name), exts - -#********************************************************************* -# -#********************************************************************* if __name__ == '__main__': import doctest doctest.testmod() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |