Verified Commit 02b2409d authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

2to3 metaclass

parent 3f6ef2c4
......@@ -2,13 +2,14 @@
# ---------------------------------------------------------------------
# Base Validator class
# ---------------------------------------------------------------------
# Copyright (C) 2007-2015 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import os
# Third-party modules
import six
# NOC modules
from noc.cm.facts.error import Error
from noc.config import config
......@@ -53,7 +54,7 @@ class ValidatorBase(type):
return m
class BaseValidator(object):
class BaseValidator(six.with_metaclass(ValidatorBase, object)):
"""
Validator base class.
Preparation call sequence:
......@@ -68,7 +69,6 @@ class BaseValidator(object):
Then errors collected via engine.iter_errors
"""
__metaclass__ = ValidatorBase
TITLE = None
DESCRIPTION = None
PRIORITY = 1000
......
......@@ -2,12 +2,36 @@
# ----------------------------------------------------------------------
# NRI Port mapper
# ----------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
import six
class BasePortMapper(object):
class PortMapperBase(type):
"""
Process @match decorators
"""
def __new__(mcs, name, bases, attrs):
n = type.__new__(mcs, name, bases, attrs)
for m in dir(n):
mm = getattr(n, m)
if hasattr(mm, "_match"):
for d in mm._match:
if d["local"] and d["profile"]:
n._profile_to_local[d["profile"]] = mm
elif d["local"] and d["platform"]:
n._platform_to_local[d["platform"]] = mm
elif not d["local"] and d["profile"]:
n._profile_to_remote[d["profile"]] = mm
elif not d["local"] and d["platform"]:
n._platform_to_remote[d["platform"]] = mm
return n
class BasePortMapper(six.with_metaclass(PortMapperBase, object)):
"""
Basic class to convert port notation from external NRI and back.
External NRI system is defined in managed object's
......@@ -24,26 +48,6 @@ class BasePortMapper(object):
_profile_to_remote = {}
_platform_to_remote = {}
class __metaclass__(type):
"""
Process @match decorators
"""
def __new__(mcs, name, bases, attrs):
n = type.__new__(mcs, name, bases, attrs)
for m in dir(n):
mm = getattr(n, m)
if hasattr(mm, "_match"):
for d in mm._match:
if d["local"] and d["profile"]:
n._profile_to_local[d["profile"]] = mm
elif d["local"] and d["platform"]:
n._platform_to_local[d["platform"]] = mm
elif not d["local"] and d["profile"]:
n._profile_to_remote[d["profile"]] = mm
elif not d["local"] and d["platform"]:
n._platform_to_remote[d["platform"]] = mm
return n
def __init__(self, managed_object):
self.managed_object = managed_object
self.profile = self.managed_object.profile.name
......@@ -60,8 +64,7 @@ class BasePortMapper(object):
mm = self._profile_to_local.get(self.profile)
if mm:
return mm(self, name)
else:
return None
return None
def to_remote(self, name):
"""
......@@ -74,8 +77,7 @@ class BasePortMapper(object):
mm = self._profile_to_remote.get(self.profile)
if mm:
return mm(self, name)
else:
return None
return None
def to_local(profile=None, platform=None):
......
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# Custom field types
# ----------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -55,12 +55,10 @@ class INETField(models.Field):
return value
class MACField(models.Field):
class MACField(six.with_metaclass(models.SubfieldBase, models.Field)):
"""
MACField maps to the PostgreSQL MACADDR field
"""
__metaclass__ = models.SubfieldBase
def db_type(self, connection):
return "MACADDR"
......@@ -84,12 +82,10 @@ class BinaryField(models.Field):
return "BYTEA"
class TextArrayField(models.Field):
class TextArrayField(six.with_metaclass(models.SubfieldBase, models.Field)):
"""
Text Array field maps to PostgreSQL TEXT[] type
"""
__metaclass__ = models.SubfieldBase
def db_type(self, connection):
return "TEXT[]"
......@@ -113,12 +109,10 @@ class TextArrayField(models.Field):
return ""
class TextArray2Field(models.Field):
class TextArray2Field(six.with_metaclass(models.SubfieldBase, models.Field)):
"""
Two-dimensioned text array field maps to PostgreSQL TEXT[][]
"""
__metaclass__ = models.SubfieldBase
def db_type(self, connection):
return "TEXT[][]"
......@@ -137,12 +131,10 @@ class TextArray2Field(models.Field):
return [[to_unicode(y) for y in x] for x in value]
class InetArrayField(models.Field):
class InetArrayField(six.with_metaclass(models.SubfieldBase, models.Field)):
"""
INETArrayField maps to PostgreSQL INET[] type
"""
__metaclass__ = models.SubfieldBase
def db_type(self, connection):
return "INET[]"
......@@ -161,9 +153,7 @@ class InetArrayField(models.Field):
return "{ " + ", ".join(value) + " }"
class DateTimeArrayField(models.Field):
__metaclass__ = models.SubfieldBase
class DateTimeArrayField(six.with_metaclass(models.SubfieldBase, models.Field)):
def db_type(self, connection):
return "TIMESTAMP[]"
......@@ -182,12 +172,10 @@ class DateTimeArrayField(models.Field):
return "{ " + ", ".join([str(x) for x in value]) + " }"
class PickledField(models.Field):
class PickledField(six.with_metaclass(models.SubfieldBase, models.Field)):
"""
Pickled object
"""
__metaclass__ = models.SubfieldBase
def db_type(self, connection):
return "BYTEA"
......@@ -207,7 +195,6 @@ class AutoCompleteTagsField(models.Field):
"""
Autocomplete tags fields
"""
def db_type(self, connection):
return "TEXT"
......@@ -220,9 +207,7 @@ class AutoCompleteTagsField(models.Field):
return super(AutoCompleteTagsField, self).formfield(**defaults)
class TagsField(models.Field):
__metaclass__ = models.SubfieldBase
class TagsField(six.with_metaclass(models.SubfieldBase, models.Field)):
def db_type(self, connection):
return "TEXT[]"
......@@ -230,8 +215,7 @@ class TagsField(models.Field):
def to_unicode(s):
if isinstance(s, unicode):
return s
else:
return unicode(s, "utf-8")
return unicode(s, "utf-8")
if value is None:
return None
......@@ -256,11 +240,10 @@ class TagsField(models.Field):
return value
class ColorField(models.Field):
class ColorField(six.with_metaclass(models.SubfieldBase, models.Field)):
"""
Color field
"""
__metaclass__ = models.SubfieldBase
default_validators = []
def db_type(self, connection):
......@@ -395,12 +378,10 @@ class CachedForeignKey(models.ForeignKey):
CachedForeignKeyDescriptor(self))
class ObjectIDArrayField(models.Field):
class ObjectIDArrayField(six.with_metaclass(models.SubfieldBase, models.Field)):
"""
ObjectIDArrayField maps to PostgreSQL CHAR[] type
"""
__metaclass__ = models.SubfieldBase
def db_type(self, connection):
return "CHAR(24)[]"
......
......@@ -2,7 +2,7 @@
# ---------------------------------------------------------------------
# Application class
# ---------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
......@@ -108,14 +108,13 @@ class ApplicationBase(type):
return m
class Application(object):
class Application(six.with_metaclass(ApplicationBase, object)):
"""
Basic application class.
Application combined by set of methods, decorated with @view.
Each method accepts requests and returns reply
"""
__metaclass__ = ApplicationBase
title = "APPLICATION TITLE"
icon = "icon_application"
glyph = "file"
......
......@@ -2,16 +2,17 @@
# ---------------------------------------------------------------------
# Datasource interface
# ---------------------------------------------------------------------
# Copyright (C) 2007-2011 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import os
# Third-party modules
import six
# NOC modules
from noc.settings import INSTALLED_APPS
datasource_registry = {}
......@@ -23,8 +24,7 @@ class DataSourceBase(type):
return m
class DataSource(object):
__metaclass__ = DataSourceBase
class DataSource(six.with_metaclass(DataSourceBase, object)):
_name = None
def __init__(self, **kwargs):
......
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------
# Periodic Task base class
# ---------------------------------------------------------------------
# Copyright (C) 2007-2011 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
"""
"""
# Python modules
import os
import datetime
import logging
# NOC modules
from noc.lib.registry import Registry
class PeriodicRegistry(Registry):
"""Registry for all periodic tasks"""
name = "PeriodicRegistry"
subdir = "periodics"
classname = "Task"
periodic_registry = PeriodicRegistry()
class TaskBase(type):
"""Metaclass for Task"""
def __new__(cls, name, bases, attrs):
m = type.__new__(cls, name, bases, attrs)
periodic_registry.register(m.name, m)
return m
class Task(object):
"""Task handler"""
__metaclass__ = TaskBase
name = None
description = ""
# A list of periodic task names which cannot be started concurrenctly
wait_for = []
# Default task timeout.
# If set to None, task has no configurable timeout,
# Otherwise it can be configured
default_timeout = None
def __init__(self, timeout=None):
if self.default_timeout:
self.timeout = timeout if timeout else self.default_timeout
else:
self.timeout = None
def execute(self):
return True
def debug(self, msg):
logging.debug("%s: %s" % (self.name, msg))
def info(self, msg):
logging.info("%s: %s" % (self.name, msg))
def error(self, msg):
logging.error("%s: %s" % (self.name, msg))
......@@ -10,6 +10,8 @@
# NOC modules
from noc.lib.registry import Registry
# Third-party modules
import six
class DownloaderRegistry(Registry):
......@@ -32,9 +34,10 @@ class DownloaderBase(type):
return m
class Downloader(object):
"""Downloader base class"""
__metaclass__ = DownloaderBase
class Downloader(six.with_metaclass(DownloaderBase, object)):
"""
Downloader base class
"""
# Profile name
name = None
......
......@@ -2,12 +2,13 @@
# ---------------------------------------------------------------------
# Calculators framework
# ---------------------------------------------------------------------
# Copyright (C) 2007-2016 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# from noc.lib.render import render
# Third-party modules
import six
# NOC modules
from noc.lib.registry import Registry
......@@ -21,9 +22,6 @@ class CalculatorRegistry(Registry):
calculator_registry = CalculatorRegistry()
#
# Calculator metaclass
#
class CalculatorBase(type):
def __new__(cls, name, bases, attrs):
m = type.__new__(cls, name, bases, attrs)
......@@ -31,11 +29,7 @@ class CalculatorBase(type):
return m
#
#
#
class Calculator(object):
__metaclass__ = CalculatorBase
class Calculator(six.with_metaclass(CalculatorBase, object)):
name = None
title = None
description = None
......@@ -57,8 +51,10 @@ class Calculator(object):
title=self.title, result=result,
description=self.description)
#
# Returns a list of pairs or None
#
def calculate(**kwargs):
"""
Returns a list of pairs or None
:param kwargs:
:return:
"""
return None
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment