""" Python module for use in the SoCal Linux Expo exhibit. project builtin functions. """ from PsychePostProcessor import PsychePostProcessor import sys, time, pymetar, urllib, string class PsycheWeather( PsychePostProcessor ): """ PsycheWeather: universal weather information at your vocal command! """ def __init__( self, args ): """ __init__( list ): list = [ command_name, weather_station ] sets the station to listen to, grabs all information, and returns the desired information. """ self.station = args[1] self.get_metar() self.set_result(getattr(self,args[0])()) def get_metar( self ): """ PsycheWeather.get_metar() grab all weather information """ def wind_subst( directionlist ): """ A function to replace NE with North-East, etc. """ result = [] try: for direction in directionlist: if direction == 'N': result.append('North') elif direction == 'W': result.append('West') elif direction == 'S': result.append('South') elif direction == 'E': result.append('East') except TypeError: result = [] return string.join(result,'-') try: weather = pymetar.MetarReport(self.station) except IOError, msg: sys.stderr.write("problem accessing the weather server %s\n" % msg) else: if weather.valid: self.location = weather.getStationName() self.time = weather.getTime() self.direction = wind_subst(weather.getWindCompass()) self.speed = weather.getWindSpeed() if weather.getTemperatureCelsius() is not None: self.temp = weather.getTemperatureCelsius() if weather.getDewPointCelsius() is not None: self.dew = weather.getDewPointCelsius() if weather.getHumidity() is not None: self.humidity = weather.getHumidity() if weather.getVisibilityKilometers() is not None: self.visibility = weather.getVisibilityKilometers() if weather.getPressure() is not None: self.pressure = weather.getPressure() self.weather = weather.getSkyConditions() else: sys.stder.write("station is down.") def get_location( self): return self.location def get_weather( self ): return "weather is " + self.weather def get_direction( self ): return "wind direction is from the %s" % self.direction def get_speed( self ): return "wind speed is %s ms" % self.speed def get_visibility( self ): return "visibility is %.1f Km" % self.visibility def get_temp( self ): return "temperature is %.1f celsius" % self.temp def get_dew( self ): return "dew point is %.1f celsius" % self.dew def get_humidity( self ): return "humidity is %.0f%%" % self.humidity def get_pressure( self ): return "pressure is %.0f hPa" % self.pressure def get_metar_time( self ): return "time is %s" % self.time def get_report( self ): """ PsycheWeather.get_report() Returns a natural language string reporting the weather. """ template=\ """ the weather conditions in %s at %s is as follows: %s. visibility is %.1f kilometers. temperature is %.1f celsius. humidity at %.0f percent. dew point at %.1f celsius. pressure at %.0f millimeters mercury.\n """ _time = (self.time.split())[1:] _time = _time[0][:2].lstrip('0')+' '+_time[0][2:4]+' '+_time[1] if self.direction: _wind = 'the wind is %s meters per second from the %s'%( self.speed, self.direction) else: _wind = 'the winds are calm' return template%(self.location, _time, _wind, self.visibility, self.temp, self.humidity, self.dew, self.pressure) ################################################################################ if __name__ == '__main__': pp = PsycheWeather(['get_report','kvny']) print pp.get_result()