1 # -*- coding: utf-8 -*-
5 This module contains the configuration class for lfm.
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',
31 filetypes
= { 'web': ('html', 'htm'),
34 'audio': ('wav', 'au', 'midi'),
35 'video': ('mpeg', 'mpg', 'avi', 'asf'),
36 'graphics': ('png', 'jpeg', 'jpg', 'gif', 'tiff', 'tif', 'xpm', 'svg'),
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,
72 'detach_terminal_at_exec': 1,
75 'sort': SORTTYPE_byName
,
79 settings
= {'encoding': 'UTF-8'}
80 confirmations
= { 'delete': 1,
83 'ask_rebuild_vfs': 1 }
84 files_ext
= { 'temp_files': ('.tmp', '.$$$', '~', '.bak'),
85 'document_files': ('.txt', '.text', '.rtf',
86 '.odt', '.odc', '.odp',
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',
99 'archive_files': ('.gz', '.bz2', '.tar', '.tgz', '.Z', '.zip',
100 '.rar', '.arj', '.cab', '.lzh', '.lha',
101 '.zoo', '.arc', '.ark',
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',
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 ######################################################################
127 self
.file = os
.path
.abspath(os
.path
.expanduser(os
.path
.join('~',
129 self
.file_start
= '#' * 10 + ' ' + LFM_NAME
+ ' ' + \
130 'Configuration File' + ' ' + '#' * 10
131 self
.progs
= {} # make a copy
132 for k
, v
in defaultprogs
.items():
134 self
.filetypes
= filetypes
135 self
.bookmarks
= bookmarks
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
))
154 if not os
.path
.exists(self
.file) or not os
.path
.isfile(self
.file):
157 title
= f
.readline()[:-1]
159 if title
and title
!= self
.file_start
:
162 cfg
= SafeConfigParser()
165 for typ
, prog
in cfg
.items('Programs'):
166 self
.progs
[typ
] = prog
168 for typ
, exts
in cfg
.items('File Types'):
169 lst
= [t
.strip() for t
in exts
.split(',')]
170 self
.filetypes
[typ
] = tuple(lst
)
172 for num
, path
in cfg
.items('Bookmarks'):
176 print 'Bad bookmark number:', num
179 if os
.path
.isdir(os
.path
.expandvars(os
.path
.expanduser(path
))):
180 self
.bookmarks
[num
] = path
182 self
.bookmarks
[num
] = ''
184 print 'Incorrect directory in bookmark[%d]: %s' % \
187 print 'Bad bookmark number:', num
189 for sec
, color
in cfg
.items('Colors'):
190 if not self
.colors
.has_key(sec
):
191 print 'Bad object name:', sec
193 (fg
, bg
) = color
.split(' ', 2)
194 self
.colors
[sec
.lower()] = (fg
.lower(), bg
.lower())
196 for what
, val
in cfg
.items('Options'):
200 print 'Bad option value: %s => %s' % (what
, val
)
202 if what
not in self
.options
.keys():
203 print 'Bad option: %s => %s' % (what
, val
)
205 self
.options
[what
] = val
206 if self
.options
['num_panes'] != 1 or self
.options
['num_panes'] != 2:
207 self
.options
['num_panes'] = 2
209 for what
, val
in cfg
.items('Settings'):
213 print 'Bad option value: %s => %s' % (what
, val
)
215 if what
not in self
.settings
.keys():
216 print 'Bad option: %s => %s' % (what
, val
)
218 self
.settings
[what
] = val
220 for what
, val
in cfg
.items('Confirmations'):
224 print 'Bad confirmation value: %s => %s' % (what
, val
)
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
)
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
)
240 buf
= self
.file_start
+ '\n'
242 buf
+= '\n[Programs]\n'
243 for k
, v
in self
.progs
.items():
244 buf
+= '%s: %s\n' % (k
, v
)
246 buf
+= '\n[File Types]\n'
247 for k
, vs
in self
.filetypes
.items():
248 buf
+= '%s: %s\n' % (k
, ', '.join(vs
))
250 buf
+= '\n[Bookmarks]\n'
251 for i
, b
in enumerate(self
.bookmarks
):
252 buf
+= '%d: %s\n' % (i
, b
)
254 buf
+= '\n[Colors]\n'
255 for k
, v
in self
.colors
.items():
256 buf
+= '%s: %s %s\n' % (k
, v
[0], v
[1])
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
)
264 buf
+= '\n[Settings]\n'
265 for k
, v
in self
.settings
.items():
266 buf
+= '%s: %s\n' % (k
, v
)
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
273 for k
, vs
in self
.files_ext
.items():
274 buf
+= '%s: %s\n' % (k
, ', '.join(vs
))
276 f
= open(self
.file, 'w')
281 ######################################################################