Skip to content

Commit

Permalink
Add --history command
Browse files Browse the repository at this point in the history
  • Loading branch information
ghickman committed Apr 6, 2014
1 parent 69e4cf4 commit 7de5d78
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tvrenamr/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .config import Config
from .errors import *
from .history import parse_history
from .logs import start_logging
from .main import File, TvRenamr
from .options import OptionParser
Expand Down Expand Up @@ -142,6 +143,9 @@ def run():
options.log_level = 10
start_logging(options.log_file, options.log_level, options.quiet)

if options.history:
return parse_history(options.log_file)

# use current directory if no args specified
if not args:
log.debug('No file or directory specified, using current directory')
Expand Down
42 changes: 42 additions & 0 deletions tvrenamr/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import functools
import logging
import os
import pydoc
import sys

from .logs import get_log_file


log = logging.getLogger('tvrenamr.history')


def parse_history(log_file_location):
log_file = get_log_file(log_file_location)

if not os.path.getsize(log_file):
log.critical('No log file found, exiting.')
sys.exit(1)

with open(log_file, 'r') as f:
logs = f.readlines()

shows = list(filter(lambda x: 'Renamed:' in x, logs))

def show_len(show):
return len(show.split('Renamed: ')[1].split(' - ')[0]) - 1

longest = max(map(show_len, shows))

def sanitise_log(log, longest):
dt, name = log.split('Renamed: ')
dt = dt.split(' ')[0].replace('T', ' ')
show, number, title = name.split(' - ')
name = (name.replace(show, show.lstrip('"').strip().ljust(longest), 1)
.replace(number, number.ljust(4), 1)
.replace(' - ', ' | '))
return '{0} | {1}'.format(dt, name.rstrip('"\n'))

sanitise = functools.partial(sanitise_log, longest=longest)

shows = map(sanitise, shows)
return pydoc.pager('\n'.join(shows))
1 change: 1 addition & 0 deletions tvrenamr/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self):
self.add_option('--debug', action='store_true', dest='debug', help=SUPPRESS_HELP)
self.add_option('-d', '--dry-run', dest='dry', action='store_true', help='Dry run your renaming.')
self.add_option('-e', '--episode', dest='episode', help='Set the episode number. Currently this will cause errors when working with more than one file.')
self.add_option('--history', action='store_true', dest='history', help='Display a list of shows renamed using the system pager.')
self.add_option('--ignore-filelist', dest='ignore_filelist', default=(), help=SUPPRESS_HELP)
self.add_option('--ignore-recursive', action='store_true', dest='ignore_recursive', help='Only use files from the root of a given directory, not entering any sub-directories.')
self.add_option('--log-file', dest='log_file', help='Set the log file location.')
Expand Down

0 comments on commit 7de5d78

Please sign in to comment.