gavo.helpers.filestuff module

File- and directory related helpers for resource utilities.

exception gavo.helpers.filestuff.Error(msg='', hint=None)[source]

Bases: gavo.utils.excs.Error

class gavo.helpers.filestuff.FileRenamer(map, showOnly=False)[source]

Bases: object

is a name mapper for file rename operations and the like.

Warning: This whole thing more or less pretends there are no symlinks.


returns a dictionary old->new of renames within path.

classmethod loadFromFile(inF, **kwargs)[source]

returns a name map for whatever is serialized in the file inF.

The format of fName is line-base, with each line being one of

  • empty – ignored

  • beginning with a hash – ignored

  • <old> -> <new> – introducing a map


returns a sequence of (old,new) pairs that, when executed, keep any new from clobbering any existing old.

The function will raise an Error if there’s a cycle in fileMap. fileMap will be destroyed by this procedure


performs a name map below path.

The rules are:

  • extensions are ignored – if we map foo to bar, foo.txt and foo.asc

    will be renamed bar.txt and foo.txt respectively

  • the order of statements in the source is irrelevant. However, we try

    not to clobber anything we’ve just renamed and will complain about cycles. Also, each file will be renamed not more than once.

gavo.helpers.filestuff.iterSources(ddId, args=[])[source]

iterates over the current sources of the data descriptor ddId (which is qualified like rdId#id

If you pass something nonempty to args, an iterator over its values will be returned. This is for convenient implementation of scripts that work on CL arguments if given, on all files otherwise.


returns name, extension for fName.

The main difference to os.path.splitext is that the main name is not allowed to contain dots and the extension can contain more than one dot.

fName is supposed to be a single file name without any path specifier (you might get away with it if your directories do not contain dots, though).