I'm working on simple opensource project. Github link. Application is written with tkinter
. It is kind of calculator. I want to allow user to configure it during installation (but only then, no separate menu for changing settings). At the moment default values are saved in class. I would like to add possibility to use: pip3 install . --install-option="--customize={'my_value1': 1, 'my_value2': 2}"
. I know that I can add them one by one, but it will be many of them that's why I decided to use dictionary. Below I'm adding my code where I try to save those settings to json
file and use it later with application:
from setuptools import setup
from setuptools.command.install import install
from json import dumps, loads, JSONDecodeError
class Handler:
def __init__(self, data):
self.config = 'WorkTimeSaver/data.json'
self.data = self.convert(data)
if self.data:
self.save()
else:
print('Wrong format of data, default values will be used.')
def convert(self, settings):
try:
data = loads(settings)
if isinstance(data, dict):
return data
except JSONDecodeError:
return False
def save(self):
with open(self.config, 'w') as config:
config.write(dumps(self.data))
class InstallCommand(install):
user_options = install.user_options + [
('customize=', None, 'pass dictionary with values used to calculate salary'),
]
def initialize_options(self):
install.initialize_options(self)
self.customize = None
def finalize_options(self):
install.finalize_options(self)
def run(self):
Handler(self.customize)
install.run(self)
setup(
name='WorkTimeSaver',
packages=['WorkTimeSaver'],
entry_points={'console_scripts': 'WorkTimeSaver=WorkTimeSaver.__main__:main'},
cmdclass={'install': InstallCommand}
)
Problem with code above is that it is not creating any json
file. Not in installation directory and not even in package directory. It is first time when I try to develop my own package. This thread helped me to write code up. I don't know how to correct it. Could you tell me please how can I fix it? Is there more elegant way to achieve it (not using json
file for example)?