Commit 50983567 authored by Dmitry Volodin's avatar Dmitry Volodin Committed by Andrey Vertiprahov
Browse files

py3: Remove six type aliases usage

parent d9dfb286
......@@ -11,7 +11,6 @@ from collections import defaultdict
import datetime
# Third-party modules
import six
from six.moves import zip
# NOC modules
......@@ -113,7 +112,7 @@ class MAC(Model):
# @todo convert mac all
if isinstance(query[k], list) and len(query[k]) == 1:
arg = query[k][0].strip()
elif isinstance(query[k], six.string_types):
elif isinstance(query[k], str):
arg = query[k].strip()
else:
arg = query[k]
......
......@@ -4,7 +4,7 @@
# USAGE:
# python manage.py convert-moin [--encoding=charset] [--language=lang] [--tags=<taglist>] <path to moin data/ >
# ---------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
......@@ -17,9 +17,6 @@ import datetime
import sys
import gc
# Third-party modules
import six
# NOC modules
from noc.aaa.models.user import User
from noc.core.management.base import BaseCommand
......@@ -64,7 +61,7 @@ class Command(BaseCommand):
#
def out(self, s):
if isinstance(s, six.text_type):
if isinstance(s, str):
sys.stdout.write(s.encode("utf-8"))
else:
sys.stdout.write(smart_bytes(smart_text(s, encoding=self.encoding)))
......
......@@ -140,7 +140,7 @@ class Command(BaseCommand):
:param s:
:return:
"""
if isinstance(s, six.string_types):
if isinstance(s, str):
return '"%s"' % s
elif isinstance(s, bool):
return "true" if s else "false"
......
......@@ -12,7 +12,6 @@ import struct
# Third-party modules
from csiphash import siphash24
import bson
import six
# NOC modules
from noc.config import config
......@@ -43,7 +42,7 @@ def bi_hash(v):
:param v:
:return:
"""
if not isinstance(v, six.string_types):
if not isinstance(v, str):
v = str(v)
bh = siphash24(SIPHASH_SEED, smart_bytes(v))
return int(struct.unpack("!Q", bh)[0] & 0x7FFFFFFFFFFFFFFF)
......
......@@ -2,13 +2,10 @@
# ----------------------------------------------------------------------
# Clickhouse query engine
# ----------------------------------------------------------------------
# Copyright (C) 2007-2017 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
import six
# Python modules
from noc.core.clickhouse.dictionary import Dictionary
from noc.models import get_model
......@@ -78,7 +75,7 @@ def in_lookup(seq, model=None):
# check int
m = []
for l in seq[1]:
if type(l) in six.integer_types or l.isdigit():
if isinstance(l, int) or l.isdigit():
m += [int(l)]
continue
if len(seq[1]) == 1:
......@@ -268,12 +265,12 @@ def to_sql(expr, model=None):
if not isinstance(v, list):
v = [v]
return op.to_sql(v, model)
elif isinstance(expr, six.string_types):
elif isinstance(expr, str):
if expr.isdigit():
return int(expr)
else:
return "'%s'" % escape_str(expr)
elif isinstance(expr, six.integer_types):
elif isinstance(expr, int):
return str(expr)
elif isinstance(expr, float):
return str(expr)
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# ClickHouse connection
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -10,7 +10,6 @@
import random
# Third-party modules
import six
from six.moves.urllib.parse import quote as urllib_quote
# NOC modules
......@@ -39,7 +38,7 @@ class ClickhouseClient(object):
def execute(self, sql=None, args=None, nodb=False, post=None, extra=None):
def q(v):
# @todo: quote dates
if isinstance(v, six.string_types):
if isinstance(v, str):
return "'%s'" % (v.replace("\\", "\\\\").replace("'", "\\'"))
else:
return str(v)
......
......@@ -333,7 +333,7 @@ class Model(six.with_metaclass(ModelBase)):
group_by = {}
order_by = {}
for i, f in enumerate(fields):
if isinstance(f["expr"], six.string_types):
if isinstance(f["expr"], str):
default_alias = f["expr"]
f["expr"] = {"$field": f["expr"]}
else:
......
......@@ -7,7 +7,6 @@
# ----------------------------------------------------------------------
# Third-party modules
import six
from typing import List
DEFAULT_ENCODING = "utf-8"
......@@ -17,22 +16,22 @@ def smart_bytes(s, encoding=DEFAULT_ENCODING):
"""
Convert strings to bytes when necessary
"""
if isinstance(s, six.binary_type):
if isinstance(s, bytes):
return s
if isinstance(s, six.text_type):
if isinstance(s, str):
return s.encode(encoding)
return six.text_type(s).encode(encoding)
return str(s).encode(encoding)
def smart_text(s, errors="strict", encoding=DEFAULT_ENCODING):
"""
Convert bytes to string when necessary
"""
if isinstance(s, six.text_type):
if isinstance(s, str):
return s
if isinstance(s, six.binary_type):
if isinstance(s, bytes):
return s.decode(encoding, errors=errors)
return six.text_type(s)
return str(s)
def bord(x):
......
......@@ -7,7 +7,6 @@
# ----------------------------------------------------------------------
# Third-party modules
import six
from typing import Dict, Optional
# NOC modules
......@@ -19,5 +18,5 @@ class BaseCollator(object):
pass
def collate(self, physical_path, interfaces):
# type: (six.string_types, Dict[six.string_types, Interface]) -> Optional[six.string_types]
# type: (str, Dict[str, Interface]) -> Optional[str]
raise NotImplementedError
......@@ -30,7 +30,7 @@ class Node(object):
__slots__ = ["token", "handler", "children", "matcher"]
def __init__(self, token):
if isinstance(token, six.string_types):
if isinstance(token, str):
self.token = Token(token)
else:
self.token = token
......@@ -152,7 +152,7 @@ class BaseNormalizerMetaclass(type):
r = []
for p in path:
if p.name:
if isinstance(p.token, six.string_types):
if isinstance(p.token, str):
args += [BasePattern.compile_gen_kwarg(p.name, p.default)]
r += [p.name]
else:
......
......@@ -2,13 +2,10 @@
# ----------------------------------------------------------------------
# ConfDB patterns
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
import six
class BasePattern(object):
# __slots__ = ["match_rest"] conflicts with py3
......@@ -63,7 +60,7 @@ class Token(BasePattern):
class BOOL(ANY):
@staticmethod
def clean(value):
if isinstance(value, six.string_types):
if isinstance(value, str):
return value.lower() in ("true", "on", "yes")
return bool(value)
......
......@@ -99,7 +99,7 @@ class BaseConfig(six.with_metaclass(ConfigBase)):
def set_parameter(self, path, value):
if value is None:
return
if isinstance(value, six.string_types):
if isinstance(value, str):
value = self.expand(value)
self._params[path].set_value(value)
......
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# Config parameters
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -10,9 +10,6 @@
import itertools
import logging
# Third-party modules
import six
# NOC modules
from noc.core.validators import is_int, is_ipv4
from noc.core.comp import smart_text
......@@ -95,7 +92,7 @@ class IntParameter(BaseParameter):
class BooleanParameter(BaseParameter):
def clean(self, v):
if isinstance(v, six.string_types):
if isinstance(v, str):
v = v.lower() in ["y", "t", "true", "yes"]
return bool(v)
......@@ -155,7 +152,7 @@ class SecondsParameter(BaseParameter):
}
def clean(self, v):
if isinstance(v, six.integer_types):
if isinstance(v, int):
return v
m = self.SCALE.get(v[-1], None)
if m is None:
......@@ -220,7 +217,7 @@ class ServiceParameter(BaseParameter):
DEFAULT_RESOLUTION_TIMEOUT = 1
def __init__(self, service, near=False, wait=True, help=None, full_result=True, critical=True):
if isinstance(service, six.string_types):
if isinstance(service, str):
self.services = [service]
else:
self.services = service
......@@ -238,7 +235,7 @@ class ServiceParameter(BaseParameter):
def set_value(self, value):
self.value = None
value = smart_text(value)
if isinstance(value, six.string_types):
if isinstance(value, str):
self.services = [value]
else:
self.services = value
......@@ -250,7 +247,7 @@ class ServiceParameter(BaseParameter):
if isinstance(self.services, list) and ":" in self.services[0]:
self.value = [ServiceItem(*i.rsplit(":", 1)) for i in self.services]
return
elif isinstance(self.services, six.string_types) and ":" in self.services:
elif isinstance(self.services, str) and ":" in self.services:
self.value = self.services
return
......
......@@ -2,13 +2,12 @@
# ----------------------------------------------------------------------
# Load config from YAML
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
import os
import six
# Third-party modules
from six.moves import zip_longest
......@@ -60,7 +59,7 @@ class YAMLProtocol(BaseProtocol):
for pp in prefix[len(current) :]:
r += ["%s%s:" % (self.INDENT * len(current), pp)]
current += [pp]
if isinstance(v, six.string_types) and v.startswith(self.ESCAPE_START):
if isinstance(v, str) and v.startswith(self.ESCAPE_START):
v = "\\" + v
r += ["%s%s: %s" % (self.INDENT * len(current), p[-1], v)]
r = "\n".join(r)
......
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# DataStream
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -15,7 +15,6 @@ import ujson
import bson
import bson.errors
import pymongo
import six
import dateutil.parser
import re
......@@ -181,7 +180,7 @@ class DataStream(object):
if isinstance(change_id, bson.ObjectId):
return change_id
# String with timestamp or ObjectId
if not isinstance(change_id, six.string_types):
if not isinstance(change_id, str):
raise ValueError("Invalid change_id")
if cls.rx_ts.search(change_id):
# Timestamp
......@@ -288,7 +287,7 @@ class DataStream(object):
:param expr: filter expression in form name(arg1, .., argN)
:return: (name, arg1, argN)
"""
if not isinstance(expr, six.string_types):
if not isinstance(expr, str):
raise ValueError("Expression must be string")
i1 = expr.find("(")
if i1 < 0:
......
......@@ -2,21 +2,18 @@
# ----------------------------------------------------------------------
# IDNA utilities
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Third-party modules
import six
# NOC modules
from noc.core.comp import smart_text, smart_bytes
IDNA_PREFIX = six.text_type("xn--")
IDNA_PREFIX = str("xn--")
def to_idna(zone):
# type: (six.text_type) -> six.text_type
# type: (str) -> str
"""
Convert literal zone name to IDNA encoding
:param zone:
......@@ -26,7 +23,7 @@ def to_idna(zone):
def from_idna(zone):
# type: (six.text_type) -> six.text_type
# type: (str) -> str
"""
Convert IDNA zone name representation to literal name
:param self:
......@@ -39,7 +36,7 @@ def from_idna(zone):
def is_idna(zone):
# type: (six.text_type) -> bool
# type: (str) -> bool
"""
Check if zone name is in IDNA representation
:param zone:
......
......@@ -2,16 +2,13 @@
# ----------------------------------------------------------------------
# ECMA-48 control sequences processing
# ----------------------------------------------------------------------
# Copyright (C) 2007-2016 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
import re
# Third-party modules
import six
# NOC modules
from noc.core.comp import smart_bytes
......@@ -108,7 +105,7 @@ rx_ecma = re.compile(get_ecma_re())
def strip_control_sequences(s):
# type: (six.binary_type) -> six.binary_type
# type: (bytes) -> bytes
"""
Normal text leaved untouched
>>> strip_control_sequences("Lorem Ipsum")
......
......@@ -13,7 +13,6 @@ import os
import csv
import itertools
from collections import namedtuple
import six
import io
# NOC modules
......@@ -88,7 +87,7 @@ class BaseExtractor(object):
def q(s):
if s == "" or s is None:
return ""
elif isinstance(s, six.text_type):
elif isinstance(s, str):
return smart_text(s)
else:
return str(s)
......
......@@ -498,7 +498,7 @@ class BaseLoader(object):
if value:
if isinstance(value, str):
return smart_text(value)
elif not isinstance(value, six.string_types):
elif not isinstance(value, str):
return str(value)
else:
return value
......
......@@ -2,7 +2,7 @@
# ----------------------------------------------------------------------
# GridVCS
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
......@@ -18,7 +18,6 @@ import gridfs
import gridfs.errors
import bsdiff4
from bson import ObjectId
import six
from typing import Tuple, Optional, Iterable
# NOC modules
......@@ -39,7 +38,7 @@ class GridVCS(object):
self.files = self.fs._GridFS__files
def get_delta(self, src, dst):
# type: (six.text_type, six.text_type) -> Tuple[six.text_type, six.binary_type]
# type: (str, str) -> Tuple[str, bytes]
"""
Calculate strings delta
:param src: Source string
......@@ -53,7 +52,7 @@ class GridVCS(object):
@classmethod
def apply_delta(cls, type, src, delta):
# type: (six.text_type, six.text_type, six.binary_type) -> six.text_type
# type: (str, str, bytes) -> str
"""
Apply delta
:param type: Delta type
......@@ -65,7 +64,7 @@ class GridVCS(object):
@staticmethod
def apply_delta_F(src, delta):
# type: (six.text_type, six.binary_type) -> six.text_type
# type: (str, bytes) -> str
"""
Raw string
:param src:
......@@ -76,7 +75,7 @@ class GridVCS(object):
@staticmethod
def apply_delta_b(src, delta):
# type: (six.text_type, six.binary_type) -> six.text_type
# type: (str, bytes) -> str
"""
Mercurial mdiff. Slow python implementation ported from Mercurial 0.4.
For legacy installations support only
......@@ -100,7 +99,7 @@ class GridVCS(object):
@staticmethod
def apply_delta_B(src, delta):
# type: (six.text_type, six.binary_type) -> six.text_type
# type: (str, bytes) -> str
"""
BSDIFF4 diff
:param src:
......@@ -111,30 +110,30 @@ class GridVCS(object):
@classmethod
def compress(cls, data, method=None):
# type: (six.binary_type, Optional[six.text_type]) -> six.binary_type
# type: (bytes, Optional[str]) -> bytes
if method:
return getattr(cls, "compress_%s" % method)(data)
return data
@classmethod
def decompress(cls, data, method=None):
# type: (six.binary_type, Optional[six.text_type]) -> six.binary_type
# type: (bytes, Optional[str]) -> bytes
if method:
return getattr(cls, "decompress_%s" % method)(data)
return data
@staticmethod
def compress_z(data):
# type: (six.binary_type) -> six.binary_type
# type: (bytes) -> bytes
return zlib.compress(smart_bytes(data))
@staticmethod
def decompress_z(data):
# type: (six.binary_type) -> six.binary_type
# type: (bytes) -> bytes
return zlib.decompress(smart_bytes(data))
def put(self, object, data, ts=None):
# type: (int, six.text_type, Optional[datetime.datetime]) -> bool
# type: (int, str, Optional[datetime.datetime]) -> bool
"""
Save data
:param object: Object id
......@@ -180,7 +179,7 @@ class GridVCS(object):
return True
def get(self, object, revision=None):
# type: (int, Optional[Revision]) -> Optional[six.text_type]
# type: (int, Optional[Revision]) -> Optional[str]
"""
Get data
:param object: Object id
......@@ -193,7 +192,7 @@ class GridVCS(object):
return smart_text(self.decompress(f.read(), f._file.get("c")))
except gridfs.errors.NoFile:
return None
data = six.text_type()
data = str()
for r in self.iter_revisions(object, reverse=True):
with self.fs.get(r.id) as f:
delta = self.decompress(f.read(), f._file.get("c"))
......@@ -236,7 +235,7 @@ class GridVCS(object):
return None
def find_revision(self, object, revision):
# type: (int, six.text_type) -> Optional[Revision]
# type: (int, str) -> Optional[Revision]
"""
:param object:
:param revision: Revision id
......@@ -249,7 +248,7 @@ class GridVCS(object):
@staticmethod
def _unified_diff(src, dst):
# type: (six.text_type, six.text_type) -> six.text_type
# type: (str, str) -> str
"""
Returns unified diff between src and dest
......@@ -260,7 +259,7 @@ class GridVCS(object):
return "\n".join(difflib.unified_diff(src.splitlines(), dst.splitlines(), lineterm=""))
def diff(self, object, rev1, rev2):
# type: (int, six.text_type, six.text_type) -> six.text_type
# type: (int, str, str) -> str
"""
Get unified diff between revisions
:param object:
......@@ -273,7 +272,7 @@ class GridVCS(object):
return self._unified_diff(src, dst)
def mdiff(self, obj1, rev1, obj2, rev2):
# type: (int, six.text_type, int, six.text_type) -> six.text_type
# type: (int, str, int, str) -> str
"""
Get unified diff between multiple object's revisions
......
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