helpshift / helpshift/herald

Python 3 compatibility

Open
#1 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
43
Forks
7
PR merge metrics
No merged PRs in 30d

Description

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 :**

```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)**

```patch
--- 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 '

```

Contributor guide

No contributing guide indexed for this repository

Research direction

Review the proposed changes in herald/rules.py, herald/baseplugin.py, herald/herald.py, herald/plugins/fileplugin.py, and herald/plugins/httpplugin.py, starting by running futurize on the codebase. Check whether the Stage 1 changes are sufficient and whether the Stage 2 future dependency is appropriate for this CLI. Done means the project runs compatibly on Python 2 and 3 without unnecessary startup overhead.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.