From: Thierry Parmentelat Date: Thu, 31 Mar 2016 08:00:40 +0000 (+0200) Subject: do not depend on types.StringTypes anymore X-Git-Tag: sfa-3.1-21~27 X-Git-Url: http://git.onelab.eu/?p=sfa.git;a=commitdiff_plain;h=30d9951e075d93127c3909dcb41be09b420b3525 do not depend on types.StringTypes anymore --- diff --git a/clientbin/getNodes.py b/clientbin/getNodes.py index d1c1623f..ec2e4a2d 100644 --- a/clientbin/getNodes.py +++ b/clientbin/getNodes.py @@ -5,6 +5,8 @@ import os from optparse import OptionParser from pprint import pprint +from sfa.util.py23 import StringType + def create_parser(): command = sys.argv[0] argv = sys.argv[1:] @@ -34,7 +36,7 @@ def print_dict(rdict, options, counter=1): if not isinstance(rdict, dict): raise "%s not a dict" % rdict for (key, value) in rdict.iteritems(): - if isinstance(value, StringTypes): + if isinstance(value, StringType): if (attributes and key in attributes) or not attributes: print tab * counter + "%s: %s" % (key, value) elif isinstance(value, list): diff --git a/clientbin/sfadump.py b/clientbin/sfadump.py index fb654e79..617635b9 100755 --- a/clientbin/sfadump.py +++ b/clientbin/sfadump.py @@ -4,7 +4,6 @@ from __future__ import with_statement import sys import os, os.path import tempfile -from types import StringTypes, ListType from argparse import ArgumentParser from sfa.util.sfalogging import logger diff --git a/sfa/dummy/dummyslices.py b/sfa/dummy/dummyslices.py index 7ab94ba2..394e816f 100644 --- a/sfa/dummy/dummyslices.py +++ b/sfa/dummy/dummyslices.py @@ -1,5 +1,4 @@ import time -from types import StringTypes from collections import defaultdict from sfa.util.sfatime import utcparse, datetime_to_epoch diff --git a/sfa/managers/registry_manager.py b/sfa/managers/registry_manager.py index 5567197d..78d933d7 100644 --- a/sfa/managers/registry_manager.py +++ b/sfa/managers/registry_manager.py @@ -1,6 +1,5 @@ from __future__ import print_function -import types # for get_key_from_incoming_ip import tempfile import os @@ -163,7 +162,7 @@ class RegistryManager: def Resolve(self, api, xrns, type=None, details=False): dbsession = api.dbsession() - if not isinstance(xrns, types.ListType): + if not isinstance(xrns, list): # try to infer type if not set and we get a single input if not type: type = Xrn(xrns).get_type() @@ -387,7 +386,7 @@ class RegistryManager: pub_key=getattr(record,'reg-keys',None) if pub_key is not None: # use only first key in record - if pub_key and isinstance(pub_key, types.ListType): pub_key = pub_key[0] + if pub_key and isinstance(pub_key, list): pub_key = pub_key[0] pkey = convert_public_key(pub_key) email = getattr(record,'email',None) @@ -416,10 +415,12 @@ class RegistryManager: elif isinstance (record, RegUser): # create RegKey objects for incoming keys if hasattr(record,'reg-keys'): - keys=getattr(record,'reg-keys') + keys = getattr(record, 'reg-keys') # some people send the key as a string instead of a list of strings - if isinstance(keys,types.StringTypes): keys=[keys] - logger.debug ("creating {} keys for user {}".format(len(keys), record.hrn)) + # note for python2/3 : no need to consider unicode in a key + if isinstance(keys, str): + keys = [keys] + logger.debug("creating {} keys for user {}".format(len(keys), record.hrn)) record.reg_keys = [ RegKey (key) for key in keys ] # update testbed-specific data if needed @@ -460,7 +461,7 @@ class RegistryManager: if type == 'user': if getattr(new_record, 'keys', None): new_key = new_record.keys - if isinstance (new_key, types.ListType): + if isinstance (new_key, list): new_key = new_key[0] # take new_key into account diff --git a/sfa/methods/Resolve.py b/sfa/methods/Resolve.py index dc34f759..fc12df1b 100644 --- a/sfa/methods/Resolve.py +++ b/sfa/methods/Resolve.py @@ -1,5 +1,3 @@ -import types - from sfa.util.xrn import Xrn, urn_to_hrn from sfa.util.method import Method @@ -37,7 +35,7 @@ class Resolve(Method): if 'details' in options: details=options['details'] else: details=False type = None - if not isinstance(xrns, types.ListType): + if not isinstance(xrns, list): type = Xrn(xrns).get_type() xrns=[xrns] hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] diff --git a/sfa/nitos/nitosslices.py b/sfa/nitos/nitosslices.py index 875a5a90..ffdb6e96 100644 --- a/sfa/nitos/nitosslices.py +++ b/sfa/nitos/nitosslices.py @@ -1,4 +1,3 @@ -from types import StringTypes from collections import defaultdict from sfa.util.sfatime import utcparse, datetime_to_epoch diff --git a/sfa/planetlab/peers.py b/sfa/planetlab/peers.py index 63c9f7ff..7c6e1b7c 100644 --- a/sfa/planetlab/peers.py +++ b/sfa/planetlab/peers.py @@ -1,5 +1,6 @@ from sfa.util.xrn import get_authority -from types import StringTypes + +from sfa.util.py23 import StringType def get_peer(pldriver, hrn): # Because of myplc native federation, we first need to determine if this @@ -15,7 +16,7 @@ def get_peer(pldriver, hrn): # check if we are already peered with this site_authority, if so peers = pldriver.shell.GetPeers( {}, ['peer_id', 'peername', 'shortname', 'hrn_root']) for peer_record in peers: - names = [name.lower() for name in peer_record.values() if isinstance(name, StringTypes)] + names = [name.lower() for name in peer_record.values() if isinstance(name, StringType)] if site_authority in names: peer = peer_record['shortname'] diff --git a/sfa/planetlab/plslices.py b/sfa/planetlab/plslices.py index ffa6f164..2b59a017 100644 --- a/sfa/planetlab/plslices.py +++ b/sfa/planetlab/plslices.py @@ -1,5 +1,4 @@ import time -from types import StringTypes from collections import defaultdict from sfa.util.sfatime import utcparse, datetime_to_epoch diff --git a/sfa/storage/alchemy.py b/sfa/storage/alchemy.py index fb8dfac1..64c39cf8 100644 --- a/sfa/storage/alchemy.py +++ b/sfa/storage/alchemy.py @@ -1,5 +1,3 @@ -from types import StringTypes - from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker diff --git a/sfa/storage/model.py b/sfa/storage/model.py index 051ba87e..923576e9 100644 --- a/sfa/storage/model.py +++ b/sfa/storage/model.py @@ -1,4 +1,3 @@ -from types import StringTypes from datetime import datetime from sqlalchemy import or_, and_ @@ -14,6 +13,7 @@ from sfa.storage.record import Record from sfa.util.sfalogging import logger from sfa.util.sfatime import utcparse, datetime_to_string from sfa.util.xml import XML +from sfa.util.py23 import StringType from sfa.trust.gid import GID @@ -99,7 +99,7 @@ class RegRecord(Base, AlchemyObj): if type: self.type=type if hrn: self.hrn=hrn if gid: - if isinstance(gid, StringTypes): self.gid=gid + if isinstance(gid, StringType): self.gid=gid else: self.gid=gid.save_to_string(save_parents=True) if authority: self.authority=authority if peer_authority: self.peer_authority=peer_authority @@ -130,7 +130,7 @@ class RegRecord(Base, AlchemyObj): @validates ('gid') def validate_gid (self, key, gid): if gid is None: return - elif isinstance(gid, StringTypes): return gid + elif isinstance(gid, StringType): return gid else: return gid.save_to_string(save_parents=True) def validate_datetime (self, key, incoming): diff --git a/sfa/storage/parameter.py b/sfa/storage/parameter.py index 6737d0bd..dc9d5b5c 100644 --- a/sfa/storage/parameter.py +++ b/sfa/storage/parameter.py @@ -5,9 +5,11 @@ # Copyright (C) 2006 The Trustees of Princeton University # -from types import NoneType, IntType, LongType, FloatType, StringTypes, DictType, TupleType, ListType +from types import NoneType, IntType, LongType, FloatType, DictType, TupleType, ListType from sfa.util.faults import SfaAPIError +from sfa.util.py23 import StringType + class Parameter: """ Typed value wrapper. Use in accepts and returns to document method @@ -89,7 +91,7 @@ def xmlrpc_type(arg): return "boolean" elif arg_type == FloatType: return "double" - elif arg_type in StringTypes: + elif issubclass(arg_type, StringType): return "string" elif arg_type == ListType or arg_type == TupleType: return "array" diff --git a/sfa/storage/record.py b/sfa/storage/record.py index 96222682..a03ce30f 100644 --- a/sfa/storage/record.py +++ b/sfa/storage/record.py @@ -1,12 +1,12 @@ from __future__ import print_function from sfa.util.sfatime import utcparse, datetime_to_string -from types import StringTypes from datetime import datetime from sfa.util.xml import XML from sfa.trust.gid import GID from sfa.util.sfalogging import logger +from sfa.util.py23 import StringType class Record: @@ -56,9 +56,9 @@ class Record: def load_from_dict (self, d): for (k,v) in d.iteritems(): # experimental - if isinstance(v, StringTypes) and v.lower() in ['true']: + if isinstance(v, StringType) and v.lower() in ['true']: v = True - if isinstance(v, StringTypes) and v.lower() in ['false']: + if isinstance(v, StringType) and v.lower() in ['false']: v = False setattr(self, k, v) diff --git a/sfa/trust/auth.py b/sfa/trust/auth.py index 86d5d4f9..512c58bd 100644 --- a/sfa/trust/auth.py +++ b/sfa/trust/auth.py @@ -2,13 +2,13 @@ # SfaAPI authentication # import sys -from types import StringTypes from sfa.util.faults import InsufficientRights, MissingCallerGID, \ MissingTrustedRoots, PermissionError, BadRequestHash, \ ConnectionKeyGIDMismatch, SfaPermissionDenied, CredentialNotVerifiable, \ Forbidden, BadArgs from sfa.util.sfalogging import logger +from sfa.util.py23 import StringType from sfa.util.config import Config from sfa.util.xrn import Xrn, get_authority @@ -62,7 +62,7 @@ class Auth: if xrns is None: xrns = [] error = (None, None) def log_invalid_cred(cred): - if not isinstance (cred, StringTypes): + if not isinstance (cred, StringType): logger.info("cannot validate credential %s - expecting a string"%cred) error = ('TypeMismatch', "checkCredentials: expected a string, received {} -- {}" diff --git a/sfa/trust/credential.py b/sfa/trust/credential.py index 37af67bd..f03e135a 100644 --- a/sfa/trust/credential.py +++ b/sfa/trust/credential.py @@ -30,12 +30,13 @@ from __future__ import print_function import os, os.path import subprocess -from types import StringTypes import datetime from StringIO import StringIO from tempfile import mkstemp from xml.dom.minidom import Document, parseString +from sfa.util.py23 import StringType + HAVELXML = False try: from lxml import etree @@ -285,7 +286,7 @@ class Credential(object): self.version = None if cred: - if isinstance(cred, StringTypes): + if isinstance(cred, StringType): string = cred self.type = Credential.SFA_CREDENTIAL_TYPE self.version = '3' @@ -301,7 +302,7 @@ class Credential(object): str = file(filename).read() # if this is a legacy credential, write error and bail out - if isinstance (str, StringTypes) and str.strip().startswith("-----"): + if isinstance (str, StringType) and str.strip().startswith("-----"): logger.error("Legacy credentials not supported any more - giving up with %s..."%str[:10]) return else: diff --git a/sfa/util/method.py b/sfa/util/method.py index 1d3de575..009220c9 100644 --- a/sfa/util/method.py +++ b/sfa/util/method.py @@ -4,10 +4,11 @@ # import time -from types import IntType, LongType, StringTypes +from types import IntType, LongType import textwrap from sfa.util.sfalogging import logger +from sfa.util.py23 import StringType from sfa.util.faults import SfaFault, SfaInvalidAPIMethod, SfaInvalidArgumentCount, SfaInvalidArgument from sfa.storage.parameter import Parameter, Mixed, python_type, xmlrpc_type @@ -233,7 +234,7 @@ class Method: # Strings are a special case. Accept either unicode or str # types if a string is expected. - if expected_type in StringTypes and isinstance(value, StringTypes): + if issubclass(expected_type, StringType) and isinstance(value, StringType): pass # Integers and long integers are also special types. Accept @@ -247,7 +248,7 @@ class Method: name) # If a minimum or maximum (length, value) has been specified - if expected_type in StringTypes: + if issubclass(expected_type, StringType): if min is not None and \ len(value.encode(self.api.encoding)) < min: raise SfaInvalidArgument("%s must be at least %d bytes long" % (name, min)) diff --git a/sfa/util/py23.py b/sfa/util/py23.py new file mode 100644 index 00000000..6f46671f --- /dev/null +++ b/sfa/util/py23.py @@ -0,0 +1,4 @@ +try: + StringType = basestring +except: + StringType = str diff --git a/sfa/util/sfatime.py b/sfa/util/sfatime.py index 3500b2b1..74356673 100644 --- a/sfa/util/sfatime.py +++ b/sfa/util/sfatime.py @@ -22,7 +22,6 @@ #---------------------------------------------------------------------- from __future__ import print_function -from types import StringTypes import time import datetime import dateutil.parser @@ -30,6 +29,7 @@ import calendar import re from sfa.util.sfalogging import logger +from sfa.util.py23 import StringType SFATIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ" @@ -55,7 +55,7 @@ For safety this can also handle inputs that are either timestamps, or datetimes # prepare the input for the checks below by # casting strings ('1327098335') to ints - if isinstance(input, StringTypes): + if isinstance(input, StringType): try: input = int(input) except ValueError: @@ -70,7 +70,7 @@ For safety this can also handle inputs that are either timestamps, or datetimes if isinstance (input, datetime.datetime): #logger.info ("argument to utcparse already a datetime - doing nothing") return input - elif isinstance (input, StringTypes): + elif isinstance (input, StringType): t = dateutil.parser.parse(input) if t.utcoffset() is not None: t = t.utcoffset() + t.replace(tzinfo=None) diff --git a/sfa/util/xml.py b/sfa/util/xml.py index f46443a8..9c4a80f0 100755 --- a/sfa/util/xml.py +++ b/sfa/util/xml.py @@ -1,10 +1,11 @@ #!/usr/bin/python -from types import StringTypes from lxml import etree from StringIO import StringIO from sfa.util.faults import InvalidXML from sfa.rspecs.elements.element import Element +from sfa.util.py23 import StringType + # helper functions to help build xpaths class XpathFilter: @staticmethod @@ -239,7 +240,7 @@ class XML: d=d.copy() # looks like iteritems won't stand side-effects for k in d.keys(): - if not isinstance(d[k],StringTypes): + if not isinstance(d[k], StringType): del d[k] element.attrib.update(d) diff --git a/sfatables/commands/moo.py b/sfatables/commands/moo.py index a8828540..65b7598e 100644 --- a/sfatables/commands/moo.py +++ b/sfatables/commands/moo.py @@ -1,5 +1,7 @@ import os, time +from sfa.util.py23 import StringType + class Command: commandline_options = [] help = "Add a new rule" @@ -152,7 +154,7 @@ class Command: # Strings are a special case. Accept either unicode or str # types if a string is expected. - if expected_type in StringTypes and isinstance(value, StringTypes): + if issubclass(expected_type, StringType) and isinstance(value, StringType): pass # Integers and long integers are also special types. Accept @@ -167,7 +169,7 @@ class Command: name) # If a minimum or maximum (length, value) has been specified - if expected_type in StringTypes: + if issubclass(expected_type, StringType): if min is not None and \ len(value.encode(self.api.encoding)) < min: raise SfaInvalidArgument("%s must be at least %d bytes long" % (name, min)) diff --git a/tools/reset_gids.py b/tools/reset_gids.py index 21e25cec..e30ed329 100755 --- a/tools/reset_gids.py +++ b/tools/reset_gids.py @@ -1,8 +1,6 @@ #!/usr/bin/env python # -*- coding:utf-8 -*- -import types - from sfa.storage.model import * from sfa.storage.alchemy import * from sfa.trust.gid import create_uuid @@ -22,7 +20,7 @@ def fix_users(): pub_key=getattr(record,'reg_keys',None) if len(pub_key) > 0: # use only first key in record - if pub_key and isinstance(pub_key, types.ListType): pub_key = pub_key[0] + if pub_key and isinstance(pub_key, list): pub_key = pub_key[0] pub_key = pub_key.key pkey = convert_public_key(pub_key) urn = Xrn (xrn=record.hrn, type='user').get_urn() diff --git a/wsdl/sfa2wsdl.py b/wsdl/sfa2wsdl.py index 2ec13072..1a8ea21e 100755 --- a/wsdl/sfa2wsdl.py +++ b/wsdl/sfa2wsdl.py @@ -17,6 +17,8 @@ from optparse import OptionParser from sfa.storage.parameter import Parameter, Mixed +from sfa.util.py23 import StringType + plc_ns="http://www.planet-lab.org/sfa" class SoapError(Exception): @@ -162,7 +164,7 @@ class WSDLGen: return "xsd:boolean" elif arg_type == FloatType: return "xsd:double" - elif arg_type in StringTypes: + elif issubclass(arg_type, StringType): return "xsd:string" else: pdb.set_trace()