[RARSLAVE] Fix default deletion option
[rarslave2.git] / RarslaveConfig.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 import os, ConfigParser
5
6 class RarslaveConfig (object):
7         """A simple class to hold the default configs for the whole program"""
8
9         DEFAULT_CONFIG='~/.config/rarslave2/rarslave2.conf'
10
11         def __read_config(self, filename=DEFAULT_CONFIG):
12                 """Attempt to open and read the rarslave config file"""
13
14                 # Make sure the filename is corrected
15                 filename = os.path.abspath(os.path.expanduser(filename))
16
17                 user_config = {}
18
19                 # Write the default config if it doesn't exist
20                 if not os.path.isfile(filename):
21                         self.write_config(default=True)
22
23                 config = ConfigParser.ConfigParser()
24                 config.read(filename)
25
26                 for section in config.sections():
27                         for option in config.options(section):
28                                 user_config[(section, option)] = config.get(section, option)
29
30                 return user_config
31
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."""
35
36                 config = ConfigParser.ConfigParser()
37
38                 # Correct filename
39                 filename = os.path.abspath(os.path.expanduser(filename))
40
41                 # Reset all config to make sure we write the default one, if necessary
42                 if default:
43                         self.__user_config = {}
44                         print 'Writing default config to %s' % (filename, )
45
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))
51
52                 # [options] section
53                 config.add_section('options')
54                 for (s, k) in self.__defaults.keys():
55                         if s == 'options':
56                                 config.set(s, k, self.get_value(s, k))
57
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))
63
64                 # [commands] section
65                 config.add_section('commands')
66                 for (s, k) in self.__defaults.keys():
67                         if s == 'commands':
68                                 config.set(s, k, self.get_value(s, k))
69
70                 # Try to make the ~/.config/rarslave/ directory
71                 if not os.path.isdir(os.path.split(filename)[0]):
72                         try:
73                                 os.makedirs(os.path.split(filename)[0])
74                         except:
75                                 print 'Could not make directory: %s' % (os.path.split(filename)[0], )
76                                 sys.exit()
77
78                 # Try to write the config file to disk
79                 try:
80                         fsock = open(filename, 'w')
81                         try:
82                                 config.write(fsock)
83                         finally:
84                                 fsock.close()
85                 except:
86                         print 'Could not open: %s for writing' % (filename, )
87                         sys.exit()
88
89         def __get_default_val(self, section, key):
90                 return self.__defaults[(section, key)]
91
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."""
95
96                 try:
97                         value = self.__user_config[(section, key)]
98                 except:
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)
102
103                 # Convert config options to native types for easier use
104                 SAFE_EVAL = ['None', 'True', 'False', '-1', '0', '1', '2']
105
106                 if value in SAFE_EVAL:
107                         value = eval (value)
108
109                 # Absolute-ize directories for easier use
110                 if section == 'directories' and value != None:
111                         value = os.path.abspath (os.path.expanduser (value))
112
113                 return value
114
115         def __init__(self):
116                 self.__defaults = {
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 -- ',
131                 }
132
133                 self.__user_config = self.__read_config()
134
135
136
137
138 def main ():
139         pass
140
141 if __name__ == '__main__':
142         main ()
143