Skip to content

Commit 162f582

Browse files
committed
fix some lint errors
1 parent a04ac1a commit 162f582

File tree

4 files changed

+46
-35
lines changed

4 files changed

+46
-35
lines changed

api/analyzers/source_analyzer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def supported_types(self) -> list[str]:
3838
"""
3939
"""
4040
return list(analyzers.keys())
41-
41+
4242
def create_entity_hierarchy(self, entity: Entity, file: File, analyzer: AbstractAnalyzer, graph: Graph):
4343
types = analyzer.get_entity_types()
4444
stack = list(entity.node.children)

api/auto_complete.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .graph import Graph
22

33
def prefix_search(repo: str, prefix: str) -> str:
4+
""" Returns a list of all entities in the repository that start with the given prefix. """
45
g = Graph(repo)
56
return g.prefix_search(prefix)
6-

api/git_utils/git_utils.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import os
21
import json
32
import logging
43

5-
from pygit2 import Commit, Diff
4+
from pygit2 import Diff
65
from ..info import *
76
from pygit2.repository import Repository
87
from pygit2.enums import DeltaStatus, CheckoutStrategy
@@ -16,6 +15,7 @@
1615
logging.basicConfig(level=logging.DEBUG, format='%(filename)s - %(asctime)s - %(levelname)s - %(message)s')
1716

1817
def GitRepoName(repo_name):
18+
""" Returns the git repository name """
1919
return "{" + repo_name + "}_git"
2020

2121
def is_ignored(file_path: str, ignore_list: List[str]) -> bool:
@@ -32,7 +32,11 @@ def is_ignored(file_path: str, ignore_list: List[str]) -> bool:
3232

3333
return any(file_path.startswith(ignore) for ignore in ignore_list)
3434

35-
def classify_changes(diff: Diff, repo: Repository, supported_types: list[str], ignore_list: List[str]) -> tuple[list[Path], list[Path], list[Path]]:
35+
def classify_changes(
36+
diff: Diff,
37+
repo: Repository,
38+
supported_types: list[str],
39+
ignore_list: List[str]) -> tuple[list[Path], list[Path], list[Path]]:
3640
"""
3741
Classifies changes into added, deleted, and modified files.
3842
@@ -48,17 +52,17 @@ def classify_changes(diff: Diff, repo: Repository, supported_types: list[str], i
4852

4953
for change in diff.deltas:
5054
if change.status == DeltaStatus.ADDED and not is_ignored(change.new_file.path, ignore_list):
51-
logging.debug(f"new file: {change.new_file}")
55+
logging.debug("new file: %s", change.new_file)
5256
file_path = Path(f"{repo.workdir}/{change.new_file.path}")
5357
if file_path.suffix in supported_types:
5458
added.append(file_path)
5559
if change.status == DeltaStatus.DELETED and not is_ignored(change.old_file.path, ignore_list):
56-
logging.debug(f"deleted file: {change.old_file.path}")
60+
logging.debug("deleted file: %s", change.old_file.path)
5761
file_path = Path(f"{repo.workdir}/{change.old_file.path}")
5862
if file_path.suffix in supported_types:
5963
deleted.append(file_path)
6064
if change.status == DeltaStatus.MODIFIED and not is_ignored(change.new_file.path, ignore_list):
61-
logging.debug(f"change file: {change.new_file.path}")
65+
logging.debug("change file: %s", change.new_file.path)
6266
file_path = Path(f"{repo.workdir}/{change.new_file.path}")
6367
if file_path.suffix in supported_types:
6468
modified.append(file_path)
@@ -83,7 +87,7 @@ def build_commit_graph(path: str, analyzer: SourceAnalyzer, repo_name: str, igno
8387
ignore_list = []
8488

8589
# Copy the graph into a temporary graph
86-
logging.info(f"Cloning source graph {repo_name} -> {repo_name}_tmp")
90+
logging.info("Cloning source graph %s -> %s_tmp", repo_name, repo_name)
8791
# Will be deleted at the end of this function
8892
g = Graph(repo_name).clone(repo_name + "_tmp")
8993
g.enable_backlog()

api/index.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
""" Main API module for CodeGraph. """
12
import os
2-
from api import *
33
from pathlib import Path
44
from functools import wraps
55
from 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
713
from api.project import Project
814
from .auto_complete import prefix_search
9-
from flask import Flask, request, jsonify
1015

1116
# Load environment variables from .env file
1217
load_dotenv()
@@ -17,13 +22,13 @@
1722
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
1823
logger = logging.getLogger(__name__)
1924

20-
# Function to verify the token
2125
SECRET_TOKEN = os.getenv('SECRET_TOKEN')
2226
def 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
2630
def 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

3540
app = Flask(__name__)
3641

37-
# Decorator to protect routes with public access
3842
def 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
301306
def 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

Comments
 (0)