"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "cloudkitty/hacking/checks.py" between
cloudkitty-12.1.0.tar.gz and cloudkitty-13.0.0.tar.gz

About: OpenStack CloudKitty provides Rating-as-a-Service for OpenStack.
The "Victoria" series (latest release).

checks.py  (cloudkitty-12.1.0):checks.py  (cloudkitty-13.0.0)
skipping to change at line 19 skipping to change at line 19
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import ast import ast
import re import re
import pep8 from hacking import core
import six import six
""" """
Guidelines for writing new hacking checks Guidelines for writing new hacking checks
- Use only for Cloudkitty specific tests. OpenStack general tests - Use only for Cloudkitty specific tests. OpenStack general tests
should be submitted to the common 'hacking' module. should be submitted to the common 'hacking' module.
- Pick numbers in the range C3xx. Find the current test with - Pick numbers in the range C3xx. Find the current test with
the highest allocated number and then pick the next value. the highest allocated number and then pick the next value.
- Keep the test method code in the source file ordered based - Keep the test method code in the source file ordered based
skipping to change at line 100 skipping to change at line 100
error = (node.lineno, node.col_offset, message, self.__class__) error = (node.lineno, node.col_offset, message, self.__class__)
self._errors.append(error) self._errors.append(error)
def _check_call_names(self, call_node, names): def _check_call_names(self, call_node, names):
if isinstance(call_node, ast.Call): if isinstance(call_node, ast.Call):
if isinstance(call_node.func, ast.Name): if isinstance(call_node.func, ast.Name):
if call_node.func.id in names: if call_node.func.id in names:
return True return True
return False return False
@core.flake8ext
def no_translate_logs(logical_line, filename): def no_translate_logs(logical_line, filename):
"""Check for 'LOG.*(_(' """Check for 'LOG.*(_('
Starting with the Pike series, OpenStack no longer supports log Starting with the Pike series, OpenStack no longer supports log
translation. translation.
* This check assumes that 'LOG' is a logger. * This check assumes that 'LOG' is a logger.
* Use filename so we can start enforcing this in specific folders instead * Use filename so we can start enforcing this in specific folders instead
of needing to do so all at once. of needing to do so all at once.
skipping to change at line 125 skipping to change at line 126
class CheckLoggingFormatArgs(BaseASTChecker): class CheckLoggingFormatArgs(BaseASTChecker):
"""Check for improper use of logging format arguments. """Check for improper use of logging format arguments.
LOG.debug("Volume %s caught fire and is at %d degrees C and climbing.", LOG.debug("Volume %s caught fire and is at %d degrees C and climbing.",
('volume1', 500)) ('volume1', 500))
The format arguments should not be a tuple as it is easy to miss. The format arguments should not be a tuple as it is easy to miss.
""" """
name = "check_logging_format_args"
version = "1.0"
CHECK_DESC = 'C310 Log method arguments should not be a tuple.' CHECK_DESC = 'C310 Log method arguments should not be a tuple.'
LOG_METHODS = [ LOG_METHODS = [
'debug', 'info', 'debug', 'info',
'warn', 'warning', 'warn', 'warning',
'error', 'exception', 'error', 'exception',
'critical', 'fatal', 'critical', 'fatal',
'trace', 'log' 'trace', 'log'
] ]
def _find_name(self, node): def _find_name(self, node):
skipping to change at line 179 skipping to change at line 183
if not len(node.args): if not len(node.args):
return super(CheckLoggingFormatArgs, self).generic_visit(node) return super(CheckLoggingFormatArgs, self).generic_visit(node)
# any argument should not be a tuple # any argument should not be a tuple
for arg in node.args: for arg in node.args:
if isinstance(arg, ast.Tuple): if isinstance(arg, ast.Tuple):
self.add_error(arg) self.add_error(arg)
return super(CheckLoggingFormatArgs, self).generic_visit(node) return super(CheckLoggingFormatArgs, self).generic_visit(node)
@core.flake8ext
def check_explicit_underscore_import(logical_line, filename): def check_explicit_underscore_import(logical_line, filename):
"""Check for explicit import of the _ function """Check for explicit import of the _ function
We need to ensure that any files that are using the _() function We need to ensure that any files that are using the _() function
to translate logs are explicitly importing the _ function. We to translate logs are explicitly importing the _ function. We
can't trust unit test to catch whether the import has been can't trust unit test to catch whether the import has been
added so we need to check for it here. added so we need to check for it here.
""" """
# Build a list of the files that have _ imported. No further # Build a list of the files that have _ imported. No further
skipping to change at line 209 skipping to change at line 214
class CheckForStrUnicodeExc(BaseASTChecker): class CheckForStrUnicodeExc(BaseASTChecker):
"""Checks for the use of str() or unicode() on an exception. """Checks for the use of str() or unicode() on an exception.
This currently only handles the case where str() or unicode() This currently only handles the case where str() or unicode()
is used in the scope of an exception handler. If the exception is used in the scope of an exception handler. If the exception
is passed into a function, returned from an assertRaises, or is passed into a function, returned from an assertRaises, or
used on an exception created in the same scope, this does not used on an exception created in the same scope, this does not
catch it. catch it.
""" """
name = "check_for_str_unicode_exc"
version = "1.0"
CHECK_DESC = ('C314 str() and unicode() cannot be used on an ' CHECK_DESC = ('C314 str() and unicode() cannot be used on an '
'exception. Remove or use six.text_type()') 'exception. Remove or use six.text_type()')
def __init__(self, tree, filename): def __init__(self, tree, filename):
super(CheckForStrUnicodeExc, self).__init__(tree, filename) super(CheckForStrUnicodeExc, self).__init__(tree, filename)
self.name = [] self.name = []
self.already_checked = [] self.already_checked = []
# Python 2 # Python 2
def visit_TryExcept(self, node): def visit_TryExcept(self, node):
skipping to change at line 253 skipping to change at line 261
super(CheckForStrUnicodeExc, self).generic_visit(node) super(CheckForStrUnicodeExc, self).generic_visit(node)
class CheckForTransAdd(BaseASTChecker): class CheckForTransAdd(BaseASTChecker):
"""Checks for the use of concatenation on a translated string. """Checks for the use of concatenation on a translated string.
Translations should not be concatenated with other strings, but Translations should not be concatenated with other strings, but
should instead include the string being added to the translated should instead include the string being added to the translated
string to give the translators the most information. string to give the translators the most information.
""" """
name = "check_for_trans_add"
version = "1.0"
CHECK_DESC = ('C315 Translated messages cannot be concatenated. ' CHECK_DESC = ('C315 Translated messages cannot be concatenated. '
'String should be included in translated message.') 'String should be included in translated message.')
TRANS_FUNC = ['_'] TRANS_FUNC = ['_']
def visit_BinOp(self, node): def visit_BinOp(self, node):
if isinstance(node.op, ast.Add): if isinstance(node.op, ast.Add):
if self._check_call_names(node.left, self.TRANS_FUNC): if self._check_call_names(node.left, self.TRANS_FUNC):
self.add_error(node.left) self.add_error(node.left)
elif self._check_call_names(node.right, self.TRANS_FUNC): elif self._check_call_names(node.right, self.TRANS_FUNC):
self.add_error(node.right) self.add_error(node.right)
super(CheckForTransAdd, self).generic_visit(node) super(CheckForTransAdd, self).generic_visit(node)
def check_oslo_namespace_imports(logical_line, physical_line, filename): @core.flake8ext
def check_oslo_namespace_imports(logical_line, noqa):
"""'oslo_' should be used instead of 'oslo.' """'oslo_' should be used instead of 'oslo.'
C317 C317
""" """
if pep8.noqa(physical_line): if noqa:
return return
if re.match(oslo_namespace_imports, logical_line): if re.match(oslo_namespace_imports, logical_line):
msg = ("C317: '%s' must be used instead of '%s'.") % ( msg = ("C317: '%s' must be used instead of '%s'.") % (
logical_line.replace('oslo.', 'oslo_'), logical_line.replace('oslo.', 'oslo_'),
logical_line) logical_line)
yield(0, msg) yield(0, msg)
@core.flake8ext
def dict_constructor_with_list_copy(logical_line): def dict_constructor_with_list_copy(logical_line):
"""Use a dict comprehension instead of a dict constructor """Use a dict comprehension instead of a dict constructor
C318 C318
""" """
msg = ("C318: Must use a dict comprehension instead of a dict constructor" msg = ("C318: Must use a dict comprehension instead of a dict constructor"
" with a sequence of key-value pairs." " with a sequence of key-value pairs."
) )
if dict_constructor_with_list_copy_re.match(logical_line): if dict_constructor_with_list_copy_re.match(logical_line):
yield (0, msg) yield (0, msg)
@core.flake8ext
def no_xrange(logical_line): def no_xrange(logical_line):
"""Ensure to not use xrange() """Ensure to not use xrange()
C319 C319
""" """
if assert_no_xrange_re.match(logical_line): if assert_no_xrange_re.match(logical_line):
yield(0, "C319: Do not use xrange().") yield(0, "C319: Do not use xrange().")
@core.flake8ext
def validate_assertTrue(logical_line): def validate_assertTrue(logical_line):
"""Use assertTrue instead of assertEqual """Use assertTrue instead of assertEqual
C312 C312
""" """
if re.match(assert_True, logical_line): if re.match(assert_True, logical_line):
msg = ("C312: Unit tests should use assertTrue(value) instead" msg = ("C312: Unit tests should use assertTrue(value) instead"
" of using assertEqual(True, value).") " of using assertEqual(True, value).")
yield(0, msg) yield(0, msg)
@core.flake8ext
def validate_assertIsNone(logical_line): def validate_assertIsNone(logical_line):
"""Use assertIsNone instead of assertEqual """Use assertIsNone instead of assertEqual
C311 C311
""" """
if re.match(assert_None, logical_line): if re.match(assert_None, logical_line):
msg = ("C311: Unit tests should use assertIsNone(value) instead" msg = ("C311: Unit tests should use assertIsNone(value) instead"
" of using assertEqual(None, value).") " of using assertEqual(None, value).")
yield(0, msg) yield(0, msg)
@core.flake8ext
def no_log_warn_check(logical_line): def no_log_warn_check(logical_line):
"""Disallow 'LOG.warn' """Disallow 'LOG.warn'
C320 C320
""" """
msg = ("C320: LOG.warn is deprecated, please use LOG.warning!") msg = ("C320: LOG.warn is deprecated, please use LOG.warning!")
if re.match(no_log_warn, logical_line): if re.match(no_log_warn, logical_line):
yield(0, msg) yield(0, msg)
def factory(register):
register(check_explicit_underscore_import)
register(no_translate_logs)
register(CheckForStrUnicodeExc)
register(CheckLoggingFormatArgs)
register(CheckForTransAdd)
register(check_oslo_namespace_imports)
register(dict_constructor_with_list_copy)
register(no_xrange)
register(validate_assertTrue)
register(validate_assertIsNone)
register(no_log_warn_check)
 End of changes. 14 change blocks. 
3 lines changed or deleted 20 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)