Skip to content

Commit

Permalink
Refactor code to use urllib.parse in parseQuery function
Browse files Browse the repository at this point in the history
  • Loading branch information
drazisil committed Aug 9, 2024
1 parent 699c50c commit cabd0f7
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pdm pytest-cov
pip install pdm
make install
- name: Lint with flake8
run: |
Expand Down
14 changes: 7 additions & 7 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ pytest = "^8.2.1"
pytest-cov = "^5.0.0"



[tool.pdm]
[tool.pdm.dev-dependencies]
test = ["pytest<9.0.0,>=8.2.1", "pytest-cov<6.0.0,>=5.0.0"]
dev = [
"flake8>=7.1.1",
"black>=24.8.0",
"pytest>=8.3.2",
"pytest-cov>=5.0.0",
]

[tool.pdm.build]
Expand Down
16 changes: 6 additions & 10 deletions pyrace/gateway/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@


class ConsoleThread(Thread, ServerBase):

def __init__(
self,
parentThread: ServerBase,
logger = getLogger("console")
) -> None:

def __init__(self, parentThread: ServerBase, logger=getLogger("console")) -> None:
Thread.__init__(self)
self.parentThread = parentThread
self.logger = logger
Expand All @@ -35,12 +31,12 @@ def __init__(
pass

def run(self) -> None:

if self.shutdownRequested:
return

reactor = Input()

self.logger.info("Started console thread")

with reactor:
Expand All @@ -59,4 +55,4 @@ def run(self) -> None:
def stop(self) -> None:
self.logger.info("Stopping console thread")
self.shutdownRequested = True
self.logger.info("Stopped console thread")
self.logger.info("Stopped console thread")
5 changes: 1 addition & 4 deletions pyrace/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import asyncio
from http.server import BaseHTTPRequestHandler, HTTPServer, ThreadingHTTPServer
from re import T
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread
from webbrowser import get

from pyrace.base import ServerBase
from pyrace.gateway.console import ConsoleThread
from pyrace.gateway.parseQuery import parseQuery
from pyrace.gateway.web import WebServer
from pyrace.shared.config import getConfig
from pyrace.shared.logging import getLogger
from sentry_sdk import capture_exception
Expand Down
7 changes: 3 additions & 4 deletions pyrace/gateway/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from email.policy import HTTP
from http.server import HTTPServer
from unittest.mock import Base


class WebServer(HTTPServer):
def __init__(self, *args):
self.isRunning = False
self.shutdownRequested = False
super().__init__(*args)



def server_activate(self):
self.isRunning = True
super(self).server_activate()
super(self).server_activate()
1 change: 1 addition & 0 deletions pyrace/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tkinter
from tkinter import StringVar, Tk, ttk, Text


def addLogMessage(message: str):
logText.configure(state="normal")
logText.insert(tkinter.END, f"{message}\n")
Expand Down
16 changes: 10 additions & 6 deletions pyrace/shared/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os


def verifyLegacyCipherSupport():
"""
Verifies the support for legacy ciphers.
Expand All @@ -27,9 +28,12 @@ def verifyLegacyCipherSupport():
Returns:
None
"""
key = os.urandom(16).hex()
dataCypher = Cipher(algorithms.ARC4(bytes.fromhex(key)), mode=None)
cmdCypher = Cipher(algorithms.TripleDES(bytes.fromhex(key)), modes.CBC(bytes.fromhex("0000000000000000")))



try:
key = os.urandom(16).hex()
_ = Cipher(algorithms.ARC4(bytes.fromhex(key)), mode=None)
_ = Cipher(
algorithms.TripleDES(bytes.fromhex(key)),
modes.CBC(bytes.fromhex("0000000000000000")),
)
except Exception as e:
raise Exception("Legacy cipher support is not available") from e
23 changes: 12 additions & 11 deletions pyrace/shared/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import logging
from tkinter import N


def getDefaultLevel():
return logging.INFO


class __Logger:
def __init__(self, name: str | None, level: int) -> None:
self.name = name
Expand All @@ -34,42 +34,43 @@ def __init__(self, name: str | None, level: int) -> None:
)

self.logger.addHandler(handler)

def getLogger(self, name: str):
level = self.level
name = f"{self.name}.{name}"
return self.__class__(name, level)

def debug(self, message: str, *args):
self.logger.debug(message, *args)

def info(self, message: str, *args):
self.logger.info(message, *args)

def warning(self, message: str, *args):
self.logger.warning(message, *args)

def error(self, message: str, *args):
self.logger.error(message, *args)

def critical(self, message: str, *args):
self.logger.critical(message, *args)

def exception(self, message: str, *args):
self.logger.exception(message, *args)

def log(self, level: int, message: str, *args):
self.logger.log(level, message, *args)


__logger = None


def getLogger(name: str | None, level: int = getDefaultLevel()) -> __Logger:
global __logger
if __logger is None:
__logger = __Logger('pyrace', level)
if name != None:
__logger = __Logger("pyrace", level)

if name is not None:
return __logger.getLogger(name)

return __logger
14 changes: 8 additions & 6 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import unittest
from pyrace.shared.config import getConfig


class TestConfig(unittest.TestCase):
def test_getConfig(self):
config = getConfig()

# Check if the configuration instance is not None
self.assertIsNotNone(config)

# Check if the configuration instance has the expected attributes
self.assertTrue(hasattr(config, "EXTERNAL_HOST"))
self.assertTrue(hasattr(config, "CERTIFICATE_FILE"))
self.assertTrue(hasattr(config, "PRIVATE_KEY_FILE"))
self.assertTrue(hasattr(config, "PUBLIC_KEY_FILE"))

# Add more assertions as needed

if __name__ == '__main__':
unittest.main()


if __name__ == "__main__":
unittest.main()
10 changes: 6 additions & 4 deletions tests/test_encryption.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import unittest
from pyrace.shared import encryption


class TestEncryption(unittest.TestCase):
def test_verifyLegacyCipherSupport(self):

try:
encryption.verifyLegacyCipherSupport()
except Exception as e:
self.fail("verifyLegacyCipherSupport() raised an exception: " + str(e))

self.assertTrue(True)

if __name__ == '__main__':
unittest.main()

if __name__ == "__main__":
unittest.main()

0 comments on commit cabd0f7

Please sign in to comment.