2021-10-15 00:58:13 +00:00
|
|
|
from app.utils.misc import read_config_bool
|
2021-03-21 01:21:41 +00:00
|
|
|
from flask import current_app
|
|
|
|
import os
|
2021-10-21 02:01:04 +00:00
|
|
|
import re
|
2021-03-21 01:21:41 +00:00
|
|
|
|
|
|
|
|
2020-05-12 23:15:53 +00:00
|
|
|
class Config:
|
|
|
|
def __init__(self, **kwargs):
|
2021-03-21 01:21:41 +00:00
|
|
|
app_config = current_app.config
|
2021-03-28 18:24:57 +00:00
|
|
|
self.url = os.getenv('WHOOGLE_CONFIG_URL', '')
|
2021-04-26 15:37:03 +00:00
|
|
|
self.lang_search = os.getenv('WHOOGLE_CONFIG_SEARCH_LANGUAGE', '')
|
2021-03-28 18:24:57 +00:00
|
|
|
self.lang_interface = os.getenv('WHOOGLE_CONFIG_LANGUAGE', '')
|
2021-04-12 20:40:59 +00:00
|
|
|
self.style = os.getenv(
|
|
|
|
'WHOOGLE_CONFIG_STYLE',
|
|
|
|
open(os.path.join(app_config['STATIC_FOLDER'],
|
|
|
|
'css/variables.css')).read())
|
2021-05-07 15:45:53 +00:00
|
|
|
self.block = os.getenv('WHOOGLE_CONFIG_BLOCK', '')
|
2021-10-21 02:01:04 +00:00
|
|
|
self.block_title = os.getenv('WHOOGLE_CONFIG_BLOCK_TITLE', '')
|
|
|
|
self.block_url = os.getenv('WHOOGLE_CONFIG_BLOCK_URL', '')
|
2021-03-28 18:24:57 +00:00
|
|
|
self.ctry = os.getenv('WHOOGLE_CONFIG_COUNTRY', '')
|
2021-06-28 14:26:51 +00:00
|
|
|
self.theme = os.getenv('WHOOGLE_CONFIG_THEME', '')
|
2021-04-14 14:42:41 +00:00
|
|
|
self.safe = read_config_bool('WHOOGLE_CONFIG_SAFE')
|
2021-06-28 14:26:51 +00:00
|
|
|
self.dark = read_config_bool('WHOOGLE_CONFIG_DARK') # deprecated
|
2021-04-14 14:42:41 +00:00
|
|
|
self.alts = read_config_bool('WHOOGLE_CONFIG_ALTS')
|
|
|
|
self.nojs = read_config_bool('WHOOGLE_CONFIG_NOJS')
|
|
|
|
self.tor = read_config_bool('WHOOGLE_CONFIG_TOR')
|
2021-03-28 18:24:57 +00:00
|
|
|
self.near = os.getenv('WHOOGLE_CONFIG_NEAR', '')
|
2021-04-14 14:42:41 +00:00
|
|
|
self.new_tab = read_config_bool('WHOOGLE_CONFIG_NEW_TAB')
|
2021-04-16 14:16:14 +00:00
|
|
|
self.view_image = read_config_bool('WHOOGLE_CONFIG_VIEW_IMAGE')
|
2021-04-14 14:42:41 +00:00
|
|
|
self.get_only = read_config_bool('WHOOGLE_CONFIG_GET_ONLY')
|
2021-10-25 21:49:09 +00:00
|
|
|
self.accept_language = False
|
2021-04-16 14:16:14 +00:00
|
|
|
|
2020-12-17 21:06:47 +00:00
|
|
|
self.safe_keys = [
|
|
|
|
'lang_search',
|
|
|
|
'lang_interface',
|
|
|
|
'ctry',
|
2021-06-28 14:26:51 +00:00
|
|
|
'dark',
|
|
|
|
'theme'
|
2020-12-17 21:06:47 +00:00
|
|
|
]
|
2020-05-12 23:15:53 +00:00
|
|
|
|
2021-04-12 20:40:59 +00:00
|
|
|
# Skip setting custom config if there isn't one
|
|
|
|
if kwargs:
|
2021-04-26 15:37:03 +00:00
|
|
|
mutable_attrs = self.get_mutable_attrs()
|
|
|
|
for attr in mutable_attrs:
|
|
|
|
if attr in kwargs.keys():
|
2021-04-12 20:40:59 +00:00
|
|
|
setattr(self, attr, kwargs[attr])
|
2021-04-26 15:37:03 +00:00
|
|
|
elif attr not in kwargs.keys() and mutable_attrs[attr] == bool:
|
|
|
|
setattr(self, attr, False)
|
2020-05-12 23:15:53 +00:00
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
|
|
return getattr(self, name)
|
|
|
|
|
|
|
|
def __setitem__(self, name, value):
|
|
|
|
return setattr(self, name, value)
|
|
|
|
|
|
|
|
def __delitem__(self, name):
|
|
|
|
return delattr(self, name)
|
|
|
|
|
|
|
|
def __contains__(self, name):
|
2020-05-23 20:27:23 +00:00
|
|
|
return hasattr(self, name)
|
2020-11-11 05:40:49 +00:00
|
|
|
|
2021-04-12 20:40:59 +00:00
|
|
|
def get_mutable_attrs(self):
|
2021-04-26 15:37:03 +00:00
|
|
|
return {name: type(attr) for name, attr in self.__dict__.items()
|
2021-04-12 20:40:59 +00:00
|
|
|
if not name.startswith("__")
|
2021-04-14 14:42:41 +00:00
|
|
|
and (type(attr) is bool or type(attr) is str)}
|
2021-04-12 20:40:59 +00:00
|
|
|
|
2020-11-11 05:40:49 +00:00
|
|
|
def is_safe_key(self, key) -> bool:
|
|
|
|
"""Establishes a group of config options that are safe to set
|
|
|
|
in the url.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
key (str) -- the key to check against
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool -- True/False depending on if the key is in the "safe"
|
|
|
|
array
|
|
|
|
"""
|
|
|
|
|
2020-12-17 21:06:47 +00:00
|
|
|
return key in self.safe_keys
|
2020-11-11 05:40:49 +00:00
|
|
|
|
2021-05-24 21:03:02 +00:00
|
|
|
def get_localization_lang(self):
|
|
|
|
"""Returns the correct language to use for localization, but falls
|
|
|
|
back to english if not set.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str -- the localization language string
|
|
|
|
"""
|
|
|
|
if (self.lang_interface and
|
|
|
|
self.lang_interface in current_app.config['TRANSLATIONS']):
|
|
|
|
return self.lang_interface
|
|
|
|
|
|
|
|
return 'lang_en'
|
|
|
|
|
2020-11-11 05:40:49 +00:00
|
|
|
def from_params(self, params) -> 'Config':
|
|
|
|
"""Modify user config with search parameters. This is primarily
|
|
|
|
used for specifying configuration on a search-by-search basis on
|
|
|
|
public instances.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
params -- the url arguments (can be any deemed safe by is_safe())
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Config -- a modified config object
|
|
|
|
"""
|
|
|
|
for param_key in params.keys():
|
|
|
|
if not self.is_safe_key(param_key):
|
|
|
|
continue
|
|
|
|
self[param_key] = params.get(param_key)
|
|
|
|
return self
|