1+ """ Main API module for CodeGraph. """
12import os
2- from api import *
33from pathlib import Path
44from functools import wraps
55from dotenv import load_dotenv
6+ from flask import Flask , request , jsonify
67
8+ from api .analyzers .source_analyzer import SourceAnalyzer
9+ from api .git_utils import git_utils
10+ from api .graph import Graph , get_repos , graph_exists
11+ from api .info import get_repo_info
12+ from api .llm import ask
713from api .project import Project
814from .auto_complete import prefix_search
9- from flask import Flask , request , jsonify
1015
1116# Load environment variables from .env file
1217load_dotenv ()
1722 format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' )
1823logger = logging .getLogger (__name__ )
1924
20- # Function to verify the token
2125SECRET_TOKEN = os .getenv ('SECRET_TOKEN' )
2226def verify_token (token ):
27+ """ Verify the token provided in the request """
2328 return token == SECRET_TOKEN or (token is None and SECRET_TOKEN is None )
2429
25- # Decorator to protect routes with token authentication
2630def token_required (f ):
31+ """ Decorator to protect routes with token authentication """
2732 @wraps (f )
2833 def decorated_function (* args , ** kwargs ):
2934 token = request .headers .get ('Authorization' ) # Get token from header
@@ -34,8 +39,8 @@ def decorated_function(*args, **kwargs):
3439
3540app = Flask (__name__ )
3641
37- # Decorator to protect routes with public access
3842def public_access (f ):
43+ """ Decorator to protect routes with public access """
3944 @wraps (f )
4045 def decorated_function (* args , ** kwargs ):
4146 public = os .environ .get ("CODE_GRAPH_PUBLIC" , "0" ) # Get public access setting
@@ -65,7 +70,7 @@ def graph_entities():
6570 return jsonify ({"status" : "Missing 'repo' parameter" }), 400
6671
6772 if not graph_exists (repo ):
68- logging .error (f "Missing project { repo } " )
73+ logging .error ("Missing project %s" , repo )
6974 return jsonify ({"status" : f"Missing project { repo } " }), 400
7075
7176 try :
@@ -75,7 +80,7 @@ def graph_entities():
7580 # Retrieve a sub-graph of up to 500 entities
7681 sub_graph = g .get_sub_graph (500 )
7782
78- logging .info (f "Successfully retrieved sub-graph for repo: { repo } " )
83+ logging .info ("Successfully retrieved sub-graph for repo: %s" , repo )
7984 response = {
8085 'status' : 'success' ,
8186 'entities' : sub_graph
@@ -84,7 +89,7 @@ def graph_entities():
8489 return jsonify (response ), 200
8590
8691 except Exception as e :
87- logging .error (f "Error retrieving sub-graph for repo '{ repo } ': { e } " )
92+ logging .error ("Error retrieving sub-graph for repo '%s ': %s" , repo , e )
8893 return jsonify ({"status" : "Internal server error" }), 500
8994
9095
@@ -118,7 +123,7 @@ def get_neighbors():
118123
119124 # Validate repo exists
120125 if not graph_exists (repo ):
121- logging .error (f "Missing project { repo } " )
126+ logging .error ("Missing project %s" , repo )
122127 return jsonify ({"status" : f"Missing project { repo } " }), 400
123128
124129 # Initialize the graph with the provided repository
@@ -128,7 +133,7 @@ def get_neighbors():
128133 neighbors = g .get_neighbors (node_ids )
129134
130135 # Log and return the neighbors
131- logging .info (f "Successfully retrieved neighbors for node IDs { node_ids } in repo '{ repo } '." )
136+ logging .info ("Successfully retrieved neighbors for node IDs %s in repo '%s '." , node_ids , repo )
132137
133138 response = {
134139 'status' : 'success' ,
@@ -153,12 +158,12 @@ def auto_complete():
153158 # Validate that 'repo' is provided
154159 repo = data .get ('repo' )
155160 if repo is None :
156- return jsonify ({'status' : f 'Missing mandatory parameter "repo"' }), 400
161+ return jsonify ({'status' : 'Missing mandatory parameter "repo"' }), 400
157162
158163 # Validate that 'prefix' is provided
159164 prefix = data .get ('prefix' )
160165 if prefix is None :
161- return jsonify ({'status' : f 'Missing mandatory parameter "prefix"' }), 400
166+ return jsonify ({'status' : 'Missing mandatory parameter "prefix"' }), 400
162167
163168 # Validate repo exists
164169 if not graph_exists (repo ):
@@ -219,7 +224,7 @@ def repo_info():
219224 # Validate the 'repo' parameter
220225 repo = data .get ('repo' )
221226 if repo is None :
222- return jsonify ({'status' : f 'Missing mandatory parameter "repo"' }), 400
227+ return jsonify ({'status' : 'Missing mandatory parameter "repo"' }), 400
223228
224229 # Initialize the graph with the provided repository name
225230 g = Graph (repo )
@@ -231,7 +236,7 @@ def repo_info():
231236 if stats is None or info is None :
232237 return jsonify ({'status' : f'Missing repository "{ repo } "' }), 400
233238
234- stats |= info
239+ stats |= info
235240
236241 # Create a response
237242 response = {
@@ -265,24 +270,24 @@ def find_paths():
265270 # Validate 'repo' parameter
266271 repo = data .get ('repo' )
267272 if repo is None :
268- return jsonify ({'status' : f 'Missing mandatory parameter "repo"' }), 400
273+ return jsonify ({'status' : 'Missing mandatory parameter "repo"' }), 400
269274
270275 # Validate 'src' parameter
271276 src = data .get ('src' )
272277 if src is None :
273- return jsonify ({'status' : f 'Missing mandatory parameter "src"' }), 400
278+ return jsonify ({'status' : 'Missing mandatory parameter "src"' }), 400
274279 if not isinstance (src , int ):
275280 return jsonify ({'status' : "src node id must be int" }), 400
276281
277282 # Validate 'dest' parameter
278283 dest = data .get ('dest' )
279284 if dest is None :
280- return jsonify ({'status' : f 'Missing mandatory parameter "dest"' }), 400
285+ return jsonify ({'status' : 'Missing mandatory parameter "dest"' }), 400
281286 if not isinstance (dest , int ):
282287 return jsonify ({'status' : "dest node id must be int" }), 400
283288
284289 if not graph_exists (repo ):
285- logging .error (f "Missing project { repo } " )
290+ logging .error ("Missing project %s" , repo )
286291 return jsonify ({"status" : f"Missing project { repo } " }), 400
287292
288293 # Initialize graph with provided repo and credentials
@@ -299,18 +304,20 @@ def find_paths():
299304@app .route ('/chat' , methods = ['POST' ])
300305@token_required # Apply token authentication decorator
301306def chat ():
307+ """ Endpoint to chat with the CodeGraph language model. """
308+
302309 # Get JSON data from the request
303310 data = request .get_json ()
304311
305312 # Validate 'repo' parameter
306313 repo = data .get ('repo' )
307314 if repo is None :
308- return jsonify ({'status' : f 'Missing mandatory parameter "repo"' }), 400
315+ return jsonify ({'status' : 'Missing mandatory parameter "repo"' }), 400
309316
310317 # Get optional 'label' and 'relation' parameters
311318 msg = data .get ('msg' )
312319 if msg is None :
313- return jsonify ({'status' : f 'Missing mandatory parameter "msg"' }), 400
320+ return jsonify ({'status' : 'Missing mandatory parameter "msg"' }), 400
314321
315322 answer = ask (repo , msg )
316323
@@ -348,7 +355,7 @@ def analyze_folder():
348355
349356 # Validate path exists and is a directory
350357 if not os .path .isdir (path ):
351- logging .error (f "Path '{ path } ' does not exist or is not a directory" )
358+ logging .error ("Path '%s ' does not exist or is not a directory" , path )
352359 return jsonify ({"status" : "Invalid path: must be an existing directory" }), 400
353360
354361 # Validate ignore list contains valid paths
@@ -392,8 +399,8 @@ def analyze_repo():
392399 data = request .get_json ()
393400 url = data .get ('repo_url' )
394401 if url is None :
395- return jsonify ({'status' : f 'Missing mandatory parameter "url"' }), 400
396- logger .debug (f 'Received repo_url: { url } ' )
402+ return jsonify ({'status' : 'Missing mandatory parameter "url"' }), 400
403+ logger .debug ('Received repo_url: %s' , url )
397404
398405 ignore = data .get ('ignore' , [])
399406
@@ -425,12 +432,12 @@ def switch_commit():
425432 # Validate that 'repo' is provided
426433 repo = data .get ('repo' )
427434 if repo is None :
428- return jsonify ({'status' : f 'Missing mandatory parameter "repo"' }), 400
435+ return jsonify ({'status' : 'Missing mandatory parameter "repo"' }), 400
429436
430437 # Validate that 'commit' is provided
431438 commit = data .get ('commit' )
432439 if commit is None :
433- return jsonify ({'status' : f 'Missing mandatory parameter "commit"' }), 400
440+ return jsonify ({'status' : 'Missing mandatory parameter "commit"' }), 400
434441
435442 # Attempt to switch the repository to the specified commit
436443 git_utils .switch_commit (repo , commit )
@@ -440,4 +447,4 @@ def switch_commit():
440447 'status' : 'success'
441448 }
442449
443- return jsonify (response ), 200
450+ return jsonify (response ), 200
0 commit comments