如何设置应用程序使其个性化? [英] How to setup application to personalize it?

查看:71
本文介绍了如何设置应用程序使其个性化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究简单的开源项目. Github链接.应用程序是用 tkinter 编写的.这是一种计算器.我想允许用户在安装过程中对其进行配置(但仅在那时,没有用于更改设置的单独菜单).目前,默认值已保存在类中.我想增加使用的可能性: pip3 install.--install-option =-customize = {'my_value1':1,'my_value2':2}" .我知道我可以一一添加它们,但是由于其中很多原因,我决定使用字典.在下面添加我的代码,在其中将这些设置保存到 json 文件中,并在以后与应用程序一起使用:

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

上面代码的问题是它没有创建任何 json 文件.不在安装目录中,甚至不在软件包目录中.这是我第一次尝试开发自己的软件包.此线程帮助我编写代码.我不知道该如何纠正.你能告诉我如何解决吗?有没有更优雅的方法来实现它(例如,不使用 json 文件)?

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

推荐答案

我相信 pip 并非旨在允许在安装时进行配置.通过自定义 setuptools 命令(例如在问题中显示),在某种情况下(在特定条件下)可以执行此操作,但是在许多情况下,这是行不通的(简而言之:内置发行版)例如wheel不包含 setup.py 文件,因此无法在安装时运行),还有许多我们不希望这样的原因工作.

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.

这篇关于如何设置应用程序使其个性化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆