Skip to content

Commit

Permalink
day 1: add basic methos
Browse files Browse the repository at this point in the history
- readfiles *
- nodesolver *
  • Loading branch information
Davi Mello authored and Davi Mello committed Jan 25, 2021
0 parents commit 1ec8aff
Show file tree
Hide file tree
Showing 20 changed files with 595 additions and 0 deletions.
159 changes: 159 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@

# Created by https://www.toptal.com/developers/gitignore/api/python,vscode
# Edit at https://www.toptal.com/developers/gitignore?templates=python,vscode

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
pytestdebug.log

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/
doc/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
pythonenv*

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# profiling data
.prof

### vscode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# End of https://www.toptal.com/developers/gitignore/api/python,vscode
*.jpg
*.mp4
*.gif

# coverage
coverage
13 changes: 13 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"python.pythonPath": "/home/dsmello-linux/projects/personal/magi/venv/bin/python3",
"python.testing.pytestArgs": [
"test"
],
"python.testing.pytestEnabled": true,
"python.testing.pytestPath":"venv/bin/pytest",
"python.linting.pylintPath": "venv/bin/pylint",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Empty file added magi/__init__.py
Empty file.
Empty file added magi/__main__.py
Empty file.
79 changes: 79 additions & 0 deletions magi/nodesolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import logging



def __get_basic_info__(raw_input:dict) -> dict:
return raw_input.get("metadata")


def __metadata_auth__(raw_metadata_auth_input:dict) -> dict:
auth_input : dict = raw_metadata_auth_input

if auth_input.get("auth"):
auth_input = auth_input.get("auth")

# basic authentication
if auth_input.get("basic"):
basic_auth : dict = auth_input.get("basic")
return {"auth":(basic_auth.get("user"), basic_auth.get("password"))}

return {}


def __metadata__(raw_input:dict) -> dict:
metadata: dict = __get_basic_info__(raw_input)

result : dict = {}

if metadata.get("url"):
result["url"] = metadata.get("url")

if metadata.get("auth"):
result = dict(**result, **__metadata_auth__(metadata))

return result


def __get_requests__(raw_input:dict) -> list :
return raw_input.get("requests")


def __url__(url_base: str, url_endpoint: str) -> str:
base : str = url_base
endpoint : str = url_endpoint

if base[-1] == "/":
base = base[:-1]

if endpoint[0] == "/":
endpoint = endpoint[1:]

return f"{base}/{endpoint}"

def __inject_basic_info__(metadata_input:dict, raw_request_input:dict) -> dict:
metadata: dict = metadata_input.copy()

result : dict = {}
for node_name in raw_request_input:
request_list : list = []
for request_item in raw_request_input[node_name]:
request_item_copy: dict = request_item.copy()
request : dict = {}
if metadata.get("url") and request_item_copy.get("url"):
request["url"] = __url__(metadata.get("url"), request_item_copy.get("url"))
metadata.pop("url")
request_item_copy.pop("url")

request = dict(**request, **metadata, **request_item_copy)

if not request.get("method"):
request["method"] = "GET"
else:
request["method"] = request.get("method").upper()
request_list.append(request)
result[node_name] = request_list
return result


# TODO: Implements the method sort
def __sort_requests__(raw_input:dict) -> dict : ...
74 changes: 74 additions & 0 deletions magi/readfiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import yaml
import logging


errors: dict = {
'bad_folder_path' : 'The path of folder not is a directory.',
'bad_path_not_exist': 'The path not exist.',
'empty_folder' : 'The folder are empty.'
}


def hello() -> str:
return "world"


def is_folder( folder_path: str, error_msg: str = errors['bad_folder_path']) -> str:
if not os.path.isdir(folder_path):
logging.error(error_msg)
raise IOError(error_msg)
return folder_path


def __clean_str__(original_str: str, bad_str: str, startswith: str = '/') -> str:
result: str = original_str.replace(bad_str, '')

if result.startswith(startswith):
return result[1:]
return result


def list_files(filepath: str, prefix: str = '') -> dict:
is_folder(filepath)

files: dict = {}

for file in os.listdir(filepath):
path: str = f"{filepath}/{file}"
if os.path.isfile(path):
files[f'{prefix}{__clean_str__(path, filepath)}'] = path

if os.path.isdir(path):
files = {**files, **list_files(path, prefix=f'{file}/')}

return files


def empty_folder(folder_path: str, error_msg: str = errors['empty_folder']) -> bool:
if not list_files(folder_path):
logging.warning(error_msg)
return False
return True


def load_file(filepath: str) -> str:
file_data: str
with open(filepath, 'r') as data:
file_data = data.read()
return file_data


def __read_yaml__(filepath: str) -> dict:
content: str = load_file(filepath)
return dict(yaml.load(content, Loader=yaml.FullLoader))


def read_yaml_files(filepath: str, sufix: str = '.yaml') -> dict:
files: dict = list_files(filepath)
result: dict = {}
for key in files.keys():
if key.endswith(sufix):
result[__clean_str__(key, sufix)] = __read_yaml__(files[key])
return result

6 changes: 6 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pylint==2.6.0
pytest==6.2.1
coverage==5.3.1
setuptools==51.3.3
wheel==0.36.2
twine==3.3.0
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.25.1
pyaml==20.4.0
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
from setuptools import setup,find_packages


def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(name='magi',
version='0.1.0',
description='Simple CLI to consume a YAML||JSON and made REST calls. Like a Deploy of K8s but for a REST.S',
long_description=read('README.md'),
long_description_content_type='text/markdown',
url='local',
author='Davi Mello',
author_email='[email protected]',
license='GPL',
packages=find_packages(),
zip_safe=False,
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
])
9 changes: 9 additions & 0 deletions test/mock/alpha/mock_test_1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
metadata: #basic_info
auth:
basic:
user: "mock_user"
password: "mock_pass"
url: "mock.site.localhost"
requests:
test_default: #By default the "magi" will do a get
- url: "/dummy"
Loading

0 comments on commit 1ec8aff

Please sign in to comment.