Files
c64c7691127702131959e57f830c1cd977954e24
nova /HACKING.rst

185 lines
5.1 KiB
ReStructuredText
Raw Normal View History

2010年05月27日 23:05:26 -07:00
Nova Style Commandments
=======================
- Step 2: Read http://www.python.org/dev/peps/pep-0008/ again
- Step 3: Read on
2010年05月27日 23:05:26 -07:00
2011年07月29日 12:49:48 -04:00
General
-------
2011年07月29日 12:54:55 -04:00
- Put two newlines between top-level code (funcs, classes, etc)
- Put one newline between methods in classes and anywhere else
2011年07月29日 12:49:48 -04:00
- Do not write "except:", use "except Exception:" at the very least
2011年07月29日 12:54:55 -04:00
- Include your name with TODOs as in "#TODO(termie)"
- Do not name anything the same name as a built-in or reserved word
2011年07月29日 12:49:48 -04:00
2010年05月27日 23:05:26 -07:00
Imports
-------
2011年07月29日 12:49:48 -04:00
- Do not import objects, only modules
- Do not import more than one module per line
- Do not make relative imports
- Order your imports by the full module path
- Organize your imports according to the following template
2010年05月27日 23:05:26 -07:00
2010年05月27日 23:05:26 -07:00
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2011年07月29日 15:42:10 -04:00
{{stdlib imports in human alphabetical order}}
2010年05月27日 23:05:26 -07:00
\n
{{third-party lib imports in human alphabetical order}}
\n
2011年07月29日 15:42:10 -04:00
{{nova imports in human alphabetical order}}
2010年05月27日 23:05:26 -07:00
\n
\n
{{begin your code}}
Human Alphabetical Order Examples
---------------------------------
2010年05月27日 23:05:26 -07:00
import httplib
import logging
import random
import StringIO
import time
import unittest
import webob.exc
2011年07月29日 12:21:46 -04:00
import nova.api.ec2
from nova.api import openstack
2010年05月27日 23:05:26 -07:00
from nova.auth import users
2011年07月29日 12:21:46 -04:00
import nova.flags
2010年05月27日 23:05:26 -07:00
from nova.endpoint import cloud
2011年07月29日 12:21:46 -04:00
from nova import test
2011年07月29日 12:44:11 -04:00
Docstrings
----------
2011年03月28日 10:46:02 -07:00
"""A one line docstring looks like this and ends in a period."""
2011年03月28日 10:46:02 -07:00
"""A multiline docstring has a one-line summary, less than 80 characters.
Then a new paragraph after a newline that explains in more detail any
general information about the function, class or method. Example usages
2011年03月29日 11:15:16 -07:00
are also great to have here if it is a complex class for function. After
you have finished your descriptions add an extra newline and close the
quotations.
2011年02月12日 13:30:31 -08:00
When writing the docstring for a class, an extra line should be placed
after the closing quotations. For more in-depth explanations for these
decisions see http://www.python.org/dev/peps/pep-0257/
2011年03月28日 10:46:02 -07:00
If you are going to describe parameters and return values, use Sphinx, the
appropriate syntax is as follows.
:param foo: the foo parameter
:param bar: the bar parameter
2011年07月29日 12:21:46 -04:00
:returns: return_type -- description of the return value
2011年07月29日 15:26:14 -04:00
:returns: description of the return value
2011年07月29日 12:21:46 -04:00
:raises: AttributeError, KeyError
"""
2011年07月29日 12:21:46 -04:00
2011年07月29日 12:44:11 -04:00
2011年07月29日 12:21:46 -04:00
Dictionaries/Lists
------------------
should be split with newlines. Embedded iterables should have their items
indented. Additionally, the last item in the dictionary should have a trailing
comma. This increases readability and simplifies future diffs.
Example::
my_dictionary = {
"image": {
"name": "Just a Snapshot",
"size": 2749573,
"properties": {
"user_id": 12,
"arch": "x86_64",
},
"things": [
"thing_one",
"thing_two",
],
"status": "ACTIVE",
},
}
2011年07月29日 12:44:11 -04:00
Calling Methods
---------------
newlines. This is not a requirement, but a guideline::
2011年07月29日 12:21:46 -04:00
unnecessarily_long_function_name('string one',
'string two',
kwarg1=constants.ACTIVE,
kwarg2=['a', 'b', 'c'])
2011年07月29日 12:21:46 -04:00
list_of_strings = [
'what_a_long_string',
'not as long',
]
dict_of_numbers = {
'one': 1,
'two': 2,
'twenty four': 24,
}
2011年07月29日 12:21:46 -04:00
object_one.call_a_method('string three',
'string four',
kwarg1=list_of_strings,
kwarg2=dict_of_numbers)
2011年07月29日 12:44:11 -04:00
2011年07月29日 13:51:49 -04:00
2011年07月29日 12:44:11 -04:00
Internationalization (i18n) Strings
2011年07月29日 15:43:23 -04:00
-----------------------------------
automatic translations of exception and log strings.
Example::
2011年07月29日 12:44:11 -04:00
msg = _("An error occurred")
raise HTTPBadRequest(explanation=msg)
template string then do the replacement.
Example::
2011年07月29日 12:44:11 -04:00
msg = _("Missing parameter: %s") % ("flavor",)
LOG.error(msg)
This helps our translators reorder parameters when needed.
Example::
2011年07月29日 12:44:11 -04:00
msg = _("The server with id %(s_id)s has no key %(m_key)s")
2011年07月29日 15:58:41 -04:00
LOG.error(msg % {"s_id": "1234", "m_key": "imageId"})
Creating Unit Tests
-------------------
For every new feature, unit tests should be created that both test and
(implicitly) document the usage of said feature. If submitting a patch for a
bug that had no unit test, a new passing unit test should be added. If a
submitted bug fix does have a unit test, be sure to add a new one that fails
without the patch and passes with the patch.
For more information on creating unit tests and utilizing the testing
infrastructure in OpenStack Nova, please read nova/testing/README.rst.