Skip to content

Commit 1ec3f9a

Browse files
bakerboy448claude
andcommitted
refactor: add helper function for config access with default fallback
- Add get_config_with_default() helper to eliminate code duplication - Replace 7 instances of config.get(key, CONFIG_LIMITS[key]['default']) pattern - Improve code maintainability and consistency - Add validation for unknown config keys - Reduces repetitive nested dictionary access throughout codebase 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ac2bd14 commit 1ec3f9a

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

modlog_wiki_publisher.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,12 @@ def sanitize_for_markdown(text: str) -> str:
317317
return ""
318318
return str(text).replace("|", " ")
319319

320+
def get_config_with_default(config: Dict[str, Any], key: str) -> Any:
321+
"""Get config value with fallback to CONFIG_LIMITS default"""
322+
if key not in CONFIG_LIMITS:
323+
raise ValueError(f"Unknown config key: {key}")
324+
return config.get(key, CONFIG_LIMITS[key]['default'])
325+
320326
def get_action_datetime(action):
321327
"""Convert action.created_utc to datetime object regardless of input type"""
322328
if isinstance(action.created_utc, (int, float)):
@@ -571,7 +577,7 @@ def update_missing_subreddits():
571577
def cleanup_old_entries(retention_days: int):
572578
"""Remove entries older than retention_days"""
573579
if retention_days <= 0:
574-
retention_days = CONFIG_LIMITS['retention_days']['default']
580+
retention_days = CONFIG_LIMITS['retention_days']['default'] # No config object available here
575581

576582
try:
577583
conn = sqlite3.connect(DB_PATH)
@@ -616,11 +622,11 @@ def get_recent_actions_from_db(config: Dict[str, Any], force_all_actions: bool =
616622
]))
617623

618624
# Get recent actions within retention period
619-
retention_days = config.get('retention_days', CONFIG_LIMITS['retention_days']['default'])
625+
retention_days = get_config_with_default(config, 'retention_days')
620626
cutoff_timestamp = int((datetime.now() - datetime.fromtimestamp(0)).total_seconds()) - (retention_days * 86400)
621627

622628
# Limit to max wiki entries
623-
max_entries = config.get('max_wiki_entries_per_page', CONFIG_LIMITS['max_wiki_entries_per_page']['default'])
629+
max_entries = get_config_with_default(config, 'max_wiki_entries_per_page')
624630

625631
placeholders = ','.join(['?'] * len(wiki_actions))
626632
# STRICT subreddit filtering - only exact matches, no nulls
@@ -874,7 +880,7 @@ def build_wiki_content(actions: List, config: Dict[str, Any]) -> str:
874880
raise ValueError(f"Cannot build wiki content - mixed subreddit data detected: {mixed_subreddits}")
875881

876882
# Enforce wiki entry limits
877-
max_entries = config.get('max_wiki_entries_per_page', CONFIG_LIMITS['max_wiki_entries_per_page']['default'])
883+
max_entries = get_config_with_default(config, 'max_wiki_entries_per_page')
878884
if len(actions) > max_entries:
879885
logger.warning(f"Truncating wiki content to {max_entries} entries (was {len(actions)})")
880886
actions = actions[:max_entries]
@@ -1165,7 +1171,7 @@ def run_continuous_mode(reddit, config: Dict[str, Any], force: bool = False):
11651171
logger.info("Starting continuous mode...")
11661172

11671173
error_count = 0
1168-
max_errors = config.get('max_continuous_errors', CONFIG_LIMITS['max_continuous_errors']['default'])
1174+
max_errors = get_config_with_default(config, 'max_continuous_errors')
11691175
first_run_force = force
11701176

11711177
while True:
@@ -1179,10 +1185,10 @@ def run_continuous_mode(reddit, config: Dict[str, Any], force: bool = False):
11791185
update_wiki_page(reddit, config['source_subreddit'], wiki_page, content, force=first_run_force)
11801186
first_run_force = False
11811187

1182-
cleanup_old_entries(config.get('retention_days', CONFIG_LIMITS['retention_days']['default']))
1188+
cleanup_old_entries(get_config_with_default(config, 'retention_days'))
11831189

11841190
interval = validate_config_value('update_interval',
1185-
config.get('update_interval', CONFIG_LIMITS['update_interval']['default']),
1191+
get_config_with_default(config, 'update_interval'),
11861192
CONFIG_LIMITS)
11871193
logger.info(f"Waiting {interval} seconds until next update...")
11881194
time.sleep(interval)
@@ -1297,7 +1303,7 @@ def main():
12971303
wiki_page = config.get('wiki_page', 'modlog')
12981304
update_wiki_page(reddit, config['source_subreddit'], wiki_page, content, force=args.force_wiki)
12991305

1300-
cleanup_old_entries(config.get('retention_days', CONFIG_LIMITS['retention_days']['default']))
1306+
cleanup_old_entries(get_config_with_default(config, 'retention_days'))
13011307

13021308
if args.continuous:
13031309
run_continuous_mode(reddit, config, force=args.force_wiki)

0 commit comments

Comments
 (0)