upgrading.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. .. _upgrading:
  2. Upgrading
  3. =========
  4. This section outlines any information and changes that might need to be made
  5. in order to update your application built on previous versions of Cement.
  6. Upgrading from 2.6.x to 2.8.x
  7. -----------------------------
  8. Cement 2.8 introduced a few incompatible changes from the previous 2.6 stable
  9. release, as noted in the :ref:`ChangeLog <changelog>`.
  10. TypeError: my_signal_hook() takes exactly 2 arguments (3 given)
  11. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  12. In Cement 2.6, functions registered to the ``signal`` hook were only
  13. expected/required to accept the ``signum`` and ``frame`` arguments, however
  14. ``signal`` hook functions must now also accept the ``app`` object as an
  15. argument as well.
  16. After upgrading to Cement 2.8, you might receive something similar to the
  17. following exception:
  18. .. code-block:: console
  19. TypeError: my_signal_hook() takes exactly 2 arguments (3 given)
  20. The fix is to simply prefix any ``signal`` hook functions with an ``app``
  21. argument.
  22. For example:
  23. .. code-block:: python
  24. def my_signal_hook(signum, frame):
  25. pass
  26. Would need to be:
  27. .. code-block:: python
  28. def my_signal_hook(app, signum, frame):
  29. pass
  30. Related:
  31. * :issue:`311`
  32. TypeError: render() got an unexpected keyword argument
  33. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  34. In Cement 2.6, output handlers were not required to accept ``**kwargs``,
  35. however this is now required to allow applications to mix different types of
  36. output handlers together that might support different features/usage.
  37. After upgrading to Cement 2.8, you might receive something similar to the
  38. following exception:
  39. .. code-block:: console
  40. TypeError: render() got an unexpected keyword argument
  41. This would most likely be the case because you have created your own custom
  42. output handler, or are using a third-party output handler that has not been
  43. updated to support Cement 2.8 yet. The fix is to simply add ``**kwargs`` to
  44. the end of the `render()` method.
  45. For example:
  46. .. code-block:: python
  47. def render(self, data):
  48. pass
  49. Would need to be:
  50. .. code-block:: python
  51. def render(self, data, **kwargs):
  52. pass
  53. CementApp.Meta.exit_on_close Defaults to False
  54. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  55. In Cement 2.6, the feature to call ``sys.exit()`` when ``app.close()`` is
  56. called was implemented, however defaulting it to ``True`` is not the ideal
  57. behavior. The default is now ``False``, making it the developers option to
  58. explicitly enable it.
  59. To revert the change, and default ``exit_on_close`` to ``True``, simply set it
  60. in ``CementApp.Meta.exit_on_close``:
  61. .. code-block:: python
  62. from cement.core.foundation import CementApp
  63. class MyApp(CementApp):
  64. class Meta:
  65. label = 'myapp'
  66. exit_on_close = True
  67. Upgrading from 2.4.x to 2.6.x
  68. -----------------------------
  69. Cement 2.6 introduced a few incompatible changes from the previous 2.4 stable
  70. release, as noted in the :ref:`ChangeLog <changelog>`.
  71. InterfaceError: Invalid handler ... missing '_meta.label'.
  72. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  73. Prior to Cement 2.5.2, ``CementBaseController.Meta.label`` defaulted to
  74. ``base``. The new default is ``None``, causing the potential for breakage of
  75. a controller that did not explicity set the ``label`` meta option.
  76. You can resolve this error by explicity setting the ``label`` meta option:
  77. .. code-block:: python
  78. class MyBaseController(CementBaseController):
  79. class Meta:
  80. label = 'base'
  81. Upgrading from 2.2.x to 2.4.x
  82. -----------------------------
  83. Cement 2.4 introduced a few incompatible changes from the previous 2.2 stable
  84. release, as noted in the :ref:`ChangeLog <changelog>`.
  85. Related:
  86. * :issue:`308`
  87. CementApp.render() Prints Output Without Calling print()
  88. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  89. Before Cement 2.3.2 the ``app.render()`` function did not actually print
  90. anything, therefore you would have to call ``print app.render()``. This
  91. now defaults to writing output to ``sys.stdout``, but can be modified for the
  92. older behavior by passing ``out=None`` when calling it:
  93. .. code-block:: python
  94. app.render(data, out=None)
  95. Additionally, you can also now write directly to a file:
  96. .. code-block:: python
  97. myfile = open('/path/to/myfile', 'w')
  98. app.render(data, out=myfile)
  99. myfile.close()
  100. error: unrecognized arguments: --json/--yaml
  101. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  102. After upgrading to Cement > 2.3.2 you might encounter the error:
  103. .. code-block:: text
  104. error: unrecognized arguments: --json
  105. Or similar errors like:
  106. .. code-block:: text
  107. error: unrecognized arguments: --yaml
  108. This is due to a design change, and a new feature allowing the end user to
  109. optionally override handlers via command line. Rather than having a unique
  110. option for every type of output handler, you now have one option that allows
  111. overriding the defined output handler by passing it the handler label.
  112. Note that only handlers that have ``overridable = True`` in their meta-data
  113. will be valid options.
  114. To resolve this issue, you simply need to pass ``-o json`` or ``-o yaml`` at
  115. command line to override the default output handler.
  116. Related:
  117. * :issue:`229`
  118. NoSectionError: No section: 'log'
  119. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  120. After upgrading to Cement > 2.3.2 you might encounter the error:
  121. .. code-block:: text
  122. NoSectionError: No section: 'log'
  123. In previous versions of Cement < 2.3.2, the default logging configuration
  124. section in the config file was ``[log]``. This has been changed to
  125. ``[log.logging]`` in order to be consistent with all other handler
  126. configuration sections.
  127. Another issue you might encounter due to the above change is that log related
  128. configuration settings read from a configuration file would no longer work.
  129. The necessary change to resolve this issue is to change all references of
  130. ``log`` in relation to the log configuration section, to ``log.logging``.
  131. Related:
  132. * :issue:`227`
  133. TypeError: load() takes no arguments (1 given)
  134. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  135. After upgrading to Cement > 2.3.2 you might encounter the error:
  136. .. code-block:: text
  137. TypeError: load() takes no arguments (1 given)
  138. Previous versions of Cement < 2.3.2 did not require an `app` argument to be
  139. passed to the `load()` functions of extensions/plugins/bootstrap modules.
  140. In Cement > 2.3.2 all extension/plugins/bootstrap modules must accept a single
  141. argument named `app` which is the application object in its current state when
  142. `load()` is called.
  143. To resolve this issue simply modify all relevant `load()` functions to accept
  144. the `app` argument. For example:
  145. .. code-block:: python
  146. def load():
  147. pass
  148. To:
  149. .. code-block:: python
  150. def load(app):
  151. pass
  152. Upgrading from 2.0.x to 2.2.x
  153. -----------------------------
  154. Cement 2.2 introduced a few incompatible changes from the previous 2.0 stable
  155. release, as noted in the :ref:`Changelog <changelog>`.
  156. ImportError: cannot import name version
  157. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  158. When attempting to install Cement > 2.1 on a system that already has an older
  159. version of Cement < 2.1 you will likely run into this error:
  160. .. code-block:: text
  161. ImportError: cannot import name version
  162. Currently we do not have a way to resolve this programatically in Cement. The
  163. resolution is to remove the older version of Cement < 2.1, and then re-install
  164. the newer version.
  165. Related:
  166. * :issue:`237`
  167. FrameworkError: Duplicate Arguments/Commands
  168. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  169. After upgrading, you might encounter one or both of the following errors
  170. related to application controllers:
  171. .. code-block:: text
  172. cement.core.exc.FrameworkError: Duplicate command named 'mycommand' found
  173. in controller '<__main__.MySecondController object at 0x10669ab50>'
  174. .. code-block:: text
  175. cement.core.exc.FrameworkError: argument -f/--foo: conflicting option
  176. string(s): -f, --foo
  177. This is likely due to a change in how application controllers are configured.
  178. By default, all controllers are of type `embedded`, meaning that their
  179. arguments and commands are added to the parent controller. To resolve this
  180. issue you can change the `stacked_type` to `nested`, meaning that the stacked
  181. controller will be an additional sub-command under the parent (nesting a new
  182. level commands/arguments).
  183. For example:
  184. .. code-block:: python
  185. class MyStackedController(CementBaseController):
  186. class Meta:
  187. label = 'my_stacked_controller'
  188. stacked_on = 'base'
  189. stacked_type = 'nested'
  190. Related:
  191. * :issue:`234`
  192. Discontinued use of Setuptools Namespace Packages
  193. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  194. Previous versions of Cement utilitized Setuptools namespace packages in order
  195. to allow external libraries (such as optional framework extensions) to use the
  196. ``cement.ext`` namespace. Meaning that an extension packaged separately could
  197. use the namespace ``cement.ext.ext_myextension`` and be imported from the
  198. ``cement.ext`` namespace as if it were shipped with the mainline sources
  199. directly. This indirectly caused issues with certain IDE's due to the fact
  200. that namespace packages do not install a proper ``__init__.py`` and are
  201. handled differently by Setuptools.
  202. With the move to merging optional extenions into mainline sources, we no
  203. longer require the use of Setuptools namespace packages. That said, if a
  204. developer had created their own extension using the ``cement.ext`` namespace,
  205. that extension would no longer work or worse may confusing Python into
  206. attempting to load ``cement.ext`` from the extension and not Cement causing
  207. even bigger problems.
  208. To resolve this issue, simply change the extension module to anything
  209. other than ``cement.ext``, such as ``myapp.ext``.
  210. Related:
  211. * :issue:`202`
  212. LoggingLogHandler Changes
  213. ^^^^^^^^^^^^^^^^^^^^^^^^^
  214. The ``clear_loggers`` meta option is now a ``list``, rather than a
  215. ``boolean``. Therefore, rather than telling LoggingLogHandler to 'clear
  216. all previously defined loggers', you are telling it to 'clear only these
  217. previously defined loggers' in the list.
  218. If your application utilizied the ``LoggingLogHandler.Meta.clear_loggers``
  219. option, you would simply need to change it from a ``boolean`` to a list of
  220. loggers such as ``['myapp', 'some_other_logging_namespace']``.
  221. Related:
  222. * :issue:`163`
  223. ConfigParserConfigHandler Changes
  224. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  225. The ``ConfigParserConfigHandler.has_key()`` function has been removed. To
  226. update your application for these changes, you would look for all code
  227. similar to the following:
  228. .. code-block:: python
  229. if myapp.config.has_key('mysection', 'mykey'):
  230. # ...
  231. And modify it to something similar to:
  232. .. code-block:: python
  233. if 'mykey' in myapp.config.keys('mysection'):
  234. # ...
  235. Related:
  236. * :issue:`173`
  237. CementApp Changes
  238. ^^^^^^^^^^^^^^^^^
  239. The ``CementApp.get_last_rendered()`` function has been deprected. Developers
  240. should now use the ``CementApp.last_rendered`` property instead. To update
  241. your application for these changes, you would look for all code similar to:
  242. .. code-block:: python
  243. CementApp.get_last_rendered()
  244. And modify it to something similar to:
  245. .. code-block:: python
  246. CementApp.last_rendered
  247. Related:
  248. * :issue:`201` - Add Deprecation Warning for CementApp.get_last_rendered()
  249. CementBaseController Changes
  250. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  251. All short-cuts such as ``log``, ``pargs``, etc have been removed from
  252. CementBaseController due to the fact that these class members could clash
  253. if the developer added a command/function of the same name. To update
  254. your application for these changes, in any classes that subclass from
  255. ``CementBaseController``, you might need to modify references to ``self.log``,
  256. ``self.pargs``, etc to ``self.app.log``, ``self.app.pargs``, etc.
  257. Additionally, if you wish to re-implement these or other shortcuts, you can
  258. do so by overriding ``_setup()`` in your controller code, and add something
  259. similar to the following:
  260. .. code-block:: python
  261. def _setup(self, *args, **kw):
  262. res = super(MyClass, self)._setup(*args, **kw)
  263. self.log = self.app.log
  264. self.pargs = self.app.pargs
  265. # etc
  266. return res
  267. An additional change to ``CementBaseController`` is that the application's
  268. ``base`` controller attached to ``YourApp.Meta.base_controller`` now must
  269. have a label of ``base``. Previously, the base controller could have any
  270. label however this is now a hard requirement. To update your application
  271. for these changes, simply change the label of your base controller to
  272. ``base``.
  273. Finally, the ``CementBaseController`` used to have members called ``hidden``,
  274. ``visible``, and ``exposed`` which were each a list of controller functions
  275. used for handling dispatch of commands, and how they are displayed in
  276. ``--help``. These members no longer exist.
  277. These members were never documented, and is very unlikely that anybody has
  278. ever used them directly. Updating your application for these changes would
  279. be outside the scope of this document.
  280. Related:
  281. * :issue:`141`
  282. * :issue:`167`
  283. * :issue:`179`
  284. Backend Changes
  285. ^^^^^^^^^^^^^^^
  286. Several backend pieces have been moved or renamed. For example
  287. ``cement.core.backend.handlers`` is now ``cement.core.backend.__handlers__``,
  288. etc. The same goes for ``cement.core.backend.SAVED_STDOUT`` which is now
  289. ``cement.core.backend.__saved_stdout__``. These are undocumented, and used
  290. specifically by Cement. It is unlikely that anyone has used these members
  291. directly, and updating your application for these changes is outside the
  292. scope of this document. See ``cement.core.backend`` to assess what, if any,
  293. change you may need to change in your code to compensate for these changes.
  294. The ``cement.core.backend.defaults()`` function has moved to
  295. ``cement.utils.misc.init_defaults()``. It's usage is exactly the same.
  296. The ``cement.core.backend.minimal_logger()`` function has moved to
  297. ``cement.utils.misc.minimal_logger``. It's usage is also the same.
  298. Related:
  299. * :issue:`177`
  300. * :issue:`178`