do not depend on types.StringTypes anymore
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 31 Mar 2016 08:00:40 +0000 (10:00 +0200)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 31 Mar 2016 09:07:58 +0000 (11:07 +0200)
21 files changed:
clientbin/getNodes.py
clientbin/sfadump.py
sfa/dummy/dummyslices.py
sfa/managers/registry_manager.py
sfa/methods/Resolve.py
sfa/nitos/nitosslices.py
sfa/planetlab/peers.py
sfa/planetlab/plslices.py
sfa/storage/alchemy.py
sfa/storage/model.py
sfa/storage/parameter.py
sfa/storage/record.py
sfa/trust/auth.py
sfa/trust/credential.py
sfa/util/method.py
sfa/util/py23.py [new file with mode: 0644]
sfa/util/sfatime.py
sfa/util/xml.py
sfatables/commands/moo.py
tools/reset_gids.py
wsdl/sfa2wsdl.py

index d1c1623..ec2e4a2 100644 (file)
@@ -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):
index fb654e7..617635b 100755 (executable)
@@ -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
index 7ab94ba..394e816 100644 (file)
@@ -1,5 +1,4 @@
 import time
-from types import StringTypes
 from collections import defaultdict
 
 from sfa.util.sfatime import utcparse, datetime_to_epoch
index 5567197..78d933d 100644 (file)
@@ -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
index dc34f75..fc12df1 100644 (file)
@@ -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]
index 875a5a9..ffdb6e9 100644 (file)
@@ -1,4 +1,3 @@
-from types import StringTypes
 from collections import defaultdict
 
 from sfa.util.sfatime import utcparse, datetime_to_epoch
index 63c9f7f..7c6e1b7 100644 (file)
@@ -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']
 
index ffa6f16..2b59a01 100644 (file)
@@ -1,5 +1,4 @@
 import time
-from types import StringTypes
 from collections import defaultdict
 
 from sfa.util.sfatime import utcparse, datetime_to_epoch
index fb8dfac..64c39cf 100644 (file)
@@ -1,5 +1,3 @@
-from types import StringTypes
-
 from sqlalchemy import create_engine
 from sqlalchemy.orm import sessionmaker
 
index 051ba87..923576e 100644 (file)
@@ -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):
index 6737d0b..dc9d5b5 100644 (file)
@@ -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"
index 9622268..a03ce30 100644 (file)
@@ -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)
 
index 86d5d4f..512c58b 100644 (file)
@@ -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 {} -- {}"
index 37af67b..f03e135 100644 (file)
@@ -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:
index 1d3de57..009220c 100644 (file)
@@ -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 (file)
index 0000000..6f46671
--- /dev/null
@@ -0,0 +1,4 @@
+try:
+    StringType = basestring
+except:
+    StringType = str
index 3500b2b..7435667 100644 (file)
@@ -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)
index f46443a..9c4a80f 100755 (executable)
@@ -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)
index a882854..65b7598 100644 (file)
@@ -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))
index 21e25ce..e30ed32 100755 (executable)
@@ -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()
index 2ec1307..1a8ea21 100755 (executable)
@@ -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()