1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 Launcher - all neccessary initialization
19 """
20
21 import sys
22 import gettext
23 import getopt
24 import locale
25 import os
26 import glob
27 import __builtin__
28
29 import libxyz
30 import libxyz.ui as uilib
31 import libxyz.const as const
32 import libxyz.core as core
33
34 from libxyz.ui import lowui
35 from libxyz.version import Version
36 from libxyz.core.plugins import PluginManager
37 from libxyz.core import logger
38 from libxyz.core import dsl
39 from libxyz.core.utils import ustring
40 from libxyz.vfs import VFSDispatcher
41
42 from libxyz.exceptions import *
43
45 """
46 Startup class
47 """
48
49 EVENT_STARTUP = u"event:startup"
50
77
78
79
111
112
113
115 """
116 Try to preset local_encoding using current locale settings.
117 After xyz conf is parsed, this value will be overriden by
118 local_encoding from conf, if defined
119 """
120
121 __builtin__.__dict__["xyzenc"] = locale.getpreferredencoding()
122
123
124
131
132
133
135 """
136 Create .xyzcmd in homedir
137 """
138
139 if os.path.isdir(self._path_sel.user_dir):
140 return
141
142 try:
143 os.makedirs(self._path_sel.user_dir)
144
145 for d in (const.CONF_DIR,
146 const.PLUGINS_DIR,
147 const.SKINS_DIR,
148 ):
149 os.mkdir(os.path.join(self._path_sel.user_dir, d))
150 except Exception:
151 return
152
153
154
156 """
157 Parse command line arguments
158 """
159
160 try:
161 _opts, _args = getopt.getopt(sys.argv[1:], self.cmdopts)
162 except getopt.GetoptError, e:
163 self.error(str(e))
164 self.usage()
165 self.quit()
166
167 for _o, _a in _opts:
168 if _o == "-d":
169 self._conf["driver"] = _a
170 elif _o == "-c":
171 _colors = int(_a)
172 self._conf["colors"] = _colors
173 elif _o == "-s":
174 self._conf["skin"] = _a
175 elif _o == "-v":
176 self.version()
177 self.quit()
178 else:
179 self.usage()
180 self.quit()
181
182
183
185 """
186 Initiate Logger object and put it into builtin namespace
187 """
188
189 _log = logger.LogLevel()
190
191 try:
192 _levels = self.xyz.conf[u"plugins"][u":sys:logger"][u"levels"]
193 except KeyError:
194 _levels = (logger.LogLevel().ALL,)
195 else:
196 if not isinstance(_levels, tuple) and not \
197 isinstance(_levels, list):
198 _levels = (_levels,)
199
200 try:
201 _levels = [getattr(_log, x) for x in _levels]
202 except Exception:
203 raise XYZValueError(_(u"Invalid value %s.\n"\
204 u"A list of valid log levels expected"
205 % ustring(_levels)))
206
207 try:
208 _lines = self.xyz.conf[u"plugins"][u":sys:logger"][u"lines"]
209
210 except KeyError:
211 _lines = 100
212
213 try:
214 _lines = abs(int(_lines))
215 except ValueError:
216 raise XYZValueError(_(u"Invalid value %s. "\
217 u"A positive integer expected" %
218 ustring(_lines)))
219
220 _logger = core.logger.Logger(self.xyz, _levels, _lines)
221
222 __builtin__.__dict__["xyzlog"] = _logger
223
224
225
260
261
262
282
283
284
292
293
294
296 """
297 Parse configuration files, first system then user one if any
298 @param conf_file: File to parse
299 """
300
301 _system, _user = self._path_sel.get_conf(conf_file)
302
303
304 self._parse_file(_system, _(u"Error parsing system config %s: %s"))
305
306
307 if os.path.exists(_user):
308 self._parse_file(_user, _(u"Error parsing user config %s: %s"))
309
310
311
313 """
314 Load all skins in system and user home dirs
315 """
316
317 user_skins, system_skins = self._path_sel.get_skins_dir()
318
319 system_skins = glob.glob(os.path.join(system_skins, "*"))
320
321 if os.path.exists(user_skins):
322 user_skins = glob.glob(os.path.join(user_skins, "*"))
323 else:
324 user_skins = []
325
326 for skin in system_skins + user_skins:
327 res = self._parse_file(skin, error=False)
328
329 if res != True:
330 self.error(_(u"Skipping skin %s due to parsing error: %s" %
331 (skin, res)), False)
332
333
334
336 """
337 Parse XYZCommander config
338 """
339
340 if tmpl is None:
341 tmpl = _(u"Error parsing config %s: %s")
342
343 try:
344 dsl.exec_file(file)
345 except DSLError, e:
346 if error:
347 self.error(tmpl % (file, ustring(str(e))))
348 else:
349 return ustring(str(e))
350
351 return True
352
353
354
356 """
357 Show usage
358 """
359
360 print _(u"""\
361 %s version %s
362 Usage: %s [-d driver][-c colors][-s skin][-vh]
363 -d -- Display driver (raw (default) or curses)
364 -c -- Number of colors terminal supports (1, 16 (default), 88, 256)
365 -s -- Skin name
366 -v -- Show version
367 -h -- Show this help message\
368 """ % (const.PROG, Version.version, os.path.basename(sys.argv[0])))
369
370
371
378
379
380
382 """
383 Quit program
384 """
385
386 sys.exit()
387
388
389
390 - def error(self, msg, quit=True):
391 """
392 Print error message and optionally quit
393 """
394
395 if self._started:
396 xyzlog.log(msg)
397 else:
398
399 print msg
400
401 if quit:
402 self.quit()
403
404
405
407 """
408 Perform shutdown procedures
409 """
410
411 if self.xyz.term is not None:
412 core.utils.restore_term(self.xyz.term)
413