/usr/share/pyshared/weboob/tools/application/base.py is in python-weboob-core 0.g-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | # -*- coding: utf-8 -*-
# Copyright(C) 2010-2012 Romain Bignon, Christophe Benz
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import logging
import optparse
from optparse import OptionGroup, OptionParser
import os
import sys
import tempfile
import warnings
from weboob.capabilities.base import ConversionWarning
from weboob.core import Weboob, CallErrors
from weboob.core.backendscfg import BackendsConfig
from weboob.tools.config.iconfig import ConfigError
from weboob.tools.log import createColoredFormatter, getLogger
from weboob.tools.misc import to_unicode
__all__ = ['BaseApplication']
class MoreResultsAvailable(Exception):
pass
class ApplicationStorage(object):
def __init__(self, name, storage):
self.name = name
self.storage = storage
def set(self, *args):
if self.storage:
return self.storage.set('applications', self.name, *args)
def delete(self, *args):
if self.storage:
return self.storage.delete('applications', self.name, *args)
def get(self, *args, **kwargs):
if self.storage:
return self.storage.get('applications', self.name, *args, **kwargs)
else:
return kwargs.get('default', None)
def load(self, default):
if self.storage:
return self.storage.load('applications', self.name, default)
def save(self):
if self.storage:
return self.storage.save('applications', self.name)
class BaseApplication(object):
"""
Base application.
This class can be herited to have some common code within weboob
applications.
"""
# ------ Class attributes --------------------------------------
# Application name
APPNAME = ''
# Configuration and work directory (if None, use the Weboob instance one)
CONFDIR = None
# Default configuration dict (can only contain key/values)
CONFIG = {}
# Default storage tree
STORAGE = {}
# Synopsis
SYNOPSIS = 'Usage: %prog [-h] [-dqv] [-b backends] ...\n'
SYNOPSIS += ' %prog [--help] [--version]'
# Description
DESCRIPTION = None
# Version
VERSION = None
# Copyright
COPYRIGHT = None
# ------ Abstract methods --------------------------------------
def create_weboob(self):
return Weboob()
def _get_completions(self):
"""
Overload this method in subclasses if you want to enrich shell completion.
@return a set object
"""
return set()
def _handle_options(self):
"""
Overload this method in application type subclass
if you want to handle options defined in subclass constructor.
"""
pass
def add_application_options(self, group):
"""
Overload this method if your application needs extra options.
These options will be displayed in an option group.
"""
pass
def handle_application_options(self):
"""
Overload this method in your application if you want to handle options defined in add_application_options.
"""
pass
# ------ BaseApplication methods -------------------------------
def __init__(self, option_parser=None):
self.logger = getLogger(self.APPNAME)
self.weboob = self.create_weboob()
if self.CONFDIR is None:
self.CONFDIR = self.weboob.workdir
self.config = None
self.options = None
if option_parser is None:
self._parser = OptionParser(self.SYNOPSIS, version=self._get_optparse_version())
else:
self._parser = option_parser
if self.DESCRIPTION:
self._parser.description = self.DESCRIPTION
app_options = OptionGroup(self._parser, '%s Options' % self.APPNAME.capitalize())
self.add_application_options(app_options)
if len(app_options.option_list) > 0:
self._parser.add_option_group(app_options)
self._parser.add_option('-b', '--backends', help='what backend(s) to enable (comma separated)')
self._parser.add_option('-e', '--exclude-backends', help='what backend(s) to exclude (comma separated)')
self._parser.add_option('-I', '--insecure', action='store_true', help='do not validate SSL')
logging_options = OptionGroup(self._parser, 'Logging Options')
logging_options.add_option('-d', '--debug', action='store_true', help='display debug messages')
logging_options.add_option('-q', '--quiet', action='store_true', help='display only error messages')
logging_options.add_option('-v', '--verbose', action='store_true', help='display info messages')
logging_options.add_option('--logging-file', action='store', type='string', dest='logging_file', help='file to save logs')
logging_options.add_option('-a', '--save-responses', action='store_true', help='save every response')
self._parser.add_option_group(logging_options)
self._parser.add_option('--shell-completion', action='store_true', help=optparse.SUPPRESS_HELP)
def deinit(self):
self.weboob.want_stop()
self.weboob.deinit()
def create_storage(self, path=None, klass=None, localonly=False):
"""
Create a storage object.
:param path: An optional specific path
:type path: :class:`str`
:param klass: What class to instance
:type klass: :class:`weboob.tools.storage.IStorage`
:param localonly: If True, do not set it on the :class:`Weboob` object.
:type localonly: :class:`bool`
:rtype: :class:`weboob.tools.storage.IStorage`
"""
if klass is None:
from weboob.tools.storage import StandardStorage
klass = StandardStorage
if path is None:
path = os.path.join(self.CONFDIR, self.APPNAME + '.storage')
elif not os.path.sep in path:
path = os.path.join(self.CONFDIR, path)
storage = klass(path)
self.storage = ApplicationStorage(self.APPNAME, storage)
self.storage.load(self.STORAGE)
if not localonly:
self.weboob.storage = storage
return storage
def load_config(self, path=None, klass=None):
"""
Load a configuration file and get his object.
:param path: An optional specific path
:type path: :class:`str`
:param klass: What class to instance
:type klass: :class:`weboob.tools.config.iconfig.IConfig`
:rtype: :class:`weboob.tools.config.iconfig.IConfig`
"""
if klass is None:
from weboob.tools.config.iniconfig import INIConfig
klass = INIConfig
if path is None:
path = os.path.join(self.CONFDIR, self.APPNAME)
elif not os.path.sep in path:
path = os.path.join(self.CONFDIR, path)
self.config = klass(path)
self.config.load(self.CONFIG)
def main(self, argv):
"""
Main method
Called by run
"""
raise NotImplementedError()
def load_backends(self, caps=None, names=None, exclude=None, *args, **kwargs):
if names is None and self.options.backends:
names = self.options.backends.split(',')
if exclude is None and self.options.exclude_backends:
exclude = self.options.exclude_backends.split(',')
loaded = self.weboob.load_backends(caps, names, exclude=exclude, *args, **kwargs)
if not loaded:
logging.info(u'No backend loaded')
return loaded
def _get_optparse_version(self):
version = None
if self.VERSION:
if self.COPYRIGHT:
version = '%s v%s %s' % (self.APPNAME, self.VERSION, self.COPYRIGHT)
else:
version = '%s v%s' % (self.APPNAME, self.VERSION)
return version
def _do_complete_obj(self, backend, fields, obj):
if fields is None or len(fields) > 0:
backend.fillobj(obj, fields)
return obj
def _do_complete_iter(self, backend, count, fields, res):
for i, sub in enumerate(res):
if count and i == count:
raise MoreResultsAvailable()
sub = self._do_complete_obj(backend, fields, sub)
yield sub
def _do_complete(self, backend, count, selected_fields, function, *args, **kwargs):
assert count is None or count > 0
if callable(function):
res = function(backend, *args, **kwargs)
else:
res = getattr(backend, function)(*args, **kwargs)
if hasattr(res, '__iter__'):
return self._do_complete_iter(backend, count, selected_fields, res)
else:
return self._do_complete_obj(backend, selected_fields, res)
def bcall_error_handler(self, backend, error, backtrace):
"""
Handler for an exception inside the CallErrors exception.
This method can be overrided to support more exceptions types.
"""
# Ignore this error.
if isinstance(error, MoreResultsAvailable):
return False
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
if logging.root.level == logging.DEBUG:
print >>sys.stderr, backtrace
else:
return True
def bcall_errors_handler(self, errors, debugmsg='Use --debug option to print backtraces', ignore=()):
"""
Handler for the CallErrors exception.
It calls `bcall_error_handler` for each error.
:param errors: Object containing errors from backends
:type errors: :class:`weboob.core.bcall.CallErrors`
:param debugmsg: Default message asking to enable the debug mode
:type debugmsg: :class:`basestring`
:param ignore: Exceptions to ignore
:type ignore: tuple[:class:`Exception`]
"""
ask_debug_mode = False
for backend, error, backtrace in errors.errors:
if isinstance(error, ignore):
continue
elif self.bcall_error_handler(backend, error, backtrace):
ask_debug_mode = True
if ask_debug_mode:
print >>sys.stderr, debugmsg
def parse_args(self, args):
self.options, args = self._parser.parse_args(args)
if self.options.shell_completion:
items = set()
for option in self._parser.option_list:
if not option.help is optparse.SUPPRESS_HELP:
items.update(str(option).split('/'))
items.update(self._get_completions())
print ' '.join(items)
sys.exit(0)
if self.options.debug or self.options.save_responses:
level = logging.DEBUG
from weboob.tools.browser import StandardBrowser
StandardBrowser.DEBUG_MECHANIZE = True
# required to actually display or save the stuff
logger = logging.getLogger("mechanize")
logger.setLevel(logging.INFO)
elif self.options.verbose:
level = logging.INFO
elif self.options.quiet:
level = logging.ERROR
else:
level = logging.WARNING
if self.options.insecure:
from weboob.tools.browser import StandardBrowser
StandardBrowser.INSECURE = True
# this only matters to developers
if not self.options.debug and not self.options.save_responses:
warnings.simplefilter('ignore', category=ConversionWarning)
handlers = []
if self.options.save_responses:
responses_dirname = tempfile.mkdtemp(prefix='weboob_session_')
print >>sys.stderr, 'Debug data will be saved in this directory: %s' % responses_dirname
StandardBrowser.SAVE_RESPONSES = True
StandardBrowser.responses_dirname = responses_dirname
handlers.append(self.create_logging_file_handler(os.path.join(responses_dirname, 'debug.log')))
# file logger
if self.options.logging_file:
handlers.append(self.create_logging_file_handler(self.options.logging_file))
else:
handlers.append(self.create_default_logger())
self.setup_logging(level, handlers)
self._handle_options()
self.handle_application_options()
return args
@classmethod
def create_default_logger(klass):
# stdout logger
format = '%(asctime)s:%(levelname)s:%(name)s:%(filename)s:%(lineno)d:%(funcName)s %(message)s'
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(createColoredFormatter(sys.stdout, format))
return handler
@classmethod
def setup_logging(klass, level, handlers):
logging.root.handlers = []
logging.root.setLevel(level)
for handler in handlers:
logging.root.addHandler(handler)
def create_logging_file_handler(self, filename):
try:
stream = open(filename, 'w')
except IOError as e:
self.logger.error('Unable to create the logging file: %s' % e)
sys.exit(1)
else:
format = '%(asctime)s:%(levelname)s:%(name)s:%(pathname)s:%(lineno)d:%(funcName)s %(message)s'
handler = logging.StreamHandler(stream)
handler.setFormatter(logging.Formatter(format))
return handler
@classmethod
def run(klass, args=None):
"""
This static method can be called to run the application.
It creates the application object, handles options, setups logging, calls
the main() method, and catches common exceptions.
You can't do anything after this call, as it *always* finishes with
a call to sys.exit().
For example:
>>> from weboob.application.myapplication import MyApplication
>>> MyApplication.run()
"""
klass.setup_logging(logging.INFO, [klass.create_default_logger()])
if args is None:
args = [(sys.stdin.encoding and arg.decode(sys.stdin.encoding) or to_unicode(arg)) for arg in sys.argv]
try:
app = klass()
except BackendsConfig.WrongPermissions as e:
print >>sys.stderr, e
sys.exit(1)
try:
try:
args = app.parse_args(args)
sys.exit(app.main(args))
except KeyboardInterrupt:
print >>sys.stderr, 'Program killed by SIGINT'
sys.exit(0)
except EOFError:
sys.exit(0)
except ConfigError as e:
print >>sys.stderr, 'Configuration error: %s' % e
sys.exit(1)
except CallErrors as e:
app.bcall_errors_handler(e)
sys.exit(1)
finally:
app.deinit()
|