-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconfig_paths.py
More file actions
130 lines (109 loc) · 5.68 KB
/
Copy pathconfig_paths.py
File metadata and controls
130 lines (109 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# config_paths.py
import os
from pathlib import Path
import configparser
import logging
# Initialize the logger for this module
logger = logging.getLogger('TelegramBotLogger') # Ensure that 'TelegramBotLogger' is initialized in main.py
# Define the base directory (the parent of the 'src' directory)
BASE_DIR = Path(__file__).resolve().parents[1]
# Path to the configuration file
CONFIG_PATH = BASE_DIR / 'config' / 'config.ini'
# Initialize the ConfigParser
config = configparser.ConfigParser()
# Initialize variables with default values
logs_directory = 'logs'
LOG_FILE_PATH = BASE_DIR / logs_directory / 'bot.log'
CHAT_LOG_FILE_PATH = BASE_DIR / logs_directory / 'chat.log'
TOKEN_USAGE_FILE_PATH = BASE_DIR / logs_directory / 'token_usage.json'
CHAT_LOG_MAX_SIZE = 10 * 1024 * 1024 # 10 MB
ELASTICSEARCH_ENABLED = False
ELASTICSEARCH_HOST = 'localhost'
ELASTICSEARCH_PORT = 9200
ELASTICSEARCH_USERNAME = ''
ELASTICSEARCH_PASSWORD = ''
# Default NWS settings
NWS_USER_AGENT = 'ChatKekeWeather/1.0 (flyingfathead@protonmail.com)'
NWS_RETRIES = 0
NWS_RETRY_DELAY = 2
# read the reminders db
data_directory_name = 'data' # Default name for data directory
REMINDERS_DB_FILENAME = 'reminders.db' # Default name for the reminders DB file
# Attempt to read the configuration file
if CONFIG_PATH.exists():
try:
config.read(CONFIG_PATH)
logger.info(f"Configuration file found and loaded from {CONFIG_PATH}.")
# Read logs directory
logs_directory = config['DEFAULT'].get('LogsDirectory', 'logs')
# Define the logs directory path
LOGS_DIR = BASE_DIR / logs_directory
# Ensure the logs directory exists
LOGS_DIR.mkdir(parents=True, exist_ok=True)
# Read data directory name from config
data_directory_name = config['DEFAULT'].get('DataDirectory', 'data')
# Update log file paths
LOG_FILE_PATH = LOGS_DIR / config['DEFAULT'].get('LogFile', 'bot.log')
CHAT_LOG_FILE_PATH = LOGS_DIR / config['DEFAULT'].get('ChatLogFile', 'chat.log')
TOKEN_USAGE_FILE_PATH = LOGS_DIR / 'token_usage.json'
# Read ChatLogMaxSizeMB and convert to bytes
ChatLogMaxSizeMB = config['DEFAULT'].getint('ChatLogMaxSizeMB', fallback=10)
CHAT_LOG_MAX_SIZE = ChatLogMaxSizeMB * 1024 * 1024
# Read Elasticsearch configurations
if 'Elasticsearch' in config:
ELASTICSEARCH_ENABLED = config['Elasticsearch'].getboolean('ElasticsearchEnabled', fallback=False)
ELASTICSEARCH_HOST = config['Elasticsearch'].get('Host', fallback='localhost')
ELASTICSEARCH_PORT = config['Elasticsearch'].getint('Port', fallback=9200)
ELASTICSEARCH_SCHEME = config.get('Elasticsearch', 'ELASTICSEARCH_SCHEME', fallback='http')
ELASTICSEARCH_USERNAME = config['Elasticsearch'].get('Username', fallback='')
ELASTICSEARCH_PASSWORD = config['Elasticsearch'].get('Password', fallback='')
logger.info(f"Elasticsearch Enabled: {ELASTICSEARCH_ENABLED}")
else:
# Elasticsearch section missing
ELASTICSEARCH_ENABLED = False
ELASTICSEARCH_HOST = 'localhost'
ELASTICSEARCH_PORT = 9200
ELASTICSEARCH_SCHEME = 'http'
ELASTICSEARCH_USERNAME = ''
ELASTICSEARCH_PASSWORD = ''
logger.warning("Elasticsearch section missing in config.ini. Using default Elasticsearch settings.")
# NWS Configuration
if 'NWS' in config:
NWS_USER_AGENT = config['NWS'].get('NWSUserAgent', fallback='ChatKekeWeather/1.0 (flyingfathead@protonmail.com)')
NWS_RETRIES = config['NWS'].getint('NWSRetries', fallback=0)
NWS_RETRY_DELAY = config['NWS'].getint('NWSRetryDelay', fallback=2)
FETCH_NWS_FORECAST = config['NWS'].getboolean('FetchNWSForecast', fallback=True)
FETCH_NWS_ALERTS = config['NWS'].getboolean('FetchNWSAlerts', fallback=True)
NWS_ONLY_ELIGIBLE_COUNTRIES = config['NWS'].getboolean('NwsOnlyEligibleCountries', fallback=True)
NWS_ELIGIBLE_COUNTRIES = config['NWS'].get('NwsEligibleCountries', fallback='US, PR, GU, AS, VI, MP').split(', ')
logger.info(f"NWS Config: User-Agent={NWS_USER_AGENT}, Retries={NWS_RETRIES}, Retry Delay={NWS_RETRY_DELAY}, Fetch Forecast={FETCH_NWS_FORECAST}, Fetch Alerts={FETCH_NWS_ALERTS}")
else:
logger.warning("NWS section not found in config.ini. Using default NWS settings.")
except Exception as e:
# Handle exceptions during config parsing
logger.error(f"Error reading configuration file: {e}")
else:
# config.ini not found
logger.warning(f"Configuration file NOT found at {CONFIG_PATH}. Using default settings. This is NOT a good idea!")
# Ensure the logs directory exists
LOGS_DIR = BASE_DIR / logs_directory
LOGS_DIR.mkdir(parents=True, exist_ok=True)
# Define log file paths
LOG_FILE_PATH = LOGS_DIR / 'bot.log'
CHAT_LOG_FILE_PATH = LOGS_DIR / 'chat.log'
TOKEN_USAGE_FILE_PATH = LOGS_DIR / 'token_usage.json'
# CHAT_LOG_MAX_SIZE already set to 10 MB
# Elasticsearch settings already set to defaults
# Define the Data Directory path
DATA_DIR = BASE_DIR / data_directory_name
# Ensure the data directory exists
try:
DATA_DIR.mkdir(parents=True, exist_ok=True)
except OSError as e:
logger.error(f"Could not create data directory {DATA_DIR}: {e}")
# Path for the reminders database
REMINDERS_DB_PATH = DATA_DIR / REMINDERS_DB_FILENAME
logger.info(f"Reminders database path set to: {REMINDERS_DB_PATH}")
# Define paths for token files
TOKEN_FILE_PATH = BASE_DIR / 'config' / 'bot_token.txt'
API_TOKEN_PATH = BASE_DIR / 'config' / 'api_token.txt'