PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/config/index.rst

https://gitlab.com/Rockyspade/astropy
ReStructuredText | 370 lines | 278 code | 92 blank | 0 comment | 0 complexity | 819c562948b45c9f96ba2fcfc122b46b MD5 | raw file
  1. .. doctest-skip-all
  2. .. _astropy_config:
  3. ***************************************
  4. Configuration system (`astropy.config`)
  5. ***************************************
  6. Introduction
  7. ============
  8. The astropy configuration system is designed to give users control of various
  9. parameters used in astropy or affiliated packages without delving into the
  10. source code to make those changes.
  11. .. note::
  12. The configuration system got a major overhaul in astropy 0.4 as
  13. part of APE3. See :ref:`config-0-4-transition` for information
  14. about updating code to use the new API.
  15. Getting Started
  16. ===============
  17. The Astropy configuration options are most easily set by modifying the
  18. configuration file. It will be automatically generated with all the
  19. default values commented out the first time you import Astropy. You
  20. can find the exact location by doing::
  21. >>> from astropy.config import get_config_dir
  22. >>> get_config_dir()
  23. And you should see the location of your configuration directory. The standard
  24. scheme generally puts your configuration directory in ``$HOME/.astropy/config``,
  25. but if you've set the environment variable ``XDG_CONFIG_HOME`` and the
  26. ``$XDG_CONFIG_HOME/astropy`` directory exists, it will instead be there.
  27. .. note::
  28. This is a slight variation from the behavior of most Linux
  29. applications with respect to ``$XDG_CONFIG_HOME``, where the
  30. default, if ``$XDG_CONFIG_HOME`` is not defined, would be to put
  31. configuration in ``~/.config/astropy``.
  32. Once you've found the configuration file, open it with your favorite editor.
  33. It should have all of the sections you might want, with descriptions and the
  34. type of the value that is accepted. Feel free to edit this as you wish, and
  35. any of these changes will be reflected when you next start Astropy. Or, if you
  36. want to see your changes immediately in your current Astropy session, just do::
  37. >>> from astropy.config import reload_config
  38. >>> reload_config()
  39. .. note::
  40. If for whatever reason your ``$HOME/.astropy`` directory is not accessible
  41. (i.e., you have astropy running somehow as root but you are not the root
  42. user), the best solution is to set the ``XDG_CONFIG_HOME`` and
  43. ``XDG_CACHE_HOME`` environment variables pointing to directories, and create
  44. an ``astropy`` directory inside each of those. Both the configuration and
  45. data download systems will then use those directories and never try to
  46. access the ``$HOME/.astropy`` directory.
  47. Using `astropy.config`
  48. ======================
  49. Accessing Values
  50. ----------------
  51. By convention, configuration parameters live inside of objects called
  52. ``conf`` at the root of each subpackage. For example, configuration
  53. parameters related to data files live in ``astropy.utils.data.conf``.
  54. This object has properties for getting and setting individual
  55. configuration parameters. For instance to get the default URL for
  56. astropy remote data do::
  57. >>> from astropy.utils.data import conf
  58. >>> conf.dataurl
  59. 'http://data.astropy.org/'
  60. Changing Values at Run-time
  61. ---------------------------
  62. Changing configuration values persistently is done by editing the
  63. configuration file as described above. Values can also, however, be
  64. modified in an active python session by setting any of the properties
  65. on a ``conf`` object.
  66. For example, if there is a part of your configuration file that looks
  67. like:
  68. .. code-block:: ini
  69. [utils.data]
  70. # URL for astropy remote data site.
  71. dataurl = http://data.astropy.org/
  72. # Time to wait for remote data query (in seconds).
  73. remote_timeout = 3.0
  74. You should be able to modify the values at run-time this way::
  75. >>> from astropy.utils.data import conf
  76. >>> conf.dataurl
  77. 'http://data.astropy.org/'
  78. >>> conf.dataurl = 'http://astropydata.mywebsite.com'
  79. >>> conf.dataurl
  80. 'http://astropydata.mywebsite.com'
  81. >>> conf.remote_timeout
  82. 3.0
  83. >>> conf.remote_timeout = 4.5
  84. >>> conf.remote_timeout
  85. 4.5
  86. Reloading Configuration
  87. -----------------------
  88. Instead of modifying the variables in python, you can also modify the
  89. configuration files and then reload them. For example, if you modify the
  90. configuration file to say:
  91. .. code-block:: ini
  92. [utils.data]
  93. # URL for astropy remote data site.
  94. dataurl = http://myotherdata.mywebsite.com/
  95. # Time to wait for remote data query (in seconds).
  96. remote_timeout = 6.3
  97. And then run the following commands::
  98. >>> conf.reload('dataurl')
  99. >>> conf.reload('remote_timeout')
  100. This should update the variables with the values from the configuration file::
  101. >>> conf.dataurl
  102. 'http://myotherdata.mywebsite.com/'
  103. >>> conf.remote_timeout
  104. 6.3
  105. You can reload all configuration parameters of a ``conf`` object at
  106. once by calling ``reload`` with no parameters::
  107. >>> conf.reload()
  108. Or if you want to reload all astropy configuration at once, use the
  109. `~astropy.config.reload_config` function::
  110. >>> config.reload_config('astropy')
  111. You can also reset a configuration parameter back to its default value. Note that this is the default value defined in the Python code, and has nothing to do with the configuration file on disk::
  112. >>> conf.reset('dataurl')
  113. >>> conf.dataurl
  114. 'http://data.astropy.org/'
  115. Upgrading astropy
  116. -----------------
  117. Each time you upgrade to a new major version of astropy, the
  118. configuration parameters may have changed.
  119. If you never edited your configuration file, there is nothing for you
  120. to do. It will automatically be replaced with a configuration file
  121. template for the newly installed version of astropy.
  122. If you did customize your configuration file, it will not be touched.
  123. Instead, a new configuration file template will be installed alongside
  124. it with the version number in the filename, for example
  125. ``astropy.0.4.cfg``. You can compare this file to your
  126. ``astropy.cfg`` file to see what needs to be changed or updated.
  127. .. _config-developer:
  128. Adding new configuration items
  129. ==============================
  130. Configuration items should be used wherever an option or setting is
  131. needed that is either tied to a system configuration or should persist
  132. across sessions of astropy or an affiliated package. Options that may
  133. affect the results of science calculations should not be configuration
  134. items, but should instead be `astropy.utils.state.ScienceState`, so
  135. it's possible to reproduce science results without them being affected
  136. by configuration parameters set in a particular environment.
  137. Admittedly, this is only a guideline, as the precise cases where a
  138. configuration item is preferred over, say, a keyword option for a
  139. function is somewhat personal preference. It is the preferred form of
  140. persistent configuration, however, and astropy packages must all use
  141. it (and it is recommended for affiliated packages).
  142. The reference guide below describes the interface for creating a
  143. ``conf`` object with a number of configuration parameters. They
  144. should be defined at the top level, i.e. in the ``__init__.py`` of
  145. each subpackage that has configuration items::
  146. """ This is the docstring at the beginning of a module
  147. """
  148. from astropy import config as _config
  149. class Conf(_config.ConfigNamespace):
  150. """
  151. Configuration parameters for my subpackage.
  152. """
  153. some_setting = _config.ConfigItem(
  154. 1, 'Description of some_setting')
  155. another_setting = _config.ConfigItem(
  156. 'string value', 'Description of another_setting')
  157. # Create an instance for the user
  158. conf = Conf()
  159. ... implementation ...
  160. def some_func():
  161. #to get the value of these options, I might do:
  162. something = conf.some_setting + 2
  163. return conf.another_setting + ' Also, I added text.'
  164. The configuration items also need to be added to the config file
  165. template. For astropy, this file is in ``astropy/astropy.cfg``. For
  166. an affiliated package called, for example, ``packagename``, the file
  167. is in ``packagename/packagename.cfg``. For the example above, the
  168. following content would be added to the config file template:
  169. .. code-block:: ini
  170. [subpackage]
  171. ## Description of some_setting
  172. # some_setting = 1
  173. ## Description of another_setting
  174. # another_setting = foo
  175. Note that the key/value pairs are commented out. This will allow for
  176. changing the default values in a future version of astropy without
  177. requiring the user to edit their configuration file to take advantage
  178. of the new defaults. By convention, the descriptions of each
  179. parameter are in comment lines starting with two hash characters
  180. (``##``) to distinguish them from commented out key/value pairs.
  181. Item Types and Validation
  182. -------------------------
  183. If not otherwise specified, a `~astropy.config.ConfigItem` gets its
  184. type from the type of the ``defaultvalue`` it is given when it is
  185. created. The item can only be set to be an object of this type.
  186. Hence::
  187. some_setting = ConfigItem(1, 'A description.')
  188. ...
  189. conf.some_setting = 1.2
  190. will fail, because ``1.2`` is a float and ``1`` is an int.
  191. Note that if you want the configuration item to be limited to a
  192. particular set of options, you should pass in a list as the
  193. ``defaultvalue`` option. The first entry in the list will be taken as
  194. the default, and the list as a whole gives all the valid options. For
  195. example::
  196. an_option = ConfigItem(
  197. ['a', 'b', 'c'],
  198. "This option can be 'a', 'b', or 'c'")
  199. ...
  200. conf.an_option = 'b' # succeeds
  201. conf.an_option = 'c' # succeeds
  202. conf.an_option = 'd' # fails!
  203. conf.an_option = 6 # fails!
  204. Finally, a `~astropy.config.ConfigItem` can be explicitly given a type
  205. via the ``cfgtype`` option::
  206. an_int_setting = ConfigItem(
  207. 1, 'A description.', cfgtype='integer')
  208. ...
  209. conf.an_int_setting = 3 # works fine
  210. conf.an_int_setting = 4.2 # fails!
  211. If the default value's type doesn't match ``cfgtype``, the
  212. `~astropy.config.ConfigItem` cannot be created::
  213. an_int_setting = ConfigItem(
  214. 4.2, 'A description.', cfgtype='integer')
  215. In summary, the default behavior (of automatically determining ``cfgtype``)
  216. is usually what you want. The main exception is when you want your
  217. configuration item to be a list. The default behavior will treat that
  218. as a list of *options* unless you explicitly tell it that the
  219. `~astropy.config.ConfigItem` itself is supposed to be a list::
  220. a_list_setting = ConfigItem([1, 2, 3], 'A description.')
  221. a_list_setting = ConfigItem([1, 2, 3], 'A description.', cfgtype='list')
  222. Details of all the valid ``cfgtype`` items can be found in the
  223. `validation section of the configobj manual
  224. <http://www.voidspace.org.uk/python/validate.html#the-standard-functions>`_.
  225. Below is a list of the valid values here for quick reference:
  226. * 'integer'
  227. * 'float'
  228. * 'boolean'
  229. * 'string'
  230. * 'ip_addr'
  231. * 'list'
  232. * 'tuple'
  233. * 'int_list'
  234. * 'float_list'
  235. * 'bool_list'
  236. * 'string_list'
  237. * 'ip_addr_list'
  238. * 'mixed_list'
  239. * 'option'
  240. * 'pass'
  241. Usage Tips
  242. ----------
  243. Keep in mind is that `~astropy.config.ConfigItem` objects can be
  244. changed at runtime by users. So it is always recommended to read their
  245. values immediately before use instead of just storing their initial
  246. value to some other variable (or used as a default for a
  247. function). For example, the following will work, but is incorrect
  248. usage::
  249. def some_func(val=conf.some_setting):
  250. return val + 2
  251. This works fine as long as the user doesn't change its value during
  252. runtime, but if they do, the function won't know about the change::
  253. >>> some_func()
  254. 3
  255. >>> conf.some_setting = 3
  256. >>> some_func() # naively should return 5, because 3 + 2 = 5
  257. 3
  258. There are two ways around this. The typical/intended way is::
  259. def some_func():
  260. """
  261. The `SOME_SETTING` configuration item influences this output
  262. """
  263. return conf.some_setting + 2
  264. Or, if the option needs to be available as a function parameter::
  265. def some_func(val=None):
  266. """
  267. If not specified, `val` is set by the `SOME_SETTING` configuration item.
  268. """
  269. return (conf.some_setting if val is None else val) + 2
  270. See Also
  271. ========
  272. .. toctree::
  273. :maxdepth: 2
  274. config_0_4_transition
  275. :doc:`/logging` (overview of `astropy.logger`)
  276. Reference/API
  277. =============
  278. .. automodapi:: astropy.config