wttr.in/lib/globals.py

97 lines
2.6 KiB
Python
Raw Normal View History

2018-10-05 20:37:16 +02:00
"""
global configuration of the project
"""
2017-03-31 20:38:35 +02:00
import logging
import os
2018-10-05 20:37:16 +02:00
MYDIR = os.path.abspath(os.path.dirname(os.path.dirname('__file__')))
2017-03-31 20:38:35 +02:00
2018-10-05 20:19:27 +02:00
GEOLITE = os.path.join(MYDIR, 'data', "GeoLite2-City.mmdb")
2017-03-31 20:38:35 +02:00
WEGO = "/home/igor/go/bin/we-lang"
2018-10-23 18:53:34 +02:00
PYPHOON = "/home/igor/pyphoon/bin/pyphoon-lolcat"
2017-03-31 20:38:35 +02:00
2018-10-27 00:17:16 +02: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 20:38:35 +02:00
2018-10-05 20:37:16 +02: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 20:38:35 +02:00
2018-10-05 20:37:16 +02: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 18:59:17 +02:00
IATA_CODES_FILE = os.path.join(MYDIR, 'share/list-of-iata-codes.txt')
2017-03-31 20:38:35 +02:00
2018-10-05 20:37:16 +02:00
TEMPLATES = os.path.join(MYDIR, 'share/templates')
STATIC = os.path.join(MYDIR, 'share/static')
2017-03-31 20:38:35 +02: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 12:11:59 +02: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)
2018-10-03 22:46:23 +02:00
LISTEN_HOST = ""
LISTEN_PORT = 8002
2018-10-23 20:04:42 +02:00
PROXY_HOST = "127.0.0.1"
PROXY_PORT = 5001
2018-10-27 00:17:16 +02:00
PROXY_CACHEDIR = os.path.join(_DATADIR, "cache/proxy-wwo/")
2018-10-23 20:04:42 +02:00
2018-10-05 20:37:16 +02:00
MY_EXTERNAL_IP = '5.9.243.187'
2018-10-05 20:19:27 +02:00
PLAIN_TEXT_AGENTS = [
"curl",
"httpie",
"lwp-request",
"wget",
"python-requests"
]
PLAIN_TEXT_PAGES = [':help', ':bash.function', ':translation']
2018-10-26 19:35:13 +02:00
_IP2LOCATION_KEY_FILE = os.environ['HOME'] + '/.ip2location.key'
if os.path.exists(_IP2LOCATION_KEY_FILE):
IP2LOCATION_KEY = open(_IP2LOCATION_KEY_FILE, 'r').read().strip()
2018-10-23 20:04:42 +02:00
2018-10-27 23:54:37 +02:00
_WWO_KEY_FILE = os.environ['HOME'] + '/.wwo.key'
if os.path.exists(_WWO_KEY_FILE):
WWO_KEY = open(_WWO_KEY_FILE, 'r').read().strip()
2017-03-31 20:38:35 +02:00
def error(text):
2018-10-05 20:37:16 +02:00
"log error `text` and raise a RuntimeError exception"
2017-03-31 20:38:35 +02:00
if not text.startswith('Too many queries'):
print text
2018-10-05 20:37:16 +02:00
logging.error("ERROR %s", text)
2017-03-31 20:38:35 +02:00
raise RuntimeError(text)
def log(text):
2018-10-05 20:37:16 +02:00
"log error `text` and do not raise any exceptions"
2017-03-31 20:38:35 +02:00
if not text.startswith('Too many queries'):
print text
logging.info(text)
2018-10-03 18:59:17 +02:00
def get_help_file(lang):
2018-10-05 20:37:16 +02:00
"Return help file for `lang`"
2018-10-03 18:59:17 +02: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 20:37:16 +02:00
return HELP_FILE