How to setup application to personalize it?

烂漫一生 提交于 2021-01-28 09:40:15

问题


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)?


回答1:


I believe pip is not designed to allow configuration at install-time. It is somewhat possible (under specific conditions) to do such a thing via custom setuptools commands like shown in the question for example, but there are many scenarios in which this will not work (in short: built distributions such as wheel do not contain the setup.py file, so it can't run at install-time) and many reasons why we don't want something like this to work.

A common recommendation (best practice?) is to run such customization during the first run of the application instead.



来源:https://stackoverflow.com/questions/60399987/how-to-setup-application-to-personalize-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!