Make stamper python-3 compatible
authorBorja Lopez <borja@codigo23.net>
Thu, 05 Sep 2019 16:38:38 +0200
changeset 71 c8e39db35e46
parent 70 3cbf9f8e714e
child 72 092388782a7e
Make stamper python-3 compatible
bin/stamp
bin/stamps
stamper/__init__.py
stamper/config.py
stamper/http.py
stamper/stamper.py
--- a/bin/stamp	Wed Sep 04 10:28:37 2019 +0200
+++ b/bin/stamp	Thu Sep 05 16:38:38 2019 +0200
@@ -2,7 +2,7 @@
 
 import sys
 from datetime import datetime
-from stamper import Stamper
+from stamper.stamper import Stamper
 
 
 def usage(name):
--- a/bin/stamps	Wed Sep 04 10:28:37 2019 +0200
+++ b/bin/stamps	Thu Sep 05 16:38:38 2019 +0200
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 
 from stamper.cli import build_args_parser
-from stamper import Stamper
+from stamper.stamper import Stamper
 
 
 if __name__ == '__main__':
--- a/stamper/__init__.py	Wed Sep 04 10:28:37 2019 +0200
+++ b/stamper/__init__.py	Thu Sep 05 16:38:38 2019 +0200
@@ -1,1 +1,1 @@
-from stamper import Stamper
+
--- a/stamper/config.py	Wed Sep 04 10:28:37 2019 +0200
+++ b/stamper/config.py	Thu Sep 05 16:38:38 2019 +0200
@@ -1,6 +1,6 @@
 
 import os
-from ConfigParser import SafeConfigParser
+from configparser import SafeConfigParser
 
 
 class Config(object):
--- a/stamper/http.py	Wed Sep 04 10:28:37 2019 +0200
+++ b/stamper/http.py	Thu Sep 05 16:38:38 2019 +0200
@@ -1,25 +1,26 @@
 
-from urllib2 import (
-    Request, urlopen, build_opener, install_opener, HTTPCookieProcessor
-    )
-from urllib import urlencode
-from urlparse import urljoin
+import http.client
+from urllib.parse import urlparse, urlencode, quote_plus
 
 
 class HTTPClient(object):
 
     def __init__(self, base_url):
-        self.base_url = base_url
-        self.request = build_opener(HTTPCookieProcessor())
-        install_opener(self.request)
+        self.parsed_url = urlparse(base_url)
+        if self.parsed_url.scheme == 'https':
+            self.conn = http.client.HTTPSConnection(self.parsed_url.netloc)
+        else:
+            self.conn = http.client.HTTPConnection(self.parsed_url.netloc)
 
-    def send_request(self, url, data=None):
-        response = self.request.open(url, data)
+    def get(self, url):
+        self.conn.request('GET', url)
+        response = conn.getresponse()
         return response.read()
 
-    def get(self, url):
-        return self.send_request(urljoin(self.base_url, url))
-
     def post(self, url, data):
-        encoded_data = urlencode(data)
-        return self.send_request(urljoin(self.base_url, url), encoded_data)
+        encoded_data = urlencode(data, quote_via=quote_plus)
+        headers = {'Content-type': 'application/x-www-form-urlencoded',
+                   'Accept': 'text/plain'}
+        self.conn.request('POST', url, encoded_data, headers)
+        response = conn.getresponse()
+        return response.read()
--- a/stamper/stamper.py	Wed Sep 04 10:28:37 2019 +0200
+++ b/stamper/stamper.py	Thu Sep 05 16:38:38 2019 +0200
@@ -291,7 +291,7 @@
                     # if there are multiple customers in the report, show the
                     # daily totals
                     print('daily total: %(total)s' % {'total': totals[day]})
-            print '-'*79
+            print('-'*79)
 
         # now calculate the totals and show them
         totals = self.totals(filter_from, filter_to, filter_descr)
@@ -317,7 +317,7 @@
             sum_tot = ''
             if totals:
                 print('------ Totals ------' % {'day': day})
-                for day, tot in totals.iteritems():
+                for day, tot in totals.items():
                     print(' %(day)s: %(total)s' % {'day': day, 'total': tot})
                     sum_tot = "%(total)s %(new)s" % {
                         'total': sum_tot,