1212
1313
1414def get_penify_config () -> Path :
15- """
16- Get the home directory for the .penify configuration file.
17- This function searches for the .penify file in the current directory
18- and its parent directories until it finds it or reaches the home directory.
15+ """Get the home directory for the .penify configuration file.
16+
17+ This function searches for the `.penify` file in the current directory
18+ and its parent directories until it finds it or reaches the home
19+ directory. If not found, it creates the `.penify` directory and an empty
20+ `config.json` file.
21+
22+ Returns:
23+ Path: The path to the `config.json` file within the `.penify` directory.
1924 """
2025 current_dir = os .getcwd ()
2126 home_dir = recursive_search_git_folder (current_dir )
@@ -43,8 +48,19 @@ def get_penify_config() -> Path:
4348
4449
4550def save_llm_config (model , api_base , api_key ):
46- """
47- Save LLM configuration settings in the .penify file.
51+ """Save LLM configuration settings in the .penify file.
52+
53+ It reads the existing configuration from the .penify file if it exists,
54+ updates or adds the LLM configuration with the provided model, API base,
55+ and API key, and then writes the updated configuration back to the file.
56+
57+ Args:
58+ model (str): The name of the language model.
59+ api_base (str): The base URL for the API.
60+ api_key (str): The API key for authentication.
61+
62+ Returns:
63+ bool: True if the LLM configuration was successfully saved, False otherwise.
4864 """
4965
5066 penify_file = get_penify_config ()
@@ -75,8 +91,19 @@ def save_llm_config(model, api_base, api_key):
7591 return False
7692
7793def save_jira_config (url , username , api_token ):
78- """
79- Save JIRA configuration settings in the .penify file.
94+ """Save JIRA configuration settings in the .penify file.
95+
96+ This function reads existing JIRA configuration from the .penify file,
97+ updates or adds new JIRA configuration details, and writes it back to
98+ the file.
99+
100+ Args:
101+ url (str): The URL of the JIRA instance.
102+ username (str): The username for accessing the JIRA instance.
103+ api_token (str): The API token used for authentication.
104+
105+ Returns:
106+ bool: True if the configuration was successfully saved, False otherwise.
80107 """
81108 from penify_hook .utils import recursive_search_git_folder
82109
@@ -108,8 +135,15 @@ def save_jira_config(url, username, api_token):
108135 return False
109136
110137def get_llm_config ():
111- """
112- Get LLM configuration from the .penify file.
138+ """Retrieve LLM configuration from the .penify file.
139+
140+ This function reads the .penify configuration file and extracts the LLM
141+ settings. If the file does not exist or contains invalid JSON, it
142+ returns an empty dictionary.
143+
144+ Returns:
145+ dict: A dictionary containing the LLM configuration, or an empty dictionary if
146+ the file is missing or invalid.
113147 """
114148 config_file = get_penify_config ()
115149 if config_file .exists ():
@@ -123,8 +157,15 @@ def get_llm_config():
123157 return {}
124158
125159def get_jira_config ():
126- """
127- Get JIRA configuration from the .penify file.
160+ """Get JIRA configuration from the .penify file.
161+
162+ This function reads the JIRA configuration from a JSON file specified in
163+ the .penify file. If the .penify file exists and contains valid JSON
164+ with a 'jira' key, it returns the corresponding configuration.
165+ Otherwise, it returns an empty dictionary.
166+
167+ Returns:
168+ dict: The JIRA configuration or an empty dictionary if not found or invalid.
128169 """
129170 config_file = get_penify_config ()
130171 if config_file .exists ():
@@ -138,8 +179,16 @@ def get_jira_config():
138179 return {}
139180
140181def config_llm_web ():
141- """
142- Open a web browser interface for configuring LLM settings.
182+ """Open a web browser interface for configuring LLM settings.
183+
184+ This function starts a temporary HTTP server that serves an HTML
185+ template for configuring Large Language Model (LLM) settings. It handles
186+ GET and POST requests to retrieve the current configuration, save new
187+ configurations, and suppress log messages. The server runs on a random
188+ port between 30000 and 50000, and it is accessible via a URL like
189+ http://localhost:<redirect_port>. The function opens this URL in the
190+ default web browser for configuration. Once configured, the server shuts
191+ down.
143192 """
144193 redirect_port = random .randint (30000 , 50000 )
145194 server_url = f"http://localhost:{ redirect_port } "
@@ -148,6 +197,15 @@ def config_llm_web():
148197
149198 class ConfigHandler (http .server .SimpleHTTPRequestHandler ):
150199 def do_GET (self ):
200+ """Handle HTTP GET requests.
201+
202+ This function processes incoming GET requests and sends appropriate
203+ responses based on the requested path. It serves an HTML template for
204+ the root path ("/") and returns a JSON response with the current LLM
205+ configuration for the "/get_config" path. For any other paths, it
206+ returns a "Not Found" error.
207+ """
208+
151209 if self .path == "/" :
152210 self .send_response (200 )
153211 self .send_header ("Content-type" , "text/html" )
@@ -189,6 +247,17 @@ def do_GET(self):
189247 self .wfile .write (b"Not Found" )
190248
191249 def do_POST (self ):
250+ """Handle POST requests on the /save endpoint.
251+
252+ This method processes incoming POST requests to save language model
253+ configuration data. It extracts the necessary parameters from the
254+ request body, saves the configuration using the provided details, and
255+ then schedules the server to shut down after a successful save.
256+
257+ Args:
258+ self (HTTPRequestHandler): The instance of the HTTPRequestHandler class handling the request.
259+ """
260+
192261 if self .path == "/save" :
193262 content_length = int (self .headers ['Content-Length' ])
194263 post_data = self .rfile .read (content_length )
@@ -240,8 +309,13 @@ def log_message(self, format, *args):
240309 print ("Configuration completed." )
241310
242311def config_jira_web ():
243- """
244- Open a web browser interface for configuring JIRA settings.
312+ """Open a web browser interface for configuring JIRA settings.
313+
314+ This function sets up a simple HTTP server using Python's built-in
315+ `http.server` module to handle GET and POST requests. The server serves
316+ an HTML page for configuration and handles saving the JIRA configuration
317+ details through API tokens and URLs. Upon successful configuration, it
318+ shuts down the server gracefully.
245319 """
246320 redirect_port = random .randint (30000 , 50000 )
247321 server_url = f"http://localhost:{ redirect_port } "
@@ -250,6 +324,14 @@ def config_jira_web():
250324
251325 class ConfigHandler (http .server .SimpleHTTPRequestHandler ):
252326 def do_GET (self ):
327+ """Handle GET requests for different paths.
328+
329+ This function processes GET requests based on the path requested. It
330+ serves an HTML template for the root path, returns a JSON configuration
331+ for a specific endpoint, and handles any other paths by returning a 404
332+ error.
333+ """
334+
253335 if self .path == "/" :
254336 self .send_response (200 )
255337 self .send_header ("Content-type" , "text/html" )
@@ -291,6 +373,16 @@ def do_GET(self):
291373 self .wfile .write (b"Not Found" )
292374
293375 def do_POST (self ):
376+ """Handle HTTP POST requests to save JIRA configuration.
377+
378+ This method processes incoming POST requests to save JIRA configuration
379+ details. It reads JSON data from the request body, extracts necessary
380+ parameters (URL, username, API token, and verify), saves the
381+ configuration using the `save_jira_config` function, and responds with
382+ success or error messages. If an exception occurs during the process, it
383+ sends a 500 Internal Server Error response.
384+ """
385+
294386 if self .path == "/save" :
295387 content_length = int (self .headers ['Content-Length' ])
296388 post_data = self .rfile .read (content_length )
@@ -345,8 +437,11 @@ def log_message(self, format, *args):
345437 print ("Configuration completed." )
346438
347439def get_token ():
348- """
349- Get the token based on priority.
440+ """Get the token based on priority from environment variables or
441+ configuration files.
442+
443+ Returns:
444+ str: The API token if found, otherwise None.
350445 """
351446 import os
352447 env_token = os .getenv ('PENIFY_API_TOKEN' )
0 commit comments