2
# Copyright (c) 2008 Canonical
4
# Written by Marc Tardif <marc@interunion.ca>
6
# This file is part of Checkbox.
8
# Checkbox is free software: you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation, either version 3 of the License, or
11
# (at your option) any later version.
13
# Checkbox is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.
25
from gettext import gettext as _
27
from logging import StreamHandler, FileHandler, Formatter
28
from optparse import OptionParser
30
from checkbox.contrib import bpickle_registry
32
from checkbox.lib.environ import get_variable
34
from checkbox.config import Config
35
from checkbox.plugin import PluginManager
36
from checkbox.reactor import Reactor
37
from checkbox.registry import RegistryManager
40
def parse_string(options):
43
options = options.strip()
48
while index < len(options) \
49
and (not options[index].isspace() \
50
or options[index - 1] == "\\"):
53
args.append(options[:index])
54
options = options[index:]
59
class Application(object):
61
reactor_factory = Reactor
63
def __init__(self, config):
65
self.reactor = self.reactor_factory()
67
# Registry manager setup
68
self.registry = RegistryManager(self._config)
70
# Plugin manager setup
71
self.plugin_manager = PluginManager(self._config,
72
self.reactor, self.registry)
76
bpickle_registry.install()
78
bpickle_registry.uninstall()
80
logging.exception("Error running reactor.")
83
self.plugin_manager.flush()
86
class ApplicationManager(object):
88
application_factory = Application
90
default_log_level = "critical"
92
def parse_options(self, args):
93
usage = _("Usage: checkbox [OPTIONS]")
94
parser = OptionParser(usage=usage)
95
parser.add_option("--version",
97
help=_("Print version information and exit."))
98
parser.add_option("-l", "--log",
100
help=_("The file to write the log to."))
101
parser.add_option("--log-level",
102
default=self.default_log_level,
103
help=_("One of debug, info, warning, error or critical."))
104
parser.add_option("-c", "--config",
108
help=_("Configuration override parameters."))
109
return parser.parse_args(args)
111
def create_application(self, args=sys.argv):
112
# Prepend environment options
113
string_options = get_variable("CHECKBOX_OPTIONS", "")
114
args[:0] = parse_string(string_options)
115
(options, args) = self.parse_options(args)
117
log_level = logging.getLevelName(options.log_level.upper())
120
log_filename = options.log
121
log_handlers.append(FileHandler(log_filename))
123
log_handlers.append(StreamHandler())
126
format = ("%(asctime)s %(levelname)-8s %(message)s")
128
for handler in log_handlers:
129
handler.setFormatter(Formatter(format))
130
logging.getLogger().addHandler(handler)
132
logging.getLogger().setLevel(log_level)
133
elif not logging.getLogger().handlers:
134
logging.disable(logging.CRITICAL)
138
sys.stderr.write(_("Missing configuration file as argument.\n"))
141
config_file = posixpath.expanduser(args[1])
142
config = Config(config_file, options.config)
146
print config.get_defaults().version
149
return self.application_factory(config)