diff options
author | Mattias Andrée <maandree@kth.se> | 2016-04-19 18:03:45 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2016-04-19 18:03:45 +0200 |
commit | 0113fdc300b36f436dbcf5326c8f652996c5326a (patch) | |
tree | 292df4136dcf887a237f2ceb7129fb6ecc4deffc | |
parent | bump year (diff) | |
download | xpybar-0113fdc300b36f436dbcf5326c8f652996c5326a.tar.gz xpybar-0113fdc300b36f436dbcf5326c8f652996c5326a.tar.bz2 xpybar-0113fdc300b36f436dbcf5326c8f652996c5326a.tar.xz |
Weather: can get METAR station from ~/.config/metar and /etc/metar
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | src/plugins/weather.py | 19 |
1 files changed, 17 insertions, 2 deletions
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') |