simplify archivebox.constants to just use benedict instead of kludgy NamedTuple

This commit is contained in:
Nick Sweeting 2024-09-26 02:36:59 -07:00
parent 80d3def206
commit 45736036e0
No known key found for this signature in database

View file

@ -2,8 +2,8 @@ __package__ = 'archivebox'
import os import os
from types import MappingProxyType import re
from typing import Set, Dict, NamedTuple, Tuple from typing import Dict
from pathlib import Path from pathlib import Path
from benedict import benedict from benedict import benedict
@ -14,10 +14,10 @@ from .misc.logging import DEFAULT_CLI_COLORS
###################### Config ########################## ###################### Config ##########################
class ConstantsConfig(NamedTuple):
VERSION: str = archivebox.__version__ VERSION: str = archivebox.VERSION
TIMEZONE: str = 'UTC'
DEFAULT_CLI_COLORS: Dict[str, str] = DEFAULT_CLI_COLORS DEFAULT_CLI_COLORS: Dict[str, str] = DEFAULT_CLI_COLORS
DISABLED_CLI_COLORS: Dict[str, str] = benedict({k: '' for k in DEFAULT_CLI_COLORS}) DISABLED_CLI_COLORS: Dict[str, str] = benedict({k: '' for k in DEFAULT_CLI_COLORS})
@ -29,7 +29,6 @@ class ConstantsConfig(NamedTuple):
USER_PLUGINS_DIR_NAME: str = 'user_plugins' USER_PLUGINS_DIR_NAME: str = 'user_plugins'
CUSTOM_TEMPLATES_DIR_NAME: str = 'user_templates' CUSTOM_TEMPLATES_DIR_NAME: str = 'user_templates'
DATA_DIR: Path = archivebox.DATA_DIR
ARCHIVE_DIR_NAME: str = 'archive' ARCHIVE_DIR_NAME: str = 'archive'
SOURCES_DIR_NAME: str = 'sources' SOURCES_DIR_NAME: str = 'sources'
PERSONAS_DIR_NAME: str = 'personas' PERSONAS_DIR_NAME: str = 'personas'
@ -67,7 +66,9 @@ class ConstantsConfig(NamedTuple):
ROBOTS_TXT_FILENAME: str = 'robots.txt' ROBOTS_TXT_FILENAME: str = 'robots.txt'
FAVICON_FILENAME: str = 'favicon.ico' FAVICON_FILENAME: str = 'favicon.ico'
STATICFILE_EXTENSIONSSTATICFILE_EXTENSIONS: frozenset[str] = frozenset(( ALLOWDENYLIST_REGEX_FLAGS: int = re.IGNORECASE | re.UNICODE | re.MULTILINE
STATICFILE_EXTENSIONS: frozenset[str] = frozenset((
# 99.999% of the time, URLs ending in these extensions are static files # 99.999% of the time, URLs ending in these extensions are static files
# that can be downloaded as-is, not html pages that need to be rendered # that can be downloaded as-is, not html pages that need to be rendered
'gif', 'jpeg', 'jpg', 'png', 'tif', 'tiff', 'wbmp', 'ico', 'jng', 'bmp', 'gif', 'jpeg', 'jpg', 'png', 'tif', 'tiff', 'wbmp', 'ico', 'jng', 'bmp',
@ -155,7 +156,7 @@ class ConstantsConfig(NamedTuple):
"sonic", # created by docker bind mount "sonic", # created by docker bind mount
)) ))
CODE_LOCATIONS = MappingProxyType(benedict({ CODE_LOCATIONS = benedict({
'PACKAGE_DIR': { 'PACKAGE_DIR': {
'path': (archivebox.PACKAGE_DIR).resolve(), 'path': (archivebox.PACKAGE_DIR).resolve(),
'enabled': True, 'enabled': True,
@ -181,9 +182,9 @@ class ConstantsConfig(NamedTuple):
'enabled': True, 'enabled': True,
'is_valid': CUSTOM_TEMPLATES_DIR.is_dir(), 'is_valid': CUSTOM_TEMPLATES_DIR.is_dir(),
}, },
})) })
DATA_LOCATIONS = MappingProxyType(benedict({ DATA_LOCATIONS = benedict({
"OUTPUT_DIR": { "OUTPUT_DIR": {
"path": archivebox.DATA_DIR.resolve(), "path": archivebox.DATA_DIR.resolve(),
"enabled": True, "enabled": True,
@ -233,17 +234,9 @@ class ConstantsConfig(NamedTuple):
"enabled": True, "enabled": True,
"is_valid": CACHE_DIR.is_dir(), "is_valid": CACHE_DIR.is_dir(),
}, },
})) })
def items(self):
return self._asdict().items()
def keys(self):
return self._asdict().keys()
def values(self):
return self._asdict().values()
CONSTANTS = ConstantsConfig()
CONSTANTS = benedict({key: value for key, value in globals().items() if key.isupper()})
CONSTANTS_CONFIG = CONSTANTS CONSTANTS_CONFIG = CONSTANTS