/
weathertest
executable file
·127 lines (94 loc) · 4.01 KB
/
weathertest
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
#!/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.
# This is a testing version of the weatherca CLI. It's used mostly for
# experimentation with new features. Don't expect it to work.
## Auth.: Ian Gable
import sys
import difflib
from difflib import SequenceMatcher
from data_gc_ca_api.cityweather import City, CityIndex
from data_gc_ca_api.__version__ import version
from optparse import OptionParser
from xml.etree.ElementTree import ElementTree
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:
return difflib.get_close_matches(city,cityList,1)[0]
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")
(options, args) = parser.parse_args()
(options, args) = parser.parse_args()
# globals
city = "Victoria"
quantity = "currentConditions/temperature"
cityindex = CityIndex()
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.quantity:
quantity = options.quantity
if cityindex.is_city(city):
cityObject = City(cityindex.data_url(city))
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"
# the following prints out all the available xpaths for testing purposes
for xpath in cityObject.get_available_quantities():
# attributelist = ""
# if isinstance(xpath['attrib'],dict):
# for attribute,value in xpath['attrib'].iteritems():
# attributelist = attributelist + " " + attribute + ":" + value
# #print attribute
# if attributelist:
# print xpath['path'] + " " + attributelist
# else:
# print xpath['path']
# #print xpath[0]
print xpath
# for forecastname in cityObject.getAvailableForecastNames():
# print forecastname
#
# for forecastperiod in cityObject.getAvailableForecastPeriods():
# print forecastperiod
if __name__ == "__main__":
main()