Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 123 lines (89 sloc) 3.8 KB
#!/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()
cityLength = len(city)
longestMatch = { 'length':0, 'city': None }
for possibleCity in cityList:
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']
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")
if and options.list:
parser.error("Can't use -c and -l at the same time")
if options.list:
for cityname in cityindex.english_city_list():
print cityname
if not
print "No City given, using, Victoria, BC (the best city)."
city =
if options.list_quantities and options.quantity:
parser.error("Can use -q and -a at the same time.")
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
if not options.quantity:
print "No requested quantity given, using temperature"
print quantity + " is: " + cityObject.get_quantity(quantity)
print city + " is not available"
bestGuess = get_best_guess(city,cityindex.english_city_list())
if bestGuess:
print "Did you mean: " + bestGuess
print "Do 'weatherca --list' to get a list of cities"
if __name__ == "__main__":