lib: Move decode function to lib.py

decode() is needed by FindCompatibleDts but importing query.py result in
an instantiation of the database (query.py line 29).

Moving it to lib.py allows to import it for FindCompatibleDts without
opening the database.

Signed-off-by: Maxime Chretien <maxime.chretien@bootlin.com>
This commit is contained in:
Maxime Chretien 2020-06-10 10:23:40 +02:00
parent e2adc03d1e
commit c1abf3d134
3 changed files with 11 additions and 11 deletions

View file

@ -20,7 +20,7 @@
import re
from urllib import parse
from query import decode
from lib import decode
class FindCompatibleDTS:
def __init__(self):
@ -50,7 +50,7 @@ class FindCompatibleDTS:
# Iterate though lines and search for idents
for num, line in enumerate(file_lines, 1):
line = query.decode(line)
line = decode(line)
if family == 'C':
ret = self.parse_c(line)
elif family == 'D':

8
lib.py
View file

@ -52,6 +52,14 @@ def unescape(bstr):
bstr = bstr.replace(a, b)
return bstr
def decode(byte_object):
# decode('ascii') fails on special chars
# FIXME: major hack until we handle everything as bytestrings
try:
return byte_object.decode('utf-8')
except UnicodeDecodeError:
return byte_object.decode('iso-8859-1')
# List of tokens which we don't want to consider as identifiers
# Typically for very frequent variable names and things redefined by #define
# TODO: allow to have per project blacklists

View file

@ -18,7 +18,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with Elixir. If not, see <http://www.gnu.org/licenses/>.
from lib import script, scriptLines
from lib import script, scriptLines, decode
import lib
import data
import os
@ -46,14 +46,6 @@ class SymbolInstance(object):
def __str__(self):
return self.__repr__()
def decode(byte_object):
# decode('ascii') fails on special chars
# FIXME: major hack until we handle everything as bytestrings
try:
return byte_object.decode('utf-8')
except UnicodeDecodeError:
return byte_object.decode('iso-8859-1')
def query(cmd, *args):
if cmd == 'versions':