Skip to content

Commit e10b28b

Browse files
committed
Update mmm_modules for python 3
1 parent 745691c commit e10b28b

File tree

6 files changed

+72
-71
lines changed

6 files changed

+72
-71
lines changed

mamamedia_modules.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import os
2222
import sys
23+
import gi
24+
gi.require_version('Gtk', '3.0')
2325

2426
cwd = os.path.split(__file__)[0]
2527

mmm_modules/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
from .buddy_panel import *
77
from .tube_helper import *
88
from .utils import *
9-
import json
9+
from . import json

mmm_modules/borderframe.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
# If you find this activity useful or end up using parts of it in one of your
1818
# own creations we would love to hear from you at [email protected] !
1919
#
20-
20+
import gi
21+
gi.require_version('Gtk', '3.0')
2122
from gi.repository import Gtk, GObject, Gdk
2223

2324
BORDER_LEFT = 1

mmm_modules/i18n.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
import os
2525
import gettext
2626
import locale
27-
27+
import gi
28+
gi.require_version('Gtk', '3.0')
2829
from gi.repository import Gtk
2930
from gi.repository import GObject
3031

@@ -166,11 +167,11 @@ def install(self, *args):
166167

167168
def gather_other_translations():
168169
from glob import glob
169-
lessons = filter(lambda x: os.path.isdir(x), glob('lessons/*'))
170-
lessons = map(lambda x: os.path.basename(x), lessons)
171-
lessons = map(lambda x: x[0].isdigit() and x[1:] or x, lessons)
172-
images = filter(lambda x: os.path.isdir(x), glob('images/*'))
173-
images = map(lambda x: os.path.basename(x), images)
170+
lessons = [x for x in glob('lessons/*') if os.path.isdir(x)]
171+
lessons = [os.path.basename(x) for x in lessons]
172+
lessons = [x[0].isdigit() and x[1:] or x for x in lessons]
173+
images = [x for x in glob('images/*') if os.path.isdir(x)]
174+
images = [os.path.basename(x) for x in images]
174175
f = file('i18n_misc_strings.py', 'w')
175176
for e in images + lessons:
176177
f.write('_("%s")\n' % e)

mmm_modules/json.py

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121

2222

2323
class _StringGenerator(object):
24-
def __init__(self, string):
25-
self.string = string
26-
self.index = -1
27-
def peek(self):
28-
i = self.index + 1
29-
if i < len(self.string):
30-
return self.string[i]
31-
else:
32-
return None
33-
def next(self):
34-
self.index += 1
35-
if self.index < len(self.string):
36-
return self.string[self.index]
37-
else:
38-
raise StopIteration
39-
def all(self):
40-
return self.string
24+
def __init__(self, string):
25+
self.string = string
26+
self.index = -1
27+
def peek(self):
28+
i = self.index + 1
29+
if i < len(self.string):
30+
return self.string[i]
31+
else:
32+
return None
33+
def __next__(self):
34+
self.index += 1
35+
if self.index < len(self.string):
36+
return self.string[self.index]
37+
else:
38+
raise StopIteration
39+
def all(self):
40+
return self.string
4141

4242
class WriteException(Exception):
4343
pass
@@ -58,7 +58,7 @@ def _read(self):
5858
self._eatWhitespace()
5959
peek = self._peek()
6060
if peek is None:
61-
raise ReadException, "Nothing to read: '%s'" % self._generator.all()
61+
raise ReadException("Nothing to read: '%s'" % self._generator.all())
6262
if peek == '{':
6363
return self._readObject()
6464
elif peek == '[':
@@ -77,7 +77,7 @@ def _read(self):
7777
self._readComment()
7878
return self._read()
7979
else:
80-
raise ReadException, "Input is not valid JSON: '%s'" % self._generator.all()
80+
raise ReadException("Input is not valid JSON: '%s'" % self._generator.all())
8181

8282
def _readTrue(self):
8383
self._assertNext('t', "true")
@@ -103,7 +103,7 @@ def _readNull(self):
103103

104104
def _assertNext(self, ch, target):
105105
if self._next() != ch:
106-
raise ReadException, "Trying to read %s: '%s'" % (target, self._generator.all())
106+
raise ReadException("Trying to read %s: '%s'" % (target, self._generator.all()))
107107

108108
def _readNumber(self):
109109
isfloat = False
@@ -119,7 +119,7 @@ def _readNumber(self):
119119
else:
120120
return int(result)
121121
except ValueError:
122-
raise ReadException, "Not a valid JSON number: '%s'" % result
122+
raise ReadException("Not a valid JSON number: '%s'" % result)
123123

124124
def _readString(self):
125125
result = ""
@@ -132,20 +132,20 @@ def _readString(self):
132132
if ch in 'brnft':
133133
ch = self.escapes[ch]
134134
elif ch == "u":
135-
ch4096 = self._next()
136-
ch256 = self._next()
137-
ch16 = self._next()
138-
ch1 = self._next()
139-
n = 4096 * self._hexDigitToInt(ch4096)
140-
n += 256 * self._hexDigitToInt(ch256)
141-
n += 16 * self._hexDigitToInt(ch16)
142-
n += self._hexDigitToInt(ch1)
143-
ch = unichr(n)
135+
ch4096 = self._next()
136+
ch256 = self._next()
137+
ch16 = self._next()
138+
ch1 = self._next()
139+
n = 4096 * self._hexDigitToInt(ch4096)
140+
n += 256 * self._hexDigitToInt(ch256)
141+
n += 16 * self._hexDigitToInt(ch16)
142+
n += self._hexDigitToInt(ch1)
143+
ch = chr(n)
144144
elif ch not in '"/\\':
145-
raise ReadException, "Not a valid escaped JSON character: '%s' in %s" % (ch, self._generator.all())
145+
raise ReadException("Not a valid escaped JSON character: '%s' in %s" % (ch, self._generator.all()))
146146
result = result + ch
147147
except StopIteration:
148-
raise ReadException, "Not a valid JSON string: '%s'" % self._generator.all()
148+
raise ReadException("Not a valid JSON string: '%s'" % self._generator.all())
149149
assert self._next() == '"'
150150
return result
151151

@@ -155,8 +155,8 @@ def _hexDigitToInt(self, ch):
155155
except KeyError:
156156
try:
157157
result = int(ch)
158-
except ValueError:
159-
raise ReadException, "The character %s is not a hex digit." % ch
158+
except ValueError:
159+
raise ReadException("The character %s is not a hex digit." % ch)
160160
return result
161161

162162
def _readComment(self):
@@ -167,7 +167,7 @@ def _readComment(self):
167167
elif second == '*':
168168
self._readCStyleComment()
169169
else:
170-
raise ReadException, "Not a valid JSON comment: %s" % self._generator.all()
170+
raise ReadException("Not a valid JSON comment: %s" % self._generator.all())
171171

172172
def _readCStyleComment(self):
173173
try:
@@ -176,10 +176,10 @@ def _readCStyleComment(self):
176176
ch = self._next()
177177
done = (ch == "*" and self._peek() == "/")
178178
if not done and ch == "/" and self._peek() == "*":
179-
raise ReadException, "Not a valid JSON comment: %s, '/*' cannot be embedded in the comment." % self._generator.all()
179+
raise ReadException ("Not a valid JSON comment: %s, '/*' cannot be embedded in the comment." % self._generator.all())
180180
self._next()
181181
except StopIteration:
182-
raise ReadException, "Not a valid JSON comment: %s, expected */" % self._generator.all()
182+
raise ReadException ("Not a valid JSON comment: %s, expected */" % self._generator.all())
183183

184184
def _readDoubleSolidusComment(self):
185185
try:
@@ -201,7 +201,7 @@ def _readArray(self):
201201
if not done:
202202
ch = self._next()
203203
if ch != ",":
204-
raise ReadException, "Not a valid JSON array: '%s' due to: '%s'" % (self._generator.all(), ch)
204+
raise ReadException("Not a valid JSON array: '%s' due to: '%s'" % (self._generator.all(), ch))
205205
assert ']' == self._next()
206206
return result
207207

@@ -211,12 +211,12 @@ def _readObject(self):
211211
done = self._peek() == '}'
212212
while not done:
213213
key = self._read()
214-
if type(key) is not types.StringType:
215-
raise ReadException, "Not a valid JSON object key (should be a string): %s" % key
214+
if type(key) is not bytes:
215+
raise ReadException("Not a valid JSON object key (should be a string): %s" % key)
216216
self._eatWhitespace()
217217
ch = self._next()
218218
if ch != ":":
219-
raise ReadException, "Not a valid JSON object: '%s' due to: '%s'" % (self._generator.all(), ch)
219+
raise ReadException("Not a valid JSON object: '%s' due to: '%s'" % (self._generator.all(), ch))
220220
self._eatWhitespace()
221221
val = self._read()
222222
result[key] = val
@@ -225,8 +225,8 @@ def _readObject(self):
225225
if not done:
226226
ch = self._next()
227227
if ch != ",":
228-
raise ReadException, "Not a valid JSON array: '%s' due to: '%s'" % (self._generator.all(), ch)
229-
assert self._next() == "}"
228+
raise ReadException("Not a valid JSON array: '%s' due to: '%s'" % (self._generator.all(), ch))
229+
assert self._next() == "}"
230230
return result
231231

232232
def _eatWhitespace(self):
@@ -242,7 +242,7 @@ def _peek(self):
242242
return self._generator.peek()
243243

244244
def _next(self):
245-
return self._generator.next()
245+
return next(self._generator)
246246

247247
class JsonWriter(object):
248248

@@ -257,18 +257,18 @@ def write(self, obj, escaped_forward_slash=False):
257257

258258
def _write(self, obj):
259259
ty = type(obj)
260-
if ty is types.DictType:
260+
if ty is dict:
261261
n = len(obj)
262262
self._append("{")
263-
for k, v in obj.items():
263+
for k, v in list(obj.items()):
264264
self._write(k)
265265
self._append(":")
266266
self._write(v)
267267
n = n - 1
268268
if n > 0:
269269
self._append(",")
270270
self._append("}")
271-
elif ty is types.ListType or ty is types.TupleType:
271+
elif ty is list or ty is tuple:
272272
n = len(obj)
273273
self._append("[")
274274
for item in obj:
@@ -277,31 +277,29 @@ def _write(self, obj):
277277
if n > 0:
278278
self._append(",")
279279
self._append("]")
280-
elif ty is types.StringType or ty is types.UnicodeType:
280+
elif ty is bytes or ty is str:
281281
self._append('"')
282-
obj = obj.replace('\\', r'\\')
282+
obj = obj.replace('\\', r'\\')
283283
if self._escaped_forward_slash:
284284
obj = obj.replace('/', r'\/')
285-
obj = obj.replace('"', r'\"')
286-
obj = obj.replace('\b', r'\b')
287-
obj = obj.replace('\f', r'\f')
288-
obj = obj.replace('\n', r'\n')
289-
obj = obj.replace('\r', r'\r')
290-
obj = obj.replace('\t', r'\t')
285+
obj = obj.replace('"', r'\"')
286+
obj = obj.replace('\b', r'\b')
287+
obj = obj.replace('\f', r'\f')
288+
obj = obj.replace('\n', r'\n')
289+
obj = obj.replace('\r', r'\r')
290+
obj = obj.replace('\t', r'\t')
291291
self._append(obj)
292292
self._append('"')
293-
elif ty is types.IntType or ty is types.LongType:
293+
elif ty is int or ty is float:
294294
self._append(str(obj))
295-
elif ty is types.FloatType:
296-
self._append("%f" % obj)
297295
elif obj is True:
298296
self._append("true")
299297
elif obj is False:
300298
self._append("false")
301299
elif obj is None:
302300
self._append("null")
303301
else:
304-
raise WriteException, "Cannot write in JSON: %s" % repr(obj)
302+
raise WriteException("Cannot write in JSON: %s" % repr(obj))
305303

306304
def write(obj, escaped_forward_slash=False):
307305
return JsonWriter().write(obj, escaped_forward_slash)

mmm_modules/notebook_reader.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__ (self, path, lang_details=None):
4242
def sync (self):
4343
""" must be called after language changes """
4444
self.lesson_array = []
45-
lessons = filter(lambda x: os.path.isdir(os.path.join(self.path, x)), os.listdir(self.path))
45+
lessons = [x for x in os.listdir(self.path) if os.path.isdir(os.path.join(self.path, x))]
4646
lessons.sort()
4747
for lesson in lessons:
4848
if lesson[0].isdigit():
@@ -58,9 +58,8 @@ def _get_lesson_filename (self, path):
5858
code, encoding = locale.getdefaultlocale()
5959
if code is None:
6060
code = 'en'
61-
files = map(lambda x: os.path.join(path, '%s.abw' % x),
62-
('_'+code.lower(), '_'+code.split('_')[0].lower(), 'default'))
63-
files = filter(lambda x: os.path.exists(x), files)
61+
files = [os.path.join(path, '%s.abw' % x) for x in ('_'+code.lower(), '_'+code.split('_')[0].lower(), 'default')]
62+
files = [x for x in files if os.path.exists(x)]
6463
return os.path.join(os.getcwd(), files[0])
6564

6665
def get_lessons (self):

0 commit comments

Comments
 (0)