[constraints] deal with py3k's string types

Related to #267910.

authorJulien Cristau <julien.cristau@logilab.fr>
changesetcfeafb5bf690
branchdefault
phasedraft
hiddenyes
parent revision#57ee237b1f48 [buildobjs] deal with py3k's string types
child revision#e77969beafdc [reader] use six.moves to get at __builtin__ or builtins module
files modified by this revision
constraints.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1413451821 -7200
# Thu Oct 16 11:30:21 2014 +0200
# Node ID cfeafb5bf690a0f5ad26f7a7c7c8ce63e9a19234
# Parent 57ee237b1f48b0d1a35bf63ec5f3dc17c603450f
[constraints] deal with py3k's string types

Related to #267910.

diff --git a/constraints.py b/constraints.py
@@ -22,10 +22,12 @@
1  import re
2  import decimal
3  import operator
4  from StringIO import StringIO
5 
6 +from six import string_types, text_type, binary_type
7 +
8  from logilab.common.deprecation import class_renamed
9 
10  import yams
11  from yams import BadSchemaDefinition
12  from yams.interfaces import IConstraint, IVocabularyConstraint
@@ -328,15 +330,15 @@
13      def check(self, entity, rtype, value):
14          """return true if the value is in the specific vocabulary"""
15          return value in self.vocabulary(entity=entity)
16 
17      def failed_message(self, key, value):
18 -        if isinstance(value, basestring):
19 -            value = '"%s"' % unicode(value)
20 +        if isinstance(value, string_types):
21 +            value = '"%s"' % text_type(value)
22              choices = ', '.join('"%s"' % val for val in self.values)
23          else:
24 -            choices = ', '.join(unicode(val) for val in self.values)
25 +            choices = ', '.join(text_type(val) for val in self.values)
26          return _('invalid value %(KEY-value)s, it must be one of %(KEY-choices)s'), {
27              key+'-value': value,
28              key+'-choices': choices}
29 
30      def vocabulary(self, **kwargs):
@@ -346,21 +348,21 @@
31      def serialize(self):
32          """serialize possible values as a csv list of evaluable strings"""
33          try:
34              sample = next(iter(self.vocabulary()))
35          except:
36 -            sample = unicode()
37 -        if not isinstance(sample, basestring):
38 +            sample = u''
39 +        if not isinstance(sample, string_types):
40              return u', '.join(repr(word) for word in self.vocabulary())
41 -        return u', '.join(repr(unicode(word).replace(',', ',,'))
42 +        return u', '.join(repr(text_type(word).replace(',', ',,'))
43                            for word in self.vocabulary())
44 
45      @classmethod
46      def deserialize(cls, value):
47          """deserialize possible values from a csv list of evaluable strings"""
48          values = [eval(w) for w in re.split('(?<!,), ', value)]
49 -        if values and isinstance(values[0], basestring):
50 +        if values and isinstance(values[0], string_types):
51              values = [v.replace(',,', ',') for v in values]
52          return cls(values)
53 
54 
55  class FormatConstraint(StaticVocabularyConstraint):
@@ -393,11 +395,11 @@
56 
57      def vocabulary(self, **kwargs):
58          return self.regular_formats
59 
60      def __str__(self):
61 -        return 'value in (%s)' % u', '.join(repr(unicode(word)) for word in self.vocabulary())
62 +        return 'value in (%s)' % u', '.join(repr(text_type(word)) for word in self.vocabulary())
63 
64  FORMAT_CONSTRAINT = FormatConstraint()
65 
66 
67  class MultipleStaticVocabularyConstraint(StaticVocabularyConstraint):
@@ -464,15 +466,15 @@
68 
69  # base types checking functions ################################################
70 
71  def check_string(eschema, value):
72      """check value is an unicode string"""
73 -    return isinstance(value, unicode)
74 +    return isinstance(value, text_type)
75 
76  def check_password(eschema, value):
77      """check value is an encoded string"""
78 -    return isinstance(value, (str, StringIO))
79 +    return isinstance(value, (binary_type, StringIO))
80 
81  def check_int(eschema, value):
82      """check value is an integer"""
83      try:
84          int(value)
@@ -525,12 +527,12 @@
85      'Password' : check_password,
86      'Bytes' :    check_file,
87      }
88 
89  BASE_CONVERTERS = {
90 -    'String' :  unicode,
91 -    'Password':  str,
92 +    'String' :   text_type,
93 +    'Password':  binary_type,
94      'Int' :      int,
95      'BigInt' :   int,
96      'Float' :    float,
97      'Boolean' :  bool,
98      'Decimal' :  decimal.Decimal,