Configuration Files and Persistence

A configuration file is a form of object persistence. It contains a serialized, plain-text, editable representation of some default state for an application program. We'll expand on the serialization techniques shown in Chapter 10, Serializing and Saving – JSON, YAML, Pickle, CSV, and XML to create files that are specifically used for application configuration. The focus on plain text means that pickle representation will be excluded. Due to the relative complexity of application configurations, CSV files aren't often used for this purpose, either.

Before a user can make use of an editable plain-text configuration file, we must design our application to be configurable. This can often require careful consideration of dependencies and limitations. Additionally, we must define some kind of configuration object that our application will use. In many cases, a configuration will be based on default values; we might want to allow for system-wide defaults with user-specific overrides to those defaults. We'll explore six representations for the configuration data, as follows:

  • INI files use a format that was pioneered as part of Windows. The file is popular, in part, because it is an incumbent among available formats and has a long history.
  • PY files are plain-old Python code. They have numerous advantages because of the familiarity and simplicity of the syntax. The configuration can be used by an import statement in the application.
  • JSON and YAML are both designed to be user-friendly and easy to edit.
  • Properties files are often used in a Java environment. They're relatively easy to work with and they are also designed to be human-friendly. There's no built-in parser for this, and this chapter includes regular expressions for this file format.
  • XML files are popular but they are wordy, which means that, sometimes, they are difficult to edit properly. macOS uses an XML-based format called a property list or a PLIST file.

Each of these forms offers us some advantages and some disadvantages; there's no single technique that can be described as the best. In many cases, the choice is based on familiarity and compatibility with other software. In Chapter 15Design Principles and Patterns, we'll return to this topic of configuration. Additionally, in later chapters, we'll make extensive use of configuration options. In this chapter, we will cover the following topics:

  • Configuration file use cases
  • Representation, persistence, state, and usability
  • Storing configurations in INI files and PY files
  • Handling more literals via the eval() variant
  • Storing configurations in PY files
  • Why exec() is a non-problem
  • Using ChainMap for defaults and overrides
  • Storing configurations in JSON or YAML
  • Using XML files such as PLIST and others
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset