-
Notifications
You must be signed in to change notification settings - Fork 10
/
weatherca
executable file
·122 lines (89 loc) · 3.8 KB
/
weatherca
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
#!/usr/bin/env python
# vim: set expandtab ts=4 sw=4:
# Copyright (C) 2011 Ian Gable
# You may distribute under the terms of either the GNU General Public
# License or the Apache v2 License, as specified in the README file.
## Auth.: Ian Gable
import sys
import difflib
from optparse import OptionParser
from difflib import SequenceMatcher
from data_gc_ca_api.cityweather import City, CityIndex
from data_gc_ca_api.__version__ import version
def get_best_guess(city, cityList):
# Find the closest matching city including the case where the
# input city matches a complete sub string. For example the case where
# someone uses "Ottawa" but environment Canada uses the name
# "Ottawa (Richmond - Metcalfe)" we want to guess the long string
# rather than the close string Oshawa
s = SequenceMatcher()
s.set_seq1(city)
cityLength = len(city)
longestMatch = { 'length':0, 'city': None }
for possibleCity in cityList:
s.set_seq2(possibleCity)
match = s.find_longest_match(0,len(city),0,len((possibleCity)))
if match[2] > longestMatch['length']:
longestMatch['length'] = match[2]
longestMatch['city'] = possibleCity
if len(city) == longestMatch['length']:
return longestMatch['city']
else:
bestguess = difflib.get_close_matches(city,cityList,1)
if bestguess:
return bestguess[0]
return None
def main():
parser = OptionParser(usage="%prog -c CITY -q QUANTITY", version = "%prog " + version)
parser.add_option("-c", "--city", dest="city",
help="The City you want information about", metavar="CITY")
parser.add_option("-q", "--quantity", dest="quantity",
help="the particular quatity toy want to know", metavar="QUANTITY")
parser.add_option("-l", "--list", action="store_true", dest="list",
help="Show a list of the available cities")
parser.add_option("-a", "--list-quantities", action="store_true", dest="list_quantities",
help="Show a list of the available quantities")
(options, args) = parser.parse_args()
# globals
city = "Victoria"
quantity = "currentConditions/temperature"
cityindex = CityIndex()
# parsing the CLI options here is getting a little convoluted
# it could be improved
if options.list_quantities and options.list:
parser.error("Can't use -l and -a at the same time")
sys.exit(1)
if options.city and options.list:
parser.error("Can't use -c and -l at the same time")
sys.exit(1)
if options.list:
for cityname in cityindex.english_city_list():
print cityname
sys.exit(1)
if not options.city:
print "No City given, using, Victoria, BC (the best city)."
else:
city = options.city
if options.list_quantities and options.quantity:
parser.error("Can use -q and -a at the same time.")
sys.exit(1)
if options.quantity:
quantity = options.quantity
if cityindex.is_city(city):
cityObject = City(cityindex.data_url(city))
if options.list_quantities:
for q in cityObject.get_available_quantities():
print q
else:
if not options.quantity:
print "No requested quantity given, using temperature"
print quantity + " is: " + cityObject.get_quantity(quantity)
else:
print city + " is not available"
bestGuess = get_best_guess(city,cityindex.english_city_list())
if bestGuess:
print "Did you mean: " + bestGuess
else:
print "Do 'weatherca --list' to get a list of cities"
if __name__ == "__main__":
main()