Package libxyz :: Module launcher
[hide private]
[frames] | no frames]

Source Code for Module libxyz.launcher

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 13  # GNU Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 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   
42 -class Launcher(object):
43 """ 44 Startup class 45 """ 46
47 - def __init__(self):
48 """ 49 Initialization 50 """ 51 52 gettext.install(u"xyzcmd") 53 54 self.cmdopts = "d:vh" 55 self.xyz = core.XYZData() 56 self.xyz.conf = {} 57 self.dsl = dsl.XYZ(self.xyz) 58 59 self._path_sel = libxyz.PathSelector() 60 self._conf = {} 61 self._saved_term = None 62 63 self._create_home()
64 65 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 66
67 - def run(self):
68 """ 69 Run commander 70 """ 71 72 self._set_enc() 73 74 self.parse_args() 75 self.parse_configs() 76 self.init_skin() 77 78 self.xyz.term = core.utils.setup_term() 79 self.xyz.hm = core.HookManager() 80 self.xyz.pm = PluginManager(self.xyz, 81 self._path_sel.get_plugins_dir()) 82 83 self.init_logger() 84 self.init_actions() 85 self.init_keys() 86 87 self.xyz.input = core.InputWrapper(self.xyz) 88 self.xyz.screen = uilib.display.init_display( 89 self._conf.get("driver", self.xyz.conf["xyz"]["term_lib"])) 90 91 self.xyz.screen.register_palette(self.xyz.skin.get_palette_list()) 92 self.xyz.skin.set_screen(self.xyz.screen) 93 self.xyz.screen.run_wrapper(self._run) 94 95 self.finalize()
96 97 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 98
99 - def _set_enc(self):
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
110 - def _run(self):
111 panel = uilib.Panel(self.xyz) 112 self.xyz.top = lowui.Filler(panel) 113 114 panel.loop()
115 116 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 117
118 - def _create_home(self):
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
139 - def parse_args(self):
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
163 - def init_logger(self):
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 # Value not defined in conf 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
205 - def init_skin(self):
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
225 - def init_keys(self):
226 """ 227 Initialize keys manager 228 """ 229 230 self.xyz.km = core.KeyManager(self.xyz, self._path_sel.get_conf( 231 const.KEYS_CONF_FILE)) 232 self.xyz.km.parse_configs()
233 234 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 235
236 - def init_actions(self):
237 """ 238 Init action manager 239 """ 240 241 self.xyz.am = core.ActionManager(self.xyz, 242 self._path_sel.get_conf( 243 const.ACTIONS_CONF_FILE)) 244 245 self.xyz.am.parse_configs()
246 247 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 248
249 - def parse_configs(self):
250 """ 251 Parse configuration 252 """ 253 254 self._parse_conf_file(const.XYZ_CONF_FILE) 255 self._parse_conf_file(const.PLUGINS_CONF_FILE) 256 self._parse_conf_file(const.ALIASES_CONF_FILE) 257 self._parse_conf_file(const.ICMD_CONF_FILE) 258 259 # local_encoding set, override guessed encoding 260 if u"local_encoding" in self.xyz.conf[u"xyz"]: 261 __builtin__.__dict__["xyzenc"] = \ 262 self.xyz.conf[u"xyz"][u"local_encoding"]
263 264 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 265
266 - def _parse_conf_file(self, conf_file):
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 # Exec system config first 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 # Now try to exec users's conf, if exists 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
291 - def usage(self):
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
306 - def version(self):
307 """ 308 Show version 309 """ 310 311 print _(u"%s version %s" % (const.PROG, Version.version))
312 313 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 314
315 - def quit(self):
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 # Before logger initiated, print errors to stdout 333 print msg 334 335 if quit: 336 self.quit()
337 338 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 339
340 - def finalize(self):
341 """ 342 Perform shutdown procedures 343 """ 344 345 if self.xyz.term is not None: 346 core.utils.restore_term(self.xyz.term)
347