mirror of
https://github.com/nikdoof/tvorganise.git
synced 2025-12-13 06:42:16 +00:00
Moved config parsing out to a custom dict, in prep to handle defaults
This commit is contained in:
@@ -6,6 +6,7 @@ import unittest
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
import tvorganise.config
|
||||
|
||||
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
|
||||
dict on completion
|
||||
"""
|
||||
dict = self.tvo._get_config('tvorganise.cfg')
|
||||
dict = tvorganise.config.Config('tvorganise.cfg')
|
||||
self.assertTrue(dict)
|
||||
|
||||
def testConfigSettings(self):
|
||||
|
||||
@@ -16,6 +16,7 @@ import shutil
|
||||
import ConfigParser
|
||||
import logging
|
||||
|
||||
import config
|
||||
|
||||
def same_partition(path1, path2):
|
||||
"""
|
||||
@@ -66,42 +67,6 @@ class TvOrganiser():
|
||||
self.__logger = logging.getLogger(self.__class__.__name__)
|
||||
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):
|
||||
"""
|
||||
Takes list of names, runs them though the regexs and breaks them down
|
||||
@@ -180,7 +145,7 @@ class TvOrganiser():
|
||||
sys.exit(1)
|
||||
|
||||
self._logger.info('Using config file: %s' % cfile)
|
||||
config = self._get_config(cfile)
|
||||
self._config = config.Config(cfile)
|
||||
|
||||
files = find_files(args)
|
||||
files = self.parse_filenames(files)
|
||||
@@ -195,7 +160,7 @@ class TvOrganiser():
|
||||
for name in files:
|
||||
filename = "%s.%s" % (name['filename'], name['ext'])
|
||||
oldfile = os.path.join(name['filepath'], filename)
|
||||
newpath = config['target_path'] % name
|
||||
newpath = self._config['target_path'] % name
|
||||
newfile = os.path.join(newpath, filename)
|
||||
|
||||
self._logger.info("Old path: %s" % oldfile)
|
||||
|
||||
62
tvorganise/config.py
Normal file
62
tvorganise/config.py
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user