Now uses seperate config file to read regex and options.

Instead of using a defined python file with the regex details, it
now brings in and parses a config file using ConfigParser. This
allows for some flexability in regards to modifying regexs.
This commit is contained in:
2010-01-26 11:57:07 +00:00
parent ebbdb3917a
commit 1041accd0d
2 changed files with 34 additions and 36 deletions

12
tvorganise.cfg Normal file
View File

@@ -0,0 +1,12 @@
[main]
# Target directory
target_path: /mnt/vault/video/TV Shows/%(file_showname)s/Season %(seasno)s/
[regex]
valid_in_names: [\w\(\).,\[\]'\ \-?!#:]
regex1: "^(%(valid_in_names)s+) - \[(\d{2})x(\d{2})\] - (%(valid_in_names)s+)$"
regex2: "^(%(valid_in_names)s+) - \[(\d{2})x(\d{2}-\d{2})\] - (%(valid_in_names)s+)$"
regex3: "^(%(valid_in_names)s+) - \[(\d{2})x(Special\d{1,2})\] - (%(valid_in_names)s+)$"
regex4: "^(%(valid_in_names)s+) - \[(\d{2})xExtra(\d{1,2})\] - (%(valid_in_names)s+)$"
regex5: "^(%(valid_in_names)s+) - \[(\d{2})] - (%(valid_in_names)s+)$"

View File

@@ -11,25 +11,35 @@ the location format defined in the configuration file.
import os, sys, re import os, sys, re
from optparse import OptionParser from optparse import OptionParser
import shutil import shutil
import ConfigParser
config = {} config = {}
regex_config={} regex = []
############################################## configpsr = ConfigParser.RawConfigParser()
# Path configs configpsr.read('tvorganise.cfg')
# Where to move the files if configpsr.has_section('main'):
config['target_path'] = "/mnt/vault/video/TV Shows/%(file_showname)s/Season %(seasno)s/" for k, v in configpsr.items('main'):
config[k] = v
if configpsr.has_section('regex'):
regex_config = {}
############################################## # Load in subs before reading in the regex
# Regex configs for k, v in configpsr.items('regex'):
if k[:5] != 'regex':
regex_config[k] = v
# Import shared filename pattern config for k, v in configpsr.items('regex'):
from filename_config import tv_regex if k[:5] == 'regex':
regex.append(re.compile(v % regex_config))
config['regex'] = regex
print config
# end configs
##############################################
def findFiles(args): def findFiles(args):
""" """
@@ -43,14 +53,9 @@ def findFiles(args):
newpath = os.path.join(cfile, sf) newpath = os.path.join(cfile, sf)
if os.path.isfile(newpath): if os.path.isfile(newpath):
allfiles.append(newpath) allfiles.append(newpath)
#end if isfile
#end for sf
elif os.path.isfile(cfile): elif os.path.isfile(cfile):
allfiles.append(cfile) allfiles.append(cfile)
#end if isdir
#end for cfile
return allfiles return allfiles
#end findFiles
def processNames(names, verbose=False): def processNames(names, verbose=False):
""" """
@@ -64,7 +69,7 @@ def processNames(names, verbose=False):
# Remove leading . from extension # Remove leading . from extension
ext = ext.replace(".", "", 1) ext = ext.replace(".", "", 1)
for r in tv_regex['with_ep_name']: for r in config['regex']:
match = r.match(filename) match = r.match(filename)
if match: if match:
showname, seasno, epno, epname = match.groups() showname, seasno, epno, epname = match.groups()
@@ -93,26 +98,8 @@ def processNames(names, verbose=False):
break # Matched - to the next file! break # Matched - to the next file!
else: else:
print "Invalid name: %s" % (f) print "Invalid name: %s" % (f)
#end for r
#end for f
return allEps return allEps
#end processNames
def confirm(question="Rename files?"):
ans = None
while ans not in ["q", "quit"]:
print "y/n/a/q?",
ans = raw_input()
if ans.lower() in ["y", "yes"]:
return True
elif ans.lower() in ["n", "no"]:
return False
elif ans.lower() in ["a", "always"]:
return "always"
else:
sys.exit(1)
#end confirm
def make_path(path): def make_path(path):
try: try:
@@ -120,7 +107,6 @@ def make_path(path):
except OSError: except OSError:
#print "Couldn't make path" #print "Couldn't make path"
pass pass
#end make_path
def does_file_exist(path): def does_file_exist(path):
try: try: