settings.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. .. _mach_settings:
  2. ========
  3. Settings
  4. ========
  5. Mach can read settings in from a set of configuration files. These
  6. configuration files are either named ``machrc`` or ``.machrc`` and
  7. are specified by the bootstrap script. In mozilla-central, these files
  8. can live in ``~/.mozbuild`` and/or ``topsrcdir``.
  9. Settings can be specified anywhere, and used both by mach core or
  10. individual commands.
  11. Core Settings
  12. =============
  13. These settings are implemented by mach core.
  14. * alias - Create a command alias. This is useful if you want to alias a command to something else, optionally including some defaults. It can either be used to create an entire new command, or provide defaults for an existing one. For example:
  15. .. parsed-literal::
  16. [alias]
  17. mochitest = mochitest -f browser
  18. browser-test = mochitest -f browser
  19. Defining Settings
  20. =================
  21. Settings need to be explicitly defined, along with their type,
  22. otherwise mach will throw when trying to access them.
  23. To define settings, use the :func:`~decorators.SettingsProvider`
  24. decorator in an existing mach command module. E.g:
  25. .. code-block:: python
  26. from mach.decorators import SettingsProvider
  27. @SettingsProvider
  28. class ArbitraryClassName(object):
  29. config_settings = [
  30. ('foo.bar', 'string'),
  31. ('foo.baz', 'int', 0, set([0,1,2])),
  32. ]
  33. ``@SettingsProvider``'s must specify a variable called ``config_settings``
  34. that returns a list of tuples. Alternatively, it can specify a function
  35. called ``config_settings`` that returns a list of tuples.
  36. Each tuple is of the form:
  37. .. code-block:: python
  38. ('<section>.<option>', '<type>', default, extra)
  39. ``type`` is a string and can be one of:
  40. string, boolean, int, pos_int, path
  41. ``default`` is optional, and provides a default value in case none was
  42. specified by any of the configuration files.
  43. ``extra`` is also optional and is a dict containing additional key/value
  44. pairs to add to the setting's metadata. The following keys may be specified
  45. in the ``extra`` dict:
  46. * ``choices`` - A set of allowed values for the setting.
  47. Wildcards
  48. ---------
  49. Sometimes a section should allow arbitrarily defined options from the user, such
  50. as the ``alias`` section mentioned above. To define a section like this, use ``*``
  51. as the option name. For example:
  52. .. parsed-literal::
  53. ('foo.*', 'string')
  54. This allows configuration files like this:
  55. .. parsed-literal::
  56. [foo]
  57. arbitrary1 = some string
  58. arbitrary2 = some other string
  59. Documenting Settings
  60. ====================
  61. All settings must at least be documented in the en_US locale. Otherwise,
  62. running ``mach settings`` will raise. Mach uses gettext to perform localization.
  63. A handy command exists to generate the localization files:
  64. .. parsed-literal::
  65. mach settings locale-gen <section>
  66. You'll be prompted to add documentation for all options in section with the
  67. en_US locale. To add documentation in another locale, pass in ``--locale``.
  68. Accessing Settings
  69. ==================
  70. Now that the settings are defined and documented, they're accessible from
  71. individual mach commands if the command receives a context in its constructor.
  72. For example:
  73. .. code-block:: python
  74. from mach.decorators import (
  75. Command,
  76. CommandProvider,
  77. SettingsProvider,
  78. )
  79. @SettingsProvider
  80. class ExampleSettings(object):
  81. config_settings = [
  82. ('a.b', 'string', 'default'),
  83. ('foo.bar', 'string'),
  84. ('foo.baz', 'int', 0, {'choices': set([0,1,2])}),
  85. ]
  86. @CommandProvider
  87. class Commands(object):
  88. def __init__(self, context):
  89. self.settings = context.settings
  90. @Command('command', category='misc',
  91. description='Prints a setting')
  92. def command(self):
  93. print(self.settings.a.b)
  94. for option in self.settings.foo:
  95. print(self.settings.foo[option])