/
oweather_controller.py
129 lines (121 loc) · 4.28 KB
/
oweather_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
import sys
import os
import os.path
import argparse
from oweather_model import OpenWeatherModel
from oweather_view import OpenWeatherView
class OpenWeatherController:
def __init__(self):
self.args = self.process_arguments()
self.model = OpenWeatherModel(self.get_api_token())
self.view = OpenWeatherView(self.args.plain)
def process_arguments(self):
parser = argparse.ArgumentParser(
description="Display weather information on the command line " +
"using Open Weather Map API")
parser.add_argument(
"-v",
"--version",
action="version",
version="%(prog)s 0.0.1")
parser.add_argument(
"-k",
dest="api_token",
type=str,
default=None,
help="connect to open weather map using API key token")
parser.add_argument(
"-p",
"--plain",
default=False,
action="store_true",
help="show output in plain-text (without pretty boarders)")
forecast_group = parser.add_mutually_exclusive_group(required=False)
forecast_group.add_argument(
"-f",
"--forecast",
dest="forecast",
action="store_true",
help="show five day three hour weather forcast")
forecast_group.add_argument(
"-d",
"--days",
dest="n",
type=self.check_days,
help="show N days daily weather forcast (N >= 16)")
unit_group = parser.add_mutually_exclusive_group(required=False)
unit_group.add_argument(
"-m",
"--metric",
dest="units",
const="metric",
action="store_const",
default="metric",
help="show weather data in metric system (default)")
unit_group.add_argument(
"-i",
"--imperial",
dest="units",
const="imperial",
action="store_const",
help="show weather data in imperial system")
parser.add_argument(
"city",
help="name of city you want to get weather for",
nargs=1)
return parser.parse_args()
def check_days(self, days):
try:
d = int(days)
if d > 16 or d < 1:
raise ValueError
except:
raise argparse.ArgumentTypeError("%s is not a valid value for days"
% days)
return d
def get_api_token(self):
if self.args.api_token is None:
try:
return self.get_rc_configs()["api_key"]
except:
print "api key is not set in .oweatherrc file"
sys.exit(1)
else:
return self.args.api_token
def get_rc_configs(self):
current_config = os.path.join(os.getcwd(), ".oweatherrc")
home_config = os.path.join(os.path.expanduser("~"), ".oweatherrc")
try:
if os.path.isfile(current_config):
return self.read_rc_configs_from_file(current_config)
elif os.path.isfile(os.path.join(home_config)):
return self.read_rc_configs_from_file(home_config)
else:
raise IOError
except IOError:
print ".oweatherrc file does not exist"
sys.exit(1)
def read_rc_configs_from_file(self, file_path):
rc_configs = {}
with open(file_path) as f:
for line in f:
key, value = line.partition("=")[::2]
rc_configs[key.strip()] = value.strip()[1:-1]
return rc_configs
def get_weather(self):
city = self.args.city[0]
forecast = self.args.forecast
days = self.args.n
units = self.args.units
if forecast:
return self.model.get_five_day_forecast(city, units)
elif days:
return self.model.get_daily_forecast(city, days, units)
else:
return self.model.get_current_weather(city, units)
if __name__ == "__main__":
controller = OpenWeatherController()
data = controller.get_weather()
controller.model.print_json(data)
controller.view.print_weather(data)