wttr.in/lib/globals.py

125 lines
3.2 KiB
Python
Raw Normal View History

2018-10-05 18:37:16 +00:00
"""
global configuration of the project
2019-05-10 15:46:44 +00:00
External environment variables:
WTTR_MYDIR
WTTR_GEOLITE
WTTR_WEGO
WTTR_LISTEN_HOST
WTTR_LISTEN_PORT
2018-10-05 18:37:16 +00:00
"""
from __future__ import print_function
2018-10-05 18:37:16 +00:00
2017-03-31 18:38:35 +00:00
import logging
import os
2018-10-05 18:37:16 +00:00
MYDIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__')))
2017-03-31 18:38:35 +00:00
2019-05-10 15:46:44 +00:00
if "WTTR_GEOLITE" in os.environ:
GEOLITE = os.environ["WTTR_GEOLITE"]
else:
GEOLITE = os.path.join(MYDIR, 'data', "GeoLite2-City.mmdb")
WEGO = os.environ.get("WTTR_WEGO", "/home/igor/go/bin/we-lang")
2018-10-23 16:53:34 +00:00
PYPHOON = "/home/igor/pyphoon/bin/pyphoon-lolcat"
2017-03-31 18:38:35 +00:00
2018-10-26 22:17:16 +00:00
_DATADIR = "/wttr.in"
_LOGDIR = "/wttr.in/log"
CACHEDIR = os.path.join(_DATADIR, "cache/wego/")
IP2LCACHE = os.path.join(_DATADIR, "cache/ip2l/")
PNG_CACHE = os.path.join(_DATADIR, "cache/png")
LOG_FILE = os.path.join(_LOGDIR, 'main.log')
2017-03-31 18:38:35 +00:00
2018-10-05 18:37:16 +00:00
ALIASES = os.path.join(MYDIR, "share/aliases")
ANSI2HTML = os.path.join(MYDIR, "share/ansi2html.sh")
BLACKLIST = os.path.join(MYDIR, "share/blacklist")
2017-03-31 18:38:35 +00:00
2018-10-05 18:37:16 +00:00
HELP_FILE = os.path.join(MYDIR, 'share/help.txt')
BASH_FUNCTION_FILE = os.path.join(MYDIR, 'share/bash-function.txt')
TRANSLATION_FILE = os.path.join(MYDIR, 'share/translation.txt')
TEST_FILE = os.path.join(MYDIR, 'share/test-NAME.txt')
2018-10-03 16:59:17 +00:00
IATA_CODES_FILE = os.path.join(MYDIR, 'share/list-of-iata-codes.txt')
2017-03-31 18:38:35 +00:00
2018-10-05 18:37:16 +00:00
TEMPLATES = os.path.join(MYDIR, 'share/templates')
STATIC = os.path.join(MYDIR, 'share/static')
2017-03-31 18:38:35 +00:00
NOT_FOUND_LOCATION = "not found"
DEFAULT_LOCATION = "oymyakon"
MALFORMED_RESPONSE_HTML_PAGE = open(os.path.join(STATIC, 'malformed-response.html')).read()
2018-10-07 10:11:59 +00:00
GEOLOCATOR_SERVICE = 'http://localhost:8004'
# number of queries from the same IP address is limited
# (minute, hour, day) limitations:
QUERY_LIMITS = (300, 3600, 24*3600)
2019-05-10 15:46:44 +00:00
LISTEN_HOST = os.environ.get("WTTR_LISTEN_HOST", "")
try:
LISTEN_PORT = int(os.environ.get("WTTR_LISTEN_PORT"))
except (TypeError, ValueError):
LISTEN_PORT = 8002
2018-10-03 20:46:23 +00:00
2018-10-23 18:04:42 +00:00
PROXY_HOST = "127.0.0.1"
PROXY_PORT = 5001
2018-10-26 22:17:16 +00:00
PROXY_CACHEDIR = os.path.join(_DATADIR, "cache/proxy-wwo/")
2018-10-23 18:04:42 +00:00
2018-10-05 18:37:16 +00:00
MY_EXTERNAL_IP = '5.9.243.187'
2018-10-05 18:19:27 +00:00
PLAIN_TEXT_AGENTS = [
"curl",
"httpie",
"lwp-request",
"wget",
"python-requests",
"OpenBSD ftp"
2018-10-05 18:19:27 +00:00
]
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation']
2018-10-26 17:35:13 +00:00
_IP2LOCATION_KEY_FILE = os.environ['HOME'] + '/.ip2location.key'
2019-05-09 15:40:22 +00:00
IP2LOCATION_KEY = None
2018-10-26 17:35:13 +00:00
if os.path.exists(_IP2LOCATION_KEY_FILE):
IP2LOCATION_KEY = open(_IP2LOCATION_KEY_FILE, 'r').read().strip()
2018-10-23 18:04:42 +00:00
2018-10-27 21:54:37 +00:00
_WWO_KEY_FILE = os.environ['HOME'] + '/.wwo.key'
2019-05-10 15:46:44 +00:00
WWO_KEY = "key-is-not-specified"
2018-10-27 21:54:37 +00:00
if os.path.exists(_WWO_KEY_FILE):
WWO_KEY = open(_WWO_KEY_FILE, 'r').read().strip()
2017-03-31 18:38:35 +00:00
def error(text):
2018-10-05 18:37:16 +00:00
"log error `text` and raise a RuntimeError exception"
2017-03-31 18:38:35 +00:00
if not text.startswith('Too many queries'):
print(text)
2018-10-05 18:37:16 +00:00
logging.error("ERROR %s", text)
2017-03-31 18:38:35 +00:00
raise RuntimeError(text)
def log(text):
2018-10-05 18:37:16 +00:00
"log error `text` and do not raise any exceptions"
2017-03-31 18:38:35 +00:00
if not text.startswith('Too many queries'):
print(text)
2017-03-31 18:38:35 +00:00
logging.info(text)
2018-11-02 17:11:49 +00:00
def debug_log(text):
"""
Write `text` to the debug log
"""
with open('/tmp/wttr.in-debug.log', 'a') as f_debug:
f_debug.write(text+'\n')
2018-10-03 16:59:17 +00:00
def get_help_file(lang):
2018-10-05 18:37:16 +00:00
"Return help file for `lang`"
2018-10-03 16:59:17 +00:00
help_file = os.path.join(MYDIR, 'share/translations/%s-help.txt' % lang)
if os.path.exists(help_file):
return help_file
2018-10-05 18:37:16 +00:00
return HELP_FILE