File: rpcli.py

package info (click to toggle)
routeplanner 0.19
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 1,740 kB
  • ctags: 264
  • sloc: python: 3,282; makefile: 60; sh: 40
file content (168 lines) | stat: -rw-r--r-- 5,333 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# -*- coding: iso-8859-1 -*-
#
# Code for the non-GUI RoutePlanner
#
# Copyright (C) 1996-2003 Chris Lawrence
# This file may be freely distributed under the terms of the RoutePlanner
# license.  A copy should appear as 'LICENSE' in the archive that this
# file was included in.
#
# $Id: rpcli.py,v 1.6 2004/10/02 12:35:53 lordsutch Exp $

import rpdbase, string, sys, os, rpunits

# If readline works, import it; else, don't worry about it
try:
    import readline
except:
    pass

def ChooseCity(prompt, db):
    while 1:
        cities = []
        while not cities:
            try:
                stuff = raw_input(prompt+'\n> ')
            except EOFError:
                return None

            if not stuff:
                return None
            cities = db.CitiesMatching(stuff)
            if not cities:
                print 'No cities matching '+stuff
                
            if len(cities) == 1:
                return cities[0]
            
            while cities:
                count = 1
                for city in cities:
                    print '%2d: %s' % (count, city)
                    count = count + 1
                    if count > 15:
                        print '(More...)'
                        break
                try:
                    stuff = raw_input('Choose a city (by number or name)\n> ')
                except EOFError:
                    return None
                if not stuff:
                    return None
                
                try:
                    num = int(stuff)
                except:
                    num = 0

                if 0 < num < count:
                    return cities[num-1]
                elif num:
                    print 'Invalid choice (must be between 1 and %d).' % \
                          (count - 1)
                    continue
                else:
                    cities = db.CitiesMatching(stuff)
                    if not cities:
                        print 'No cities matching '+stuff
                        continue
                    elif len(cities) == 1:
                        return cities[0]
# End of ChooseCity

def NicerTime(min):
    return '%2d:%02d' % (min / 60, min % 60)

def mainloop(database='Basic-USA.rpl3.gz'):
    print rpdbase.VERSION
    print rpdbase.COPYRIGHT
    
    units = rpdbase.UNITS_METRIC
    if database and not os.path.exists(database):
        database = '/usr/share/routeplanner/'+database
        
    if database:
        print 'Loading database '+database+'...',
        db = rpdbase.RPDatabase(database)
        units = db.units
    else:
        print 'No database specified.'
        return 1
    print '%d cities, %d routes' % (len(db.cities), len(db.routes))
    print 'Created by %s <%s>' % (db.author, db.author_email)
    print
    
    while 1:
        city = ChooseCity('Please enter the name of the city you want '
                          'to start at:', db)
        if not city:
            return 1
        print 'Selected: '+str(city)
        
        path = [city]
        while city:
            city = ChooseCity('Enter destination for next leg (or an empty '
                              'destination to plan route):', db)
            if city:
                print 'Selected: '+str(city)
                path.append(city)
        
        if len(path) < 2:
            print 'You really need to specify two or more cities.'
            continue

        method = 0
        while method == 0:
            print 'Choose routing method:'
            for i in range(len(rpdbase.methods)):
                print '%2d: %s' % (i+1, rpdbase.methods[i])

            junk = raw_input('> ')
            try:
                method = int(junk)
            except:
                pass
            if not (1 <= method <= len(rpdbase.methods)):
                method = 0
        
        method = method - 1
        print 'Planning route ('+rpdbase.methods[method]+'):'
        for entry in path:
            print '  '+str(entry)

        try:
            import timing
        except ImportError:
            timing = None
        print "Calculating route...",
        sys.stdout.flush()
        trail = []
        if timing:
            timing.start()
            for i in range(len(path)-1):
                trail.append( db.NewNavigate(path[i], path[i+1],
                                             method=method) )
            timing.finish()
            print "done in %d ms." % timing.milli()
        else:
            for i in range(len(path)-1):
                trail.append( db.NewNavigate(path[i], path[i+1],
                                             method=method) )
            print "done."
        
        mpgs = rpdbase.defeff
        defmpg = rpdbase.defmpg
        speeds = rpdbase.defspeed
        if units != rpunits.UNITS_US:
            defmpg = rpunits.ConvertEfficiency(defmpg, rpunits.UNITS_US, units)
            mpgs = map(rpunits.ConvertEfficiency, mpgs,
                       rpunits.UNITS_US, units)
            speeds = map(lambda x, y=units: rpunits.Distance(x).AsUnit(y),
                         speeds)

        displist = rpdbase.ProduceTrail(trail, db, units, mpgs, defmpg, speeds)
        print rpdbase.FormatRoute(displist, path, units)
        # We is done

    # End of while
# End of mainloop