1

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

stovfl
  • 14,998
  • 7
  • 24
  • 51
Ethr
  • 431
  • 1
  • 3
  • 17
  • 2
    Can you just have a flat file that holds configs with a boolean that is set to zero. Then on first run of program you check that boolean then ask the user to update some configs in a pop up window and after that the boolean is set to 1 so from now on it does not ask to update. – Mike - SMT Feb 25 '20 at 18:06
  • ***"allow user to configure it during installation (but only then"***: Such a approach is kind of hassle. How will a user edit a mistyped data? – stovfl Feb 25 '20 at 18:12

1 Answers1

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.

sinoroc
  • 18,409
  • 2
  • 39
  • 70