/
command_line.py
144 lines (119 loc) · 4.79 KB
/
command_line.py
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
"""
Veliberator script
"""
import os
import sys
import gettext
from optparse import OptionParser
import sqlalchemy
import veliberator
from veliberator.station import Station
from veliberator.station import UnknowStation
from veliberator.cartography import Cartography
from veliberator.models import db_connection
from veliberator.settings import DATABASE_URI
from veliberator.settings import STATION_AROUND_RADIUS
from veliberator.geofinder import GeoFinderError
from veliberator.geofinder import AddressGeoFinder
from veliberator.models import StationInformation
translation = gettext.translation(
'veliberator',
os.path.join(os.path.dirname(veliberator.__file__), 'locale'))
_ = translation.gettext
__ = translation.ngettext
def synchronization():
print _('-> Online synchronization...')
Cartography.synchronize()
print _('-> Synchronization complete !')
def show_status(station, distance=None):
print _("Station '%s'") % station.id
if distance:
print _('%(address)s (%(distance).2fm)') % {
'address': station.informations.full_address,
'distance': distance}
else:
print station.informations.full_address
print __('%(available)s/%(total)s bike available',
'%(available)s/%(total)s bikes available',
station.status.available) % {
'available': station.status.available,
'total': station.status.total}
print __('%s parking place', '%s parking places',
station.status.free) % station.status.free
def display_free_stations(stations, places, max_display):
displayed = 0
for station_information in stations:
if displayed == max_display:
break
station = Station(station_information.id)
if station.is_free(places):
print '----------'
show_status(station, station_information.distance)
displayed += 1
class VeliberatorOptionParser(OptionParser):
"""Customized OptionParser with
correct gettext handling"""
def _add_help_option(self):
self.add_option('-h', '--help', action='help',
help=_('Show this help message and exit'))
def _add_version_option(self):
self.add_option('--version', action='version',
help=_("Show program's version number and exit"))
def print_help(self, file=None):
if file is None:
file = sys.stdout
file.write(self.format_help())
def cmdline():
parser = VeliberatorOptionParser(
usage=_('%prog [station_id] [address] [options]'),
version='%s %s' % ('%prog', veliberator.__version__))
parser.add_option('-d', '--database', dest='database',
help=_('The SQLURI of the database'),
default=DATABASE_URI)
parser.add_option('-p', '--places', dest='places', type='int',
help=_('The number of places you want'), default=1)
parser.add_option('-m', '--max-stations', dest='max_stations', type='int',
help=_('The maximun stations around proposed'),
default=5)
parser.add_option('--synchronize', dest='synchronize', action='store_true',
help=_('Only do a synchronization of the cartography'))
(options, args) = parser.parse_args()
print '-==* Veliberator v%s *==-' % veliberator.__version__
try:
db_connection(options.database)
except sqlalchemy.exc.OperationalError:
if options.synchronize:
sys.exit(_('-> The database %s is unreachable.') %
options.database)
else:
print _('-> The database is unreachable, switch on RAM.')
print _('-> Edit the configuration file, '
'for removing this message.')
db_connection('sqlite://')
if options.synchronize:
synchronization()
sys.exit(0)
if not StationInformation.query.count():
synchronization()
if args:
user_input = args[0]
else:
user_input = raw_input(_('Station ID or complete address:\n'))
if user_input.isdigit():
try:
station = Station(user_input)
except UnknowStation:
sys.exit(_('The Station ID does not exist.'))
show_status(station)
if not station.is_free(options.places):
print _('Searching on the closest stations...')
display_free_stations(station.stations_around,
options.places, options.max_stations)
else:
try:
finder = AddressGeoFinder(user_input)
except GeoFinderError:
sys.exit(_('The provided address is not valid or imprecise.'))
display_free_stations(
finder.get_stations_around(STATION_AROUND_RADIUS),
options.places, options.max_stations)