Skip to content

Commit

Permalink
ci: Implement pre-commit validation
Browse files Browse the repository at this point in the history
  • Loading branch information
titom73 committed Aug 29, 2023
1 parent 7c23724 commit 46fcb4f
Show file tree
Hide file tree
Showing 19 changed files with 949 additions and 549 deletions.
64 changes: 64 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
files: ^(eos_downloader)/

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- id: check-merge-conflict

# - repo: https://github.com/pycqa/isort
# rev: 5.12.0
# hooks:
# - id: isort
# name: Check for changes when running isort on all python files

- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
name: Check for changes when running Black on all python files

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
name: Check for PEP8 error on Python files
args:
- --config=/dev/null
- --max-line-length=165

- repo: local # as per https://pylint.pycqa.org/en/latest/user_guide/installation/pre-commit-integration.html
hooks:
- id: pylint
entry: pylint
language: python
name: Check for Linting error on Python files
description: This hook runs pylint.
types: [python]
args:
- -rn # Only display messages
- -sn # Don't display the score
- --rcfile=pylintrc # Link to config file

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
hooks:
- id: mypy
args:
- --config-file=pyproject.toml
additional_dependencies:
- "click==8.1.3"
- "click-help-colors==0.9.1"
- "pydantic~=2.0"
- "PyYAML==6.0"
- "requests>=2.27"
- "rich~=13.4"
- types-paramiko
- types-requests
files: eos_downloader
27 changes: 18 additions & 9 deletions eos_downloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@
EOS Downloader module.
"""

from __future__ import (absolute_import, division,
print_function, unicode_literals, annotations)
from __future__ import (
absolute_import,
annotations,
division,
print_function,
unicode_literals,
)

import dataclasses
from typing import Any
import json
import importlib.metadata
import json
from typing import Any

__author__ = '@titom73'
__email__ = 'tom@inetsix.net'
__date__ = '2022-03-16'
__author__ = "@titom73"
__email__ = "tom@inetsix.net"
__date__ = "2022-03-16"
__version__ = importlib.metadata.version("eos-downloader")

# __all__ = ["CvpAuthenticationItem", "CvFeatureManager", "EOSDownloader", "ObjectDownloader", "reverse"]

ARISTA_GET_SESSION = "https://www.arista.com/custom_data/api/cvp/getSessionCode/"

ARISTA_SOFTWARE_FOLDER_TREE = "https://www.arista.com/custom_data/api/cvp/getFolderTree/"
ARISTA_SOFTWARE_FOLDER_TREE = (
"https://www.arista.com/custom_data/api/cvp/getFolderTree/"
)

ARISTA_DOWNLOAD_URL = "https://www.arista.com/custom_data/api/cvp/getDownloadLink/"

Expand All @@ -36,11 +44,12 @@
MSG_INVALID_DATA = """Invalid data returned by server
"""

EVE_QEMU_FOLDER_PATH = '/opt/unetlab/addons/qemu/'
EVE_QEMU_FOLDER_PATH = "/opt/unetlab/addons/qemu/"


class EnhancedJSONEncoder(json.JSONEncoder):
"""Custom JSon encoder."""

def default(self, o: Any) -> Any:
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
Expand Down
22 changes: 13 additions & 9 deletions eos_downloader/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,32 @@

import click
from rich.console import Console

import eos_downloader
from eos_downloader.cli.get import commands as get_commands
from eos_downloader.cli.debug import commands as debug_commands
from eos_downloader.cli.get import commands as get_commands
from eos_downloader.cli.info import commands as info_commands


@click.group()
@click.pass_context
@click.option('--token', show_envvar=True, default=None, help='Arista Token from your customer account')
@click.option(
"--token",
show_envvar=True,
default=None,
help="Arista Token from your customer account",
)
def ardl(ctx: click.Context, token: str) -> None:
"""Arista Network Download CLI"""
ctx.ensure_object(dict)
ctx.obj['token'] = token
ctx.obj["token"] = token


@click.command()
def version() -> None:
"""Display version of ardl"""
console = Console()
console.print(f'ardl is running version {eos_downloader.__version__}')
console.print(f"ardl is running version {eos_downloader.__version__}")


@ardl.group(no_args_is_help=True)
Expand All @@ -54,6 +60,7 @@ def debug(ctx: click.Context) -> None:
# pylint: disable=redefined-builtin
"""Debug commands to work with ardl"""


# ANTA CLI Execution


Expand All @@ -66,11 +73,8 @@ def cli() -> None:
debug.add_command(debug_commands.xml)
ardl.add_command(version)
# Load CLI
ardl(
obj={},
auto_envvar_prefix='arista'
)
ardl(obj={}, auto_envvar_prefix="arista")


if __name__ == '__main__':
if __name__ == "__main__":
cli()
41 changes: 30 additions & 11 deletions eos_downloader/cli/debug/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,51 @@

@click.command()
@click.pass_context
@click.option('--output', default=str('arista.xml'), help='Path to save XML file', type=click.Path(), show_default=True)
@click.option('--log-level', '--log', help='Logging level of the command', default=None, type=click.Choice(['debug', 'info', 'warning', 'error', 'critical'], case_sensitive=False))
@click.option(
"--output",
default=str("arista.xml"),
help="Path to save XML file",
type=click.Path(),
show_default=True,
)
@click.option(
"--log-level",
"--log",
help="Logging level of the command",
default=None,
type=click.Choice(
["debug", "info", "warning", "error", "critical"], case_sensitive=False
),
)
def xml(ctx: click.Context, output: str, log_level: str) -> None:
# sourcery skip: remove-unnecessary-cast
"""Extract XML directory structure"""
console = Console()
# Get from Context
token = ctx.obj['token']
token = ctx.obj["token"]

logger.remove()
if log_level is not None:
logger.add("eos-downloader.log", rotation="10 MB", level=log_level.upper())

my_download = eos_downloader.eos.EOSDownloader(
image='unset',
software='EOS',
version='unset',
image="unset",
software="EOS",
version="unset",
token=token,
hash_method='sha512sum')
hash_method="sha512sum",
)

my_download.authenticate()
xml_object: ET.ElementTree = my_download._get_folder_tree() # pylint: disable=protected-access
xml_object: ET.ElementTree = (
my_download.get_folder_tree()
) # pylint: disable=protected-access
xml_content = xml_object.getroot()

xmlstr = minidom.parseString(ET.tostring(xml_content)).toprettyxml(indent=" ", newl='')
with open(output, "w", encoding='utf-8') as f:
xmlstr = minidom.parseString(ET.tostring(xml_content)).toprettyxml(
indent=" ", newl=""
)
with open(output, "w", encoding="utf-8") as f:
f.write(str(xmlstr))

console.print(f'XML file saved in: { output }')
console.print(f"XML file saved in: { output }")
Loading

0 comments on commit 46fcb4f

Please sign in to comment.