From 5520552f52c2a68bcdf135cd2e626efe1ee6d97b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 20 Feb 2017 20:16:18 +0100 Subject: Get METAR station from ~/.config/metar if not specified MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/weather.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'src/weather.py') diff --git a/src/weather.py b/src/weather.py index 268dcbf..c21ebfa 100644 --- a/src/weather.py +++ b/src/weather.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) +# Copyright © 2014, 2017 Mattias Andrée (maandree@member.fsf.org) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -20,7 +20,7 @@ from subprocess import Popen, PIPE -def weather(station, downloader = None): +def weather(station = None, downloader = None): ''' Get a brief weather report @@ -29,8 +29,10 @@ def weather(station, downloader = None): received it. Additionally some airports do not update while closed, and updates while closed are less accurate. - @param station:str The station's International Civil Aviation - Organization airport code + @param station:str? The station's International Civil Aviation + Organization airport code. If `None`, + $HOME/.config/metar is read, with fallback to + ~/.config/metar and /etc/metar. @param downloader:(url:str)?→list A function that, with an URL as input, returns a command to download the file at the URL to stdout @return :(sky:str, visiblity:(:int, :float)?, weather:list)? @@ -43,6 +45,23 @@ def weather(station, downloader = None): be `None`. The weather is a list that can, and often is, empty. `None` is return if observation data cannot be downloaded. ''' + ## Get station + if station is None: + import os + try: + home = os.environ['HOME'] + with open('%s/.config/metar' % home, 'rb') as file: + station = file.read() + except: + try: + import pwd + home = pwd.getpwuid(os.getuid()).pw_dir + with open('%s/.config/metar' % home, 'rb') as file: + station = file.read() + except: + with open('/etc/metar' % home, 'rb') as file: + station = file.read() + station = station.decode('utf-8', 'replace').split('\n')[0] ## URI of METAR #url = 'http://weather.noaa.gov/pub/data/observations/metar/decoded/%s.TXT' url = 'http://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT' -- cgit v1.2.3-70-g09d2