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-2012 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
22 # this mode just lists the aliases defined in the config
23 if selections.get_bool("list"):
24     print( weather.list_aliases(selections.config) )
25
26 # this mode lists details of aliases defined in the config
27 elif selections.get_bool("longlist"):
28     print( weather.list_aliases(selections.config, detail=True) )
29
30 # this mode builds the correlation data files
31 elif selections.get_bool("build_sets"):
32     weather.correlate()
33
34 # if no arguments were provided
35 elif not selections.arguments:
36     import sys
37
38     # substitute defaults if we have any
39     if selections.config.has_option("default", "defargs"):
40         sys.argv += selections.config.get("default", "defargs").split(",")
41         selections = weather.Selections()
42
43     # otherwise be helpful
44     else:
45         sys.argv += ("--help",)
46         selections = weather.Selections()
47
48 # these modes analyze correlations
49 if selections.get_bool("info"):
50     weather.guess(
51         selections.arguments[0],
52         path=selections.get("setpath"),
53         info=selections.get_bool("info"),
54         cache_search=(
55             selections.get_bool("cache") \
56                 and selections.get_bool("cache_search")
57         ),
58         cacheage=selections.getint("cacheage"),
59         cachedir=selections.get("cachedir")
60     )
61
62 # normal operation
63 else:
64     output = ""
65     for argument in selections.arguments:
66         if selections.get_bool("conditions", argument) or not (
67             selections.get_bool("alert", argument) \
68                 or selections.get_bool("forecast", argument)
69             ):
70             partial = weather.get_metar(
71                 uri=selections.get("metar", argument),
72                 verbose=selections.get_bool("verbose", argument),
73                 quiet=selections.get_bool("quiet", argument),
74                 headers=selections.get("headers", argument),
75                 imperial=selections.get_bool("imperial", argument),
76                 metric=selections.get_bool("metric", argument),
77                 cache_data=(
78                     selections.get_bool("cache") \
79                         and selections.get_bool("cache_data")
80                 ),
81                 cacheage=selections.getint("cacheage"),
82                 cachedir=selections.get("cachedir")
83             )
84             if partial: output += partial + "\n"
85         if selections.get_bool("forecast", argument) \
86             or selections.get_bool("alert", argument):
87             alert_text = ""
88             if selections.get_bool("alert", argument):
89                 atypes = selections.get("atypes", argument).split(",")
90             else:
91                 atypes = []
92             if selections.get_bool("forecast", argument):
93                 atypes = ["zone_forecast"] + atypes
94             for atype in atypes:
95                 partial = weather.get_alert(
96                     uri=selections.get(atype, argument),
97                     verbose=selections.get_bool("verbose", argument),
98                     quiet=selections.get_bool("quiet", argument),
99                     cache_data=(
100                         selections.get_bool("cache") \
101                             and selections.get_bool("cache_data")
102                     ),
103                     cacheage=selections.getint("cacheage"),
104                     cachedir=selections.get("cachedir")
105                 )
106                 if partial:
107                     alert_text += "***** %s *****\n%s\n" % (
108                         atype.replace("_", " ").title(),
109                         partial
110                     )
111             if not alert_text:
112                 alert_text = "(no current alerts for this zone)\n"
113             output += alert_text
114     output = output.strip()
115     if output: print( output )