# HG changeset patch # User Vlad Glagolev # Date 1248638530 -14400 # Node ID a754b01955c75d0d1462d94920cd4845b895494e # Parent bf34534a0acd57ec14c46054a9ce207e19a3a100 1st stage of work for final 0.2.0 diff -r bf34534a0acd -r a754b01955c7 ChangeLog --- a/ChangeLog Thu Nov 06 23:02:41 2008 +0300 +++ b/ChangeLog Mon Jul 27 00:02:10 2009 +0400 @@ -1,3 +1,9 @@ +2009-07-26 Vlad Glagolev + * version 0.2.0 + * fixed max-day limit to 5 as supported by weather.com + * added missing parameters in request (needed since 2009-07-07) + * rewritten the engine in OOP + 2008-11-06 Vlad Glagolev * bump to 0.1.2 * removed useless installation of text files for now diff -r bf34534a0acd -r a754b01955c7 LICENSE --- a/LICENSE Thu Nov 06 23:02:41 2008 +0300 +++ b/LICENSE Mon Jul 27 00:02:10 2009 +0400 @@ -1,4 +1,5 @@ -Copyright (c) 2008 Vlad Glagolev . All rights reserved. +Copyright (c) 2008-2009 Vlad Glagolev +All rights reserved. Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above diff -r bf34534a0acd -r a754b01955c7 pycweather/__init__.py --- a/pycweather/__init__.py Thu Nov 06 23:02:41 2008 +0300 +++ b/pycweather/__init__.py Mon Jul 27 00:02:10 2009 +0400 @@ -2,7 +2,8 @@ # # This file is part of PycWeather # -# Copyright (c) 2008 Vlad Glagolev . All rights reserved. +# Copyright (c) 2008-2009 Vlad Glagolev +# All rights reserved. # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -18,4 +19,4 @@ """ pycweather/__init__.py: initialization script """ -__version__ = "0.1.2" +__version__ = "0.2.0" diff -r bf34534a0acd -r a754b01955c7 pycweather/dmanager.py --- a/pycweather/dmanager.py Thu Nov 06 23:02:41 2008 +0300 +++ b/pycweather/dmanager.py Mon Jul 27 00:02:10 2009 +0400 @@ -2,7 +2,8 @@ # # This file is part of PycWeather # -# Copyright (c) 2008 Vlad Glagolev . All rights reserved. +# Copyright (c) 2008-2009 Vlad Glagolev +# All rights reserved. # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -18,38 +19,33 @@ """ pycweather/dmanager.py: weather display manager """ -from cStringIO import StringIO import getopt import os import sys -import urllib2 - -from lxml.etree import parse, tostring, fromstring, XMLSyntaxError from pycweather import __version__ -from pycweather.template import XSL +from pycweather import weather -# correct url to the XOAP service -XOAP_URL = "http://xoap.weather.com/" - -# default values stay for Moscow (RU) location, 0 days to display (means only +# default values stay for Moscow (RU) location, 1 days to display (means only # current day), metric measurement system and internal XSL stylesheet usage _CODE = "RSXX0063" _DAYS = 1 _UNIT = "m" -_XSL = None + +# TODO: default XSL template +_TEMPLATE = os.path.join(os.path.dirname(__file__), "template.xsl") def usage(): print """Usage: %s [options]\n -h, --help show this help -v, --version version information + -a, --auth authority data in format: partner-id:license-key -c, --code location ID code (default is %s) - -d, --days number of days to display [1-10] (default is %d) + -d, --days number of days to display [1-5] (default is %d) -u, --unit measurement system [m]etric|[i]mperial (default is %s) -s, --search search for the specified location (city, state, etc.) - -i, --int dump internal XSL stylesheet to stdout -x, --xsl custom XSL file\n""" % (sys.argv[0], _CODE, _DAYS, _UNIT) @@ -61,59 +57,26 @@ exit(0) -def search(location): - data = urllib2.urlopen(XOAP_URL + "search/search?where=%s" % location) - - tree = data.read() - - try: - xml = fromstring(tree) - except XMLSyntaxError: - print "XML syntax error occured while parsing content: ", tree - exit(2) - - if not xml.getchildren(): - print "No location has been found!" - elif xml.tag == "error": - print "Error: %s" % xml.getchildren()[0].text - else: - print "Code | Location\n--------+----------" - for loc in xml.xpath("loc"): - print loc.get("id") + ": " + loc.text - - print "--\nTo catch weather information use: %s -c " % sys.argv[0] - - exit(0) - - -def preview(xml, xsl): - try: - xsl_file = parse(xsl) if xsl else fromstring(XSL) - except XMLSyntaxError: - print "XML syntax error occured while parsing XSL stylesheet" - exit(2) - - document = parse(StringIO(tostring(xml))) - - print document.xslt(xsl_file) - - def main(): try: - opts, args = getopt.getopt(sys.argv[1:], "hvc:d:u:s:ix:", ["help", \ - "version", "code=", "days=", "unit=", \ - "search=", "int", "xsl="]) + opts, args = getopt.getopt(sys.argv[1:], "hva:c:d:u:s:x:", ["help", \ + "version", "auth=", "code=", "days=", \ + "unit=", "search=", "xsl="]) except getopt.error, msg: print msg usage() exit(2) + forecast = weather.Weather() + for o, a in opts: if o in ("-h", "--help"): usage() exit(0) elif o in ("-v", "--version"): version() + elif o in ("-a", "--auth"): + forecast.auth(a) elif o in ("-c", "--code"): code = a elif o in ("-d", "--days"): @@ -121,9 +84,7 @@ elif o in ("-u", "--unit"): unit = a elif o in ("-s", "--search"): - search(a) - elif o in ("-i", "--int"): - print XSL + forecast.search(a) exit(0) elif o in ("-x", "--xsl"): xsl = a @@ -145,30 +106,12 @@ try: xsl + except NameError: + xsl = _TEMPLATE - if not os.access(xsl, os.R_OK): - print "Error reading custom XSL file, using internal stylesheet..." - xsl = _XSL - except NameError: - xsl = _XSL + forecast.load(xsl) - try: - data = urllib2.urlopen(XOAP_URL + \ - "weather/local/%s?cc=*&dayf=%d&unit=%s" % (code, - days, unit)) - except: - print "Unable to connect to %s" % XOAP_URL - exit(2) - - tree = data.read() - - try: - xml = fromstring(tree) - except XMLSyntaxError: - print "XML syntax error occured while parsing content: ", tree - exit(2) - - if xml.tag == "error": - print "Error: %s" % xml.xpath("err")[0].text + if forecast.id and forecast.key: + forecast.preview(code, days, unit) else: - preview(xml, xsl) + print "Look at http://registration.weather.com/ursa/xmloap/step1" diff -r bf34534a0acd -r a754b01955c7 pycweather/template.py --- a/pycweather/template.py Thu Nov 06 23:02:41 2008 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -# -*- coding: utf-8 -*- -# -# This file is part of PycWeather -# -# Copyright (c) 2008 Vlad Glagolev . All rights reserved. -# -# Permission to use, copy, modify, and distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -""" pycweather/template.py: XSL stylesheet for displaying weather """ - -from pycweather import __version__ - - -XSL = """ - - - - - - - - PycWeather %s - - - Location: (, ) - Temperature: ° - - Windchill: ° - - Conditions: - Wind: - - - 0 - - - - - - - () - - - - - - - - - , - - (Tomorrow) - - : - - - - - - Day ( - ° - - - Night ( - ° - - - , - - ) - - ; - - -""" % __version__ diff -r bf34534a0acd -r a754b01955c7 pycweather/weather.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pycweather/weather.py Mon Jul 27 00:02:10 2009 +0400 @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +# +# This file is part of PycWeather +# +# Copyright (c) 2009 Vlad Glagolev +# All rights reserved. +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +""" pycweather/weather.py: Weather class """ + +from cStringIO import StringIO +from urllib2 import urlopen + +from lxml.etree import parse, tostring, fromstring, XMLSyntaxError + +from pycweather import __version__ + + +# correct url to the XOAP service +XOAP_URL = "http://xoap.weather.com/" + + +class Weather: + + def __init__(self): + self.xsl = None + self.id = None + self.key = None + + def auth(self, credentials): + try: + self.id, self.key = credentials.split(":") + except: + print "Invalid credentials" + + def search(self, location): + data = urlopen(XOAP_URL + "search/search?where=%s" % location) + + tree = data.read() + + try: + xml = fromstring(tree) + except XMLSyntaxError: + print "XML syntax error occured while parsing content: ", tree + + return -1 + + if not xml.getchildren(): + print "No location has been found!" + elif xml.tag == "error": + print "Error: %s" % xml.getchildren()[0].text + else: + print "Code | Location\n--------+----------" + for loc in xml.xpath("loc"): + print loc.get("id") + ": " + loc.text + + print "--\nTo catch weather information use: pycweather -c " + + def load(self, xsl_file): + try: + self.xsl = parse(xsl_file) + except XMLSyntaxError: + print "XML syntax error occured while parsing XSL stylesheet" + except: + print "Unable to read XSL file" + + def preview(self, code, days, unit): + try: + data = urlopen(XOAP_URL + \ + "weather/local/%s?cc=*&dayf=%d&unit=%s&par=%s&key=%s" % \ + (code, days, unit, self.id, self.key)) + except: + print "Unable to connect to %s" % XOAP_URL + + return -1 + + tree = data.read() + + try: + xml = fromstring(tree) + except XMLSyntaxError: + print "XML syntax error occured while parsing content: ", tree + + return -1 + + if xml.tag == "error": + print "Error: %s" % xml.xpath("err")[0].text + + return -1 + else: + document = parse(StringIO(tostring(xml))) + + print document.xslt(self.xsl) diff -r bf34534a0acd -r a754b01955c7 setup.py --- a/setup.py Thu Nov 06 23:02:41 2008 +0300 +++ b/setup.py Mon Jul 27 00:02:10 2009 +0400 @@ -3,7 +3,8 @@ # # This file is part of PycWeather # -# Copyright (c) 2008 Vlad Glagolev . All rights reserved. +# Copyright (c) 2008-2009 Vlad Glagolev +# All rights reserved. # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -32,6 +33,7 @@ author_email = "enqlave@gmail.com", packages = ["pycweather"], scripts = ["bin/pycweather"], + data_files = [('share/pycweather', ["share/template.xsl"])], description = "Weather display manager for conky", platforms = ["Linux", "Unix"], long_description = "PycWeather is pure-pythonic weather display manager \ diff -r bf34534a0acd -r a754b01955c7 share/template.xsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/share/template.xsl Mon Jul 27 00:02:10 2009 +0400 @@ -0,0 +1,82 @@ + + + + + + + + + + + PycWeather + + + Location: (, ) + Temperature: ° + + Windchill: ° + + Conditions: + Wind: + + + 0 + + + + + + + () + + + + + + + + + , + + (Tomorrow) + + : + + + + + + Day ( + ° + + + Night ( + ° + + + , + + ) + + ; + + +