import argparse
import configparser
import json
import os
import pyowm
import string
import sys

from i3weather.log import logger


DEFAULT_FORMAT = '{short_status} - {temp} F - {wind_speed} Mph'


def error(message, *args):
    logger.critical('ERROR: ' + message, *args)
    sys.exit(1)


def print_line(message):
    sys.stdout.write(message + '\n')
    sys.stdout.flush()


def read_line():
    try:
        line = sys.stdin.readline().strip()
        if not line:
            sys.exit(3)
        return line
    except KeyboardInterrupt:
        sys.exit()


def get_weather(info, owm, location, str_format, unit):
    obs = owm.weather_at_place(location)
    weather = obs.get_weather()
    wind = weather.get_wind()
    loc = obs.get_location()
    values = {
        'temp': weather.get_temperature(unit)['temp'],
        'temp_unit': unit,
        'pressure': weather.get_pressure()['press'],
        'status': string.capwords(weather.get_detailed_status()),
        'short_status': string.capwords(weather.get_status()),
        'wind_dir': wind['deg'],
        'wind_speed': wind['speed'],
        'location': loc.get_name(),
    }

    return {
        'name': 'weather',
        'full_text': str_format.format(**values)
    }
    return info


def loop_weather(owm, location, str_format, unit, position):
    # Version line
    print_line(read_line())
    # Line starting infinite json array
    print_line(read_line())

    while True:
        line, prefix = read_line(), ''
        if line.startswith(','):
            line, prefix = line[1:], ','

        j = json.loads(line)
        weather = get_weather(j, owm, location, str_format, unit)
        j.insert(position, weather)
        print_line(prefix + json.dumps(j))


def get_value(config, section, key, default=None):
    try:
        return config.get(section, key)
    except (configparser.NoOptionError, configparser.NoSectionError) as ex:
        if default is not None:
            return default
        raise ex


def main():
    parser = argparse.ArgumentParser(
        'Adds OpenWeatherMap information to i3status'
    )
    parser.add_argument('--config', default='~/.i3weather')
    args = parser.parse_args()

    try:
        with open(os.path.expanduser(args.config)) as fh:
            config = configparser.SafeConfigParser()
            config.read_file(fh)
    except FileNotFoundError:
        error('Could not find configuration file at %s', args.config)

    try:
        api_key = config.get('owm', 'api_key')
    except configparser.NoOptionError:
        error('API key is not set in config')

    owm = pyowm.OWM(api_key)
    try:
        owm.is_API_online()
    except pyowm.exceptions.api_call_error.APICallError:
        error('Invalid API key in config')

    try:
        location = config.get('owm', 'location')
    except configparser.NoOptionError:
        error('No location set in config')

    str_format = get_value(config, 'i3weather', 'format', DEFAULT_FORMAT)
    temp_unit = get_value(config, 'owm', 'temp_unit', 'fahrenheit').lower()
    if temp_unit not in ('fahrenheit', 'celsius'):
        error('Invalid temperature unit %s', temp_unit)

    position = int(get_value(config, 'i3weather', 'position', 0))
    loop_weather(owm, location, str_format, temp_unit, position)

@@ -579,19 +579,31 @@
 r(info, 
-owm
+weather_manager
 , locati
@@ -635,19 +635,31 @@
   obs = 
-owm
+weather_manager
 .weather
@@ -700,21 +700,15 @@
 obs.
-get_
 weather
-()
 %0A   
@@ -723,20 +723,16 @@
 weather.
-get_
 wind()%0A 
@@ -748,20 +748,16 @@
 obs.
-get_
 location
 ()%0A 
@@ -752,18 +752,16 @@
 location
-()
 %0A    val
@@ -792,20 +792,16 @@
 weather.
-get_
 temperat
@@ -878,20 +878,16 @@
 her.
-get_
 pressure
 ()%5B'
@@ -882,18 +882,16 @@
 pressure
-()
 %5B'press'
@@ -935,20 +935,16 @@
 weather.
-get_
 detailed
@@ -946,26 +946,24 @@
 ailed_status
-()
 ),%0A        '
@@ -1005,20 +1005,14 @@
 her.
-get_
 status
-()
 ),%0A 
@@ -1108,18 +1108,12 @@
 loc.
-get_
 name
-()
 ,%0A  
@@ -1239,35 +1239,47 @@
 ef loop_weather(
-owm
+weather_manager
 , location, str_
@@ -1611,19 +1611,31 @@
 ther(j, 
-owm
+weather_manager
 , locati
@@ -2298,12 +2298,8 @@
 ser.
-Safe
 Conf
@@ -2622,139 +2622,46 @@
     
-try:%0A        owm.is_API_online()%0A    except pyowm.exceptions.api_call_error.APICallError:%0A        error('Invalid API key in config'
+weather_manager = owm.weather_manager(
 )%0A%0A 
@@ -3141,19 +3141,31 @@
 weather(
-owm
+weather_manager
 , locati
@@ -3197,12 +3197,52 @@
 , position)%0A
+%0A%0Aif __name__ == %22__main__%22:%0A    main()%0A

make i3weather work with pyOWM versions 3.0 and above.