~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/config.py

  • Committer: Robey Pointer
  • Date: 2006-12-19 02:19:42 UTC
  • Revision ID: robey@lag.net-20061219021942-vvymd4ep9yac58mg
tone down the logging a bit

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# Copyright (C) 2008, 2009 Canonical Ltd
3
 
#
4
 
# This program is free software; you can redistribute it and/or modify
5
 
# it under the terms of the GNU General Public License as published by
6
 
# the Free Software Foundation; either version 2 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This program is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU General Public License for more details.
13
 
#
14
 
"""Configuration tools for Loggerhead."""
15
 
 
16
 
from optparse import OptionParser
17
 
import sys
18
 
import tempfile
19
 
 
20
 
from bzrlib import config
21
 
 
22
 
_temporary_sql_dir = None
23
 
 
24
 
def _get_temporary_sql_dir():
25
 
    global _temporary_sql_dir
26
 
    if _temporary_sql_dir is None:
27
 
        _temporary_sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
28
 
    return _temporary_sql_dir
29
 
 
30
 
def command_line_parser():
31
 
    parser = OptionParser("%prog [options] <path>")
32
 
    parser.set_defaults(
33
 
        user_dirs=False,
34
 
        show_version=False,
35
 
        log_folder=None,
36
 
        use_cdn=False,
37
 
        sql_dir=None,
38
 
        allow_writes=False,
39
 
        )
40
 
    parser.add_option("--user-dirs", action="store_true",
41
 
                      help="Serve user directories as ~user.")
42
 
    parser.add_option("--trunk-dir", metavar="DIR",
43
 
                      help="The directory that contains the trunk branches.")
44
 
    parser.add_option("--port", dest="user_port",
45
 
                      help=("Port Loggerhead should listen on "
46
 
                            "(defaults to 8080)."))
47
 
    parser.add_option("--host", dest="user_host",
48
 
                      help="Host Loggerhead should listen on.")
49
 
    parser.add_option("--protocol", dest="protocol",
50
 
                      help=("Protocol to use: http, scgi, fcgi, ajp"
51
 
                           "(defaults to http)."))
52
 
    parser.add_option("--log-level", default=None, action='callback',
53
 
                      callback=_optparse_level_to_int_level,
54
 
                      type="string",
55
 
                      help="Set the verbosity of logging. Can either"
56
 
                           " be set to a numeric or string"
57
 
                           " (eg, 10=debug, 30=warning)")
58
 
    parser.add_option("--memory-profile", action="store_true",
59
 
                      help="Profile the memory usage using Dozer.")
60
 
    parser.add_option("--prefix", dest="user_prefix",
61
 
                      help="Specify host prefix.")
62
 
    parser.add_option("--profile", action="store_true",
63
 
                      help="Generate callgrind profile data to "
64
 
                        "%d-stats.callgrind on each request.")
65
 
    parser.add_option("--reload", action="store_true",
66
 
                      help="Restarts the application when changing python"
67
 
                           " files. Only used for development purposes.")
68
 
    parser.add_option("--log-folder",
69
 
                      help="The directory to place log files in.")
70
 
    parser.add_option("--version", action="store_true", dest="show_version",
71
 
                      help="Print the software version and exit")
72
 
    parser.add_option("--use-cdn", action="store_true", dest="use_cdn",
73
 
                      help="Serve YUI from Yahoo!'s CDN")
74
 
    parser.add_option("--cache-dir", dest="sql_dir",
75
 
                      help="The directory to place the SQL cache in")
76
 
    parser.add_option("--allow-writes", action="store_true",
77
 
                      help="Allow writing to the Bazaar server.")
78
 
    return parser
79
 
 
80
 
 
81
 
_log_levels = {
82
 
    'debug': 10,
83
 
    'info': 20,
84
 
    'warning': 30,
85
 
    'error': 40,
86
 
    'critical': 50,
87
 
}
88
 
 
89
 
def _optparse_level_to_int_level(option, opt_str, value, parser):
90
 
    parser.values.log_level = _level_to_int_level(value)
91
 
 
92
 
 
93
 
def _level_to_int_level(value):
94
 
    """Convert a string level to an integer value."""
95
 
    if value is None:
96
 
        return None
97
 
    try:
98
 
        return int(value)
99
 
    except ValueError:
100
 
        pass
101
 
    return _log_levels[value.lower()]
102
 
 
103
 
 
104
 
class LoggerheadConfig(object):
105
 
    """A configuration object."""
106
 
 
107
 
    def __init__(self, argv=None):
108
 
        if argv is None:
109
 
            argv = sys.argv[1:]
110
 
        self._parser = command_line_parser()
111
 
        self._options, self._args = self._parser.parse_args(argv)
112
 
 
113
 
        sql_dir = self.get_option('sql_dir')
114
 
        if sql_dir is None:
115
 
            sql_dir = _get_temporary_sql_dir()
116
 
        self.SQL_DIR = sql_dir
117
 
 
118
 
    def get_option(self, option):
119
 
        """Get the value for the config option, either
120
 
        from ~/.bazaar/bazaar.conf or from the command line.
121
 
        All loggerhead-specific settings start with 'http_'
122
 
        """
123
 
        global_config = config.GlobalConfig().get_user_option('http_'+option)
124
 
        cmd_config = getattr(self._options, option)
125
 
        if global_config is not None and (
126
 
            cmd_config is None or cmd_config is False):
127
 
            return global_config
128
 
        else:
129
 
            return cmd_config
130
 
 
131
 
    def get_log_level(self):
132
 
        opt = self.get_option('log_level')
133
 
        return _level_to_int_level(opt)
134
 
 
135
 
    def get_arg(self, index):
136
 
        """Get an arg from the arg list."""
137
 
        return self._args[index]
138
 
 
139
 
    def print_help(self):
140
 
        """Wrapper around OptionParser.print_help."""
141
 
        return self._parser.print_help()
142
 
 
143
 
    @property
144
 
    def arg_count(self):
145
 
        """Return the number of args from the option parser."""
146
 
        return len(self._args)