Skip to content

Commit b82bcf8

Browse files
committed
Ruff and pylint fixes for pebblo/app module (daxa-ai#236)
* ruff formating and pylint fixes for pebblo/app module
1 parent 8d8dc8a commit b82bcf8

File tree

14 files changed

+1004
-723
lines changed

14 files changed

+1004
-723
lines changed

pebblo/app/api/api.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
class App:
77
"""
8-
Controller Class for all the api endpoints for App resource.
8+
Controller Class for all the api endpoints for App resource.
99
"""
10+
1011
def __init__(self, prefix: str):
1112
self.router = APIRouter(prefix=prefix)
1213

pebblo/app/api/local_ui_api.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
from pathlib import Path
12
from fastapi import APIRouter, Request
23
from fastapi.templating import Jinja2Templates
3-
from pebblo.app.service.local_ui_service import AppData
44
from fastapi.responses import FileResponse
5+
from pebblo.app.service.local_ui_service import AppData
56
from pebblo.app.enums.enums import CacheDir
67
from pebblo.app.utils.utils import get_full_path
7-
from pathlib import Path
88

9-
templates = Jinja2Templates(directory=Path(__file__).parent.parent.absolute() / "pebblo-ui")
9+
10+
templates = Jinja2Templates(
11+
directory=Path(__file__).parent.parent.absolute() / "pebblo-ui"
12+
)
1013

1114

1215
class App:
@@ -25,7 +28,7 @@ def dashboard(request: Request):
2528
{
2629
"request": request,
2730
"data": app_data.get_all_apps_details(),
28-
"proxy": CacheDir.proxy.value,
31+
"proxy": CacheDir.PROXY.value,
2932
},
3033
)
3134

@@ -37,14 +40,14 @@ def app_details(request: Request, app_name: str):
3740
{
3841
"request": request,
3942
"data": app_data.get_app_details(app_name),
40-
"proxy": CacheDir.proxy.value,
43+
"proxy": CacheDir.PROXY.value,
4144
},
4245
)
4346

4447
@staticmethod
45-
def get_report(request: Request, app_name: str):
48+
def get_report(app_name: str):
4649
# File path for app report
47-
file_path = f"{get_full_path(CacheDir.home_dir.value)}/{app_name}/{CacheDir.report_file_name.value}"
50+
file_path = f"{get_full_path(CacheDir.HOME_DIR.value)}/{app_name}/{CacheDir.REPORT_FILE_NAME.value}"
4851
# To view the file in the browser, use "inline" for the media_type
4952
headers = {"Access-Control-Expose-Headers": "Content-Disposition"}
5053
# Create a FileResponse object with the file path, media type and headers

pebblo/app/config/config.py

+24-32
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,62 @@
1+
import pathlib
12
import yaml
2-
33
from pydantic import BaseSettings, Field
4-
import pathlib
4+
55

66
# Default config value
77
dir_path = pathlib.Path().absolute()
88

9+
910
# Port BaseModel
1011
class PortConfig(BaseSettings):
11-
host: str = Field(default='localhost')
12+
host: str = Field(default="localhost")
1213
port: int = Field(default=8000)
1314

1415

1516
# Report BaseModel
1617
class ReportConfig(BaseSettings):
17-
format: str = Field(default='pdf')
18-
renderer: str = Field(default='weasyprint')
18+
format: str = Field(default="pdf")
19+
renderer: str = Field(default="weasyprint")
1920
outputDir: str = Field(dir_path)
2021

2122

2223
# Logging BaseModel
2324
class LoggingConfig(BaseSettings):
24-
level: str = Field(default='info')
25+
level: str = Field(default="info")
2526

2627

2728
# ConfigFile BaseModel
2829
class Config(BaseSettings):
29-
daemon: PortConfig
30+
daemon: PortConfig
3031
reports: ReportConfig
3132
logging: LoggingConfig
3233

33-
def load_config(path) -> Config:
34+
35+
def load_config(path) -> dict:
3436
try:
3537
# If Path does not exist in command, set default config value
3638
conf_obj = Config(
37-
daemon=PortConfig(
38-
host='localhost',
39-
port=8000
40-
),
39+
daemon=PortConfig(host="localhost", port=8000),
4140
reports=ReportConfig(
42-
format='pdf',
43-
renderer='weasyprint',
44-
outputDir='~/.pebblo'
41+
format="pdf", renderer="weasyprint", outputDir="~/.pebblo"
4542
),
46-
logging=LoggingConfig(
47-
level='info'
48-
)
43+
logging=LoggingConfig(level="info"),
4944
)
5045
if not path:
5146
# Setting Default config details
5247
return conf_obj.dict()
5348

5449
# If Path exist, set config value
55-
else:
56-
con_file = path
57-
try:
58-
with open(con_file, "r") as output:
59-
cred_json = yaml.safe_load(output)
60-
parsed_config = Config.parse_obj(cred_json)
61-
config_dict = parsed_config.dict()
62-
return config_dict
63-
except IOError as err:
64-
print(f"no credentials file found at {con_file}. Error : {err}")
65-
return conf_obj.dict()
50+
con_file = path
51+
try:
52+
with open(con_file, "r") as output:
53+
cred_json = yaml.safe_load(output)
54+
parsed_config = Config.parse_obj(cred_json)
55+
config_dict = parsed_config.dict()
56+
return config_dict
57+
except IOError as err:
58+
print(f"no credentials file found at {con_file}. Error : {err}")
59+
return conf_obj.dict()
6660

6761
except Exception as err:
68-
print(f'Error while loading config details, err: {err}')
69-
70-
62+
print(f"Error while loading config details, err: {err}")

pebblo/app/config/service.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ def __init__(self, config_details):
3737
self.app.include_router(local_ui_router_instance.router)
3838
# Fetching Details from Config File
3939
self.config_details = config_details
40-
self.port = self.config_details.get('daemon', {}).get('port', 8000)
41-
self.host = self.config_details.get('daemon', {}).get('host', 'localhost')
42-
self.log_level = self.config_details.get('logging', {}).get('level', 'info')
40+
self.port = self.config_details.get("daemon", {}).get("port", 8000)
41+
self.host = self.config_details.get("daemon", {}).get("host", "localhost")
42+
self.log_level = self.config_details.get("logging", {}).get("level", "info")
4343

4444
async def create_main_api_server(self):
4545
self.app.mount(
4646
path="/static",
47-
app=NoCacheStaticFiles(directory=Path(__file__).parent.parent.absolute() / "pebblo-ui"),
47+
app=NoCacheStaticFiles(
48+
directory=Path(__file__).parent.parent.absolute() / "pebblo-ui"
49+
),
4850
name="static",
4951
)
5052

5153
# Add config Details to Uvicorn
5254
config = uvicorn.Config(
53-
app=self.app,
54-
host=self.host,
55-
port=self.port,
56-
log_level=self.log_level)
55+
app=self.app, host=self.host, port=self.port, log_level=self.log_level
56+
)
5757
server = uvicorn.Server(config)
5858
await server.serve()
5959

pebblo/app/daemon.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
"""
2+
This is entry point for Pebblo(Pebblo Server and Local UI)
3+
"""
14
from contextlib import redirect_stderr, redirect_stdout
2-
5+
import argparse
36
from io import StringIO
47
from tqdm import tqdm
58
from pebblo.app.config.config import load_config
6-
import argparse
79

810

911
config_details = {}
@@ -14,7 +16,7 @@ def start():
1416
global config_details
1517
# For loading config file details
1618
parser = argparse.ArgumentParser(description="Pebblo CLI")
17-
parser.add_argument('--config', type=str, help="config file path")
19+
parser.add_argument("--config", type=str, help="config file path")
1820
args = parser.parse_args()
1921
path = args.config
2022
p_bar = tqdm(range(10))
@@ -50,13 +52,14 @@ def classifier_init(p_bar):
5052
p_bar.update(1)
5153

5254

53-
def server_start(config_details, p_bar):
55+
def server_start(config: dict, p_bar: tqdm):
5456
"""Start server."""
5557
p_bar.write("Pebblo server starting ...")
5658
# Starting Uvicorn Service Using config details
5759
from pebblo.app.config.service import Service
60+
5861
p_bar.update(1)
59-
svc = Service(config_details)
62+
svc = Service(config_details=config)
6063
p_bar.update(2)
6164
p_bar.close()
6265
svc.start()

pebblo/app/enums/enums.py

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
"""
2-
These are all enums related to Inspector.
2+
These are all enums related to Pebblo Server.
33
"""
44
from enum import Enum
55
from pebblo.app.daemon import config_details
66

77

88
class CacheDir(Enum):
9-
metadata_folder = "/metadata"
10-
metadata_file_path = f"{metadata_folder}/metadata.json"
11-
report_data_file_name = "report.json"
12-
report_file_name = f"pebblo_report.{config_details.get('reports', {}).get('format')}"
13-
home_dir = config_details.get('reports', {}).get('outputDir', '~/.pebblo')
14-
renderer = config_details.get('reports', {}).get('renderer')
15-
format = config_details.get('reports', {}).get('format')
16-
proxy = f"http://{config_details.get('daemon', {}).get('host')}:{config_details.get('daemon', {}).get('port')}"
17-
9+
"""
10+
Enums for cache directory
11+
"""
12+
METADATA_FOLDER = "/metadata"
13+
METADATA_FILE_PATH = f"{METADATA_FOLDER}/metadata.json"
14+
REPORT_DATA_FILE_NAME = "report.json"
15+
REPORT_FILE_NAME = (
16+
f"pebblo_report.{config_details.get('reports', {}).get('format')}"
17+
)
18+
HOME_DIR = config_details.get("reports", {}).get("outputDir", "~/.pebblo")
19+
RENDERER = config_details.get("reports", {}).get("renderer")
20+
FORMAT = config_details.get("reports", {}).get("format")
21+
PROXY = (f"http://{config_details.get('daemon', {}).get('host')}:"
22+
f"{config_details.get('daemon', {}).get('port')}")
1823

1924

2025
class ReportConstants(Enum):
21-
snippets_limit = 100
22-
top_findings_limit = 5
23-
loader_history_limit = 5
26+
"""
27+
Enums for report
28+
"""
29+
SNIPPET_LIMIT = 100
30+
TOP_FINDINGS_LIMIT = 5
31+
LOADER_HISTORY__LIMIT = 5

pebblo/app/routers/routers.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1+
"""
2+
Routes for Pebblo Server
3+
"""
14
from pebblo.app.api.api import App
25

36
# Create an instance of APp with a specific prefix
47
router_instance = App(prefix="/v1")
58

69
# Add routes to the class-based router
7-
router_instance.router.add_api_route("/app/discover", App.discover, methods=["POST"], response_model=dict)
8-
router_instance.router.add_api_route("/loader/doc", App.loader_doc, methods=["POST"], response_model=dict)
10+
router_instance.router.add_api_route(
11+
"/app/discover", App.discover, methods=["POST"], response_model=dict
12+
)
13+
router_instance.router.add_api_route(
14+
"/loader/doc", App.loader_doc, methods=["POST"], response_model=dict
15+
)

0 commit comments

Comments
 (0)