11import logging
2+ import os
23from datetime import date
34from importlib import metadata
45
56import click
67from dotenv import load_dotenv
78from lightman_ai .ai .utils import AGENT_CHOICES
8- from lightman_ai .constants import DEFAULT_CONFIG_FILE , DEFAULT_CONFIG_SECTION , DEFAULT_ENV_FILE
9+ from lightman_ai .constants import (
10+ DEFAULT_AGENT ,
11+ DEFAULT_CONFIG_FILE ,
12+ DEFAULT_CONFIG_SECTION ,
13+ DEFAULT_ENV_FILE ,
14+ DEFAULT_LOG_LEVEL ,
15+ DEFAULT_SCORE ,
16+ DEFAULT_TIME_ZONE ,
17+ VERBOSE_LOG_LEVEL ,
18+ )
919from lightman_ai .core .config import FileConfig , FinalConfig , PromptConfig
1020from lightman_ai .core .exceptions import ConfigNotFoundError , InvalidConfigError , PromptNotFoundError
1121from lightman_ai .core .sentry import configure_sentry
12- from lightman_ai .core .settings import Settings
1322from lightman_ai .exceptions import MultipleDateSourcesError
1423from lightman_ai .main import lightman
1524from lightman_ai .utils import get_start_date
1625
1726logger = logging .getLogger ("lightman" )
27+ logging .basicConfig (
28+ format = "%(asctime)s [%(levelname)s] %(name)s: %(message)s" ,
29+ datefmt = "%Y-%m-%d %H:%M:%S" ,
30+ )
1831
1932
2033def get_version () -> str :
@@ -72,6 +85,7 @@ def entry_point() -> None:
7285@click .option ("--start-date" , type = click .DateTime (formats = ["%Y-%m-%d" ]), help = "Start date to retrieve articles" )
7386@click .option ("--today" , is_flag = True , help = "Retrieve articles from today." )
7487@click .option ("--yesterday" , is_flag = True , help = "Retrieve articles from yesterday." )
88+ @click .option ("-v" , is_flag = True , help = "Be more verbose on output." )
7589def run (
7690 agent : str ,
7791 prompt : str ,
@@ -85,18 +99,30 @@ def run(
8599 start_date : date | None ,
86100 today : bool ,
87101 yesterday : bool ,
102+ v : bool ,
88103) -> int :
89104 """
90105 Entrypoint of the application.
91106
92107 Holds no logic. It loads the configuration, calls the main method and returns 0 when succesful .
93108 """
94109 load_dotenv (env_file or DEFAULT_ENV_FILE )
95- configure_sentry ()
96110
97- settings = Settings .try_load_from_file (env_file )
111+ if v :
112+ logger .setLevel (VERBOSE_LOG_LEVEL )
113+ else :
114+ try :
115+ env_log_level = os .getenv ("LOG_LEVEL" )
116+ log_level = env_log_level .upper () if env_log_level else DEFAULT_LOG_LEVEL
117+ logger .setLevel (log_level )
118+ except ValueError :
119+ logger .setLevel (DEFAULT_LOG_LEVEL )
120+ logger .warning ("Invalid logging level. Using default value." )
121+
122+ configure_sentry (logger .level )
123+
98124 try :
99- start_datetime = get_start_date (settings , yesterday , today , start_date )
125+ start_datetime = get_start_date (os . getenv ( "TIME_ZONE" , DEFAULT_TIME_ZONE ) , yesterday , today , start_date )
100126 except MultipleDateSourcesError as e :
101127 raise click .UsageError (e .args [0 ]) from e
102128
@@ -105,9 +131,9 @@ def run(
105131 config_from_file = FileConfig .get_config_from_file (config_section = config , path = config_file )
106132 final_config = FinalConfig .init_from_dict (
107133 data = {
108- "agent" : agent or config_from_file .agent or settings . AGENT ,
134+ "agent" : agent or config_from_file .agent or DEFAULT_AGENT ,
109135 "prompt" : prompt or config_from_file .prompt ,
110- "score_threshold" : score or config_from_file .score_threshold or settings . SCORE ,
136+ "score_threshold" : score or config_from_file .score_threshold or DEFAULT_SCORE ,
111137 "model" : model or config_from_file .model ,
112138 }
113139 )
@@ -126,5 +152,11 @@ def run(
126152 start_date = start_datetime ,
127153 )
128154 relevant_articles_metadata = [f"{ article .title } ({ article .link } )" for article in relevant_articles ]
129- logger .warning ("Found these articles: \n - %s" , "\n - " .join (relevant_articles_metadata ))
155+
156+ if relevant_articles_metadata :
157+ articles = f"Found these articles:\n * { '\n * ' .join (relevant_articles_metadata )} "
158+ click .echo (click .style (articles ))
159+ else :
160+ click .echo (click .style ("No relevant articles found." ))
161+
130162 return 0
0 commit comments