Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruff and pylint fixes for pebblo/app module #236

Merged
merged 9 commits into from
Mar 1, 2024
3 changes: 2 additions & 1 deletion pebblo/app/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

class App:
"""
Controller Class for all the api endpoints for App resource.
Controller Class for all the api endpoints for App resource.
"""

def __init__(self, prefix: str):
self.router = APIRouter(prefix=prefix)

Expand Down
17 changes: 10 additions & 7 deletions pebblo/app/api/local_ui_api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from pathlib import Path
from fastapi import APIRouter, Request
from fastapi.templating import Jinja2Templates
from pebblo.app.service.local_ui_service import AppData
from fastapi.responses import FileResponse
from pebblo.app.service.local_ui_service import AppData
from pebblo.app.enums.enums import CacheDir
from pebblo.app.utils.utils import get_full_path
from pathlib import Path

templates = Jinja2Templates(directory=Path(__file__).parent.parent.absolute() / "pebblo-ui")

templates = Jinja2Templates(
directory=Path(__file__).parent.parent.absolute() / "pebblo-ui"
)


class App:
Expand All @@ -25,7 +28,7 @@ def dashboard(request: Request):
{
"request": request,
"data": app_data.get_all_apps_details(),
"proxy": CacheDir.proxy.value,
"proxy": CacheDir.PROXY.value,
},
)

Expand All @@ -37,14 +40,14 @@ def app_details(request: Request, app_name: str):
{
"request": request,
"data": app_data.get_app_details(app_name),
"proxy": CacheDir.proxy.value,
"proxy": CacheDir.PROXY.value,
},
)

@staticmethod
def get_report(request: Request, app_name: str):
def get_report(app_name: str):
# File path for app report
file_path = f"{get_full_path(CacheDir.home_dir.value)}/{app_name}/{CacheDir.report_file_name.value}"
file_path = f"{get_full_path(CacheDir.HOME_DIR.value)}/{app_name}/{CacheDir.REPORT_FILE_NAME.value}"
# To view the file in the browser, use "inline" for the media_type
headers = {"Access-Control-Expose-Headers": "Content-Disposition"}
# Create a FileResponse object with the file path, media type and headers
Expand Down
56 changes: 24 additions & 32 deletions pebblo/app/config/config.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,62 @@
import pathlib
import yaml

from pydantic import BaseSettings, Field
import pathlib


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


# Port BaseModel
class PortConfig(BaseSettings):
host: str = Field(default='localhost')
host: str = Field(default="localhost")
port: int = Field(default=8000)


# Report BaseModel
class ReportConfig(BaseSettings):
format: str = Field(default='pdf')
renderer: str = Field(default='weasyprint')
format: str = Field(default="pdf")
renderer: str = Field(default="weasyprint")
outputDir: str = Field(dir_path)


# Logging BaseModel
class LoggingConfig(BaseSettings):
level: str = Field(default='info')
level: str = Field(default="info")


# ConfigFile BaseModel
class Config(BaseSettings):
daemon: PortConfig
daemon: PortConfig
reports: ReportConfig
logging: LoggingConfig

def load_config(path) -> Config:

def load_config(path) -> dict:
try:
# If Path does not exist in command, set default config value
conf_obj = Config(
daemon=PortConfig(
host='localhost',
port=8000
),
daemon=PortConfig(host="localhost", port=8000),
reports=ReportConfig(
format='pdf',
renderer='weasyprint',
outputDir='~/.pebblo'
format="pdf", renderer="weasyprint", outputDir="~/.pebblo"
),
logging=LoggingConfig(
level='info'
)
logging=LoggingConfig(level="info"),
)
if not path:
# Setting Default config details
return conf_obj.dict()

# If Path exist, set config value
else:
con_file = path
try:
with open(con_file, "r") as output:
cred_json = yaml.safe_load(output)
parsed_config = Config.parse_obj(cred_json)
config_dict = parsed_config.dict()
return config_dict
except IOError as err:
print(f"no credentials file found at {con_file}. Error : {err}")
return conf_obj.dict()
con_file = path
try:
with open(con_file, "r") as output:
cred_json = yaml.safe_load(output)
parsed_config = Config.parse_obj(cred_json)
config_dict = parsed_config.dict()
return config_dict
except IOError as err:
print(f"no credentials file found at {con_file}. Error : {err}")
return conf_obj.dict()

except Exception as err:
print(f'Error while loading config details, err: {err}')


print(f"Error while loading config details, err: {err}")
16 changes: 8 additions & 8 deletions pebblo/app/config/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ def __init__(self, config_details):
self.app.include_router(local_ui_router_instance.router)
# Fetching Details from Config File
self.config_details = config_details
self.port = self.config_details.get('daemon', {}).get('port', 8000)
self.host = self.config_details.get('daemon', {}).get('host', 'localhost')
self.log_level = self.config_details.get('logging', {}).get('level', 'info')
self.port = self.config_details.get("daemon", {}).get("port", 8000)
self.host = self.config_details.get("daemon", {}).get("host", "localhost")
self.log_level = self.config_details.get("logging", {}).get("level", "info")

async def create_main_api_server(self):
self.app.mount(
path="/static",
app=NoCacheStaticFiles(directory=Path(__file__).parent.parent.absolute() / "pebblo-ui"),
app=NoCacheStaticFiles(
directory=Path(__file__).parent.parent.absolute() / "pebblo-ui"
),
name="static",
)

# Add config Details to Uvicorn
config = uvicorn.Config(
app=self.app,
host=self.host,
port=self.port,
log_level=self.log_level)
app=self.app, host=self.host, port=self.port, log_level=self.log_level
)
server = uvicorn.Server(config)
await server.serve()

Expand Down
13 changes: 8 additions & 5 deletions pebblo/app/daemon.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""
This is entry point for Pebblo(Pebblo Server and Local UI)
"""
from contextlib import redirect_stderr, redirect_stdout

import argparse
from io import StringIO
from tqdm import tqdm
from pebblo.app.config.config import load_config
import argparse


config_details = {}
Expand All @@ -14,7 +16,7 @@ def start():
global config_details
# For loading config file details
parser = argparse.ArgumentParser(description="Pebblo CLI")
parser.add_argument('--config', type=str, help="config file path")
parser.add_argument("--config", type=str, help="config file path")
args = parser.parse_args()
path = args.config
p_bar = tqdm(range(10))
Expand Down Expand Up @@ -50,13 +52,14 @@ def classifier_init(p_bar):
p_bar.update(1)


def server_start(config_details, p_bar):
def server_start(config: dict, p_bar: tqdm):
"""Start server."""
p_bar.write("Pebblo server starting ...")
# Starting Uvicorn Service Using config details
from pebblo.app.config.service import Service

p_bar.update(1)
svc = Service(config_details)
svc = Service(config_details=config)
p_bar.update(2)
p_bar.close()
svc.start()
34 changes: 21 additions & 13 deletions pebblo/app/enums/enums.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
"""
These are all enums related to Inspector.
These are all enums related to Pebblo Server.
"""
from enum import Enum
from pebblo.app.daemon import config_details


class CacheDir(Enum):
metadata_folder = "/metadata"
metadata_file_path = f"{metadata_folder}/metadata.json"
report_data_file_name = "report.json"
report_file_name = f"pebblo_report.{config_details.get('reports', {}).get('format')}"
home_dir = config_details.get('reports', {}).get('outputDir', '~/.pebblo')
renderer = config_details.get('reports', {}).get('renderer')
format = config_details.get('reports', {}).get('format')
proxy = f"http://{config_details.get('daemon', {}).get('host')}:{config_details.get('daemon', {}).get('port')}"

"""
Enums for cache directory
"""
METADATA_FOLDER = "/metadata"
METADATA_FILE_PATH = f"{METADATA_FOLDER}/metadata.json"
REPORT_DATA_FILE_NAME = "report.json"
REPORT_FILE_NAME = (
f"pebblo_report.{config_details.get('reports', {}).get('format')}"
)
HOME_DIR = config_details.get("reports", {}).get("outputDir", "~/.pebblo")
RENDERER = config_details.get("reports", {}).get("renderer")
FORMAT = config_details.get("reports", {}).get("format")
PROXY = (f"http://{config_details.get('daemon', {}).get('host')}:"
f"{config_details.get('daemon', {}).get('port')}")


class ReportConstants(Enum):
snippets_limit = 100
top_findings_limit = 5
loader_history_limit = 5
"""
Enums for report
"""
SNIPPET_LIMIT = 100
TOP_FINDINGS_LIMIT = 5
LOADER_HISTORY__LIMIT = 5
11 changes: 9 additions & 2 deletions pebblo/app/routers/routers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""
Routes for Pebblo Server
"""
from pebblo.app.api.api import App

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

# Add routes to the class-based router
router_instance.router.add_api_route("/app/discover", App.discover, methods=["POST"], response_model=dict)
router_instance.router.add_api_route("/loader/doc", App.loader_doc, methods=["POST"], response_model=dict)
router_instance.router.add_api_route(
"/app/discover", App.discover, methods=["POST"], response_model=dict
)
router_instance.router.add_api_route(
"/loader/doc", App.loader_doc, methods=["POST"], response_model=dict
)
Loading
Loading