Skip to content

Commit

Permalink
support program settings (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juice-XIJ authored May 10, 2022
1 parent e2ab10d commit e16d840
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude=forza_motorsport/*
# exit-zero treats all errors as warnings. The GitHub editor is 255 chars wide
flake8 . --count --max-complexity=10 --max-line-length=255 --statistics --exclude=forza_motorsport/* --ignore=E402,C901
flake8 . --count --max-complexity=10 --max-line-length=255 --statistics --exclude=forza_motorsport/* --ignore=E402,C901,E241
- name: Test with pytest
run: |
pytest
Expand Down
1 change: 1 addition & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class ConfigVersion(enum.Enum):

# repo path
root_path = os.path.dirname(os.path.abspath(__file__))
setting_filename = 'settings.json'

# socket information
ip = '127.0.0.1'
Expand Down
7 changes: 5 additions & 2 deletions forza.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys
import os
import sys
import time
from concurrent.futures.thread import ThreadPoolExecutor
from os import listdir
Expand All @@ -11,11 +11,11 @@
from fdp import ForzaDataPacket

import constants
from constants import ConfigVersion
import gear_helper
import helper
import keyboard_helper
from car_info import CarInfo
from constants import ConfigVersion
from logger import Logger


Expand Down Expand Up @@ -58,6 +58,9 @@ def __init__(self, threadPool: ThreadPoolExecutor, logger: Logger = None, packet
if not os.path.exists(self.config_folder):
os.makedirs(self.config_folder)

# init constants from config if existed
helper.load_settings(self)

# === car data ===
self.gear_ratios = {}
self.rpm_torque_map = {}
Expand Down
1 change: 1 addition & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ def exit_handler(self, event):
event
"""
shutdown(self.forza5, self.threadPool, self.listener)
helper.dump_settings(self.forza5)
self.forza5.logger.info('bye~')
self.root.destroy()

Expand Down
72 changes: 71 additions & 1 deletion helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from matplotlib.pyplot import cm

import constants
import keyboard_helper
from constants import ConfigVersion


Expand Down Expand Up @@ -169,6 +170,28 @@ def convert(n: object):
return n.item()


def dump_settings(forza: CarInfo):
"""
write following variables from forza to settings.json:
clutch
upshift
downshift
Args:
forza (CarInfo): car info
"""
json_data = {
'clutch': forza.clutch,
'upshift': forza.upshift,
'downshift': forza.downshift,
}

settings_path = os.path.join(forza.config_folder, constants.setting_filename)
forza.logger.info(f'saving program settings to {settings_path}')
with open(settings_path, "w") as f:
json.dump(json_data, f, default=convert, indent=4)


def dump_config(forza: CarInfo, config_version: ConfigVersion = constants.default_config_version):
"""dump config
Expand All @@ -177,6 +200,8 @@ def dump_config(forza: CarInfo, config_version: ConfigVersion = constants.defaul
"""
try:
forza.logger.debug(f'{dump_config.__name__} started')
config_name = get_config_name(forza, config_version)
forza.logger.info(f'saving config {config_name}')
config = {
# === dump data and result ===
'version': config_version.name,
Expand All @@ -192,7 +217,7 @@ def dump_config(forza: CarInfo, config_version: ConfigVersion = constants.defaul
'records': forza.records,
}

with open(os.path.join(forza.config_folder, get_config_name(forza, config_version)), "w") as f:
with open(os.path.join(forza.config_folder, config_name), "w") as f:
json.dump(config, f, default=convert, indent=4)
finally:
forza.logger.debug(f'{dump_config.__name__} ended')
Expand Down Expand Up @@ -244,6 +269,51 @@ def get_config_name(forza: CarInfo, config_version: ConfigVersion = constants.de
forza.logger.warning(f'Invalid config version {str(config_version)}')


def load_settings(forza: CarInfo):
"""
overwrite following variables in forza:
clutch
upshift
downshift
Args:
forza (CarInfo): car info
"""
forza.logger.debug(f'{load_settings.__name__} started')
settings_path = os.path.join(forza.config_folder, constants.setting_filename)
try:
valid_keys = keyboard_helper.keybind.keys()
if os.path.exists(settings_path):
forza.logger.info(f'loading program settings from {settings_path}')
with open(settings_path, "r") as f:
settings = json.loads(f.read())

if 'clutch' in settings:
clutch_shortcut = settings['clutch']
if clutch_shortcut in valid_keys:
forza.clutch = clutch_shortcut
else:
forza.logger.warning(f'clutch shortcut {clutch_shortcut} in {settings_path} is not valid')

if 'upshift' in settings:
upshift_shortcut = settings['upshift']
if upshift_shortcut in valid_keys:
forza.upshift = upshift_shortcut
else:
forza.logger.warning(f'upshift shortcut {upshift_shortcut} in {settings_path} is not valid')

if 'downshift' in settings:
downshift_shortcut = settings['downshift']
if downshift_shortcut in valid_keys:
forza.downshift = downshift_shortcut
else:
forza.logger.warning(f'downshift shortcut {downshift_shortcut} in {settings_path} is not valid')
except Exception as e:
forza.logger.warning(f'failed to load settings {settings_path}: {e}')
finally:
forza.logger.debug(f'{load_settings.__name__} ended')


def load_config(forza: CarInfo, path: str):
"""load config as carinfo
Expand Down

0 comments on commit e16d840

Please sign in to comment.