Commit 66694638 authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Merge branch 'noc-py3-clean-2' into 'master'

2to3 basestring

See merge request noc/noc!1982
parents e6267de4 aa6307dd
......@@ -2,15 +2,15 @@
# ---------------------------------------------------------------------
# Basic DLink parser
# ---------------------------------------------------------------------
# Copyright (C) 2007-2015 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
from collections import defaultdict
# Third-party modules
from pyparsing import nums, Word, Group, Optional, Suppress, Combine,\
Literal, delimitedList
import six
from pyparsing import nums, Word, Group, Optional, Suppress, Combine, Literal, delimitedList
# NOC modules
from noc.cm.parsers.base import BaseParser
from noc.core.ip import IPv4
......@@ -18,8 +18,8 @@ from noc.lib.validators import is_ipv4, is_int
class BaseDLinkParser(BaseParser):
STATUSES = set(["sntp"])
SERVICES = set(["telnet", "web", "ssh", "password_recovery"])
STATUSES = {"sntp"}
SERVICES = {"telnet", "web", "ssh", "password_recovery"}
def parse(self, config):
# Various protocol statuses
......@@ -78,7 +78,7 @@ class BaseDLinkParser(BaseParser):
"""
for s, _, _ in PORT_EXPR.scanString(expr):
for x in s.asList():
if isinstance(x, basestring):
if isinstance(x, six.string_types):
# Single port
yield x
elif len(x) == 2:
......@@ -96,7 +96,7 @@ class BaseDLinkParser(BaseParser):
elif x[1] == ":(":
pfx = "%s:%%s" % x[0]
for y in x[2:]:
if isinstance(y, basestring):
if isinstance(y, six.string_types):
yield pfx % y
else:
for i in range(int(y[0]), int(y[1]) + 1):
......
......@@ -2,7 +2,7 @@
# ---------------------------------------------------------------------
# Basic RouterOS parser
# ---------------------------------------------------------------------
# Copyright (C) 2007-2015 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
......@@ -10,7 +10,9 @@
import re
from collections import defaultdict
# Third-party modules
from pyparsing import *
import six
from pyparsing import (Word, Suppress, alphanums, QuotedString, Group, LineStart, Literal,
restOfLine, ZeroOrMore, Optional, FollowedBy)
# NOC modules
from noc.cm.parsers.pyparser import BasePyParser
......@@ -55,7 +57,7 @@ class RouterOSParser(BasePyParser):
return dict((k, v) for k, v in tokens)
def get_interface_fact(self, name):
if isinstance(name, basestring):
if isinstance(name, six.string_types):
return super(RouterOSParser, self).get_interface_fact(name)
else:
default_name = name[2]
......@@ -76,7 +78,7 @@ class RouterOSParser(BasePyParser):
def on_set(self, tokens):
if self.set_handler:
if isinstance(tokens[0], basestring) or tokens[0][0] == "find":
if isinstance(tokens[0], six.string_types) or tokens[0][0] == "find":
name = tokens[0]
args = self.parse_kvp(tokens[1:])
else:
......
......@@ -2,7 +2,7 @@
# ---------------------------------------------------------------------
# CLIPS rule-based validator
# ---------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
......@@ -10,7 +10,8 @@
from __future__ import absolute_import
import logging
import re
# Django modules
# Third-party modules
import six
from django.template import Template, Context
# NOC modules
from .base import BaseValidator
......@@ -49,7 +50,7 @@ class CLIPSValidator(BaseValidator):
# CLIPS Escape
for n in ctx:
v = ctx[n]
if isinstance(v, basestring):
if isinstance(v, six.string_types):
v = v.replace("\\", "\\\\").replace("\"", "\\\"")
ctx[n] = v
# Insert rule number to (assert (error ..))
......
......@@ -6,11 +6,15 @@
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from __future__ import absolute_import
# Third-party modules
from tornado.iostream import IOStream
import tornado.gen
# Third-party modules
import six
# NOC modules
from base import CLI
from .base import CLI
IAC = chr(0xFF) # Interpret As Command
DONT = chr(0xFE)
......@@ -196,7 +200,7 @@ class TelnetIOStream(IOStream):
:param opt:
:return:
"""
if isinstance(opt, basestring):
if isinstance(opt, six.string_types):
opt = ord(opt)
return "%s %s" % (
IAC_CMD.get(cmd, cmd),
......
......@@ -2,13 +2,15 @@
# ----------------------------------------------------------------------
# SNMP SET PDU generator
# ----------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
from __future__ import absolute_import
import random
# Third-party modules
import six
# NOC modules
from .ber import BEREncoder
from .consts import PDU_SET_REQUEST
......@@ -33,7 +35,7 @@ def set_pdu(community, varbinds, request_id=None, version=SNMP_v2c):
for oid, value in varbinds:
if value is None:
v = e.encode_null()
elif isinstance(value, basestring):
elif isinstance(value, six.string_types):
v = e.encode_octet_string(value)
elif isinstance(value, (int, long)):
v = e.encode_int(value)
......
......@@ -2,11 +2,12 @@
# ---------------------------------------------------------------------
# FM models utils
# ---------------------------------------------------------------------
# Copyright (C) 2007-2018 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Third-party modules
import six
from bson import ObjectId
......@@ -40,7 +41,7 @@ def get_severity(alarms):
def f(a):
if hasattr(a, "id"):
return a.id
elif isinstance(a, basestring):
elif isinstance(a, six.string_types):
return ObjectId(a)
else:
return a
......
......@@ -2,7 +2,7 @@
# ---------------------------------------------------------------------
# ModelInterface model
# ---------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
......@@ -11,6 +11,7 @@ import os
from threading import Lock
import operator
# Third-party modules
import six
from mongoengine.document import Document, EmbeddedDocument
from mongoengine.fields import (StringField, BooleanField, ListField,
EmbeddedDocumentField, UUIDField)
......@@ -72,7 +73,7 @@ class ModelInterfaceAttr(EmbeddedDocument):
return int(value)
def clean_float(self, value):
if isinstance(value, basestring):
if isinstance(value, six.string_types):
return float(value.replace(",", "."))
else:
return float(value)
......@@ -158,7 +159,7 @@ class ModelInterface(Document):
if a.name in v:
vv = v[a.name]
if a.type == "strlist":
if isinstance(vv, basestring):
if isinstance(vv, six.string_types):
vv = [vv]
r = set()
for x in vv:
......
......@@ -2,12 +2,15 @@
# ---------------------------------------------------------------------
# Pretty JSON formatter
# ---------------------------------------------------------------------
# Copyright (C) 2007-2013 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import uuid
from functools import reduce
# Third-party modules
import six
# NOC modules
from noc.lib.escape import json_escape
from noc.lib.text import indent
......@@ -25,7 +28,7 @@ class PrettyJSON(object):
def convert(cls, o, i, order=None):
if o is None:
return indent("null", i)
if isinstance(o, basestring):
if isinstance(o, six.string_types):
return indent("\"%s\"" % json_escape(o), i)
elif isinstance(o, bool):
return indent("true" if o else "false", i)
......
......@@ -2,10 +2,13 @@
# ---------------------------------------------------------------------
# Routing Policy Specification Language (RFC2622) routines
# ---------------------------------------------------------------------
# Copyright (C) 2007-2012 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Third-party modules
import six
RPLS_IDENT = 20
......@@ -20,7 +23,7 @@ def rpsl_format(rpsl, ident=None):
ident = RPLS_IDENT
out = []
mask = "%%-%ds%%s" % ident
if isinstance(rpsl, basestring):
if isinstance(rpsl, six.string_types):
rpsl = rpsl.split("\n")
for l in [x for x in rpsl if ":" in x]:
k, v = l.split(":", 1)
......
......@@ -2,10 +2,9 @@
# ----------------------------------------------------------------------
# Abstract script interfaces
# ----------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# pylint: disable=c0301, basestring-builtin
# Python modules
import re
......@@ -336,7 +335,7 @@ class InstanceOfParameter(Parameter):
super(InstanceOfParameter, self).__init__(required=required,
default=default)
self.cls = cls
if isinstance(cls, basestring):
if isinstance(cls, six.string_types):
self.is_valid = self.is_valid_classname
else:
self.is_valid = self.is_valid_instance
......@@ -390,7 +389,7 @@ class SubclassOfParameter(Parameter):
super(SubclassOfParameter, self).__init__(required=required,
default=default)
self.cls = cls
if isinstance(cls, basestring):
if isinstance(cls, six.string_types):
self.is_valid = self.is_valid_classname
else:
self.is_valid = self.is_valid_class
......@@ -638,7 +637,7 @@ class DateTimeParameter(StringParameter):
def form_clean(self, value):
if value is None and self.default is not None:
value = self.default
if isinstance(value, basestring):
if isinstance(value, six.string_types):
if "." in value:
dt, _, us = value.partition(".")
dt = datetime.datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S")
......@@ -670,7 +669,7 @@ class DateTimeShiftParameter(StringParameter):
def form_clean(self, value):
if value is None and self.default is not None:
value = self.default
if isinstance(value, basestring):
if isinstance(value, six.string_types):
if "." in value:
dt, _, us = value.partition(".")
dt = datetime.datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S")
......@@ -939,7 +938,7 @@ class VLANIDMapParameter(StringParameter):
>>> VLANIDMapParameter().clean("")
''
"""
if isinstance(value, basestring) and not value.strip():
if isinstance(value, six.string_types) and not value.strip():
return ""
vp = VLANIDParameter()
try:
......@@ -1109,7 +1108,7 @@ class GeoPointParameter(Parameter):
return [float(x) for x in value]
except ValueError:
self.raise_error(value)
elif isinstance(value, basestring):
elif isinstance(value, six.string_types):
v = value.replace(" ", "")
if not v or "," not in v:
self.raise_error(value)
......@@ -1211,7 +1210,7 @@ class TagsParameter(Parameter):
if type(value) in (list, tuple):
v = [unicode(v).strip() for v in value]
return [x for x in v if x]
elif isinstance(value, basestring):
elif isinstance(value, six.string_types):
v = [unicode(x.strip()) for x in value.split(",")]
return [x for x in v if x]
else:
......@@ -1224,7 +1223,7 @@ class ColorParameter(Parameter):
return self.default
if type(value) in (int, long):
return value
if isinstance(value, basestring):
if isinstance(value, six.string_types):
if value.startswith("#"):
value = value[1:]
if len(value) == 6:
......
......@@ -2,12 +2,14 @@
# ---------------------------------------------------------------------
# Cisco.IOSXR.get_mpls_vpn
# ---------------------------------------------------------------------
# Copyright (C) 2007-2012 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import re
# Third-party modules
import six
# NOC modules
from noc.core.script.base import BaseScript
from noc.sa.interfaces.igetmplsvpn import IGetMPLSVPN
......@@ -20,7 +22,7 @@ class Script(BaseScript):
rx_vrf = re.compile(r"^VRF\s+(?P<name>[^;]+);\s+RD\s+(?P<rd>[^;]+);.+$")
def is_not_set(self, s):
if isinstance(s, basestring):
if isinstance(s, six.string_types):
return s.strip().lower() == "not set"
return False
......
......@@ -10,6 +10,8 @@
import re
import logging
import new
# Third-party modules
import six
# NOC modules
from noc.inv.models.interface import Interface
from noc.inv.models.subinterface import SubInterface
......@@ -206,15 +208,14 @@ class Rule(object):
c += [" return None"]
# Vars binding
if self.vars:
# pylint: disable=basestring-builtin
has_expressions = any(v for v in self.vars.values()
if not isinstance(v, basestring))
if not isinstance(v, six.string_types))
if has_expressions:
# Callculate vars context
c += ["var_context = {'event': event}"]
c += ["var_context.update(e_vars)"]
for k, v in self.vars.items():
if isinstance(v, basestring):
if isinstance(v, six.string_types):
c += ["e_vars[\"%s\"] = \"%s\"" % (k, pyq(v))]
else:
c += ["e_vars[\"%s\"] = eval(self.vars[\"%s\"], {}, var_context)" % (k, k)]
......
......@@ -2,13 +2,14 @@
# ---------------------------------------------------------------------
# CustomerPortal client
# ---------------------------------------------------------------------
# Copyright (C) 2007-2015 The NOC Project
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import logging
# Third-party modules
import six
import requests
import ujson
# Python modules
......@@ -156,7 +157,7 @@ class CPClient(object):
if org:
info["org"] = org.strip()
if industries:
if isinstance(industries, basestring):
if isinstance(industries, six.string_types):
industries = [x.strip() for x in industries.split(",")
if x.strip()]
info["industries"] = industries
......@@ -180,7 +181,7 @@ class CPClient(object):
if org:
info["org"] = org.strip()
if industries:
if isinstance(industries, basestring):
if isinstance(industries, six.string_types):
industries = [x.strip() for x in industries.split(",")
if x.strip()]
info["industries"] = industries
......
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