Skip to main content

Cross-platform python module to store application config via native subsystems such as Windows Registry or NSUserDefaults.

Project description

https://travis-ci.org/GreatFruitOmsk/nativeconfig.svg?branch=master https://badge.fury.io/py/nativeconfig.png

Developers of cross-platform applications often face problems when they need to interact with the system. Config files are no exception, since every popular OS has its own format and guidelines.

nativeconfig addresses this problem in an elegant and Pythonic way:

import os
from nativeconfig import PreferredConfig, StringOption, IntOption

class MyConfig(PreferredConfig):
    CONFIG_VERSION = __version__
    REGISTRY_PATH = r'Software\MyApp'
    JSON_PATH = os.path.expanduser('~/.config/MyApp/config')

    first_name = StringOption('FirstName')
    last_name = StringOption('LastName')
    age = IntOption('Age')

will store config in Registry on Windows, in NSUserDefaults on Mac OS X and in json-formatted file everywhere else.

Versioning

The task that every developer is going to face. Fortunately nativeconfig has everything to assist you. Each config is versioned and default to 1.0. Its version is stored in the config backend under the “ConfigVersion” name which can be altered by modifying the CONFIG_VERSION_OPTION_NAME class variable.

You should override it in custom subclass by defining the CONFIG_VERSION variable. Value that usually makes most sense is the __version__ variable. Each time config is instantiated the migrate method is called. Implementation of the base class simply updates value of the “ConfigVersion” (or whatever you called it) option to the actual value. Reasonably, but insufficiently. Let’s see what we can do:

class MyConfig(PreferredConfig):
    CONFIG_VERSION = __version__
    REGISTRY_PATH = r'Software\MyApp'
    JSON_PATH = os.path.expanduser('~/.config/MyApp/config')

    first_name = StringOption('FirstName')
    last_name = StringOption('LastName')

    def migrate(self, version):
        if version is None:
            # Either called for the very first time OR user's backed is broken because it lacks value of the ConfigVersion option.
            pass

        if version <= <newer version>:
            # Obviously <= will not work for strings. You should use your own comparison function that follows you versioning guidelines.
            pass

        if version <= <newest version>:
            # Version should be checked starting from the oldest to the current so you can gracefully migrate even the oldest user's config.
            # `if` is used instead of `elif` for the same reason: you may need to migrate user's data through multiple versions of the config file.
            pass

        if version <= <most recent version>:
            pass


        super().migrage(version)  # always call base class implementation at the end!

TL;DR three simple rules:

  1. Check from the oldest to the newest version

  2. User if instead of elif

  3. Call super at the end

Tests

To run tests, use the python -m test command.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nativeconfig-2.0.6.tar.gz (11.3 kB view details)

Uploaded Source

File details

Details for the file nativeconfig-2.0.6.tar.gz.

File metadata

File hashes

Hashes for nativeconfig-2.0.6.tar.gz
Algorithm Hash digest
SHA256 a423d7a1d927c2ba4272a91a0a767d8241fa6dcb65b8ebb754d6a3e59aa1cf05
MD5 5937bb2bc2f412ae666803c793832173
BLAKE2b-256 55d8451fddb9eeb23ab6c2071ca692fee904afac03f3f876b6e3e29dcfb7c46a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page