From 577d49ac3ccf4f4093e8f8cf25c3ccb9385f74b3 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Sun, 16 Aug 2015 19:00:00 +0200
Subject: [PATCH 31/34] Py3 modernization: libmodernize.fixes.fix_dict_six

In Python 3 dict methods like values(), items() and keys() return views
rather than lists. The iter equivalents are gone. Use six to use
iterators on Python 2 and 3.

In some places like setup.py a list is required. Use
list(somedict.values()) to get a list on all Python versions.
---
 base/common/python/pki/cert.py                        | 14 ++++++++------
 base/common/python/pki/cli.py                         |  3 ++-
 base/common/python/pki/encoder.py                     | 12 +++++++-----
 base/common/python/pki/key.py                         |  8 +++++---
 base/common/python/pki/profile.py                     | 15 ++++++++-------
 base/kra/functional/drmclient_deprecated.py           |  2 +-
 base/server/python/pki/server/deployment/pkihelper.py |  2 +-
 setup.py                                              |  2 +-
 8 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/base/common/python/pki/cert.py b/base/common/python/pki/cert.py
index 6d0d408338e983d1626aa46d52fd2eb8b0bbb0ce..2d58949cd28f69a8aea72e2a90b3808b7a9380be 100644
--- a/base/common/python/pki/cert.py
+++ b/base/common/python/pki/cert.py
@@ -25,6 +25,8 @@ from __future__ import print_function
 import copy
 import json
 
+from six import iteritems
+
 import pki
 import pki.client as client
 import pki.encoder as encoder
@@ -75,7 +77,7 @@ class CertData(object):
         """ Return CertData object from JSON dict """
         cert_data = cls()
 
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['Link']:
                 if k in CertData.json_attribute_names:
                     setattr(cert_data, CertData.json_attribute_names[k], v)
@@ -131,7 +133,7 @@ class CertDataInfo(object):
     def from_json(cls, attr_list):
         """ Return CertDataInfo object from JSON dict """
         cert_data_info = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['Link']:
                 if k in CertDataInfo.json_attribute_names:
                     setattr(cert_data_info,
@@ -221,7 +223,7 @@ class CertRequestInfo(object):
     def from_json(cls, attr_list):
         cert_request_info = cls()
 
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['Link']:
                 if k in CertRequestInfo.json_attribute_names:
                     setattr(cert_request_info,
@@ -336,7 +338,7 @@ class CertSearchRequest(object):
         if len(cert_search_params) == 0:
             setattr(self, 'serialNumberRangeInUse', True)
 
-        for param, value in cert_search_params.viewitems():
+        for param, value in iteritems(cert_search_params):
             if param not in CertSearchRequest.search_params:
                 raise ValueError('Invalid search parameter: ' + param)
 
@@ -499,7 +501,7 @@ class CertEnrollmentRequest(object):
 
         enroll_request = cls()
 
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['Input', 'Output']:
                 if k in CertEnrollmentRequest.json_attribute_names:
                     setattr(enroll_request,
@@ -592,7 +594,7 @@ class CertReviewResponse(CertEnrollmentRequest):
         # First read the values for attributes defined in CertEnrollmentRequest
         review_response = super(CertReviewResponse, cls).from_json(attr_list)
 
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['ProfilePolicySet'] and k not in \
                     CertEnrollmentRequest.json_attribute_names:
                 if k in CertReviewResponse.json_attribute_names:
diff --git a/base/common/python/pki/cli.py b/base/common/python/pki/cli.py
index 57d13a0fa1e7a0f89d120edcbe08ea3498e2a0da..802ef01aa1df3bf95c741741cbc3aa1a9793796a 100644
--- a/base/common/python/pki/cli.py
+++ b/base/common/python/pki/cli.py
@@ -24,6 +24,7 @@ from __future__ import print_function
 import collections
 import getopt
 import sys
+from six import itervalues
 
 
 class CLI(object):
@@ -75,7 +76,7 @@ class CLI(object):
 
         print('Commands:')
 
-        for module in self.modules.itervalues():
+        for module in itervalues(self.modules):
             full_name = module.get_full_name()
             print(' {:30}{:30}'.format(full_name, module.description))
 
diff --git a/base/common/python/pki/encoder.py b/base/common/python/pki/encoder.py
index cebcc566b2e2533667c2979bdc6a0a62b1f5db03..bf5d2e473522f2e2348da535ae4029633b5eee46 100644
--- a/base/common/python/pki/encoder.py
+++ b/base/common/python/pki/encoder.py
@@ -1,5 +1,6 @@
 from __future__ import absolute_import
 import json
+from six import iteritems, itervalues
 
 TYPES = {}
 NOTYPES = {}
@@ -35,10 +36,10 @@ class CustomTypeEncoder(json.JSONEncoder):
     # pylint: disable=E0202
 
     def default(self, obj):
-        for k, v in TYPES.iteritems():
+        for k, v in iteritems(TYPES):
             if isinstance(obj, v):
                 return {k: obj.__dict__}
-        for t in NOTYPES.itervalues():
+        for t in itervalues(NOTYPES):
             if isinstance(obj, t):
                 return self.attr_name_conversion(obj.__dict__, type(obj))
         return json.JSONEncoder.default(self, obj)
@@ -48,9 +49,9 @@ class CustomTypeEncoder(json.JSONEncoder):
         if not hasattr(object_class, 'json_attribute_names'):
             return attr_dict
         reverse_dict = {v: k for k, v in
-                        object_class.json_attribute_names.iteritems()}
+                        iteritems(object_class.json_attribute_names)}
         new_dict = dict()
-        for k, v in attr_dict.iteritems():
+        for k, v in iteritems(attr_dict):
             if k in reverse_dict:
                 new_dict[reverse_dict[k]] = v
             else:
@@ -60,7 +61,8 @@ class CustomTypeEncoder(json.JSONEncoder):
 
 def CustomTypeDecoder(dct):  # nopep8
     if len(dct) == 1:
-        type_name, value = dct.items()[0]
+        type_name = list(dct)[0]
+        value = dct[type_name]
         if type_name in TYPES:
             return TYPES[type_name].from_dict(value)
     return dct
diff --git a/base/common/python/pki/key.py b/base/common/python/pki/key.py
index 8064ffc19ac4ce0dff0293130a1771e4265a7920..7b9a463a09cc0ef364d97c435a38be17119e97e0 100644
--- a/base/common/python/pki/key.py
+++ b/base/common/python/pki/key.py
@@ -30,6 +30,8 @@ import base64
 import json
 import urllib
 
+from six import iteritems
+
 import pki
 import pki.encoder as encoder
 
@@ -70,7 +72,7 @@ class KeyData(object):
     def from_json(cls, attr_list):
         """ Return a KeyData object from a JSON dict """
         key_data = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k in KeyData.json_attribute_names:
                 setattr(key_data, KeyData.json_attribute_names[k], v)
             else:
@@ -125,7 +127,7 @@ class KeyInfo(object):
     def from_json(cls, attr_list):
         """ Return KeyInfo from JSON dict """
         key_info = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k in KeyInfo.json_attribute_names:
                 setattr(key_info, KeyInfo.json_attribute_names[k], v)
             else:
@@ -190,7 +192,7 @@ class KeyRequestInfo(object):
     def from_json(cls, attr_list):
         """ Return a KeyRequestInfo object from a JSON dict. """
         key_request_info = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k in KeyRequestInfo.json_attribute_names:
                 setattr(key_request_info,
                         KeyRequestInfo.json_attribute_names[k], v)
diff --git a/base/common/python/pki/profile.py b/base/common/python/pki/profile.py
index b9c55a4e7395487d9dc0948a214b2ef7a86fbd23..7d6bb5ef22fcf28e4d63b75ea0b3466dc2d05c0b 100644
--- a/base/common/python/pki/profile.py
+++ b/base/common/python/pki/profile.py
@@ -22,6 +22,7 @@ from __future__ import print_function
 import json
 import os
 
+from six import iteritems
 
 import pki
 import pki.client as client
@@ -60,7 +61,7 @@ class ProfileDataInfo(object):
             return None
 
         profile_data_info = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k in ProfileDataInfo.json_attribute_names:
                 setattr(profile_data_info,
                         ProfileDataInfo.json_attribute_names[k], v)
@@ -130,7 +131,7 @@ class Descriptor(object):
             return None
 
         descriptor = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k in Descriptor.json_attribute_names:
                 setattr(descriptor,
                         Descriptor.json_attribute_names[k], v)
@@ -262,7 +263,7 @@ class ProfileInput(object):
             return None
         profile_input = cls()
 
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['Attribute', 'ConfigAttribute']:
                 if k in ProfileInput.json_attribute_names:
                     setattr(profile_input,
@@ -347,7 +348,7 @@ class ProfileOutput(object):
             return None
 
         profile_output = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['attributes']:
                 if k in ProfileOutput.json_attribute_names:
                     setattr(profile_output,
@@ -469,7 +470,7 @@ class PolicyDefault(object):
             return None
 
         policy_def = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['policyAttribute', 'params']:
                 if k in PolicyDefault.json_attribute_names:
                     setattr(policy_def,
@@ -588,7 +589,7 @@ class PolicyConstraint(object):
             return None
 
         policy_constraint = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['constraint']:
                 if k in PolicyConstraint.json_attribute_names:
                     setattr(policy_constraint,
@@ -921,7 +922,7 @@ class Profile(object):
     @classmethod
     def from_json(cls, attr_list):
         profile_data = cls()
-        for k, v in attr_list.iteritems():
+        for k, v in iteritems(attr_list):
             if k not in ['Input', 'Output', 'PolicySets']:
                 if k in Profile.json_attribute_names:
                     setattr(profile_data,
diff --git a/base/kra/functional/drmclient_deprecated.py b/base/kra/functional/drmclient_deprecated.py
index 5d74a3a008141fdf3fbd7480ccb6e5dcd5617288..a5b985fee3584b134ca978c1b9f5ee90d9429f00 100644
--- a/base/kra/functional/drmclient_deprecated.py
+++ b/base/kra/functional/drmclient_deprecated.py
@@ -1067,7 +1067,7 @@ response = test_kra.list_security_data(client_id, "active")
 print(response)
 
 # create recovery request
-key_id = response.keys()[0]
+key_id = list(response)[0]
 print(key_id)
 response = test_kra.submit_recovery_request(key_id)
 print(response)
diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py
index de3b1bc558dcc9e8a80b048bd53c75bacdc64223..efe85c85d200498f54a2ea6aaa24485ac09cf8f9 100644
--- a/base/server/python/pki/server/deployment/pkihelper.py
+++ b/base/server/python/pki/server/deployment/pkihelper.py
@@ -3025,7 +3025,7 @@ class KRAConnector:
                 "Trying old interface. " + str(e),
                 extra=config.PKI_INDENTATION_LEVEL_2)
             info = sd.get_old_security_domain_info()
-        return info.systems['CA'].hosts.values()
+        return list(info.systems['CA'].hosts.values())
 
     def execute_using_pki(
             self, caport, cahost, subsystemnick,
diff --git a/setup.py b/setup.py
index 005000668bfe406e7a22004b0389edc2047f00ba..8b606fb815ca74a4f35a48dbf22323c4a7b020c5 100644
--- a/setup.py
+++ b/setup.py
@@ -72,7 +72,7 @@ setup(
         'base/server/sbin/pkispawn',
         'base/java-tools/bin/pki',
     ],
-    data_files=upgrades.items(),
+    data_files=list(upgrades.items()),
     classifiers=[
         'Development Status :: 5 - Production/Stable',
         'Environment :: Web Environment',
-- 
2.4.3

