~loggerhead-team/loggerhead/trunk-rich

« back to all changes in this revision

Viewing changes to loggerhead/config.py

  • Committer: Michael Hudson
  • Date: 2008-06-18 01:08:59 UTC
  • mto: This revision was merged to the branch mainline in revision 164.
  • Revision ID: michael.hudson@canonical.com-20080618010859-euunmral6tiuioxh
create loggerhead.apps package, move branch app in there

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''Configuration tools for Loggerhead.'''
2
 
from optparse import OptionParser
3
 
import sys
4
 
import tempfile
5
 
 
6
 
 
7
 
_temporary_sql_dir = None
8
 
 
9
 
def _get_temporary_sql_dir():
10
 
    global _temporary_sql_dir
11
 
    if _temporary_sql_dir is None:
12
 
        _temporary_sql_dir = tempfile.mkdtemp(prefix='loggerhead-cache-')
13
 
    return _temporary_sql_dir
14
 
 
15
 
def command_line_parser():
16
 
    parser = OptionParser("%prog [options] <path>")
17
 
    parser.set_defaults(
18
 
        user_dirs=False,
19
 
        show_version=False,
20
 
        log_folder=None,
21
 
        use_cdn=False,
22
 
        sql_dir=None,
23
 
        )
24
 
    parser.add_option("--user-dirs", action="store_true", dest="user_dirs",
25
 
                      help="Serve user directories as ~user.")
26
 
    parser.add_option("--trunk-dir", metavar="DIR",
27
 
                      help="The directory that contains the trunk branches.")
28
 
    parser.add_option("--port", dest="user_port",
29
 
                      help=("Port Loggerhead should listen on "
30
 
                            "(defaults to 8080)."))
31
 
    parser.add_option("--host", dest="user_host",
32
 
                      help="Host Loggerhead should listen on.")
33
 
    parser.add_option('--memory-profile', action='store_true',
34
 
                      dest='memory_profile',
35
 
                      help='Profile the memory usage using heapy.')
36
 
    parser.add_option("--prefix", dest="user_prefix",
37
 
                      help="Specify host prefix.")
38
 
    parser.add_option("--profile", action="store_true", dest="profile",
39
 
                      help="Generate callgrind profile data to "
40
 
                        "%d-stats.callgrind on each request.")
41
 
    parser.add_option("--reload", action="store_true", dest="reload",
42
 
                      help="Restarts the application when changing python"
43
 
                           " files. Only used for development purposes.")
44
 
    parser.add_option('--log-folder', dest="log_folder",
45
 
                      type=str, help="The directory to place log files in.")
46
 
    parser.add_option("--version", action="store_true", dest="show_version",
47
 
                      help="Print the software version and exit")
48
 
    parser.add_option('--use-cdn', action='store_true',
49
 
                      help="Serve YUI from Yahoo!'s CDN")
50
 
    parser.add_option('--cache-dir', dest='sql_dir',
51
 
                      help="The directory to place the SQL cache in")
52
 
    return parser
53
 
 
54
 
 
55
 
class LoggerheadConfig(object):
56
 
    '''A configuration object.'''
57
 
 
58
 
    def __init__(self):
59
 
        self._parser = command_line_parser()
60
 
        self._options, self._args = self._parser.parse_args(sys.argv[1:])
61
 
 
62
 
        sql_dir = self.get_option('sql_dir')
63
 
        if sql_dir is None:
64
 
            sql_dir = _get_temporary_sql_dir()
65
 
        self.SQL_DIR = sql_dir
66
 
 
67
 
    def get_option(self, option):
68
 
        '''Get an option from the options dict.'''
69
 
        return getattr(self._options, option)
70
 
 
71
 
    def get_arg(self, index):
72
 
        '''Get an arg from the arg list.'''
73
 
        return self._args[index]
74
 
 
75
 
    def print_help(self):
76
 
        '''Wrapper around OptionParser.print_help.'''
77
 
        return self._parser.print_help()
78
 
 
79
 
    @property
80
 
    def arg_count(self):
81
 
        '''Return the number of args from the option parser.'''
82
 
        return len(self._args)
83