2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
4 import os, ConfigParser
6 class RarslaveConfig (object):
7 """A simple class to hold the default configs for the whole program"""
9 DEFAULT_CONFIG='~/.config/rarslave2/rarslave2.conf'
11 def __read_config(self, filename=DEFAULT_CONFIG):
12 """Attempt to open and read the rarslave config file"""
14 # Make sure the filename is corrected
15 filename = os.path.abspath(os.path.expanduser(filename))
19 # Write the default config if it doesn't exist
20 if not os.path.isfile(filename):
21 self.write_config(default=True)
23 config = ConfigParser.ConfigParser()
26 for section in config.sections():
27 for option in config.options(section):
28 user_config[(section, option)] = config.get(section, option)
32 def write_config(self, filename=DEFAULT_CONFIG, default=False):
33 """Write out the current config to the config file. If you set default=True, then
34 the default config file will be written."""
36 config = ConfigParser.ConfigParser()
39 filename = os.path.abspath(os.path.expanduser(filename))
41 # Reset all config to make sure we write the default one, if necessary
43 self.__user_config = {}
44 print 'Writing default config to %s' % (filename, )
46 # [directories] section
47 config.add_section('directories')
48 for (s, k) in self.__defaults.keys():
49 if s == 'directories':
50 config.set(s, k, self.get_value(s, k))
53 config.add_section('options')
54 for (s, k) in self.__defaults.keys():
56 config.set(s, k, self.get_value(s, k))
58 # [regular_expressions] section
59 config.add_section('regular expressions')
60 for (s, k) in self.__defaults.keys():
61 if s == 'regular expressions':
62 config.set(s, k, self.get_value(s, k))
65 config.add_section('commands')
66 for (s, k) in self.__defaults.keys():
68 config.set(s, k, self.get_value(s, k))
70 # Try to make the ~/.config/rarslave/ directory
71 if not os.path.isdir(os.path.split(filename)[0]):
73 os.makedirs(os.path.split(filename)[0])
75 print 'Could not make directory: %s' % (os.path.split(filename)[0], )
78 # Try to write the config file to disk
80 fsock = open(filename, 'w')
86 print 'Could not open: %s for writing' % (filename, )
89 def __get_default_val(self, section, key):
90 return self.__defaults[(section, key)]
92 def get_value(self, section, key):
93 """Get a config value. Attempts to get the value from the user's
94 config first, and then uses the default."""
97 value = self.__user_config[(section, key)]
99 # This should work, unless you write something stupid
100 # into the code, so DON'T DO IT
101 value = self.__get_default_val(section, key)
103 # Convert config options to native types for easier use
104 SAFE_EVAL = ['None', 'True', 'False', '-1', '0', '1', '2']
106 if value in SAFE_EVAL:
109 # Absolute-ize directories for easier use
110 if section == 'directories' and value != None:
111 value = os.path.abspath (os.path.expanduser (value))
117 ('directories', 'working_directory') : '~/downloads/usenet',
118 ('directories', 'extract_directory') : None,
119 ('options', 'recursive') : True,
120 ('options', 'interactive') : False,
121 ('options', 'output_loglevel') : 0,
122 ('regular expressions', 'par2_regex') : '^.*\.par2$',
123 ('regular expressions', 'delete_regex') :
124 '^.*\.(par2|\d|\d\d\d|rar|r\d\d|zip)$',
125 ('regular expressions', 'basename_regex') :
126 '^(.+)\.(par2|vol\d+\+\d+|\d\d\d|part\d+|rar|zip|avi|mp4|mkv|ogm)$',
127 ('commands', 'unrar') : 'unrar x -o+ -- ',
128 ('commands', 'unzip') : 'unzip \"%s\" -d \"%s\" ',
129 ('commands', 'noextract') : 'mv \"%s\" \"%s\" ',
130 ('commands', 'par2repair') : 'par2repair -- ',
133 self.__user_config = self.__read_config()
141 if __name__ == '__main__':