PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/doc/configuration.rst

https://bitbucket.org/pypy/pypy/
ReStructuredText | 191 lines | 138 code | 53 blank | 0 comment | 0 complexity | 82baa91558f0756df9f5710496ade8a8 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. PyPy's Configuration Handling
  2. =============================
  3. Due to more and more available configuration options it became quite annoying to
  4. hand the necessary options to where they are actually used and even more
  5. annoying to add new options. To circumvent these problems configuration
  6. management was introduced. There all the necessary options are stored in a
  7. configuration object, which is available nearly everywhere in the `RPython
  8. toolchain`_ and in the standard interpreter so that adding new options becomes
  9. trivial. Options are organized into a tree. Configuration objects can be
  10. created in different ways, there is support for creating an optparse command
  11. line parser automatically.
  12. .. _RPython toolchain: https://rpython.readthedocs.org/
  13. Main Assumption
  14. ---------------
  15. Configuration objects are produced at the entry points and handed down to
  16. where they are actually used. This keeps configuration local but available
  17. everywhere and consistent. The configuration values are created using the
  18. command line.
  19. API Details
  20. -----------
  21. The handling of options is split into two parts: the description of which
  22. options are available, what their possible values and defaults are and how they
  23. are organized into a tree. A specific choice of options is bundled into a
  24. configuration object which has a reference to its option description (and
  25. therefore makes sure that the configuration values adhere to the option
  26. description).
  27. This splitting is remotely similar to the distinction between types and
  28. instances in the type systems of the rtyper: the types describe what sort of
  29. fields the instances have.
  30. The Options are organized in a tree. Every option has a name, as does every
  31. option group. The parts of the full name of the option are separated by dots:
  32. e.g. ``config.translation.thread``.
  33. Description of Options
  34. ~~~~~~~~~~~~~~~~~~~~~~
  35. All the constructors take a ``name`` and a ``doc`` argument as first arguments
  36. to give the option or option group a name and to document it. Most constructors
  37. take a ``default`` argument that specifies the default value of the option. If
  38. this argument is not supplied the default value is assumed to be ``None``.
  39. Most constructors
  40. also take a ``cmdline`` argument where you can specify what the command line
  41. option should look like (for example cmdline="-v --version"). If ``cmdline`` is
  42. not specified a default cmdline option is created that uses the name of the
  43. option together with its full path. If ``None`` is passed in as ``cmdline`` then
  44. no command line option is created at all.
  45. Some options types can specify requirements to specify that a particular choice
  46. for one option works only if a certain choice for another option is used. A
  47. requirement is specified using a list of pairs. The first element of the pair
  48. gives the path of the option that is required to be set and the second element
  49. gives the required value.
  50. ``OptionDescription``
  51. +++++++++++++++++++++
  52. This class is used to group suboptions.
  53. ``__init__(self, name, doc, children)``
  54. ``children`` is a list of option descriptions (including
  55. ``OptionDescription`` instances for nested namespaces).
  56. ``ChoiceOption``
  57. ++++++++++++++++
  58. Represents a choice out of several objects. The option can also have the value
  59. ``None``.
  60. ``__init__(self, name, doc, values, default=None, requires=None, cmdline=DEFAULT)``
  61. ``values`` is a list of values the option can possibly take,
  62. ``requires`` is a dictionary mapping values to lists of of two-element
  63. tuples.
  64. ``BoolOption``
  65. ++++++++++++++
  66. Represents a choice between ``True`` and ``False``.
  67. ``__init__(self, name, doc, default=None, requires=None, suggests=None, cmdline=DEFAULT, negation=True)``
  68. ``default`` specifies the default value of the option. ``requires`` is
  69. a list of two-element tuples describing the requirements when the
  70. option is set to true, ``suggests`` is a list of the same structure but
  71. the options in there are only suggested, not absolutely necessary. The
  72. difference is small: if the current option is set to True, both the
  73. required and the suggested options are set. The required options cannot
  74. be changed later, though. ``negation`` specifies whether the negative
  75. commandline option should be generated.
  76. ``IntOption``
  77. +++++++++++++
  78. Represents a choice of an integer.
  79. ``__init__(self, name, doc, default=None, cmdline=DEFAULT)``
  80. ``FloatOption``
  81. +++++++++++++++
  82. Represents a choice of a floating point number.
  83. ``__init__(self, name, doc, default=None, cmdline=DEFAULT)``
  84. ``StrOption``
  85. +++++++++++++
  86. Represents the choice of a string.
  87. ``__init__(self, name, doc, default=None, cmdline=DEFAULT)``
  88. Configuration Objects
  89. ~~~~~~~~~~~~~~~~~~~~~
  90. ``Config`` objects hold the chosen values for the options (of the default,
  91. if no choice was made). A ``Config`` object is described by an
  92. ``OptionDescription`` instance. The attributes of the ``Config`` objects are the
  93. names of the children of the ``OptionDescription``. Example::
  94. >>> from rpython.config.config import OptionDescription, Config, BoolOption
  95. >>> descr = OptionDescription("options", "", [
  96. ... BoolOption("bool", "", default=False)])
  97. >>>
  98. >>> config = Config(descr)
  99. >>> config.bool
  100. False
  101. >>> config.bool = True
  102. >>> config.bool
  103. True
  104. Description of the (useful) methods on ``Config``:
  105. ``__init__(self, descr, **overrides)``:
  106. ``descr`` is an instance of ``OptionDescription`` that describes the
  107. configuration object. ``overrides`` can be used to set different default
  108. values (see method ``override``).
  109. ``override(self, overrides)``:
  110. override default values. This marks the overridden values as defaults,
  111. which makes it possible to change them (you can usually change values
  112. only once). ``overrides`` is a dictionary of path strings to values.
  113. ``set(self, **kwargs)``:
  114. "do what I mean"-interface to option setting. Searches all paths
  115. starting from that config for matches of the optional arguments and sets
  116. the found option if the match is not ambiguous.
  117. Production of optparse Parsers
  118. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  119. To produce an optparse parser use the function ``to_optparse``. It will create
  120. an option parser using callbacks in such a way that the config object used for
  121. creating the parser is updated automatically.
  122. ``to_optparse(config, useoptions=None, parser=None)``:
  123. Returns an optparse parser. ``config`` is the configuration object for
  124. which to create the parser. ``useoptions`` is a list of options for
  125. which to create command line options. It can contain full paths to
  126. options or also paths to an option description plus an additional ".*"
  127. to produce command line options for all sub-options of that description.
  128. If ``useoptions`` is ``None``, then all sub-options are turned into
  129. cmdline options. ``parser`` can be an existing parser object, if
  130. ``None`` is passed in, then a new one is created.
  131. The usage of config objects in PyPy
  132. -----------------------------------
  133. The two large parts of PyPy, the Python interpreter and the RPython
  134. toolchain, have two separate sets of options. The translation toolchain options
  135. can be found on the ``config`` attribute of all ``TranslationContext``
  136. instances and are described in :source:`rpython/config/translationoption.py`. The interpreter options
  137. are attached to the object space, also under the name ``config`` and are
  138. described in :source:`pypy/config/pypyoption.py`.