Skip to content

Commit

Permalink
Add hilighter
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Jan 16, 2019
1 parent 8dfd46e commit 8ea2f97
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 33 deletions.
71 changes: 71 additions & 0 deletions pydbc/scripts/hilighter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__copyright__ = """
pySART - Simplified AUTOSAR-Toolkit for Python.
( C) 2010-2019 by Christoph Schueler <cpu12.gems.googlemail.com>
All Rights Reserved
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
s. FLOSS-EXCEPTION.txt
"""
__author__ = 'Christoph Schueler'
__version__ = '0.1.0'

import colorama


class NullHilighter:

def coloredText(self, color, msg):
return msg

def errorText(self, msg):
return msg

def successText(self, msg):
return msg

def progressText(self, msg):
return msg


class ColorHilighter:

def __init__(self):
colorama.init(convert = args.winout, strip = False)

def coloredText(self, color, msg):
return "{}{}".format(color, msg)

def errorText(self, msg):
return coloredText(colorama.Fore.RED, msg)

def successText(self, msg):
return coloredText(colorama.Fore.GREEN, msg)

def progressText(self, msg):
return coloredText(colorama.Fore.BLUE, msg)

def _resetColorStyle(self):
print(colorama.Style.RESET_ALL, end = "", flush = True)


def makeHilighter(param):
return NullHilighter()

54 changes: 21 additions & 33 deletions pydbc/scripts/vndb_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,66 +30,56 @@
import argparse

import io
import os
import pathlib
from pprint import pprint
import pkgutil
import sqlite3
import sys

import antlr4
import colorama

from pydbc import parser
from pydbc.dbcListener import DbcListener
from pydbc.ldfListener import LdfListener

from pydbc.db import CanDatabase
from pydbc.db.creator import Creator
from pydbc.db.load.dbc import DbcLoader
from pydbc.db.load import (DbcLoader, LdfLoader)
from pydbc.template import renderTemplateFromText

from pydbc.db.common import Queries
from .hilighter import makeHilighter


template = pkgutil.get_data("pydbc", "cgen/templates/dbc.tmpl")

def coloredText(color, msg):
return "{}{}".format(color, msg)

def errorText(msg):
return coloredText(colorama.Fore.RED, msg)

def successText(msg):
return coloredText(colorama.Fore.GREEN, msg)

def progressText(msg):
return coloredText(colorama.Fore.BLUE, msg)

def resetColorStyle():
print(colorama.Style.RESET_ALL, end = "", flush = True)
hl = makeHilighter(None)

def execute(fun, name, *args):
try:
fun(*args)
except Exception as e:
msg = errorText(" Exiting import function due to exception while {}".format(name))
msg = hl.errorText(" Exiting import function due to exception while {}".format(name))
if not isinstance(e, sqlite3.DatabaseError):
msg += ": {}".format(str(e))
print("{}\n".format(msg), flush = True)
print(str(e))
resetColorStyle()
#sys.exit(1)
return False
else:
return True

def importFile(name):
def importFile(pth):
global ucout

pth, fname = os.path.split(name)
fnbase, fnext = os.path.splitext(fname)

fname = pth.parts[-1]
fnbase = pth.stem
fnext = pth.suffix
print("PTH-ABS:", pth.parent, fname, )

db = CanDatabase(r"{}.vndb".format(fnbase))

print(progressText("Processing file '{}'...").format(name), flush = True)
resetColorStyle()
print(hl.progressText("Processing file '{}'...").format(fname), flush = True)

cr = Creator(db)
if not execute(cr.dropTables, "dropping tables"):
Expand All @@ -100,10 +90,9 @@ def importFile(name):
pa = parser.ParserWrapper('dbc', 'dbcfile', DbcListener)

try:
tree = pa.parseFromFile("{}".format(name), encoding = "utf-8" if ucout else "latin-1", trace = False)
tree = pa.parseFromFile("{}".format(pth.absolute()), encoding = "utf-8" if ucout else "latin-1", trace = False)
except Exception as e:
print(errorText(" Exiting import function due to exception while parsing: {}\n".format(str(e))), flush = True)
resetColorStyle()
print(hl.errorText(" Exiting import function due to exception while parsing: {}\n".format(str(e))), flush = True)
return

#print("Finished ANTLR parsing.", flush = True)
Expand All @@ -127,8 +116,7 @@ def importFile(name):
with io.open("{}.render".format(fnbase), "w", encoding = "utf-8" if ucout else "latin-1", newline = "\r\n") as outf:
outf.write(res)

print(successText("OK, done.\n"), flush = True)
resetColorStyle()
print(hl.successText("OK, done.\n"), flush = True)
#print("-" * 80, flush = True)

ucout = False
Expand All @@ -146,10 +134,10 @@ def main():
parser.add_argument("-w", help = "Format output for Windows console.", dest = "winout", action = "store_true")
parser.add_argument("-u", help = "Generate UTF-8 encoded output (otherwise Latin-1).", dest = "ucout", action = "store_true")
args = parser.parse_args()
colorama.init(convert = args.winout, strip = False)
ucout = args.ucout
for name in args.dbcfile:
importFile(name)
for arg in args.dbcfile:
for pth in pathlib.Path().glob(arg):
importFile(pth)

if __name__ == '__main__':
main()
Expand Down

0 comments on commit 8ea2f97

Please sign in to comment.