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 __builtin__
27
28 import libxyz
29 import libxyz.ui as uilib
30 import libxyz.const as const
31 import libxyz.core as core
32
33 from libxyz.ui import lowui
34 from libxyz.version import Version
35 from libxyz.core.plugins import PluginManager
36 from libxyz.core import logger
37 from libxyz.core import dsl
38 from libxyz.core.utils import ustring
39
40 from libxyz.exceptions import *
41
43 """
44 Startup class
45 """
46
64
65
66
96
97
98
100 """
101 Try to preset local_encoding using current locale settings.
102 After xyz conf is parsed, this value will be overriden by
103 local_encoding from conf, if defined
104 """
105
106 __builtin__.__dict__["xyzenc"] = locale.getpreferredencoding()
107
108
109
115
116
117
119 """
120 Create .xyzcmd in homedir
121 """
122
123 if os.path.isdir(self._path_sel.user_dir):
124 return
125
126 try:
127 os.makedirs(self._path_sel.user_dir)
128
129 for d in (const.CONF_DIR,
130 const.PLUGINS_DIR,
131 const.SKINS_DIR,
132 ):
133 os.mkdir(os.path.join(self._path_sel.user_dir, d))
134 except Exception:
135 return
136
137
138
140 """
141 Parse command line arguments
142 """
143
144 try:
145 _opts, _args = getopt.getopt(sys.argv[1:], self.cmdopts)
146 except getopt.GetoptError, e:
147 self.error(str(e))
148 self.usage()
149 self.quit()
150
151 for _o, _a in _opts:
152 if _o == "-d":
153 self._conf["driver"] = _a
154 elif _o == "-v":
155 self.version()
156 self.quit()
157 else:
158 self.usage()
159 self.quit()
160
161
162
164 """
165 Initiate Logger object and put it into builtin namespace
166 """
167
168 _log = logger.LogLevel()
169
170 try:
171 _levels = self.xyz.conf[u"plugins"][u":sys:logger"][u"levels"]
172 except KeyError:
173 _levels = (logger.LogLevel().ALL,)
174 else:
175 if not isinstance(_levels, tuple) and not \
176 isinstance(_levels, list):
177 _levels = (_levels,)
178
179 try:
180 _levels = [getattr(_log, x) for x in _levels]
181 except Exception:
182 raise XYZValueError(_(u"Invalid value %s.\n"\
183 u"A list of valid log levels expected"
184 % ustring(_levels)))
185
186 try:
187 _lines = self.xyz.conf[u"plugins"][u":sys:logger"][u"lines"]
188
189 except KeyError:
190 _lines = 100
191
192 try:
193 _lines = abs(int(_lines))
194 except ValueError:
195 raise XYZValueError(_(u"Invalid value %s. "\
196 u"A positive integer expected" %
197 ustring(_lines)))
198
199 _logger = core.logger.Logger(self.xyz, _levels, _lines)
200
201 __builtin__.__dict__["xyzlog"] = _logger
202
203
204
206 """
207 Initialize skin
208 """
209
210 _system, _user = self._path_sel.get_skin(
211 self.xyz.conf[u"xyz"][u"skin"])
212
213 _path = self._path_sel.get_first_of((_user, _system))
214
215 if _path is None:
216 _path = self._path_sel.get_skin(const.DEFAULT_SKIN)[0]
217
218 try:
219 self.xyz.skin = core.Skin(_system)
220 except SkinError, e:
221 self.error(_(u"Unable to load skin file: %s" % e))
222
223
224
233
234
235
246
247
248
263
264
265
267 """
268 Parse configuration files, first system then user one if any
269 @param conf_file: File to parse
270 """
271
272 _system, _user = self._path_sel.get_conf(conf_file)
273
274
275 try:
276 dsl.exec_file(_system)
277 except DSLError, e:
278 self.error(_(u"Error parsing system config %s: %s") %
279 (_system, ustring(str(e))))
280
281
282 if os.path.exists(_user):
283 try:
284 dsl.exec_file(_user)
285 except DSLError, e:
286 self.error(_(u"Error parsing user config %s: %s") %
287 (_user, ustring(str(e))))
288
289
290
292 """
293 Show usage
294 """
295
296 print _(u"""\
297 %s version %s
298 Usage: %s [-d driver][-vh]
299 -d -- Display driver (raw (default) or curses)
300 -v -- Show version
301 -h -- Show this help message\
302 """ % (const.PROG, Version.version, os.path.basename(sys.argv[0])))
303
304
305
312
313
314
316 """
317 Quit program
318 """
319
320 sys.exit()
321
322
323
324 - def error(self, msg, quit=True):
325 """
326 Print error message and optionally quit
327 """
328
329 try:
330 xyzlog.log(msg)
331 except NameError:
332
333 print msg
334
335 if quit:
336 self.quit()
337
338
339
341 """
342 Perform shutdown procedures
343 """
344
345 if self.xyz.term is not None:
346 core.utils.restore_term(self.xyz.term)
347