Source code for egaia.egaia_tag

import os
import uuid

import egaia_root
import egaia_parsefn
import egaia_log
import egaia_list
import egaia_docx
import egaia_sanitize


[docs]def mkuuid(): """Generate a new uuid.""" return str(uuid.uuid4())
[docs]def getTaggedName(filepath, UUID=None): """Return a filename tagged with uuid""" fileParts = egaia_parsefn.parseFilename(filepath) if fileParts[1]: # file including uuid return filepath # file with extension if not UUID: UUID = mkuuid() return ".".join([fileParts[0], UUID, fileParts[2]])
[docs]def tagFile(filepath, UUID=None): """Rename a file, tagged with a uuid""" dirname = os.path.dirname(filepath) tagged = os.path.join(dirname, getTaggedName(filepath, UUID=UUID)) if tagged != filepath: print "Renaming %s as %s..." % (filepath, tagged) os.rename(filepath, tagged) egaia_log.logRename(filepath, tagged) return True else: return False
[docs]def untagFile(filepath): """Rename a file, with uuid tag removed""" dirname = os.path.dirname(filepath) (basename, uuid, extension) = egaia_parsefn.parseFilename(filepath) untagged = os.path.join(dirname, '.'.join([basename, extension])) if untagged != filepath: print "Renaming %s as %s..." % (filepath, untagged) os.rename(filepath, untagged) egaia_log.logRename(filepath, untagged) return True else: return False
[docs]def tagFiles(): """Tag ALL the files in a collection""" filepaths = egaia_list.listFiles(filter_type='untagged') if not filepaths: print "No files left to tag." return for filepath in filepaths: # Careful: the filepaths will change if we tag directories! filepath = egaia_sanitize.sanitize(filepath) # FIXME: match untagged metadata or derivative files to their originals tagFile(filepath)
[docs]def tagDirs(): """Tag the directories ending in ".dir" or ".vclips".""" filepaths = egaia_list.listDirs(filter_type='untagged') if not filepaths: print "No directories left to tag." return for filepath in filepaths: tagFile(filepath)
[docs]def untagFiles(): """UNTAG ALL the files in a collection""" filepaths = egaia_list.listFiles(filter_type='tagged') if not filepaths: print "No files to untag!" return for filepath in filepaths: untagFile(filepath)
def _cli(args): """egaia tag Tag new filenames with uuid identifiers and create metadata documents where they do not exist. Usage: egaia tag --help egaia tag [ --untag ] """ # We need to be in a collection in order to search for matching filenames # from the root; metadata and other files may not be in the same directory if not egaia_root.get_root(): exit('Please run "egaia init" to create a new collection here.') if args['--untag']: untagFiles() else: tagFiles() tagDirs() egaia_docx.update(update_existing=False)