~ubuntu-branches/ubuntu/precise/checkbox/precise

« back to all changes in this revision

Viewing changes to checkbox/application.py

  • Committer: Bazaar Package Importer
  • Author(s): Marc Tardif
  • Date: 2009-01-20 16:46:15 UTC
  • Revision ID: james.westby@ubuntu.com-20090120164615-7iz6nmlef41h4vx2
Tags: 0.4
* Setup bzr-builddeb in native mode.
* Removed LGPL notice from the copyright file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2008 Canonical
 
3
#
 
4
# Written by Marc Tardif <marc@interunion.ca>
 
5
#
 
6
# This file is part of Checkbox.
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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/>.
 
20
#
 
21
import sys
 
22
import logging
 
23
import posixpath
 
24
 
 
25
from gettext import gettext as _
 
26
 
 
27
from logging import StreamHandler, FileHandler, Formatter
 
28
from optparse import OptionParser
 
29
 
 
30
from checkbox.contrib import bpickle_registry
 
31
 
 
32
from checkbox.lib.environ import get_variable
 
33
 
 
34
from checkbox.config import Config
 
35
from checkbox.plugin import PluginManager
 
36
from checkbox.reactor import Reactor
 
37
from checkbox.registry import RegistryManager
 
38
 
 
39
 
 
40
def parse_string(options):
 
41
    args = []
 
42
    while True:
 
43
        options = options.strip()
 
44
        if not options:
 
45
            break
 
46
 
 
47
        index = 0
 
48
        while index < len(options) \
 
49
              and (not options[index].isspace() \
 
50
                  or options[index - 1] == "\\"):
 
51
           index += 1
 
52
 
 
53
        args.append(options[:index])
 
54
        options = options[index:]
 
55
 
 
56
    return args
 
57
 
 
58
 
 
59
class Application(object):
 
60
 
 
61
    reactor_factory = Reactor
 
62
 
 
63
    def __init__(self, config):
 
64
        self._config = config
 
65
        self.reactor = self.reactor_factory()
 
66
 
 
67
        # Registry manager setup
 
68
        self.registry = RegistryManager(self._config)
 
69
 
 
70
        # Plugin manager setup
 
71
        self.plugin_manager = PluginManager(self._config,
 
72
            self.reactor, self.registry)
 
73
 
 
74
    def run(self):
 
75
        try:
 
76
            bpickle_registry.install()
 
77
            self.reactor.run()
 
78
            bpickle_registry.uninstall()
 
79
        except:
 
80
            logging.exception("Error running reactor.")
 
81
            raise
 
82
 
 
83
        self.plugin_manager.flush()
 
84
 
 
85
 
 
86
class ApplicationManager(object):
 
87
 
 
88
    application_factory = Application
 
89
 
 
90
    default_log_level = "critical"
 
91
 
 
92
    def parse_options(self, args):
 
93
        usage = _("Usage: checkbox [OPTIONS]")
 
94
        parser = OptionParser(usage=usage)
 
95
        parser.add_option("--version",
 
96
                          action="store_true",
 
97
                          help=_("Print version information and exit."))
 
98
        parser.add_option("-l", "--log",
 
99
                          metavar="FILE",
 
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",
 
105
                          action="append",
 
106
                          type="string",
 
107
                          default=[],
 
108
                          help=_("Configuration override parameters."))
 
109
        return parser.parse_args(args)
 
110
 
 
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)
 
116
 
 
117
        log_level = logging.getLevelName(options.log_level.upper())
 
118
        log_handlers = []
 
119
        if options.log:
 
120
            log_filename = options.log
 
121
            log_handlers.append(FileHandler(log_filename))
 
122
        else:
 
123
            log_handlers.append(StreamHandler())
 
124
 
 
125
        # Logging setup
 
126
        format = ("%(asctime)s %(levelname)-8s %(message)s")
 
127
        if log_handlers:
 
128
            for handler in log_handlers:
 
129
                handler.setFormatter(Formatter(format))
 
130
                logging.getLogger().addHandler(handler)
 
131
            if log_level:
 
132
                logging.getLogger().setLevel(log_level)
 
133
        elif not logging.getLogger().handlers:
 
134
            logging.disable(logging.CRITICAL)
 
135
 
 
136
        # Config setup
 
137
        if len(args) != 2:
 
138
            sys.stderr.write(_("Missing configuration file as argument.\n"))
 
139
            sys.exit(1)
 
140
 
 
141
        config_file = posixpath.expanduser(args[1])
 
142
        config = Config(config_file, options.config)
 
143
 
 
144
        # Check options
 
145
        if options.version:
 
146
            print config.get_defaults().version
 
147
            sys.exit(0)
 
148
 
 
149
        return self.application_factory(config)