Rules and conventions are necessary to insure a minimal coherence and consistency of the DIRAC software. Compliance with the rules and conventions is mainly based on the good will of all the contributors, who are working for the success of the overall project.
The DIRAC code is formatted with black. This can be configured to run automatically before each commit using pre-commit. We recommend installing pre-commit system wide, using your operating system’s package manager, e.g. apt-get install pre-commit on Debian based system, dnf install pre-commit on Fedora like systems or brew install pre-commit on macOS. Once installed it can be enabled for your local clone using:
pre-commit install --allow-missing-config
In addition most editors can be configured to automatically format code with black, see [black’s documentation](https://black.readthedocs.io/en/stable/integrations/editors.html) for details.
Commit messages must be between 20 and 150 chars, and follow the format
type: docs, feat, fix, refactor, style or test
scope (optional): any extra info, (like DMS or whatever)
DIRAC code is organized in packages corresponding to Systems. Systems packages are split into the following standard subpackages:
contains Service Handler modules together with possible auxiliary modules
contains Agent modules together with possible auxiliary modules
contains Database definitions and front-end classes
contains System commands codes
Some System packages might also have additional
Any unit tests and other testing codes
Web portal codes following the same structure as described in Developing Web Portal Pages.
Packages are sets of Python modules and eventually compilable source code together with the instructions to use, build and test it. Source code files are maintained in the git code repository.
Each package has a unique name, that should be written such that each word starts with an initial capital letter ( “CamelCase” convention ). Example: DataManagementSystem.
Module Coding Conventions
Each module should define the following variables in its global scope:
__RCSID__ = "$Id$"
this is the SVN macro substituted by the module revision number.
__docformat__ = "restructedtext en"
this is a variable specifying the mark-up language used for the module inline documentation ( doc strings ). See Documenting your developments for more details on the inline code documentation.
The first executable string in each module is a doc string describing the module functionality and giving instructions for its usage. The string is using ReStructedText mark-up language.
Standard python modules are imported using:
Public modules from other packages are imported using:
Proper naming the code elements is very important for the code clarity especially in a project with multiple developers. As a general rule, names should be meaningful but not too long.
Names are usually made of several words, written together without underscore, each first letter of a word being uppercased ( CamelCase convention ). The case of the first letter is specified by other rules. Only alphanumeric characters are allowed.
Names are case sensitive, but names that differ only by the case should not be used.
Avoid single characters and meaningless names like “jjj”, except for local loops or array indexes.
Class names must be nouns, or noun phrases. The first letter is capital.
Class data attribute names must be nouns, or noun phrases. The first letter is lower case. The last word should represent the type of the variable value if it is not clear from the context otherwise. Examples: fileList, nameString, pilotAgentDict.
Function names and Class method names must be verbs or verb phrases, the first letter in lower case. Examples: getDataMember, executeThisPieceOfCode.
Class data member accessor methods are named after the attribute name with a “set” or “get” prefix.
Class data attributes must be considered as private and must never be accessed from outside the class. Accessor methods should be provided if necessary.
Private methods of a module or class must start by double underscore to explicitly prevent its use from other modules.
Python files should contain a definition of a single class, they may contain auxiliary (private) classes if needed. The name of the file should be the same as the name of the main class defined in the file
A constructor must always initialize all attributes which may be used in the class.
Methods and arguments
Methods must not change their arguments. Use assignment to an internal variable if the argument value should be modified.
Methods should consistently return a Result (S_OK or S_ERROR) structure. A single return value is only allowed for simple methods that can not fail after the code is debugged.
Returned Result structures must always be tested for possible failures.
Exception mechanism should be used only to trap “unusual” problems. Use Result structures instead to report failure details.
It is important to try to get a similar look, for an easier maintenance, as most of the code writers will eventually be replaced during the lifetime of the project.
Readability and maintainability
When doing lookup in dictionaries, don’t use
dict.has_key(x)- it is deprecated and much slower than
x in dict. Also, in python 3.0 this isn’t valid.
Comments and doc strings
Comments should be abundant, and must follow the rules of automatic documentation by the sphinx tool using ReStructedText mark-up.
Each class and method definition should start with the doc strings. See Documenting your developments for more details.
Use blank lines to separate blocks of statements but not blank commented lines.