exit from pyview by ESC
[lfm.git] / lfm / config.py
blob6705dceb092df449d9f1ec2ba55f53b0440af025
1 # -*- coding: utf-8 -*-
3 """config.py
5 This module contains the configuration class for lfm.
6 """
9 import os, os.path
10 from ConfigParser import SafeConfigParser
12 from __init__ import LFM_NAME, sysprogs
13 from files import SORTTYPE_byName
14 from utils import get_shell_output
17 ######################################################################
18 ##### Some variables and default values
19 CONFIG_FILE = '.lfmrc'
20 defaultprogs = { 'shell': 'bash',
21 'pager': 'pyview',
22 'editor': 'mcedit',
23 'web': 'galeon',
24 'ogg': 'ogg123',
25 'mp3': 'mpg321',
26 'audio': 'esdplay',
27 'video': 'mplayer',
28 'graphics': 'gthumb',
29 'pdf': 'evince',
30 'ps': 'evince' }
31 filetypes = { 'web': ('html', 'htm'),
32 'ogg': ('ogg', ),
33 'mp3': ('mp3', ),
34 'audio': ('wav', 'au', 'midi'),
35 'video': ('mpeg', 'mpg', 'avi', 'asf'),
36 'graphics': ('png', 'jpeg', 'jpg', 'gif', 'tiff', 'tif', 'xpm', 'svg'),
37 'pdf': ('pdf', ),
38 'ps': ('ps', ) }
39 bookmarks = [
40 '~/devel/',
41 '/mnt',
42 '/media',
43 '/etc',
44 '/home',
45 '/usr/share/doc',
46 '/etc',
47 '/root',
48 '/',
49 '$HOME' ]
50 colors = { 'title': ('cyan', 'blue'),
51 'files': ('white', 'black'),
52 'current_file': ('blue', 'cyan'),
53 'messages': ('magenta', 'cyan'),
54 'help': ('green', 'black'),
55 'file_info': ('red', 'black'),
56 'error_messages1': ('white', 'red'),
57 'error_messages2': ('black', 'red'),
58 'buttons': ('yellow', 'red'),
59 'selected_file': ('yellow', 'black'),
60 'current_selected_file': ('yellow', 'cyan'),
61 'tabs': ('white', 'blue'),
62 'temp_files': ( 'white', 'black'),
63 'document_files': ('blue', 'black'),
64 'media_files': ('blue', 'black'),
65 'archive_files': ('yellow', 'black'),
66 'source_files': ('cyan', 'black'),
67 'graphics_files': ('magenta', 'black'),
68 'data_files': ('magenta', 'black') }
69 options = { 'save_conf_at_exit': 1,
70 'show_output_after_exec': 1,
71 'rebuild_vfs': 0,
72 'detach_terminal_at_exec': 1,
73 'show_dotfiles': 1,
74 'num_panes': 2,
75 'sort': SORTTYPE_byName,
76 'sort_mix_dirs': 0,
77 'sort_mix_cases': 1,
78 'color_files': 1 }
79 settings = {'encoding': 'UTF-8'}
80 confirmations = { 'delete': 1,
81 'overwrite': 1,
82 'quit': 0,
83 'ask_rebuild_vfs': 1 }
84 files_ext = { 'temp_files': ('.tmp', '.$$$', '~', '.bak'),
85 'document_files': ('.txt', '.text', '.rtf',
86 '.odt', '.odc', '.odp',
87 '.abw', '.gnumeric',
88 '.sxw', '.sxc', '.sxp', '.sdw', '.sdc', '.sdp',
89 '.ps', '.pdf', '.dvi', '.bib', '.tex',
90 '.xml', '.xsd', '.xslt', '.sgml', '.dtd',
91 '.html', '.shtml', '.htm', '.css',
92 '.mail', '.msg', '.letter', '.ics', '.vcs', '.vcard',
93 '.lsm', '.po', '.man', '.1', '.info',
94 '.doc', '.xls', '.ppt', '.pps'),
95 'media_files': ('.mp2', '.mp3', '.mpg', '.ogg', '.mpeg', '.wav',
96 '.avi', '.asf', '.mov', '.mol', '.mpl', '.xm', '.med',
97 '.mid', '.midi', '.umx', '.wma', '.acc', '.wmv',
98 '.swf'),
99 'archive_files': ('.gz', '.bz2', '.tar', '.tgz', '.Z', '.zip',
100 '.rar', '.arj', '.cab', '.lzh', '.lha',
101 '.zoo', '.arc', '.ark',
102 '.rpm', '.deb'),
103 'source_files': ('.c', '.h', '.cc', '.hh', '.cpp', '.hpp',
104 '.py', '.pl', '.pm', '.inc',
105 '.asm', '.pas', '.f', '.f90', '.pov', '.m', '.pas',
106 '.cgi', '.php', '.phps', '.tcl', '.tk',
107 '.js', '.java', '.jav', '.jasm',
108 '.diff', '.patch',
109 '.sh', '.bash', '.awk', '.m4', '.el',
110 '.st', '.mak', '.sl', '.ada', '.caml',
111 '.ml', '.mli', '.mly', '.mll', '.mlp', '.prg'),
112 'graphics_files': ('.jpg', '.jpeg', '.gif', '.png', '.tif', '.tiff',
113 '.pcx', '.bmp', '.xpm', '.xbm', '.eps', '.pic',
114 '.rle', '.ico', '.wmf', '.omf', '.ai', '.cdr',
115 '.xcf', '.dwb', '.dwg', '.dxf', '.svg', '.dia'),
116 'data_files': ('.dta', '.nc', '.dbf', '.mdn', '.db', '.mdb', '.dat',
117 '.fox', '.dbx', '.mdx', '.sql', '.mssql', '.msql',
118 '.ssql', '.pgsql', '.cdx', '.dbi') }
121 ######################################################################
122 ##### Configuration
123 class Config:
124 """Config class"""
126 def __init__(self):
127 self.file = os.path.abspath(os.path.expanduser(os.path.join('~',
128 CONFIG_FILE)))
129 self.file_start = '#' * 10 + ' ' + LFM_NAME + ' ' + \
130 'Configuration File' + ' ' + '#' * 10
131 self.progs = {} # make a copy
132 for k, v in defaultprogs.items():
133 self.progs[k] = v
134 self.filetypes = filetypes
135 self.bookmarks = bookmarks
136 self.colors = colors
137 self.options = options
138 self.settings = settings
139 self.confirmations = confirmations
140 self.files_ext = files_ext
143 def check_progs(self):
144 for k, v in defaultprogs.items():
145 r = get_shell_output('%s \"%s\"' % (sysprogs['which'], v))
146 if r:
147 self.progs[k] = v
148 else:
149 self.progs[k] = ''
152 def load(self):
153 # check config file
154 if not os.path.exists(self.file) or not os.path.isfile(self.file):
155 return -1
156 f = open(self.file)
157 title = f.readline()[:-1]
158 f.close()
159 if title and title != self.file_start:
160 return -2
161 # all ok, proceed
162 cfg = SafeConfigParser()
163 cfg.read(self.file)
164 # programs
165 for typ, prog in cfg.items('Programs'):
166 self.progs[typ] = prog
167 # file types
168 for typ, exts in cfg.items('File Types'):
169 lst = [t.strip() for t in exts.split(',')]
170 self.filetypes[typ] = tuple(lst)
171 # bookmarks
172 for num, path in cfg.items('Bookmarks'):
173 try:
174 num = int(num)
175 except ValueError:
176 print 'Bad bookmark number:', num
177 continue
178 if 0 <= num <= 9:
179 if os.path.isdir(os.path.expandvars(os.path.expanduser(path))):
180 self.bookmarks[num] = path
181 elif not path:
182 self.bookmarks[num] = ''
183 else:
184 print 'Incorrect directory in bookmark[%d]: %s' % \
185 (num, path)
186 else:
187 print 'Bad bookmark number:', num
188 # colours
189 for sec, color in cfg.items('Colors'):
190 if not self.colors.has_key(sec):
191 print 'Bad object name:', sec
192 else:
193 (fg, bg) = color.split(' ', 2)
194 self.colors[sec.lower()] = (fg.lower(), bg.lower())
195 # options
196 for what, val in cfg.items('Options'):
197 try:
198 val = int(val)
199 except ValueError:
200 print 'Bad option value: %s => %s' % (what, val)
201 else:
202 if what not in self.options.keys():
203 print 'Bad option: %s => %s' % (what, val)
204 else:
205 self.options[what] = val
206 if self.options['num_panes'] != 1 or self.options['num_panes'] != 2:
207 self.options['num_panes'] = 2
208 # settings
209 for what, val in cfg.items('Settings'):
210 try:
211 val = str(val)
212 except ValueError:
213 print 'Bad option value: %s => %s' % (what, val)
214 else:
215 if what not in self.settings.keys():
216 print 'Bad option: %s => %s' % (what, val)
217 else:
218 self.settings[what] = val
219 # confirmations
220 for what, val in cfg.items('Confirmations'):
221 try:
222 val = int(val)
223 except ValueError:
224 print 'Bad confirmation value: %s => %s' % (what, val)
225 else:
226 if what not in self.confirmations.keys():
227 print 'Bad confirmation option: %s => %s' % (what, val)
228 elif val != 0 and val != 1:
229 print 'Bad confirmation value: %s => %s' % (what, val)
230 else:
231 self.confirmations[what] = val
232 # File types for color
233 for typ, exts in cfg.items('Files'):
234 lst = [t.strip() for t in exts.split(',')]
235 self.files_ext[typ] = tuple(lst)
238 def save(self):
239 # title
240 buf = self.file_start + '\n'
241 # progs
242 buf += '\n[Programs]\n'
243 for k, v in self.progs.items():
244 buf += '%s: %s\n' % (k, v)
245 # filetypes
246 buf += '\n[File Types]\n'
247 for k, vs in self.filetypes.items():
248 buf += '%s: %s\n' % (k, ', '.join(vs))
249 # bookmarks
250 buf += '\n[Bookmarks]\n'
251 for i, b in enumerate(self.bookmarks):
252 buf += '%d: %s\n' % (i, b)
253 # colorus
254 buf += '\n[Colors]\n'
255 for k, v in self.colors.items():
256 buf += '%s: %s %s\n' % (k, v[0], v[1])
257 # options
258 buf += '\n[Options]\n'
259 buf += '# sort:\tNone = 0, byName = 1, byName_rev = 2, bySize = 3,\n'
260 buf += '# \tbySize_rev = 4, byDate = 5, byDate_rev = 6\n'
261 for k, v in self.options.items():
262 buf += '%s: %s\n' % (k, v)
263 # settings
264 buf += '\n[Settings]\n'
265 for k, v in self.settings.items():
266 buf += '%s: %s\n' % (k, v)
267 # confirmations
268 buf += '\n[Confirmations]\n'
269 for k, v in self.confirmations.items():
270 buf += '%s: %s\n' % (k, v)
271 # File types for color
272 buf += '\n[Files]\n'
273 for k, vs in self.files_ext.items():
274 buf += '%s: %s\n' % (k, ', '.join(vs))
275 # write to file
276 f = open(self.file, 'w')
277 f.write(buf)
278 f.close()
281 ######################################################################