summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShaheed Haque <srhaque@theiet.org>2013-01-14 19:50:32 (GMT)
committerShaheed Haque <srhaque@theiet.org>2013-01-14 19:50:32 (GMT)
commit2b9ebe39b508de4295a2a4f5757748c4c900af48 (patch)
tree2d6331975c9c6791fb1fd6aa886c10ac00a19ed0
parent18ccee0f4c2b728d45a7521641efb29662fc3335 (diff)
Get running on Python3
Adapt to semantics changes in Python3. Now works on both Python3 and 2!
-rw-r--r--kate/plugins/pate/src/plugins/gid/gid.py19
-rw-r--r--kate/plugins/pate/src/plugins/gid/idutils.py54
2 files changed, 52 insertions, 21 deletions
diff --git a/kate/plugins/pate/src/plugins/gid/gid.py b/kate/plugins/pate/src/plugins/gid/gid.py
index b25e32f..071c1ba 100644
--- a/kate/plugins/pate/src/plugins/gid/gid.py
+++ b/kate/plugins/pate/src/plugins/gid/gid.py
@@ -39,12 +39,19 @@ import os.path
import re
import sip
import subprocess
+import sys
import time
idDatabase = None
searchBar = None
completionModel = None
+def toStr(_bytes):
+ if sys.hexversion > 0x03000000:
+ return str(_bytes, "utf-8")
+ else:
+ return str(_bytes)
+
class ConfigWidget(QWidget):
"""Configuration widget for this plugin."""
#
@@ -252,13 +259,13 @@ class MatchesModel(HistoryModel):
# The line number follows.
#
lineNumberStart = etagDefinition + len(tokenBytes)
- lineNumberEnd = etagBytes.find(",", lineNumberStart)
+ lineNumberEnd = toStr(etagBytes).find(",", lineNumberStart)
return int(etagBytes[lineNumberStart:lineNumberEnd]) - 1
- if etagBytes.startswith(bytearray(etagsCmd[0])):
+ if etagBytes.startswith(bytearray(etagsCmd[0], "latin-1")):
#
# An error message was printed starting with "etags".
#
- raise IOError(unicode(etagBytes, "latin-1"))
+ raise IOError(toStr(etagBytes))
return None
def _scanFile(self, regexp, filterRe, token, fileName, isDeclaration):
@@ -302,7 +309,7 @@ class MatchesModel(HistoryModel):
#
self.add(fileName, "task-reject", "", 0, 0)
except IOError as e:
- self.add(fileName, "face-sad", str(e), None, None)
+ self.add(fileName, "face-sad", toStr(e), None, None)
return definitionIndex
def add(self, fileName, icon, text, line, column):
@@ -357,7 +364,7 @@ class MatchesModel(HistoryModel):
declarationRe = "(" + declarationRe.replace(".", "\.") + ")$"
declarationRe = re.compile(declarationRe, re.IGNORECASE)
startBoredomQuery = time.time()
- previousBoredomQuery = startBoredomQuery - self._boredomInterval / 2
+ previousBoredomQuery = startBoredomQuery - self._boredomInterval // 2
#
# For each file, list the lines where a match is found.
#
@@ -600,7 +607,7 @@ class SearchBar(QObject):
searchBar.dataSource.setFile(kate.configuration["idFile"])
fileSet = True
except IOError as detail:
- KMessageBox.error(self.parent(), str(detail), i18n("ID database error"))
+ KMessageBox.error(self.parent(), toStr(detail), i18n("ID database error"))
#
# Check the transformation settings.
#
diff --git a/kate/plugins/pate/src/plugins/gid/idutils.py b/kate/plugins/pate/src/plugins/gid/idutils.py
index e25caba..72faa90 100644
--- a/kate/plugins/pate/src/plugins/gid/idutils.py
+++ b/kate/plugins/pate/src/plugins/gid/idutils.py
@@ -31,6 +31,25 @@ import array
import mmap
import os
import struct
+import sys
+
+def toStr(_bytes):
+ if sys.hexversion > 0x03000000:
+ return str(_bytes, "utf-8")
+ else:
+ return str(_bytes)
+
+def toBytes(name):
+ if sys.hexversion > 0x03000000:
+ if isinstance(name, str):
+ return bytearray(name.encode("utf-8"))
+ else:
+ return bytearray(name)
+ else:
+ if isinstance(name, unicode):
+ return bytearray(name.encode("utf-8"))
+ else:
+ return bytearray(name)
class _FileLinkDb():
"""Read the file link database in an ID file.
@@ -165,7 +184,7 @@ class _FileLinkDb():
parentIndex = self.rawData[i + 3]
parentIndex = (parentIndex << 8) + self.rawData[i + 2]
parentIndex = (parentIndex << 8) + self.rawData[i + 1]
- return str(name), flags, parentIndex
+ return toStr(name), flags, parentIndex
class _TokenDb():
"""Read the file link database in an ID file.
@@ -199,7 +218,10 @@ class _TokenDb():
def __getitem__(self, key):
try:
# single element
- return ord(self.mapped[self.offset + key])
+ if sys.hexversion > 0x03000000:
+ return self.mapped[self.offset + key]
+ else:
+ return ord(self.mapped[self.offset + key])
except TypeError:
# A slice
return bytearray(self.mapped[self.offset + key.start:self.offset + key.stop])
@@ -298,7 +320,7 @@ class _TokenDb():
#
# More than enough...
#
- bitsVecSize = (maxFiles + 7) / 4
+ bitsVecSize = (maxFiles + 7) // 4
self.bitsVec = array.array('B')
for i in range(bitsVecSize):
self.bitsVec.append(0)
@@ -322,9 +344,7 @@ class _TokenDb():
See idutils lid.c.
"""
- if isinstance(name, unicode):
- name = name.encode("utf-8")
- token = bytearray(name)
+ token = toBytes(name)
token.append(0)
tokenSize = len(token)
start = 0
@@ -333,7 +353,7 @@ class _TokenDb():
order = None
while start < end:
- offset = start + (end - start) / 2
+ offset = start + (end - start) // 2
offset = self._skip_past_00(offset)
if offset >= end:
offset = start
@@ -376,7 +396,7 @@ class _TokenDb():
#
while self.rawData[offset + i] != 0:
i += 1
- name = str(self.rawData[offset:offset + i])
+ name = toStr(self.rawData[offset:offset + i])
else:
raise IndexError("Not found: " + name)
@@ -398,9 +418,7 @@ class _TokenDb():
IndexError is thrown.
"""
offset = self._skip_past_00(startingOffset)
- if isinstance(name, unicode):
- name = name.encode("utf-8")
- token = bytearray(name)
+ token = toBytes(name)
token.append(0)
tokenSize = len(token)
@@ -430,7 +448,7 @@ class _TokenDb():
#
while self.rawData[offset + i] != 0:
i += 1
- name = str(self.rawData[offset:offset + i])
+ name = toStr(self.rawData[offset:offset + i])
#
# Are we done?
#
@@ -513,7 +531,7 @@ class _TokenDb():
while (bit & 0xff) != 0:
if (bit & hits) != 0:
self.flinkv_0[flinkv] = self.fileLinkDb.memberOffsets[members]
- #print "FOUND MEMBER", x.file(self.flinkv_0[flinkv])
+ #print("FOUND MEMBER", self.flinkv_0[flinkv])
flinkv += 1
members += 1
if members >= end:
@@ -638,8 +656,7 @@ class Lookup():
data = bytearray(end - start)
bytesRead = self.file.readinto(data)
if bytesRead < len(data):
- print("Read {} bytes instead of {}".format(bytesRead, len(data)))
- raise IOError
+ raise IOError("Read {} bytes instead of {}".format(bytesRead, len(data)))
return data
def __repr__(self):
@@ -665,3 +682,10 @@ class Lookup():
"""Return the (offset, name) matching the prefix token after the given offset."""
return self.tokenDb.nextPrefixMatch(offset, token)
+if __name__ == "__main__":
+ dataSource = Lookup()
+ dataSource.setFile("ID")
+ #tokenFlags, hitCount, files = dataSource.literalSearch("fib_route_looped")
+ tokenFlags, hitCount, files = dataSource.literalSearch("cef_dpss_open")
+ print(tokenFlags, hitCount, files)
+