diff --git a/lib/globals.py b/lib/globals.py index 94c3a32..4af832a 100644 --- a/lib/globals.py +++ b/lib/globals.py @@ -1,112 +1,13 @@ """ -Global configuration of the project. -All hardcoded pathes and other data should be -(theoretically) here. +Global functions that our used everywhere in the project. +Please, no global variables here. +For the configuration related things see `config.py` """ + from __future__ import print_function import sys import logging -import os -import yaml -from pygments.styles import get_all_styles - -DOCKERIZED = False # set to True if the service is running in a Docker container - -SERVER_ADDRESS = '0.0.0.0' -SERVER_PORT = 8002 - -MYDIR = os.path.abspath(os.path.join(__file__, '..', '..')) -_CONF_FILE = os.path.join(MYDIR, 'etc/config.yaml') - -LOCAL_REPOSITORIES = os.path.join(os.environ['HOME'], '.cheat.sh', 'upstream') - -if DOCKERIZED: - REDISHOST = 'redis' -else: - REDISHOST = 'localhost' - -ANSI2HTML = os.path.join(MYDIR, "share/ansi2html.sh") - -LOG_FILE = os.path.join(MYDIR, 'log/main.log') -FILE_QUERIES_LOG = os.path.join(MYDIR, 'log/queries.log') - -TEMPLATES = os.path.join(MYDIR, 'share/templates') -STATIC = os.path.join(MYDIR, 'share/static') -PATH_VIM_ENVIRONMENT = os.path.join(MYDIR, 'share/vim') - -USE_OS_PACKAGES = True # set to False if you pull cheat sheets repositories from GitHub -if USE_OS_PACKAGES: - PATH_CHEAT_SHEETS_SPOOL = "/home/igor/cheat.sheets/spool/" - PATH_LEARNXINY = "/home/igor/git/github.com/adambard/learnxinyminutes-docs" -else: - PATH_CHEAT_SHEETS_SPOOL = os.path.join(MYDIR, "cheatsheets/spool/") - PATH_LEARNXINY = os.path.join(MYDIR, "cheatsheets/learnxinyminutes-docs") - -GITHUB_REPOSITORY = { - "late.nz" : 'chubin/late.nz', - "cheat.sheets" : 'chubin/cheat.sheets', - "cheat.sheets dir" : 'chubin/cheat.sheets', - "tldr" : 'tldr-pages/tldr', - "cheat" : 'chrisallenlane/cheat', - "learnxiny" : 'adambard/learnxinyminutes-docs', - "internal" : '', - "search" : '', - "unknown" : '', -} - -CONFIG = { - 'adapters.active': [ - "tldr", - "cheat", - "fosdem", - "translation", - "rosetta", - "late.nz", - "question", - "cheat.sheets", - "cheat.sheets dir", - "learnxiny", - ], - 'adapters.mandatory': [ - "search", - ], - 'routing.table': [ - ("^$", "search"), - ("^[^/]*/rosetta(/|$)", "rosetta"), - ("^:", "internal"), - ("/:list$", "internal"), - ("/$", "cheat.sheets dir"), - ("", "cheat.sheets"), - ("", "cheat"), - ("", "tldr"), - ("", "late.nz"), - ("", "fosdem"), - ("^[^/]*$", "unknown"), - ("", "learnxiny"), - ("^[a-z][a-z]-[a-z][a-z]$", "translation"), - ] - } - -MAX_SEARCH_LEN = 20 - -# -# Reading configuration from etc/config.yaml -# config overrides default settings -# -if os.path.exists(_CONF_FILE): - _CONFIG = yaml.load(_CONF_FILE, Loader=yaml.SafeLoader) - if 'server' in _CONFIG: - _SERVER_CONFIG = _CONFIG['server'] - if 'address' in _SERVER_CONFIG: - SERVER_ADDRESS = _SERVER_CONFIG['address'] - if 'port' in _SERVER_CONFIG: - SERVER_ADDRESS = _SERVER_CONFIG['port'] - - -COLOR_STYLES = sorted(list(get_all_styles())) - -MALFORMED_RESPONSE_HTML_PAGE = open(os.path.join(STATIC, 'malformed-response.html')).read() def fatal(text): """ @@ -121,7 +22,7 @@ def error(text): """ Log error `text` and produce a RuntimeError exception """ - if not text.startswith('Too many queries'): + if not text.startswith("Too many queries"): print(text) logging.error("ERROR %s", text) raise RuntimeError(text) @@ -130,6 +31,6 @@ def log(text): """ Log error `text` (if it does not start with 'Too many queries') """ - if not text.startswith('Too many queries'): + if not text.startswith("Too many queries"): print(text) logging.info(text)