Skip to content

Commit

Permalink
Pre-commit checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jyejare committed Dec 15, 2023
1 parent 766ce7e commit fc42f4c
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 146 deletions.
17 changes: 12 additions & 5 deletions apix/diff.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# -*- encoding: utf-8 -*-
"""Determine the changes between two API versions."""
from pathlib import Path

import attr
import yaml
from pathlib import Path
from logzero import logger
from apix.helpers import get_latest, get_previous, load_api

from apix.helpers import get_latest
from apix.helpers import get_previous
from apix.helpers import load_api


@attr.s()
Expand All @@ -28,7 +32,8 @@ def __attrs_post_init__(self):
)
if not self.ver2:
# get the version before ver1
self.ver2 = get_previous(self.api_name, self.ver1, self.data_dir, self.mock)
self.ver2 = get_previous(
self.api_name, self.ver1, self.data_dir, self.mock)

@staticmethod
def _truncate(diff_dict):
Expand Down Expand Up @@ -150,12 +155,14 @@ def save_diff(self, return_path=False):

if self.mock:
fpath = Path(
f"{self.data_dir}tests/APIs/{self.api_name}/{self.ver2}-to-{self.ver1}-diff.yaml"
f"{self.data_dir}tests/APIs/{self.api_name}"
f"/{self.ver2}-to-{self.ver1}-diff.yaml"
)
else:
ftype = "comp-diff" if self.compact else "diff"
fpath = Path(
f"{self.data_dir}APIs/{self.api_name}/{self.ver2}-to-{self.ver1}-{ftype}.yaml"
f"{self.data_dir}APIs/{self.api_name}/"
f"{self.ver2}-to-{self.ver1}-{ftype}.yaml"
)
if fpath.exists():
fpath.unlink()
Expand Down
19 changes: 13 additions & 6 deletions apix/explore.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Explore and API and save the results."""
import aiohttp
import asyncio
import time
from pathlib import Path

import aiohttp
import attr
import requests
import time
import yaml
from logzero import logger
from pathlib import Path
from apix.parsers import apipie, test

from apix.parsers import apipie
from apix.parsers import test


@attr.s()
Expand Down Expand Up @@ -35,7 +38,9 @@ def __attrs_post_init__(self):
logger.warning("No known parser specified! Please review documentation.")

async def _async_get(self, session, link):
"""visit a page and download the content, returning the link and content"""
"""
visit a page and download the content returning the link and content
"""
async with session.get(self.host_url + link[1], ssl=False) as response:
content = await response.read()
logger.debug(link[1])
Expand All @@ -53,7 +58,9 @@ async def _async_loop(self, links):
self._queue.append(result)

def _visit_links(self, links, retries=3):
"""main controller for asynchronous page visiting, will attempt 3 retries"""
"""
Main controller for asynchronous page visiting, will attempt 3 retries
"""
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(self._async_loop(links))
Expand Down
8 changes: 5 additions & 3 deletions apix/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- encoding: utf-8 -*-
"""A collection of miscellaneous helpers that don't quite fit in."""
import yaml
from copy import deepcopy
from pathlib import Path

import yaml
from logzero import logger


Expand Down Expand Up @@ -86,11 +87,12 @@ def save_api(api_name, version, api_dict, data_dir=None, compact=False, mock=Fal
"""Save the dict to yaml, if the file doesn't exist"""
if mock:
a_path = Path(
f"{data_dir}tests/APIs/{api_name}/{version}{'-comp' if compact else ''}.yaml"
f"{data_dir}tests/APIs/{api_name}/{version}"
f"{'-comp' if compact else ''}.yaml"
)
else:
a_path = Path(
f"{data_dir}APIs/{api_name}/{version}{'-comp' if compact else ''}.yaml"
f"{data_dir}APIs/{api_name}/{version}" f"{'-comp' if compact else ''}.yaml"
)
a_path.parent.mkdir(parents=True, exist_ok=True)
logger.info(f"Saving {api_name} v{version} to {a_path}")
Expand Down
10 changes: 6 additions & 4 deletions apix/parsers/apipie.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class APIPie:
def _compile_method(method_dict):
"""form the parameters and paths lists"""
params = [
f'{param["name"]} ~ {"required" if param["required"] else "optional"} ~ {param["expected_type"]}'
(
f'{param["name"]} ~ '
f'{"required" if param["required"] else "optional"} ~ '
f'{param["expected_type"]}'
)
for param in method_dict["params"]
]
paths = [
Expand All @@ -41,9 +45,7 @@ def scrape_content(self, result):
self._data[name]["methods"].append(
{method["name"]: self._compile_method(method)}
)
self.params.update(
{param["name"]: param for param in method["params"]}
)
self.params.update({param["name"]: param for param in method["params"]})

def yaml_format(self, ingore=None):
"""Return the compiled data in a yaml-friendly format"""
Expand Down
1 change: 0 additions & 1 deletion apix/parsers/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
https://www.google.com/search?q=apix
"""
import attr
from logzero import logger
from lxml import html


Expand Down
43 changes: 27 additions & 16 deletions candore/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import asyncio
import json
from pathlib import Path

import click

from candore.errors import ModeError
from candore.modules.api_lister import APILister
from candore.modules.extractor import Extractor
from candore.modules.comparator import Comparator
from candore.errors import ModeError
from candore.modules.extractor import Extractor
from candore.modules.report import Reporting
import click
import json
from pathlib import Path
import asyncio


class Candore:

def __init__(self, settings):
self.settings = settings
self.api_lister = APILister(settings=self.settings)
Expand All @@ -23,26 +24,36 @@ async def save_all_entities(self, mode, output_file, full):
:param mode: Pre or Post
:param output_file: Output file name
:param full: If True, save entities from all pages of the components, else just saves first page
:param full: If True, save entities from all pages of the components,
else just saves first page
:return: None
"""
if mode not in ['pre', 'post']:
if mode not in ["pre", "post"]:
raise ModeError("Extracting mode must be 'pre' or 'post'")

async with Extractor(settings=self.settings, apilister=self.api_lister) as extractor:
async with Extractor(
settings=self.settings, apilister=self.api_lister
) as extractor:
if full:
extractor.full = True
data = await extractor.extract_all_entities()

if not data:
click.echo('Entities data is not data found!')
click.echo("Entities data is not data found!")

file_path = Path(output_file) if output_file else Path(f'{mode}_entities.json')
with file_path.open(mode='w') as entfile:
file_path = Path(output_file) if output_file else Path(f"{mode}_entities.json")
with file_path.open(mode="w") as entfile:
json.dump(data, entfile)
click.echo(f'Entities data saved to {file_path}')

def compare_entities(self, pre_file=None, post_file=None, output=None, report_type=None, record_evs=None):
click.echo(f"Entities data saved to {file_path}")

def compare_entities(
self,
pre_file=None,
post_file=None,
output=None,
report_type=None,
record_evs=None,
):
comp = Comparator(settings=self.settings)
if record_evs:
comp.record_evs = True
Expand Down
47 changes: 34 additions & 13 deletions candore/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import click
import asyncio
from pprint import pprint

import click

from candore import Candore
import asyncio
from candore.config import candore_settings

# Click Interactive for Cloud Resources Cleanup
Expand All @@ -12,8 +14,8 @@
invoke_without_command=True,
)
@click.option("--version", is_flag=True, help="Installed version of candore")
@click.option("--settings-file", '-s', default=None, help="Settings file path")
@click.option("--components-file", '-c', default=None, help="Components file path")
@click.option("--settings-file", "-s", default=None, help="Settings file path")
@click.option("--components-file", "-c", default=None, help="Components file path")
@click.pass_context
def candore(ctx, version, settings_file, components_file):
if version:
Expand All @@ -23,44 +25,63 @@ def candore(ctx, version, settings_file, components_file):
click.echo(f"Version: {ver}")
candore_obj = Candore(
settings=candore_settings(
option_settings_file=settings_file,
option_components_file=components_file
option_settings_file=settings_file, option_components_file=components_file
)
)
ctx.__dict__['candore'] = candore_obj
ctx.__dict__["candore"] = candore_obj


@candore.command(help="List API lister endpoints from Product")
@click.pass_context
def apis(ctx):
"""List API lister endpoints from Product"""
print("List of API lister endpoints from Product")
import ipdb; ipdb.set_trace()
import ipdb

ipdb.set_trace()
candore_obj = ctx.parent.candore
pprint(candore_obj.list_endpoints())


@candore.command(help="Extract and save data using API lister endpoints")
@click.option("--mode", type=str, help="The mode must be 'pre' or 'post'")
@click.option("-o", "--output", type=str, help="The output file name")
@click.option("--full", is_flag=True, help="Extract data from all the pages of a component")
@click.option(
"--full", is_flag=True, help="Extract data from all the pages of a component"
)
@click.pass_context
def extract(ctx, mode, output, full):
loop = asyncio.get_event_loop()
candore_obj = ctx.parent.candore
loop.run_until_complete(candore_obj.save_all_entities(mode=mode, output_file=output, full=full))
loop.run_until_complete(
candore_obj.save_all_entities(mode=mode, output_file=output, full=full)
)


@candore.command(help="Compare pre and post upgrade data")
@click.option("--pre", type=str, help="The pre upgrade json file")
@click.option("--post", type=str, help="The post upgrade json file")
@click.option("-o", "--output", type=str, help="The output file name")
@click.option("-t", "--report-type", type=str, default='json', help="The type of report GSheet, JSON, or webpage")
@click.option("--record-evs", is_flag=True, help="Record Expected Variations in reporting")
@click.option(
"-t",
"--report-type",
type=str,
default="json",
help="The type of report GSheet, JSON, or webpage",
)
@click.option(
"--record-evs", is_flag=True, help="Record Expected Variations in reporting"
)
@click.pass_context
def compare(ctx, pre, post, output, report_type, record_evs):
candore_obj = ctx.parent.candore
candore_obj.compare_entities(pre_file=pre, post_file=post, output=output, report_type=report_type, record_evs=record_evs)
candore_obj.compare_entities(
pre_file=pre,
post_file=post,
output=output,
report_type=report_type,
record_evs=record_evs,
)


if __name__ == "__main__":
Expand Down
18 changes: 14 additions & 4 deletions candore/config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
from pathlib import Path, PurePath
from pathlib import Path
from pathlib import PurePath

from dynaconf import Dynaconf
from dynaconf.validator import Validator

CURRENT_DIRECTORY = Path().resolve()


def candore_settings(option_settings_file=None, option_components_file=None):
settings_file = PurePath(option_settings_file) if option_settings_file else PurePath(CURRENT_DIRECTORY, 'settings.yaml')
components_file = PurePath(option_components_file) if option_components_file else PurePath(CURRENT_DIRECTORY, 'components.yaml')
settings_file = (
PurePath(option_settings_file)
if option_settings_file
else PurePath(CURRENT_DIRECTORY, "settings.yaml")
)
components_file = (
PurePath(option_components_file)
if option_components_file
else PurePath(CURRENT_DIRECTORY, "components.yaml")
)
# Initialize and Configure Settings
settings = Dynaconf(
core_loaders=["YAML"],
Expand All @@ -22,7 +32,7 @@ def candore_settings(option_settings_file=None, option_components_file=None):

def validate_settings(settings):
provider_settings = [
f"candore.{setting_key}" for setting_key in settings.to_dict().get('CANDORE')
f"candore.{setting_key}" for setting_key in settings.to_dict().get("CANDORE")
]
settings.validators.register(Validator(*provider_settings, ne=None))
try:
Expand Down
3 changes: 2 additions & 1 deletion candore/errors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# List of custom exceptions used by Candore modules


class NoDataFound(Exception):
pass


class ModeError(Exception):
pass

19 changes: 12 additions & 7 deletions candore/modules/api_lister.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@


class APILister:

def __init__(self, settings):
self.explorer = AsyncExplorer(name=settings.candore.product_name, version=settings.candore.version, host_url=settings.candore.base_url, base_path=settings.candore.docpath, parser=settings.candore.parser)
self.explorer = AsyncExplorer(
name=settings.candore.product_name,
version=settings.candore.version,
host_url=settings.candore.base_url,
base_path=settings.candore.docpath,
parser=settings.candore.parser,
)
self.list_endpoints = {}

def _endpoints(self):
Expand All @@ -16,14 +21,14 @@ def lister_endpoints(self):
list_endpoints = {}

for component, data in self._endpoints().items():
methods = data.get('methods', [])
methods = data.get("methods", [])
component_list_apis = [
path.lstrip('GET ') for method in methods
for path in method.get('index', {}).get('paths', [])
if 'id' not in path
path.lstrip("GET ")
for method in methods
for path in method.get("index", {}).get("paths", [])
if "id" not in path
]
list_endpoints[component] = component_list_apis

self.list_endpoints = list_endpoints
return list_endpoints

Loading

0 comments on commit fc42f4c

Please sign in to comment.