added --prepend and --smart-prepend

This commit is contained in:
Karl Voit 2019-10-20 00:14:38 +02:00
parent 385ba28608
commit 3e938903a1
2 changed files with 55 additions and 8 deletions

View file

@ -77,7 +77,7 @@ found words can be completed via TAB.
#+BEGIN_src
Usage:
appendfilename.py [<options>] <list of files>
appendfilename [<options>] <list of files>
This tool inserts text between the old file name and optional tags or file extension.
@ -92,9 +92,9 @@ When renaming a symbolic link whose source file has a matching file
name, the source file gets renamed as well.
Example usages:
appendfilename.py --text="of projectA" "the presentation.pptx"
appendfilename --text="of projectA" "the presentation.pptx"
... results in "the presentation of projectA.pptx"
appendfilename.py "2013-05-09T16.17_img_00042 -- fun.jpeg"
appendfilename "2013-05-09T16.17_img_00042 -- fun.jpeg"
... with interactive input of "Peter" results in:
"2013-05-09T16.17_img_00042 Peter -- fun.jpeg"
@ -103,12 +103,17 @@ Example usages:
:license: GPL v3 or any later version
:URL: https://github.com/novoid/appendfilename
:bugreports: via github or <tools@Karl-Voit.at>
:version: 2017-08-29
:version: 2019-10-19
Options:
-h, --help show this help message and exit
-t TEXT, --text=TEXT the text to add to the file name
-p, --prepend Do the opposite: instead of appending the text,
prepend the text
--smart-prepend Like "--prepend" but do respect date/time-stamps:
insert new text between "YYYY-MM-DD(Thh.mm(.ss))" and
rest
-s, --dryrun enable dryrun mode: just simulate what would happen,
do not modify file(s)
-v, --verbose enable verbose mode
@ -116,10 +121,29 @@ Options:
--version display version and exit
#+END_src
** Installation
Get it from [[https://github.com/novoid/appendfilename][GitHub]] or install it via «pip install appendfilename».
Get it from [[https://github.com/novoid/appendfilename][GitHub]] or install it via =pip install appendfilename=
** Smart Prepend
Although =appendfilename= was created mainly to /add text at the end
of a file name/, it may also insert text at the beginning of a file
name using the =--prepend= parameter.
A variance of that is =--smart-prepend=. Following examples
demonstrate the effects on smart prepending "new text" with various
file names:
: new text foo bar.txt
: 2019-10-20 new text foo bar.txt
: 2019-10-20T12.34 new text foo bar.txt
: 2019-10-20T12.34.56 new text foo bar.txt
As you can see, =--smart-prepend= does take into account that a given
date/time-stamp according to [[https://github.com/novoid/date2name][date2name]] and [[https://karl-voit.at/managing-digital-photographs/][this article]] will always
stay the first part of a file name, prepending the "new text" between
the date/time-stamp and the rest.
** Bonus: integrating into Geeqie (or similar file browsers)

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
PROG_VERSION = u"Time-stamp: <2018-11-18 22:55:19 vk>"
PROG_VERSION = u"Time-stamp: <2019-10-19 17:04:17 vk>"
# TODO:
# * fix parts marked with «FIXXME»
@ -26,6 +26,7 @@ FILENAME_TAG_SEPARATOR = ' -- ' # between file name and (optional) list of tags
BETWEEN_TAG_SEPARATOR = ' ' # between tags (not that relevant in this tool)
TEXT_SEPARATOR = ' ' # between old file name and inserted text
RENAME_SYMLINK_ORIGINALS_WHEN_RENAMING_SYMLINKS = True # if current file is a symlink with the same name, also rename source file
WITHTIME_AND_SECONDS_PATTERN = re.compile('^(\d{4,4}-[01]\d-[0123]\d(([T :_-])([012]\d)([:.-])([012345]\d)(([:.-])([012345]\d))?)?)[- _.](.+)')
USAGE = "\n\
appendfilename [<options>] <list of files>\n\
@ -80,6 +81,12 @@ parser = OptionParser(usage=USAGE)
parser.add_option("-t", "--text", dest="text",
help="the text to add to the file name")
parser.add_option("-p", "--prepend", dest="prepend", action="store_true",
help="Do the opposite: instead of appending the text, prepend the text")
parser.add_option("--smart-prepend", dest="smartprepend", action="store_true",
help="Like \"--prepend\" but do respect date/time-stamps: insert new text between \"YYYY-MM-DD(Thh.mm(.ss))\" and rest")
parser.add_option("-s", "--dryrun", dest="dryrun", action="store_true",
help="enable dryrun mode: just simulate what would happen, do not modify file(s)")
@ -288,7 +295,19 @@ def handle_file(filename, text, dryrun):
return
try:
new_filename = os.path.join(os.path.dirname(filename), old_basename + TEXT_SEPARATOR + text + tags_with_extension)
if options.prepend:
new_filename = os.path.join(os.path.dirname(filename), text + TEXT_SEPARATOR + old_basename + tags_with_extension)
elif options.smartprepend:
match = re.match(WITHTIME_AND_SECONDS_PATTERN, filename)
if not match:
logging.debug('can\'t find a date/time-stamp, doing a simple prepend')
new_filename = os.path.join(os.path.dirname(filename), text + TEXT_SEPARATOR + old_basename + tags_with_extension)
else:
logging.debug('date/time-stamp found, insert text between date/time-stamp and rest')
new_filename = os.path.join(os.path.dirname(filename), match.group(1) + TEXT_SEPARATOR + text + TEXT_SEPARATOR + match.group(len(match.groups())))
#import pdb; pdb.set_trace()
else:
new_filename = os.path.join(os.path.dirname(filename), old_basename + TEXT_SEPARATOR + text + tags_with_extension)
except:
error_exit(7, "Error while trying to build new filename: " + str(sys.exc_info()[0]))
assert(isinstance(new_filename, str))
@ -321,6 +340,10 @@ def main():
error_exit(1, "Options \"--verbose\" and \"--quiet\" found. " +
"This does not make any sense, you silly fool :-)")
if options.prepend and options.smartprepend:
error_exit(3, "Options \"--prepend\" and \"--smart-prepend\" found. " +
"This does not make any sense, you silly fool :-)")
if len(sys.argv) < 2:
# not a single command line parameter is given -> print help instead of asking for a string
parser.print_help()