Moved config parsing out to a custom dict, in prep to handle defaults

This commit is contained in:
2010-01-29 12:03:48 +00:00
parent 02f8c15d2b
commit f79b771a38
3 changed files with 67 additions and 39 deletions

View File

@@ -6,6 +6,7 @@ import unittest
import os import os
import shutil import shutil
import tempfile import tempfile
import tvorganise.config
class testTvOrganise(unittest.TestCase): class testTvOrganise(unittest.TestCase):
""" """
@@ -17,7 +18,7 @@ class testTvOrganise(unittest.TestCase):
Simple test to check to see if the config parser actually returns a Simple test to check to see if the config parser actually returns a
dict on completion dict on completion
""" """
dict = self.tvo._get_config('tvorganise.cfg') dict = tvorganise.config.Config('tvorganise.cfg')
self.assertTrue(dict) self.assertTrue(dict)
def testConfigSettings(self): def testConfigSettings(self):

View File

@@ -16,6 +16,7 @@ import shutil
import ConfigParser import ConfigParser
import logging import logging
import config
def same_partition(path1, path2): def same_partition(path1, path2):
""" """
@@ -66,42 +67,6 @@ class TvOrganiser():
self.__logger = logging.getLogger(self.__class__.__name__) self.__logger = logging.getLogger(self.__class__.__name__)
return self.__logger return self.__logger
def _get_config(self, cfile):
"""
Parses the TVOrganiser style config file and produces a dict
with all the elements contained within.
Also, all regex specified in the file are compiled
"""
config = {}
configpsr = ConfigParser.RawConfigParser()
configpsr.read(cfile)
if configpsr.has_section('main'):
for key, value in configpsr.items('main'):
config[key] = value
if configpsr.has_section('regex'):
regex_config = {}
regex = []
# Load in subs before reading in the regex
for key, value in configpsr.items('regex'):
if key[:5] != 'regex':
regex_config[key] = value
for key, value in configpsr.items('regex'):
if key[:5] == 'regex':
regex.append(re.compile(value % regex_config))
config['regex'] = regex
self._config = config
return config
def parse_filenames(self, names): def parse_filenames(self, names):
""" """
Takes list of names, runs them though the regexs and breaks them down Takes list of names, runs them though the regexs and breaks them down
@@ -180,7 +145,7 @@ class TvOrganiser():
sys.exit(1) sys.exit(1)
self._logger.info('Using config file: %s' % cfile) self._logger.info('Using config file: %s' % cfile)
config = self._get_config(cfile) self._config = config.Config(cfile)
files = find_files(args) files = find_files(args)
files = self.parse_filenames(files) files = self.parse_filenames(files)
@@ -195,7 +160,7 @@ class TvOrganiser():
for name in files: for name in files:
filename = "%s.%s" % (name['filename'], name['ext']) filename = "%s.%s" % (name['filename'], name['ext'])
oldfile = os.path.join(name['filepath'], filename) oldfile = os.path.join(name['filepath'], filename)
newpath = config['target_path'] % name newpath = self._config['target_path'] % name
newfile = os.path.join(newpath, filename) newfile = os.path.join(newpath, filename)
self._logger.info("Old path: %s" % oldfile) self._logger.info("Old path: %s" % oldfile)

62
tvorganise/config.py Normal file
View File

@@ -0,0 +1,62 @@
import ConfigParser
import re
def defaults():
"""
Creates a ConfigParser instance and fills it with the default settings
"""
config = ConfigParser.ConfigParser()
config.add_section('main')
config.set('main', 'target_path', '/media/%(showname)s/Season %(seasonnum)s/')
config.add_section('regex')
config.set('regex', 'valid_in_names', "[\\w\\(\\).,\\[\\]'\\ \\-?!#:]")
return config
class Config(dict):
"""
Config class loads and parses TVOrganiser style config files, presenting
as a dict with compiled re objects as needed.
"""
def __init__(self, cfile=None):
super(Config, self).__init__()
if cfile:
self.load(cfile)
def load(self, cfile):
"""
Parses the TVOrganiser style config file and produces a dict
with all the elements contained within.
Also, all regex specified in the file are compiled
"""
configpsr = ConfigParser.RawConfigParser()
configpsr.read(cfile)
if configpsr.has_section('main'):
for key, value in configpsr.items('main'):
self[key] = value
if configpsr.has_section('regex'):
regex_config = {}
regex = []
# Load in subs before reading in the regex
for key, value in configpsr.items('regex'):
if key[:5] != 'regex':
regex_config[key] = value
for key, value in configpsr.items('regex'):
if key[:5] == 'regex':
regex.append(re.compile(value % regex_config))
self['regex'] = regex