'|'.join('(?:%s)' % _regex_for_level(level, hint)
for level, hint in _all_log_levels.iteritems()))
-
-oslo_namespace_imports_dot = re.compile(r"from[\s]*oslo[.]")
-oslo_namespace_imports_root = re.compile(r"from[\s]*oslo[\s]*import[\s]*")
+oslo_namespace_imports_dot = re.compile(r"import[\s]+oslo[.][^\s]+")
+oslo_namespace_imports_from_dot = re.compile(r"from[\s]+oslo[.]")
+oslo_namespace_imports_from_root = re.compile(r"from[\s]+oslo[\s]+import[\s]+")
def validate_log_translations(logical_line, physical_line, filename):
yield (0, msg)
-def check_oslo_namespace_imports(logical_line, blank_before, filename):
- if re.match(oslo_namespace_imports_dot, logical_line):
+def check_oslo_namespace_imports(logical_line):
+ if re.match(oslo_namespace_imports_from_dot, logical_line):
msg = ("N323: '%s' must be used instead of '%s'.") % (
logical_line.replace('oslo.', 'oslo_'),
logical_line)
yield(0, msg)
- elif re.match(oslo_namespace_imports_root, logical_line):
+ elif re.match(oslo_namespace_imports_from_root, logical_line):
msg = ("N323: '%s' must be used instead of '%s'.") % (
logical_line.replace('from oslo import ', 'import oslo_'),
logical_line)
yield(0, msg)
+ elif re.match(oslo_namespace_imports_dot, logical_line):
+ msg = ("N323: '%s' must be used instead of '%s'.") % (
+ logical_line.replace('import', 'from').replace('.', ' import '),
+ logical_line)
+ yield(0, msg)
def factory(register):
# License for the specific language governing permissions and limitations
# under the License.
+import testtools
+
from neutron.hacking import checks
from neutron.tests import base
self.assertEqual(
0, len(list(checks.check_assert_called_once_with(pass_code,
"neutron/tests/test_assert.py"))))
+
+ def test_check_oslo_namespace_imports(self):
+ def check(s, fail=True):
+ func = checks.check_oslo_namespace_imports
+ if fail:
+ self.assertIsInstance(next(func(s)), tuple)
+ else:
+ with testtools.ExpectedException(StopIteration):
+ next(func(s))
+
+ check('from oslo_utils import importutils', fail=False)
+ check('import oslo_messaging', fail=False)
+ check('from oslo.utils import importutils')
+ check('from oslo import messaging')
+ check('import oslo.messaging')