|
| 1 | +Cinder Style Commandments |
| 2 | +======================= |
| 3 | + |
| 4 | +- Step 1: Read http://www.python.org/dev/peps/pep-0008/ |
| 5 | +- Step 2: Read http://www.python.org/dev/peps/pep-0008/ again |
| 6 | +- Step 3: Read on |
| 7 | + |
| 8 | + |
| 9 | +General |
| 10 | +------- |
| 11 | +- Put two newlines between top-level code (funcs, classes, etc) |
| 12 | +- Put one newline between methods in classes and anywhere else |
| 13 | +- Long lines should be wrapped in parentheses |
| 14 | + in preference to using a backslash for line continuation. |
| 15 | +- Do not write "except:", use "except Exception:" at the very least |
| 16 | +- Include your name with TODOs as in "#TODO(termie)" |
| 17 | +- Do not shadow a built-in or reserved word. Example:: |
| 18 | + |
| 19 | + def list(): |
| 20 | + return [1, 2, 3] |
| 21 | + |
| 22 | + mylist = list() # BAD, shadows `list` built-in |
| 23 | + |
| 24 | + class Foo(object): |
| 25 | + def list(self): |
| 26 | + return [1, 2, 3] |
| 27 | + |
| 28 | + mylist = Foo().list() # OKAY, does not shadow built-in |
| 29 | + |
| 30 | +- Use the "is not" operator when testing for unequal identities. Example:: |
| 31 | + |
| 32 | + if not X is Y: # BAD, intended behavior is ambiguous |
| 33 | + pass |
| 34 | + |
| 35 | + if X is not Y: # OKAY, intuitive |
| 36 | + pass |
| 37 | + |
| 38 | +- Use the "not in" operator for evaluating membership in a collection. Example:: |
| 39 | + |
| 40 | + if not X in Y: # BAD, intended behavior is ambiguous |
| 41 | + pass |
| 42 | + |
| 43 | + if X not in Y: # OKAY, intuitive |
| 44 | + pass |
| 45 | + |
| 46 | + if not (X in Y or X in Z): # OKAY, still better than all those 'not's |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +Imports |
| 51 | +------- |
| 52 | +- Do not import objects, only modules (*) |
| 53 | +- Do not import more than one module per line (*) |
| 54 | +- Do not make relative imports |
| 55 | +- Order your imports by the full module path |
| 56 | +- Organize your imports according to the following template |
| 57 | + |
| 58 | +(*) exceptions are: |
| 59 | + |
| 60 | +- imports from ``migrate`` package |
| 61 | +- imports from ``sqlalchemy`` package |
| 62 | +- imports from ``cinder.db.sqlalchemy.session`` module |
| 63 | + |
| 64 | +Example:: |
| 65 | + |
| 66 | + # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 67 | + {{stdlib imports in human alphabetical order}} |
| 68 | + \n |
| 69 | + {{third-party lib imports in human alphabetical order}} |
| 70 | + \n |
| 71 | + {{cinder imports in human alphabetical order}} |
| 72 | + \n |
| 73 | + \n |
| 74 | + {{begin your code}} |
| 75 | + |
| 76 | + |
| 77 | +Human Alphabetical Order Examples |
| 78 | +--------------------------------- |
| 79 | +Example:: |
| 80 | + |
| 81 | + import httplib |
| 82 | + import logging |
| 83 | + import random |
| 84 | + import StringIO |
| 85 | + import time |
| 86 | + import unittest |
| 87 | + |
| 88 | + import eventlet |
| 89 | + import webob.exc |
| 90 | + |
| 91 | + import cinder.api.ec2 |
| 92 | + from cinder.api import openstack |
| 93 | + from cinder.auth import users |
| 94 | + from cinder.endpoint import cloud |
| 95 | + import cinder.flags |
| 96 | + from cinder import test |
| 97 | + |
| 98 | + |
| 99 | +Docstrings |
| 100 | +---------- |
| 101 | +Example:: |
| 102 | + |
| 103 | + """A one line docstring looks like this and ends in a period.""" |
| 104 | + |
| 105 | + |
| 106 | + """A multi line docstring has a one-line summary, less than 80 characters. |
| 107 | + |
| 108 | + Then a new paragraph after a newline that explains in more detail any |
| 109 | + general information about the function, class or method. Example usages |
| 110 | + are also great to have here if it is a complex class for function. |
| 111 | + |
| 112 | + When writing the docstring for a class, an extra line should be placed |
| 113 | + after the closing quotations. For more in-depth explanations for these |
| 114 | + decisions see http://www.python.org/dev/peps/pep-0257/ |
| 115 | + |
| 116 | + If you are going to describe parameters and return values, use Sphinx, the |
| 117 | + appropriate syntax is as follows. |
| 118 | + |
| 119 | + :param foo: the foo parameter |
| 120 | + :param bar: the bar parameter |
| 121 | + :returns: return_type -- description of the return value |
| 122 | + :returns: description of the return value |
| 123 | + :raises: AttributeError, KeyError |
| 124 | + """ |
| 125 | + |
| 126 | + |
| 127 | +Dictionaries/Lists |
| 128 | +------------------ |
| 129 | +If a dictionary (dict) or list object is longer than 80 characters, its items |
| 130 | +should be split with newlines. Embedded iterables should have their items |
| 131 | +indented. Additionally, the last item in the dictionary should have a trailing |
| 132 | +comma. This increases readability and simplifies future diffs. |
| 133 | + |
| 134 | +Example:: |
| 135 | + |
| 136 | + my_dictionary = { |
| 137 | + "image": { |
| 138 | + "name": "Just a Snapshot", |
| 139 | + "size": 2749573, |
| 140 | + "properties": { |
| 141 | + "user_id": 12, |
| 142 | + "arch": "x86_64", |
| 143 | + }, |
| 144 | + "things": [ |
| 145 | + "thing_one", |
| 146 | + "thing_two", |
| 147 | + ], |
| 148 | + "status": "ACTIVE", |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + |
| 153 | +Calling Methods |
| 154 | +--------------- |
| 155 | +Calls to methods 80 characters or longer should format each argument with |
| 156 | +newlines. This is not a requirement, but a guideline:: |
| 157 | + |
| 158 | + unnecessarily_long_function_name('string one', |
| 159 | + 'string two', |
| 160 | + kwarg1=constants.ACTIVE, |
| 161 | + kwarg2=['a', 'b', 'c']) |
| 162 | + |
| 163 | + |
| 164 | +Rather than constructing parameters inline, it is better to break things up:: |
| 165 | + |
| 166 | + list_of_strings = [ |
| 167 | + 'what_a_long_string', |
| 168 | + 'not as long', |
| 169 | + ] |
| 170 | + |
| 171 | + dict_of_numbers = { |
| 172 | + 'one': 1, |
| 173 | + 'two': 2, |
| 174 | + 'twenty four': 24, |
| 175 | + } |
| 176 | + |
| 177 | + object_one.call_a_method('string three', |
| 178 | + 'string four', |
| 179 | + kwarg1=list_of_strings, |
| 180 | + kwarg2=dict_of_numbers) |
| 181 | + |
| 182 | + |
| 183 | +Internationalization (i18n) Strings |
| 184 | +----------------------------------- |
| 185 | +In order to support multiple languages, we have a mechanism to support |
| 186 | +automatic translations of exception and log strings. |
| 187 | + |
| 188 | +Example:: |
| 189 | + |
| 190 | + msg = _("An error occurred") |
| 191 | + raise HTTPBadRequest(explanation=msg) |
| 192 | + |
| 193 | +If you have a variable to place within the string, first internationalize the |
| 194 | +template string then do the replacement. |
| 195 | + |
| 196 | +Example:: |
| 197 | + |
| 198 | + msg = _("Missing parameter: %s") % ("flavor",) |
| 199 | + LOG.error(msg) |
| 200 | + |
| 201 | +If you have multiple variables to place in the string, use keyword parameters. |
| 202 | +This helps our translators reorder parameters when needed. |
| 203 | + |
| 204 | +Example:: |
| 205 | + |
| 206 | + msg = _("The server with id %(s_id)s has no key %(m_key)s") |
| 207 | + LOG.error(msg % {"s_id": "1234", "m_key": "imageId"}) |
| 208 | + |
| 209 | + |
| 210 | +Creating Unit Tests |
| 211 | +------------------- |
| 212 | +For every new feature, unit tests should be created that both test and |
| 213 | +(implicitly) document the usage of said feature. If submitting a patch for a |
| 214 | +bug that had no unit test, a new passing unit test should be added. If a |
| 215 | +submitted bug fix does have a unit test, be sure to add a new one that fails |
| 216 | +without the patch and passes with the patch. |
| 217 | + |
| 218 | +For more information on creating unit tests and utilizing the testing |
| 219 | +infrastructure in OpenStack Cinder, please read cinder/testing/README.rst. |
| 220 | + |
| 221 | + |
| 222 | +openstack-common |
| 223 | +---------------- |
| 224 | + |
| 225 | +A number of modules from openstack-common are imported into the project. |
| 226 | + |
| 227 | +These modules are "incubating" in openstack-common and are kept in sync |
| 228 | +with the help of openstack-common's update.py script. See: |
| 229 | + |
| 230 | + http://wiki.openstack.org/CommonLibrary#Incubation |
| 231 | + |
| 232 | +The copy of the code should never be directly modified here. Please |
| 233 | +always update openstack-common first and then run the script to copy |
| 234 | +the changes across. |
| 235 | + |
| 236 | +OpenStack Trademark |
| 237 | +------------------- |
| 238 | + |
| 239 | +OpenStack is a registered trademark of OpenStack, LLC, and uses the |
| 240 | +following capitalization: |
| 241 | + |
| 242 | + OpenStack |
| 243 | + |
| 244 | + |
| 245 | +Commit Messages |
| 246 | +--------------- |
| 247 | +Using a common format for commit messages will help keep our git history |
| 248 | +readable. Follow these guidelines: |
| 249 | + |
| 250 | + First, provide a brief summary (it is recommended to keep the commit title |
| 251 | + under 50 chars). |
| 252 | + |
| 253 | + The first line of the commit message should provide an accurate |
| 254 | + description of the change, not just a reference to a bug or |
| 255 | + blueprint. It must be followed by a single blank line. |
| 256 | + |
| 257 | + If the change relates to a specific driver (libvirt, xenapi, qpid, etc...), |
| 258 | + begin the first line of the commit message with the driver name, lowercased, |
| 259 | + followed by a colon. |
| 260 | + |
| 261 | + Following your brief summary, provide a more detailed description of |
| 262 | + the patch, manually wrapping the text at 72 characters. This |
| 263 | + description should provide enough detail that one does not have to |
| 264 | + refer to external resources to determine its high-level functionality. |
| 265 | + |
| 266 | + Once you use 'git review', two lines will be appended to the commit |
| 267 | + message: a blank line followed by a 'Change-Id'. This is important |
| 268 | + to correlate this commit with a specific review in Gerrit, and it |
| 269 | + should not be modified. |
| 270 | + |
| 271 | +For further information on constructing high quality commit messages, |
| 272 | +and how to split up commits into a series of changes, consult the |
| 273 | +project wiki: |
| 274 | + |
| 275 | + http://wiki.openstack.org/GitCommitMessages |
0 commit comments