More Python 3 rework

Sun, 16 Aug 2015 19:27:14 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 16 Aug 2015 19:27:14 +0300
changeset 153
497b7290977d
parent 152
1b734faab67a
child 154
df862cca1773

More Python 3 rework

hgpoll.py file | annotate | diff | comparison | revisions
mod_util.py file | annotate | diff | comparison | revisions
utility.py file | annotate | diff | comparison | revisions
--- a/hgpoll.py	Sun Aug 16 10:59:22 2015 +0300
+++ b/hgpoll.py	Sun Aug 16 19:27:14 2015 +0300
@@ -63,13 +63,9 @@
 	return repo
 
 def check_repo_exists (name):
-	' Check if a repository exists '
-	print ('Checking that %s exists...' % name)
-
+	' Ensures that the repository exists '
 	repo = get_repo_by_name (name)
-
-	if not os.path.exists (repo.name):
-		os.makedirs (repo.name)
+	os.makedirs (repo.name, exist_ok = True)
 
 	if not repo.is_valid():
 		# If the repo does not exist, clone it.
@@ -86,7 +82,6 @@
 			print ('Cloning done.')
 		except Exception as e:
 			raise HgProcessError ('Unable to clone %s from %s: %s' % (repo.name, repo.clone_url, e))
-			quit (1)
 
 	if not repo.is_valid():
 		raise HgProcessError ('''%s is not a valid repository after cloning '''
@@ -323,15 +318,15 @@
 		messageSizeClass = LENGTH_FULL
 
 	# Post it all on IRC now
-	for message in messages[messageSizeClass]:
-		for irc_client in Irc.all_clients:
-			for channel in irc_client.channels:
-				if not channel.get_value ('btannounce', False):
-					continue
+	for irc_client in Irc.all_clients:
+		for channel in irc_client.channels:
+			if not channel.get_value ('btannounce', False):
+				continue
 
-				if not repo.published and not channel.get_value ('allpublishing', False):
-					continue
+			if not repo.published and not channel.get_value ('allpublishing', False):
+				continue
 
+			for message in messages[messageSizeClass]:
 				irc_client.privmsg (channel.get_value ('name'), message)
 
 	db.commit()
--- a/mod_util.py	Sun Aug 16 10:59:22 2015 +0300
+++ b/mod_util.py	Sun Aug 16 19:27:14 2015 +0300
@@ -34,7 +34,7 @@
 import modulecore as ModuleCore
 import utility
 import calc
-import urllib
+import urllib.parse
 
 ModuleData = {
 	'commands':
@@ -223,7 +223,7 @@
 	reply ('Result: %s' % utility.shorten_link (args['link']))
 
 def cmd_py (reply, args, **rest):
-	url = 'http://eval.appspot.com/eval?statement=' + urllib.quote (args['command'])
+	url = 'http://eval.appspot.com/eval?statement=' + urllib.parse.quote (args['command'])
 	result = utility.read_url (url).splitlines()
 
 	# \x0f is the 'reset colors' code, prepended to all reply lines to prevent other bots from
--- a/utility.py	Sun Aug 16 10:59:22 2015 +0300
+++ b/utility.py	Sun Aug 16 19:27:14 2015 +0300
@@ -36,10 +36,7 @@
 except ImportError as e:
 	print ('Unable to import bitly_api: %s' % e)
 
-try:
-	import urllib.request
-except ImportError:
-	import urllib2
+import urllib.request
 
 def shorten_link (link):
 	if 'bitly_api' in sys.modules:
@@ -55,10 +52,8 @@
 	return link
 
 def read_url (url):
-	if 'urllib.request' in sys.modules:
-		return urllib.request.urlopen (urllib.request.Request (url)).read()
-	else:
-		return urllib2.urlopen (url).read()
+	data = urllib.request.urlopen (urllib.request.Request (url)).read()
+	return data.decode ('utf-8')
 
 def make_progress_bar (p, barLength, colored=True):
 	BoldChar, ColorChar = (Irc.BoldChar, Irc.ColorChar)

mercurial