date2name now handles items in (sub)folders

This commit is contained in:
Karl Voit 2011-09-25 14:19:27 +02:00
parent 368d4e5a8a
commit bcc4a5cfdc

127
date2name
View file

@ -1,23 +1,27 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Latest change: Fri Mar 27 00:27:30 CET 2009
# Time-stamp: <2011-09-25 14:17:42 vk>
"""
date2name
~~~~~~~~~
This script adds (or removes) datestamps to (or from) file(s)
:copyright: (c) 2009 by Karl Voit <tools@Karl-Voit.at>
:copyright: (c) 2009-2011 by Karl Voit <tools@Karl-Voit.at>
:license: GPL v2 or any later version
:bugreports: <tools@Karl-Voit.at>
"""
## changes:
## * v0.0.1 -> 0.1: script is able to handle files and folders in (sub)directories
import re, os, time, logging, sys
from optparse import OptionParser
# global variables
PROG_VERSION = "0.0.6"
PROG_VERSION = "0.1"
FORMATSTRING_COMPACT = "%Y%m%d"
FORMATSTRING_STANDARD = "%Y-%m-%d"
FORMATSTRING_MONTH = "%Y-%m"
@ -100,7 +104,7 @@ def handle_logging():
logging.basicConfig(level=logging.INFO, format=FORMAT)
def get_converted_itemname(matched_pattern, item):
def get_converted_basename(matched_pattern, item):
"""returns a new filename based on found timestamp information and currently selected datestamp format"""
if matched_pattern == "compact":
@ -157,91 +161,94 @@ def get_timestamp_from_file(formatstring, item):
logging.error("internal error: not ctime nor mtime chosen! You can correct this by giving a parameter")
def generate_new_itemname(formatstring, item):
def generate_new_basename(formatstring, basename):
"""generates the new itemname; considering options.nocorrections"""
if options.nocorrections or NODATESTAMP_PATTERN.match( item ):
logging.debug("item \"%s\" matches nodatestamp-pattern or option nocorrections is set: skipping further pattern matching" % item)
new_itemname = get_timestamp_from_file(formatstring, item)
if options.nocorrections or NODATESTAMP_PATTERN.match( basename ):
logging.debug("basename \"%s\" matches nodatestamp-pattern or option nocorrections is set: skipping further pattern matching" % basename)
new_basename = get_timestamp_from_file(formatstring, basename)
elif WITHTIME_PATTERN.match( item ):
logging.debug("item \"%s\" matches withtime-pattern" % item)
elif WITHTIME_PATTERN.match( basename ):
logging.debug("basename \"%s\" matches withtime-pattern" % basename)
if options.withtime:
logging.debug("old pattern is the same as the recognised, itemname stays the same")
return item
logging.debug("old pattern is the same as the recognised, basename stays the same")
return basename
else:
new_itemname = get_converted_itemname("withtime", item)
new_basename = get_converted_basename("withtime", basename)
elif STANDARD_PATTERN.match( item ):
logging.debug("item \"%s\" matches standard-pattern" % item)
elif STANDARD_PATTERN.match( basename ):
logging.debug("basename \"%s\" matches standard-pattern" % basename)
if not options.withtime and not options.compact and not options.month:
logging.debug("old pattern is the same as the recognised, itemname stays the same")
return item
logging.debug("old pattern is the same as the recognised, basename stays the same")
return basename
else:
new_itemname = get_converted_itemname("standard", item)
new_basename = get_converted_basename("standard", basename)
elif COMPACT_PATTERN.match( item ):
logging.debug("item \"%s\" matches compact-pattern" % item)
elif COMPACT_PATTERN.match( basename ):
logging.debug("basename \"%s\" matches compact-pattern" % basename)
if options.compact:
logging.debug("old pattern is the same as the recognised, itemname stays the same")
return item
logging.debug("old pattern is the same as the recognised, basename stays the same")
return basename
else:
new_itemname = get_converted_itemname("compact", item)
new_basename = get_converted_basename("compact", basename)
elif MONTH_PATTERN.match( item ):
logging.debug("item \"%s\" matches month-pattern" % item)
elif MONTH_PATTERN.match( basename ):
logging.debug("basename \"%s\" matches month-pattern" % basename)
if options.month:
logging.debug("old pattern is the same as the recognised, itemname stays the same")
return item
logging.debug("old pattern is the same as the recognised, basename stays the same")
return basename
else:
new_itemname = get_converted_itemname("month", item)
new_basename = get_converted_basename("month", basename)
else:
logging.debug("item \"%s\" does not match any known datestamp-pattern" % item)
new_itemname = get_timestamp_from_file(formatstring, item)
logging.debug("basename \"%s\" does not match any known datestamp-pattern" % basename)
new_basename = get_timestamp_from_file(formatstring, basename)
logging.debug("new itemname is \"%s\"" % new_itemname)
logging.debug("new basename is \"%s\"" % new_basename)
return new_itemname
return new_basename
def get_full_name(path, filename):
return path + "/" + filename
def handle_item(item, formatstring):
def handle_item(path, basename, formatstring):
"""Handle timestamp adding or removing with directories or files"""
options.remove = False
if options.remove:
logging.debug("removing timestamp from file \"%s\"" % item)
logging.debug("removing timestamp from base \"%s\"" % basename)
## FIXXME: implement datestamp removing
logging.error("Sorry! Removing of datestamps is not yet implemented")
else:
logging.debug("#######################+++~~- adding timestamp to file \"%s\"" % item)
logging.debug("#######################+++~~- adding timestamp to base \"%s\"" % basename)
if options.onlyfiles and os.path.isdir(item):
logging.debug("skipping directory \"%s\" because of command line option \"-f\"" % item)
if options.onlyfiles and os.path.isdir(basename):
logging.debug("skipping directory \"%s\" because of command line option \"-f\"" % basename)
return
if options.onlydirectories and os.path.isfile(item):
logging.debug("skipping file \"%s\" because of command line option \"-d\"" % item)
if options.onlydirectories and os.path.isfile(basename):
logging.debug("skipping file \"%s\" because of command line option \"-d\"" % basename)
return
new_itemname = generate_new_itemname(formatstring, item)
new_basename = generate_new_basename(formatstring, basename)
logging.debug("new itemname for \"%s\" will be \"%s\"" % ( item, new_itemname ))
logging.debug("new itemname for \"%s\" will be \"%s\"" % ( basename, new_basename ))
if options.dryrun:
if item == new_itemname:
logging.info("%s ... no modification" % item)
if basename == new_basename:
logging.info("%s ... no modification" % basename)
else:
logging.info("%-40s > %s" % ( item, new_itemname ) )
logging.info("%-40s > %s" % ( get_full_name(path,basename), new_basename ) )
else:
if item == new_itemname:
logging.info("\"%s\" ... no modification" % item)
if basename == new_basename:
logging.info("\"%s\" ... no modification" % get_full_name(path,basename))
else:
logging.debug("\"%s\" > \"%s\"" % ( item, new_itemname ) )
logging.info("%-40s > %s" % ( item, new_itemname ) )
os.rename(item, new_itemname)
logging.debug("\"%s\" > \"%s\"" % ( get_full_name(path,basename), new_basename ) )
logging.info("%-40s > %s" % ( get_full_name(path, basename), new_basename ) )
os.rename(basename, new_basename)
def main():
@ -287,13 +294,23 @@ def main():
logging.debug("no option given for format string; taking standard format")
formatstring = FORMATSTRING_STANDARD
original_path = os.getcwd()
for item in filelist:
if os.path.isdir(item):
logging.debug("is directory: %s" % item)
handle_item(item, formatstring)
elif os.path.isfile(item):
logging.debug("is file: %s" % item)
handle_item(item, formatstring)
if os.path.isdir(item) or os.path.isfile(item):
logging.debug("handling item: " + item + " <-----------------")
path = os.path.dirname(item)
logging.debug("has directory: " + path)
basename = os.path.basename(item)
logging.debug("has basename: " + basename)
if path:
os.chdir(path)
logging.debug("changed cwd to: " + os.getcwd())
handle_item(os.path.dirname(item), os.path.basename(item), formatstring)
os.chdir(original_path)
else:
logging.critical("%s: is no file or directory (broken link?)" % item)