summaryrefslogtreecommitdiffstats
path: root/ipalib
diff options
context:
space:
mode:
authorChristian Heimes <cheimes@redhat.com>2018-02-14 16:59:50 +0100
committerChristian Heimes <cheimes@redhat.com>2018-02-15 09:41:30 +0100
commit8ffa33c24ecae7be41421669ff6114ae56e9a6e7 (patch)
treeff6fb41bf6fff88240661df96f0bb0d47f63ef78 /ipalib
parent86a6fdcc4316a26f40c9e0c0a92d21bec94ccd9c (diff)
downloadfreeipa-8ffa33c24ecae7be41421669ff6114ae56e9a6e7.tar.gz
freeipa-8ffa33c24ecae7be41421669ff6114ae56e9a6e7.tar.xz
freeipa-8ffa33c24ecae7be41421669ff6114ae56e9a6e7.zip
Generate same API.txt under Python 2 and 3
Use Python 3's reprlib with customizations to create same API.txt under Python 2 and 3. Some plugins have been slightly altered to use stable sorting for dynamically created parameter lists. Signed-off-by: Christian Heimes <cheimes@redhat.com> Reviewed-By: Alexander Bokovoy <abokovoy@redhat.com>
Diffstat (limited to 'ipalib')
-rw-r--r--ipalib/output.py3
-rw-r--r--ipalib/parameters.py9
-rw-r--r--ipalib/util.py38
3 files changed, 46 insertions, 4 deletions
diff --git a/ipalib/output.py b/ipalib/output.py
index b10458463..afcbefa11 100644
--- a/ipalib/output.py
+++ b/ipalib/output.py
@@ -25,6 +25,7 @@ import six
from ipalib.plugable import ReadOnly, lock
from ipalib.capabilities import client_has_capability
from ipalib.text import _
+from ipalib.util import apirepr
if six.PY3:
unicode = str
@@ -98,7 +99,7 @@ class Output(ReadOnly):
if not value:
continue
if isinstance(value, tuple):
- value = repr(list(value))
+ value = apirepr(list(value))
else:
value = repr(value)
yield '%s=%s' % (key, value)
diff --git a/ipalib/parameters.py b/ipalib/parameters.py
index ead56c018..902ae3401 100644
--- a/ipalib/parameters.py
+++ b/ipalib/parameters.py
@@ -123,7 +123,7 @@ from ipalib.text import Gettext, FixMe
from ipalib.util import json_serialize, validate_idna_domain
from ipalib.x509 import (
load_der_x509_certificate, IPACertificate, default_backend)
-from ipalib.util import strip_csr_header
+from ipalib.util import strip_csr_header, apirepr
from ipapython import kerberos
from ipapython.dn import DN
from ipapython.dnsutil import DNSName
@@ -600,9 +600,12 @@ class Param(ReadOnly):
elif isinstance(value, six.integer_types):
value = str(value)
elif isinstance(value, (tuple, set, frozenset)):
- value = repr(list(value))
- else:
+ value = apirepr(list(value))
+ elif key == 'cli_name':
+ # always represented as native string
value = repr(value)
+ else:
+ value = apirepr(value)
yield '%s=%s' % (key, value)
def __call__(self, value, **kw):
diff --git a/ipalib/util.py b/ipalib/util.py
index 96fcb33ea..16754a36e 100644
--- a/ipalib/util.py
+++ b/ipalib/util.py
@@ -66,6 +66,11 @@ from ipapython.dnsutil import DNSName
from ipapython.dnsutil import resolve_ip_addresses
from ipapython.admintool import ScriptError
+if sys.version_info >= (3, 2):
+ import reprlib # pylint: disable=import-error
+else:
+ reprlib = None
+
if six.PY3:
unicode = str
@@ -1213,3 +1218,36 @@ def open_in_pager(data):
pager_process.communicate()
except IOError:
pass
+
+
+if reprlib is not None:
+ class APIRepr(reprlib.Repr):
+ builtin_types = {
+ bool, int, float,
+ str, bytes,
+ dict, tuple, list, set, frozenset,
+ type(None),
+ }
+
+ def __init__(self):
+ super(APIRepr, self).__init__()
+ # no limitation
+ for k, v in self.__dict__.items():
+ if isinstance(v, int):
+ setattr(self, k, sys.maxsize)
+
+ def repr_str(self, x, level):
+ """Output with u'' prefix"""
+ return 'u' + repr(x)
+
+ def repr_type(self, x, level):
+ if x is str:
+ return "<type 'unicode'>"
+ if x in self.builtin_types:
+ return "<type '{}'>".format(x.__name__)
+ else:
+ return repr(x)
+
+ apirepr = APIRepr().repr
+else:
+ apirepr = repr