Skip to content

Commit

Permalink
(app): roll back to pyx to python
Browse files Browse the repository at this point in the history
  • Loading branch information
happer64bit committed Aug 6, 2024
1 parent 5b6781d commit 62c0bdd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 48 deletions.
35 changes: 14 additions & 21 deletions hyperscript_cli/parser.pyx → hyperscript_cli/parser.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
from typing import Dict, Any
from typing import Dict, Any, Union, List
from colorama import Fore, init
import requests
import yaml
import os
import re
from cpython cimport bool

init(autoreset=True)

cdef class Parser:
cdef public dict config
cdef public bool skip_error
cdef public bool verbose
cdef public int success_count
cdef public int fail_count

def __init__(self, config: Dict[str, Any], bool skip_error, bool verbose=False):
class Parser:
def __init__(self, config: Dict[str, Any], skip_error: bool, verbose: bool = False) -> None:
self.config = self._process_env_vars(config)
self.skip_error = skip_error
self.verbose = verbose
self.success_count = 0
self.fail_count = 0

def run_test(self):
def run_test(self) -> None:
global_url = self.config['global']['url']
global_headers = self.config['global'].get('headers', {})
global_cookies = self.config['global'].get('cookies', {})
Expand All @@ -46,7 +39,7 @@ def run_test(self):
except Exception as e:
self._handle_error(f"Unexpected error occurred: {str(e)}", name)

cdef void _check_response(self, response, expect: Dict[str, Any], name: str):
def _check_response(self, response: requests.Response, expect: Dict[str, Any], name: str) -> None:
try:
expected_status = expect.get('status')
expected_content_type = expect.get('contentType')
Expand Down Expand Up @@ -95,33 +88,33 @@ def run_test(self):
except Exception as e:
self._handle_error(f"ERROR DURING RESPONSE VALIDATION: {str(e)}", name)

cdef bool _compare_nested(self, actual: Any, expected: Any):
def _compare_nested(self, actual: Any, expected: Any) -> bool:
if isinstance(expected, dict):
for key, value in expected.items():
if key not in actual or not self._compare_nested(actual[key], value):
return False
return True
return actual == expected

cdef bool _compare_contains(self, actual: Any, contains: Dict[str, Any]):
def _compare_contains(self, actual: Any, contains: Dict[str, Any]) -> bool:
for key, value in contains.items():
if key not in actual or actual[key] != value:
return False
return True

cdef bool _compare_less_than(self, actual: Any, less_than: Dict[str, Any]):
def _compare_less_than(self, actual: Any, less_than: Dict[str, Any]) -> bool:
for key, value in less_than.items():
if key not in actual or not actual[key] < value:
return False
return True

cdef bool _compare_greater_than(self, actual: Any, greater_than: Dict[str, Any]):
def _compare_greater_than(self, actual: Any, greater_than: Dict[str, Any]) -> bool:
for key, value in greater_than.items():
if key not in actual or not actual[key] > value:
return False
return True

cdef void _handle_error(self, message: str, name: str):
def _handle_error(self, message: str, name: str) -> None:
simplified_message = message
if 'Max retries exceeded' in message:
simplified_message = "Unable to connect to the server. Please check if the server is running and reachable."
Expand All @@ -139,18 +132,18 @@ def run_test(self):
if not self.skip_error:
raise RuntimeError(simplified_message)

cdef void _log_success(self, name: str):
def _log_success(self, name: str) -> None:
success_message = f"{Fore.GREEN}✔ SUCCESS: {name}{Fore.RESET}"
print(success_message)
self.success_count += 1

def show_summary(self):
def show_summary(self) -> None:
total_tests = self.success_count + self.fail_count
print(f"\n{Fore.GREEN}SUCCESS: {self.success_count}{Fore.RESET}, "
f"{Fore.RED}FAILURE: {self.fail_count}{Fore.RESET}, "
f"{Fore.WHITE}TOTAL: {total_tests}{Fore.RESET}\n")

cdef dict _process_env_vars(self, config: Dict[str, Any]):
def _process_env_vars(self, config: Dict[str, Any]) -> Dict[str, Any]:
"""Replace placeholders in the config with environment variable values."""
def replace_env_vars(value: Any) -> Any:
if isinstance(value, str):
Expand All @@ -163,7 +156,7 @@ def replace_env_vars(value: Any) -> Any:

return replace_env_vars(config)

cpdef dict parse_config(str config_file):
def parse_config(config_file: str) -> Dict[str, Any]:
try:
with open(config_file, 'r') as file:
config = yaml.safe_load(file)
Expand Down
4 changes: 1 addition & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
requests
colorama
Cython
python_msvc
colorama
27 changes: 3 additions & 24 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,19 @@
from setuptools import setup, find_packages, Extension
from Cython.Build import cythonize
import os

long_description = ""
if os.path.exists('README.md'):
with open('README.md', 'r') as file:
long_description = file.read()

# Define Cython extension
extensions = [
Extension(
name="hyperscript_cli.parser",
sources=["hyperscript_cli/parser.pyx"],
extra_compile_args=['/O2']
)
]

cython_directives = {'embedsignature': True}

setup(
name='hyperscript-cli',
version='1.0.5',
version='1.0.4',
description='Powerful HTTP Request Tester',
long_description=long_description,
long_description=open('README.md').read(),
long_description_content_type='text/markdown',
author='Happer',
author_email='[email protected]',
url='https://github.com/happer64bit/hyperscript',
packages=find_packages(),
install_requires=[
'requests',
'pyyaml',
'colorama',
'Cython',
'python_msvc'
'colorama'
],
classifiers=[
'Development Status :: 4 - Beta',
Expand All @@ -57,5 +37,4 @@
},
include_package_data=True,
zip_safe=False,
ext_modules=cythonize(extensions, compiler_directives=cython_directives, language_level='3'),
)

0 comments on commit 62c0bdd

Please sign in to comment.