Bug database updates
[breadcrumb.git] / code / runclient.py
blobb86827566e9c84cb6afcde566c1783516244a3fb
1 #!/usr/bin/env python
2 # -*- coding: utf8 -*-
3 """A script to start a typical breadcrumb session."""
5 # Copyright (C) 2008 Laurens Van Houtven <lvh at laurensvh.be>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import logging
20 import optparse
21 import sys
23 import breadcrumb.client.gps as gps
24 import breadcrumb.client.tracking as tracking
26 def main(manual_args = None):
27 """Tries to start a typical breadcrumb client.
29 The optional manual_args arument should be a list that looks somewhat like
30 sys.argv[1:] (a list of arguments). It is provided because WinCE doesn't
31 have system() -- this provides a workaround so you can start this function
32 with different a different set of argvs using a small Python script.
33 """
34 opts = getopt(manual_args)
35 set_verbosity(opts)
37 logging.info('Breadcrumb client')
39 tracker = tracking.Tracker(host = opts.host, port = opts.port)
41 if opts.fake_gps:
42 logging.debug("fake GPS specified...")
43 gpsdevice = gps.connect(platform = 'fake',
44 sentence_filename = opts.fake_file)
45 elif opts.gpsd_host and opts.gpsd_port:
46 logging.debug("gpsd GPS specified...")
47 gpsdevice = gps.connect(platform = 'gpsd',
48 host = opts.gpsd_host,
49 port = opts.gpsd_port)
50 else:
51 logging.debug("No GPS specified...")
52 host, port = opts.gpsd_host, opts.serial_port
53 gpsdevice = gps.connect(host = host, port = port)
55 tracker.gps = gpsdevice
57 tracker.start()
58 tracker.gps.close()
60 logging.debug("Closed the GPS port.")
62 def getopt(manual_args):
63 """Gets the command line options."""
64 optparser_config = {
65 ('-F', '--fake-gps'): {
66 'dest': 'fake_gps',
67 'action': 'store_true',
68 'help': 'Replay sentences with a simulated ("fake") GPS.'
71 ('', '--fake-file'): {
72 'default': None,
73 'dest': 'fake_file',
74 'help': 'Uses a non-standard sentence file.',
77 ('-H', '--host'): {
78 'dest': 'host',
79 'help': 'The host to connect to.',
80 'metavar': 'HOST',
83 ('-P', '--port'): {
84 'default': None,
85 'dest': 'port',
86 'help': 'The TCP/UDP port the server is listening on.',
87 'metavar': 'PORT',
90 ('-s', '--serial-port'): {
91 'default': None,
92 'dest': 'serial_port',
93 'help': 'The serial port the GPS is on.',
94 'metavar': 'PORT',
97 ('-g', '--gpsd-host'): {
98 'default': None,
99 'dest': 'gpsd_host',
100 'help': 'The hostname of the gpsd server.',
101 'metavar': "HOST",
104 ('-p', '--gpsd-port'): {
105 'default': None,
106 'dest': 'gpsd_port',
107 'help': 'The port of the gpsd server.',
108 'metavar': "PORT",
111 ('-v', '--verbose'): {
112 'dest': 'verbose',
113 'action': 'store_true',
114 'help': 'Show debug messages.',
117 ('-q', '--quiet'): {
118 'dest': 'quiet',
119 'action': 'store_true',
120 'help': 'Only show errors.',
123 ('-Q', '--very-quiet'): {
124 'dest': 'veryquiet',
125 'action': 'store_true',
126 'help': "Don't even show errors (not recommended).",
130 optparser = optparse.OptionParser()
131 for flags, options in optparser_config.iteritems():
132 optparser.add_option(*flags, **options)
134 # We have no positional arguments:
135 if manual_args is None:
136 return optparser.parse_args()[0]
137 else:
138 return optparser.parse_args(manual_args)[0]
140 def set_verbosity(opts):
141 """Sets the verbosity level of the logging module from optparser input."""
142 if opts.verbose:
143 logging.getLogger().setLevel(logging.DEBUG)
144 elif opts.quiet:
145 logging.getLogger().setLevel(logging.ERROR)
146 elif opts.veryquiet:
147 logging.getLogger().disable()
148 else:
149 logging.getLogger().setLevel(logging.INFO)
151 if __name__ == "__main__":
152 main()