quickstart.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. .. MediaGoblin Documentation
  2. Written in 2011, 2012 by MediaGoblin contributors
  3. To the extent possible under law, the author(s) have dedicated all
  4. copyright and related and neighboring rights to this software to
  5. the public domain worldwide. This software is distributed without
  6. any warranty.
  7. You should have received a copy of the CC0 Public Domain
  8. Dedication along with this software. If not, see
  9. <http://creativecommons.org/publicdomain/zero/1.0/>.
  10. ===========
  11. Quick Start
  12. ===========
  13. This is a quick start. It's not comprehensive, but it walks through
  14. writing a basic plugin called "sampleplugin" which logs "I've been
  15. started!" when ``setup_plugin()`` has been called.
  16. .. todo: Rewrite this to be a useful plugin
  17. Step 1: Files and directories
  18. =============================
  19. GNU MediaGoblin plugins are Python projects at heart. As such, you should
  20. use a standard Python project directory tree::
  21. sampleplugin/
  22. |- README
  23. |- LICENSE
  24. |- setup.py
  25. |- sampleplugin/
  26. |- __init__.py
  27. The outer ``sampleplugin`` directory holds all the project files.
  28. The ``README`` should cover what your plugin does, how to install it,
  29. how to configure it, and all the sorts of things a README should
  30. cover.
  31. The ``LICENSE`` should have the license under which you're
  32. distributing your plugin.
  33. The inner ``sampleplugin`` directory is the Python package that holds
  34. your plugin's code.
  35. The ``__init__.py`` denotes that this is a Python package. It also
  36. holds the plugin code and the ``hooks`` dict that specifies which
  37. hooks the sampleplugin uses.
  38. Step 2: README
  39. ==============
  40. Here's a rough ``README``. Generally, you want more information
  41. because this is the file that most people open when they want to learn
  42. more about your project.
  43. ::
  44. README
  45. ======
  46. This is a sample plugin. It logs a line when ``setup__plugin()`` is
  47. run.
  48. Step 3: LICENSE
  49. ===============
  50. GNU MediaGoblin plugins must be licensed under the AGPLv3 or later. So
  51. the LICENSE file should be the AGPLv3 text which you can find at
  52. `<http://www.gnu.org/licenses/agpl-3.0.html>`_
  53. Step 4: setup.py
  54. ================
  55. This file is used for packaging and distributing your plugin.
  56. We'll use a basic one::
  57. from setuptools import setup, find_packages
  58. setup(
  59. name='sampleplugin',
  60. version='1.0',
  61. packages=find_packages(),
  62. include_package_data=True,
  63. install_requires=[],
  64. license='AGPLv3',
  65. )
  66. See `<http://docs.python.org/distutils/index.html#distutils-index>`_
  67. for more details.
  68. Step 5: the code
  69. ================
  70. The code for ``__init__.py`` looks like this:
  71. .. code-block:: python
  72. :linenos:
  73. :emphasize-lines: 12,23
  74. import logging
  75. from mediagoblin.tools.pluginapi import Plugin, get_config
  76. # This creates a logger that you can use to log information to
  77. # the console or a log file.
  78. _log = logging.getLogger(__name__)
  79. # This is the function that gets called when the setup
  80. # hook fires.
  81. def setup_plugin():
  82. _log.info("I've been started!")
  83. config = get_config('sampleplugin')
  84. if config:
  85. _log.info('%r' % config)
  86. else:
  87. _log.info('There is no configuration set.')
  88. # This is a dict that specifies which hooks this plugin uses.
  89. # This one only uses one hook: setup.
  90. hooks = {
  91. 'setup': setup_plugin
  92. }
  93. Line 12 defines the ``setup_plugin`` function.
  94. Line 23 defines ``hooks``. When MediaGoblin loads this file, it sees
  95. ``hooks`` and registers all the callables with their respective hooks.
  96. Step 6: Installation and configuration
  97. ======================================
  98. To install the plugin for development, you need to make sure it's
  99. available to the Python interpreter that's running MediaGoblin.
  100. There are a couple of ways to do this, but we're going to pick the
  101. easy one.
  102. Use ``python`` from your MediaGoblin virtual environment and do::
  103. python setup.py develop
  104. Any changes you make to your plugin will be available in your
  105. MediaGoblin virtual environment.
  106. Then adjust your ``mediagoblin.ini`` file to load the plugin::
  107. [plugins]
  108. [[sampleplugin]]
  109. Step 7: That's it!
  110. ==================
  111. When you launch MediaGoblin, it'll load the plugin and you'll see
  112. evidence of that in the log file.
  113. That's it for the quick start!
  114. Where to go from here
  115. =====================
  116. See the documentation on the :ref:`plugin-api-chapter` for code
  117. samples and other things you can use when building your plugin. If
  118. your plugin needs its own database models, see
  119. :ref:`plugin-database-chapter`.
  120. See `Hitchhiker's Guide to Packaging
  121. <http://guide.python-distribute.org/>`_ for more information on
  122. packaging your plugin.