Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
refactor: restructure source code
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Aug 9, 2024
1 parent 01d4d90 commit 9138ac6
Show file tree
Hide file tree
Showing 28 changed files with 150 additions and 143 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ jobs:

- name: Set up Python Dependencies
run: |
python -m pip install --upgrade pip setuptools
python -m pip install -r requirements-dev.txt --no-warn-script-location
python -m pip install --upgrade pip setuptools wheel
python -m pip install -r requirements-dev.txt
- name: Compile Locale Translations
run: |
Expand Down Expand Up @@ -108,7 +108,7 @@ jobs:
--tb=native \
--verbose \
--color=yes \
--cov=pyra \
--cov=src \
tests
- name: Upload coverage
Expand Down
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ cython_debug/
node_modules/
*package-lock.json

# RetroArcher directories
# project files and directories
logs/

# RetroArcher files
*config.ini
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ RUN groupadd -g 1000 retroarcher && \
RUN mkdir -p /config
VOLUME /config

CMD ["python", "retroarcher.py"]
CMD ["python", "./src/retroarcher.py"]

EXPOSE 9696
HEALTHCHECK --start-period=90s CMD python retroarcher.py --docker_healthcheck || exit 1
2 changes: 1 addition & 1 deletion scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def build():
"""Sets arguments for pyinstaller, creates spec, and builds binaries."""
pyinstaller_args = [
'retroarcher.py',
'./src/retroarcher.py',
'--onefile',
'--noconfirm',
'--paths=./',
Expand Down
Empty file added src/__init__.py
Empty file.
10 changes: 5 additions & 5 deletions pyra/__init__.py → src/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from typing import Union

# local imports
from pyra import config
from pyra import definitions
from pyra import helpers
from pyra import logger
from common import config
from common import definitions
from common import helpers
from common import logger

# get logger
log = logger.get_logger(name=__name__)
Expand Down Expand Up @@ -121,7 +121,7 @@ def stop(exit_code: Union[int, str] = 0, restart: bool = False):
>>> stop(exit_code=0, restart=False)
"""
# stop the tray icon
from pyra.tray_icon import tray_end
from common.tray_icon import tray_end
try:
tray_end()
except AttributeError:
Expand Down
10 changes: 5 additions & 5 deletions pyra/config.py → src/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
from validate import Validator, ValidateError

# local imports
from pyra import definitions
from pyra import logger
from pyra import locales
from common import definitions
from common import logger
from common import locales

# get log
log = logger.get_logger(name=__name__)
Expand Down Expand Up @@ -47,14 +47,14 @@ def on_change_tray_toggle() -> bool:
See Also
--------
pyra.tray_icon.tray_toggle : ``on_change_tray_toggle`` is an alias of this function.
common.tray_icon.tray_toggle : ``on_change_tray_toggle`` is an alias of this function.
Examples
--------
>>> on_change_tray_toggle()
True
"""
from pyra import tray_icon
from common import tray_icon
return tray_icon.tray_toggle()


Expand Down
13 changes: 8 additions & 5 deletions pyra/definitions.py → src/common/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ class Paths:
The purpose of this class is to ensure consistency when using these paths.
PYRA_DIR : str
The directory containing the retroarcher python files.
COMMON_DIR : str
The directory containing the common python files.
SRC_DIR : str
The directory containing the application python files
ROOT_DIR : str
The root directory of the application. This is where the source files exist.
DATA_DIR : str
Expand All @@ -139,11 +141,12 @@ class Paths:
Examples
--------
>>> Paths.logs
>>> Paths.LOG_DIR
'.../logs'
"""
PYRA_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(PYRA_DIR)
COMMON_DIR = os.path.dirname(os.path.abspath(__file__))
SRC_DIR = os.path.dirname(COMMON_DIR)
ROOT_DIR = os.path.dirname(SRC_DIR)
DATA_DIR = ROOT_DIR
BINARY_PATH = os.path.abspath(os.path.join(DATA_DIR, 'retroarcher.py'))

Expand Down
10 changes: 5 additions & 5 deletions pyra/hardware.py → src/common/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import psutil

# local imports
from pyra import definitions
from pyra import helpers
from pyra import locales
from pyra import logger
from common import definitions
from common import helpers
from common import locales
from common import logger

try:
cpu_name = cpuinfo.cpu.info[0]['ProcessorNameString'].strip()
Expand Down Expand Up @@ -335,7 +335,7 @@ def chart_data() -> dict:
See Also
--------
pyra.webapp.callback_dashboard : A callback called by javascript to get this data.
common.webapp.callback_dashboard : A callback called by javascript to get this data.
Examples
--------
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions pyra/locales.py → src/common/locales.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
from babel import localedata

# local imports
from pyra import config
from pyra.definitions import Paths
from pyra import logger
from common import config
from common.definitions import Paths
from common import logger

default_domain = 'retroarcher'
default_locale = 'en'
Expand Down
32 changes: 16 additions & 16 deletions pyra/logger.py → src/common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
from configobj import ConfigObj

# local imports
import pyra
from pyra import definitions
from pyra import helpers
import common
from common import definitions
from common import helpers

# These settings are for file logging only
py_name = 'pyra'
py_name = 'common'
MAX_SIZE = 5000000 # 5 MB
MAX_FILES = 5

Expand Down Expand Up @@ -67,7 +67,7 @@ def blacklist_config(config: ConfigObj):
Examples
--------
>>> config_object = pyra.config.create_config(config_file='config.ini')
>>> config_object = common.config.create_config(config_file='config.ini')
>>> blacklist_config(config=config_object)
"""
blacklist = set()
Expand Down Expand Up @@ -102,7 +102,7 @@ class NoThreadFilter(logging.Filter):
Examples
--------
>>> NoThreadFilter('main')
<pyra.logger.NoThreadFilter object at 0x...>
<common.logger.NoThreadFilter object at 0x...>
"""

def __init__(self, threadName):
Expand Down Expand Up @@ -152,7 +152,7 @@ class BlacklistFilter(logging.Filter):
Examples
--------
>>> BlacklistFilter()
<pyra.logger.BlacklistFilter object at 0x...>
<common.logger.BlacklistFilter object at 0x...>
"""

def __init__(self):
Expand Down Expand Up @@ -223,7 +223,7 @@ class RegexFilter(logging.Filter):
Examples
--------
>>> RegexFilter()
<pyra.logger.RegexFilter object at 0x...>
<common.logger.RegexFilter object at 0x...>
"""

def __init__(self):
Expand Down Expand Up @@ -301,7 +301,7 @@ class PublicIPFilter(RegexFilter):
Examples
--------
>>> PublicIPFilter()
<pyra.logger.PublicIPFilter object at 0x...>
<common.logger.PublicIPFilter object at 0x...>
"""

def __init__(self):
Expand Down Expand Up @@ -358,7 +358,7 @@ class EmailFilter(RegexFilter):
Examples
--------
>>> EmailFilter()
<pyra.logger.EmailFilter object at 0x...>
<common.logger.EmailFilter object at 0x...>
"""

def __init__(self):
Expand Down Expand Up @@ -414,7 +414,7 @@ class PlexTokenFilter(RegexFilter):
Examples
--------
>>> PlexTokenFilter()
<pyra.logger.PlexTokenFilter object at 0x...>
<common.logger.PlexTokenFilter object at 0x...>
"""

def __init__(self):
Expand Down Expand Up @@ -568,7 +568,7 @@ def setup_loggers():
"""
loggers_list = [py_name, 'werkzeug']

submodules = pkgutil.iter_modules(pyra.__path__)
submodules = pkgutil.iter_modules(common.__path__)

for submodule in submodules:
loggers_list.append(f'{py_name}.{submodule[1]}')
Expand Down Expand Up @@ -613,7 +613,7 @@ def init_logger(log_name: str) -> logging.Logger:

# Configure the logger to accept all messages
logger.propagate = False
logger.setLevel(logging.DEBUG if pyra.DEBUG else logging.INFO)
logger.setLevel(logging.DEBUG if common.DEBUG else logging.INFO)

# Setup file logger
file_formatter = logging.Formatter('%(asctime)s - %(levelname)-7s :: %(threadName)s : %(message)s',
Expand All @@ -631,7 +631,7 @@ def init_logger(log_name: str) -> logging.Logger:
logger.addHandler(file_handler)

# Setup console logger
if not pyra.QUIET:
if not common.QUIET:
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s :: %(threadName)s : %(message)s',
'%Y-%m-%d %H:%M:%S')
console_handler = logging.StreamHandler()
Expand All @@ -643,7 +643,7 @@ def init_logger(log_name: str) -> logging.Logger:
# Add filters to log handlers
# Only add filters after the config file has been initialized
# Nothing prior to initialization should contain sensitive information
if not pyra.DEV and pyra.CONFIG:
if not common.DEV and common.CONFIG:
log_handlers = logger.handlers
for handler in log_handlers:
handler.addFilter(BlacklistFilter())
Expand All @@ -652,7 +652,7 @@ def init_logger(log_name: str) -> logging.Logger:
handler.addFilter(PlexTokenFilter())

# Install exception hooks
if log_name == py_name: # all tracebacks go to 'pyra.log'
if log_name == py_name: # all tracebacks go to 'common.log'
_init_hooks(logger)

# replace warn
Expand Down
6 changes: 3 additions & 3 deletions pyra/threads.py → src/common/threads.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
Examples
--------
>>> from pyra import config, threads, tray_icon
>>> from common import config, threads, tray_icon
>>> config_object = config.create_config(config_file='config.ini')
>>> tray_icon.icon = tray_icon.tray_initialize()
>>> threads.run_in_thread(target=tray_icon.tray_run, name='pystray', daemon=True).start()
>>> from pyra import config, threads, webapp
>>> from common import config, threads, webapp
>>> config_object = config.create_config(config_file='config.ini')
>>> threads.run_in_thread(target=webapp.start_webapp, name='Flask', daemon=True).start()
* Serving Flask app 'pyra.webapp' (lazy loading)
* Serving Flask app 'common.webapp' (lazy loading)
...
* Running on http://.../ (Press CTRL+C to quit)
"""
Expand Down
24 changes: 12 additions & 12 deletions pyra/tray_icon.py → src/common/tray_icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
from PIL import Image

# local imports
import pyra
from pyra import config
from pyra import definitions
from pyra import helpers
from pyra import locales
from pyra import logger
from pyra import threads
import common
from common import config
from common import definitions
from common import helpers
from common import locales
from common import logger
from common import threads

# setup
_ = locales.get_text()
Expand Down Expand Up @@ -197,7 +197,7 @@ def tray_run_threaded() -> bool:
--------
tray_initialize : This function first, initializes the tray icon using ``tray_initialize()``.
tray_run : Then, ``tray_run`` is executed in a thread.
pyra.threads.run_in_thread : Run a method within a thread.
common.threads.run_in_thread : Run a method within a thread.
Examples
--------
Expand Down Expand Up @@ -243,26 +243,26 @@ def tray_quit():
"""
Shutdown RetroArcher.
Set the 'pyra.SIGNAL' variable to 'shutdown'.
Set the 'common.SIGNAL' variable to 'shutdown'.
Examples
--------
>>> tray_quit()
"""
pyra.SIGNAL = 'shutdown'
common.SIGNAL = 'shutdown'


def tray_restart():
"""
Restart RetroArcher.
Set the 'pyra.SIGNAL' variable to 'restart'.
Set the 'common.SIGNAL' variable to 'restart'.
Examples
--------
>>> tray_restart()
"""
pyra.SIGNAL = 'restart'
common.SIGNAL = 'restart'


def tray_run():
Expand Down
Loading

0 comments on commit 9138ac6

Please sign in to comment.