Imported from archive.
[weather.git] / weather
1 #!/usr/bin/env python
2 # distributions may wish to edit the above to refer to a specific interpreter
3 # path, such as #!/usr/bin/python
4
5 # Copyright (c) 2006-2010 Jeremy Stanley <fungi@yuggoth.org>. Permission to
6 # use, copy, modify, and distribute this software is granted under terms
7 # provided in the LICENSE file distributed with this software.
8
9 """Wrapper utility using the weather.py module."""
10
11 # added so distributors can consistently specify a private module location
12 private_module_path = None
13 if private_module_path:
14    import sys
15    sys.path.insert(1, private_module_path)
16
17 import weather
18
19 # initialize options and configs
20 selections = weather.Selections()
21 get = selections.get
22 get_bool = selections.get_bool
23
24 # this mode just lists the aliases defined in the config
25 if get_bool("list"): print weather.list_aliases(selections.config)
26
27 # normal operation
28 else:
29    output = ""
30    for argument in selections.arguments:
31       if get_bool("conditions", argument) or not (
32          get_bool("alert", argument) or get_bool("forecast", argument)
33       ):
34          partial = weather.get_metar(
35             id=get("id", argument),
36             verbose=get_bool("verbose", argument),
37             quiet=get_bool("quiet", argument),
38             headers=get("headers", argument),
39             murl=get("murl", argument),
40             imperial=get_bool("imperial", argument),
41             metric=get_bool("metric", argument)
42          )
43          if partial: output += partial + "\n"
44       if get_bool("forecast", argument) or not (
45          get_bool("alert", argument) or get_bool("conditions", argument)
46       ):
47          partial = weather.get_forecast(
48             city=get("city", argument),
49             st=get("st", argument),
50             verbose=get_bool("verbose", argument),
51             quiet=get_bool("quiet", argument),
52             flines=get("flines", argument),
53             furl=get("furl", argument),
54             imperial=get_bool("imperial", argument),
55             metric=get_bool("metric", argument)
56          )
57          if partial: output += partial + "\n"
58       if get_bool("alert", argument) or not (
59          get_bool("conditions", argument) or get_bool("forecast", argument)
60       ):
61          alert_text = ""
62          for atype in get("atypes", argument).split(","):
63             for zone in get("zones", argument).split(","):
64                partial = weather.get_alert(
65                   zone=zone,
66                   verbose=get_bool("verbose", argument),
67                   quiet=get_bool("quiet", argument),
68                   atype=atype,
69                   aurl=get("aurl", argument),
70                   imperial=get_bool("imperial", argument),
71                   metric=get_bool("metric", argument)
72                )
73                if partial: alert_text += partial + "\n"
74          if not alert_text: alert_text = "(no current alerts for your zones)\n"
75          output += alert_text
76       output = output.strip()
77       if output: print( output )