From 0113fdc300b36f436dbcf5326c8f652996c5326a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 19 Apr 2016 18:03:45 +0200 Subject: Weather: can get METAR station from ~/.config/metar and /etc/metar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/plugins/weather.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/plugins') diff --git a/src/plugins/weather.py b/src/plugins/weather.py index 75b89d1..15e8573 100644 --- a/src/plugins/weather.py +++ b/src/plugins/weather.py @@ -47,12 +47,27 @@ class Weather: ''' - def __init__(self, station): + def __init__(self, station = None): ''' Constructor - @param station:str The station's ICAO code (International Civil Aviation Organization airport code) + @param station:str? The station's ICAO code (International Civil Aviation Organization airport code), + if `None`, ~/.config/metar or /etc/metar will be used (see metar(1)) ''' + import os, pwd + if station is None: + try: + filename = os.environ['HOME'] if 'HOME' in os.environ else '' + if len(filename) == 0: + filename = pwd.getpwuid(os.getuid()).pw_dir + filename = '%s/.config/metar' % filename + if not os.path.isfile(filename): + filename = '/etc/metar' + except: + filename = '/etc/metar' + with open(filename, 'rb') as file: + station = file.read() + station = station.decode('utf-8', 'strict').split('\n')[0] self.icao = station url = 'http://weather.noaa.gov/pub/data/observations/metar/decoded/%s.TXT' % station decoded = spawn_read('curl', url).split('\n') -- cgit v1.2.3-70-g09d2