Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 compatibility #1

Open
tirkarthi opened this issue May 15, 2018 · 0 comments
Open

Python 3 compatibility #1

tirkarthi opened this issue May 15, 2018 · 0 comments

Comments

@tirkarthi
Copy link

The project seems to be incompatible with Python 3. You can use the futurize module to generate code that is compatible with 2 and 3. I ran the module across the codebase and the patches as below. Feel free to close this if Python 3 compatibility is not needed. Stage 2 involves using futurize as dependency which might be slight overkill if this is intended to be a CLI used very frequently with respect to startup times.

Stage 1 patch :

--- herald/rules.py	(original)
+++ herald/rules.py	(refactored)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from __future__ import print_function
 import re
 import logging
 
@@ -189,7 +190,7 @@
         try:
             value = float(value)
         except ValueError:
-            print 'value must be of type int or float! value is {}'.format(value)
+            print('value must be of type int or float! value is {}'.format(value))
             raise
 
         for rule in self._parsed_rules:

Stage 2 patch : (Requires future as a dependency since it handles module reorganization)

--- herald/baseplugin.py	(original)
+++ herald/baseplugin.py	(refactored)
@@ -1,7 +1,10 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from builtins import str
+from builtins import object
 from gevent import monkey
+from future.utils import with_metaclass
 monkey.patch_all()
 
 import time
@@ -37,7 +40,7 @@
             cls.plugins.append(cls)
 
 
-class HeraldBasePlugin(object):
+class HeraldBasePlugin(with_metaclass(PluginMount, object)):
     """
     All plugins should inherit from this class.
 
@@ -45,7 +48,6 @@
     all plugin classes are available as a list in the `plugins` attribute.
 
     """
-    __metaclass__ = PluginMount
 
     def __init__(self, name, *args, **kwargs):
         self.name = name
--- herald/herald.py	(original)
+++ herald/herald.py	(refactored)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from builtins import str
 from gevent import monkey
 monkey.patch_all()
 
--- herald/plugins/fileplugin.py	(original)
+++ herald/plugins/fileplugin.py	(refactored)
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from builtins import str
 import json
 from herald.baseplugin import HeraldPlugin
 
--- herald/plugins/httpplugin.py	(original)
+++ herald/plugins/httpplugin.py	(refactored)
@@ -1,10 +1,13 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from future import standard_library
+standard_library.install_aliases()
+from builtins import str
 from gevent import monkey
 monkey.patch_all()
 
-import urllib2
+import urllib.request, urllib.error, urllib.parse
 import socket
 import json
 from herald.baseplugin import HeraldPlugin
@@ -23,13 +26,13 @@
         self.is_json = kwargs.get('is_json', False)
 
     def run(self, timeout=10):
-        req = urllib2.Request(self.url)
+        req = urllib.request.Request(self.url)
         response = ''
         try:
-            infourl = urllib2.urlopen(req, timeout=timeout)
-        except urllib2.HTTPError as e:
+            infourl = urllib.request.urlopen(req, timeout=timeout)
+        except urllib.error.HTTPError as e:
             self.logger.warning('HTTPError: get failed, http code: %s', e.code)
-        except urllib2.URLError as e:
+        except urllib.error.URLError as e:
             self.logger.critical('URLError: failed to reach a server, '
                                  'reason: %s', e.reason)
         except socket.timeout:
--- herald/rules.py	(original)
+++ herald/rules.py	(refactored)
@@ -1,6 +1,10 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from __future__ import division
+from builtins import str
+from past.utils import old_div
+from builtins import object
 import re
 import logging
 
@@ -93,7 +97,7 @@
 
         """
         for rule in self.rules:
-            action, pattern = rule.items()[0]
+            action, pattern = list(rule.items())[0]
             if re.match(pattern, str(value)):
                 return action
         else:
@@ -155,7 +159,7 @@
                     threshold = rule['pct']
                     min_resp = rule.get('min_threshold_response', 1)
                 else:
-                    action, threshold = rule.items()[0]
+                    action, threshold = list(rule.items())[0]
 
                 m = re.match(self.op_regex, str(threshold))
                 op, th = m.groups()
@@ -197,7 +201,7 @@
             if action == 'pct':
                 # calculate the percentage of traffic to be sent based on
                 # the threshold. The op value is ignored.
-                pct = int(100 - ((value / float(threshold)) * 100))
+                pct = int(100 - ((old_div(value, float(threshold))) * 100))
                 if pct <= 0:
                     min_resp = rule[3]
                     self.logger.warn('Pct value {} less than 0, responding with min '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant