Skip to content

Commit 7f7e87e

Browse files
committed
fix: Solve json encoder Neo4j datetime
1 parent 93ec411 commit 7f7e87e

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

app/apis/managers/pip_service.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ async def requires_pip_packages(pkg_name: str, version_dist: str) -> dict[str, l
6363
data = data[0].replace('(', '').replace(')', '').replace(' ', '').replace("'", '')
6464
pos = await get_first_position(data, ['<', '>', '=', '!', '~'])
6565
dist = data[:pos]
66-
ctcs = await parse_pip_constraints(data[pos:])
67-
if (
68-
dist in require_packages and
69-
isinstance(require_packages[dist], dict) and
70-
isinstance(ctcs, list)
71-
):
72-
for ctc in ctcs:
73-
if ctc not in require_packages[dist]:
74-
require_packages[dist].append(ctcs)
75-
else:
76-
if '.' not in ctcs and ctcs != '':
77-
continue
78-
require_packages[dist] = ctcs
66+
require_packages[dist] = await parse_pip_constraints(data[pos:])
7967
return require_packages
8068
return {}

app/main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from time import sleep
21
from fastapi import FastAPI, Request
32
from fastapi.exceptions import RequestValidationError
43
from fastapi.exception_handlers import (
@@ -9,19 +8,22 @@
98
from starlette.exceptions import HTTPException
109
from starlette.responses import Response
1110
from apscheduler.schedulers.background import BackgroundScheduler
11+
from time import sleep
1212
from app.controllers import nvd_updater
1313
from app.router import api_router
1414
from app.services import create_indexes
1515

16+
1617
DESCRIPTION = '''
1718
A backend for dependency graph building, atribution of vulnerabilities and reasoning
1819
over it.
1920
'''
2021

22+
2123
app = FastAPI(
2224
title='Depex',
2325
description=DESCRIPTION,
24-
version='0.5.0',
26+
version='0.6.0',
2527
contact={
2628
'name': 'Antonio Germán Márquez Trujillo',
2729
'url': 'https://github.com/GermanMT',
@@ -41,7 +43,9 @@ async def startup_event() -> None:
4143
await create_indexes()
4244
await nvd_updater()
4345
scheduler = BackgroundScheduler()
44-
scheduler.add_job(nvd_updater, 'interval', seconds=216000)
46+
scheduler.add_job(nvd_updater, 'interval', seconds=7200)
47+
# TODO: Create a job for updating exploit database
48+
# scheduler.add_job(exploit_db_updater, 'interval', seconds=86400)
4549
scheduler.start()
4650
break
4751
except:
@@ -57,6 +61,7 @@ async def custom_http_exception_handler(request: Request, exc: HTTPException) ->
5761
async def validation_exception_handler(request: Request, exc: RequestValidationError) -> Response:
5862
return await request_validation_exception_handler(request, exc)
5963

64+
6065
app.add_middleware(
6166
CORSMiddleware,
6267
allow_origins=[],
@@ -65,4 +70,5 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
6570
allow_headers=['*']
6671
)
6772

73+
6874
app.include_router(api_router)

app/utils/json_encoder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from json import JSONEncoder, loads
22
from typing import Any
33
from datetime import datetime
4+
from neo4j.time import DateTime
45
from bson import ObjectId
56

67

@@ -10,6 +11,8 @@ def default(self, o: Any) -> Any:
1011
return str(o)
1112
if isinstance(o, datetime):
1213
return str(o)
14+
if isinstance(o, DateTime):
15+
return str(o)
1316
return JSONEncoder.default(self, o)
1417

1518

app/utils/metrics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ async def mean(impacts: list[float]) -> float:
66

77
async def weighted_mean(impacts: list[float]) -> float:
88
if impacts:
9-
return sum([var**2 * 0.1 for var in impacts]) / sum([var * 0.1 for var in impacts])
9+
return sum(var**2 * 0.1 for var in impacts) / sum(var * 0.1 for var in impacts)
1010
return 0.

app/utils/parse_pip_constraints.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ async def parse_pip_constraints(raw_constraints: str) -> str:
1010
else:
1111
ctcs.append(ctc.strip())
1212
if ctcs:
13-
return await clean_pip_constraints(ctcs)
13+
clean_ctcs = await clean_pip_constraints(ctcs)
14+
if clean_ctcs:
15+
return clean_ctcs
1416
return 'any'
1517

1618

1719
async def clean_pip_constraints(raw_constraints: list[str]) -> str:
1820
constraints = []
1921
for raw_constraint in raw_constraints:
20-
if ' ' not in raw_constraint:
21-
continue
2222
if ' ' not in raw_constraint:
23+
if raw_constraint.isalpha():
24+
continue
2325
for index, char in enumerate(raw_constraint):
2426
if char.isdigit():
2527
raw_constraint = raw_constraint[:index] + ' ' + raw_constraint[index:]
@@ -30,7 +32,7 @@ async def clean_pip_constraints(raw_constraints: list[str]) -> str:
3032
version = version[:pos - 1]
3133
constraints.append('>= ' + version)
3234
constraints.append('< ' + version[:pos - 2] + str(int(version[pos - 2]) + 1))
33-
elif '=' in operator and all([symbol not in operator for symbol in ['<', '>', '~', '!']]):
35+
elif '=' in operator and all(symbol not in operator for symbol in ('<', '>', '~', '!')):
3436
constraints.append('== ' + version)
3537
elif '!=' in operator and '*' in version:
3638
pos = version.find('*')

0 commit comments

Comments
 (0)