Identifier matching rules

This commit is contained in:
Mikaël Bouillot 2017-02-05 11:14:48 +01:00
parent cf0e1bd666
commit 8ad62ba1b6
2 changed files with 32 additions and 5 deletions

28
lib.py
View file

@ -1,12 +1,15 @@
#!/usr/bin/python3
from subprocess import run, PIPE
import subprocess
import bsddb3
import re
def echo (bstr):
print (bstr.decode(), end='')
def script (*args):
p = run (('./script.sh',) + args, stdout=PIPE)
args = ('./script.sh',) + args
p = subprocess.run (args, stdout=subprocess.PIPE)
p = p.stdout
return p
@ -28,3 +31,24 @@ def unescape (bstr):
b = b.encode()
bstr = bstr.replace (a, b)
return bstr
def isIdent (bstr):
if re.search (b'_', bstr):
return True
elif re.search (b'^[A-Z0-9]*$', bstr):
return True
else:
return False
class Table:
def __init__ (self, filename):
self.db = bsddb3.db.DB()
# FIXME: hardcoded path
self.db.open ('databases/' + filename, flags=bsddb3.db.DB_RDONLY)
def exists (self, key):
return self.db.exists (key)
class DB:
def __init__ (self):
self.defs = Table ('definitions.db')

View file

@ -1,7 +1,10 @@
#!/usr/bin/python3
from sys import argv
from lib import echo, script, scriptLines, unescape
from lib import echo, script, scriptLines
import lib
db = lib.DB()
cmd = argv[1]
@ -26,10 +29,10 @@ elif cmd == 'file':
toBe = True
for tok in tokens:
toBe = not toBe
if toBe:
if toBe and db.defs.exists (tok) and lib.isIdent (tok):
tok = b'\033[31m' + tok + b'\033[0m'
else:
tok = unescape (tok)
tok = lib.unescape (tok)
echo (tok)
else:
p = script ('get-file', version, path)