Commit a753d2c6 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Merge branch 'master' of https://github.com/dbrabera/opengeode

Conflicts:
	opengeode.py
parents 75d7a427 b400aeeb
...@@ -1730,37 +1730,8 @@ class OG_MainWindow(QtGui.QMainWindow, object): ...@@ -1730,37 +1730,8 @@ class OG_MainWindow(QtGui.QMainWindow, object):
super(OG_MainWindow, self).closeEvent(event) super(OG_MainWindow, self).closeEvent(event)
def opengeode(): def parse_args():
''' Tool entry point ''' ''' Parse command line arguments '''
# Catch Ctrl-C to stop the app from the console
signal.signal(signal.SIGINT, signal.SIG_DFL)
# Initialize logging
terminal_formatter = logging.Formatter(
fmt="[%(levelname)s] %(message)s")
handler_console = logging.StreamHandler()
handler_console.setFormatter(terminal_formatter)
LOG.addHandler(handler_console)
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(sys.argv)
app.setApplicationName('OpenGEODE')
app.setWindowIcon(QtGui.QIcon(':icons/input.png'))
# Set all encodings to utf-8 in Qt
QtCore.QTextCodec.setCodecForCStrings(
QtCore.QTextCodec.codecForName('UTF-8'))
# Bypass system-default font, to harmonize size on all platforms
font_database = QtGui.QFontDatabase()
font_database.addApplicationFont(':fonts/Ubuntu-RI.ttf')
font_database.addApplicationFont(':fonts/Ubuntu-R.ttf')
font_database.addApplicationFont(':fonts/Ubuntu-B.ttf')
font_database.addApplicationFont(':fonts/Ubuntu-BI.ttf')
app.setFont(QtGui.QFont('Ubuntu', 10))
# Parse the command line
parser = argparse.ArgumentParser(version=__version__) parser = argparse.ArgumentParser(version=__version__)
parser.add_argument('-g', '--debug', action='store_true', default=False, parser.add_argument('-g', '--debug', action='store_true', default=False,
help='Display debug information') help='Display debug information')
...@@ -1779,101 +1750,195 @@ def opengeode(): ...@@ -1779,101 +1750,195 @@ def opengeode():
parser.add_argument('--split', dest='split', action='store_true', parser.add_argument('--split', dest='split', action='store_true',
help='Save pictures in multiple files (one per floating item)') help='Save pictures in multiple files (one per floating item)')
parser.add_argument('files', metavar='file.pr', type=str, nargs='*', parser.add_argument('files', metavar='file.pr', type=str, nargs='*',
help='SDL file(s)') help='SDL file(s)')
options = parser.parse_args() return parser.parse_args()
ret = 0
def init_logging(options):
''' Initialize logging '''
terminal_formatter = logging.Formatter(fmt="[%(levelname)s] %(message)s")
handler_console = logging.StreamHandler()
handler_console.setFormatter(terminal_formatter)
LOG.addHandler(handler_console)
level = logging.DEBUG if options.debug else logging.INFO level = logging.DEBUG if options.debug else logging.INFO
# Set log level for all libraries # Set log level for all libraries
LOG.setLevel(level) LOG.setLevel(level)
try: try:
for module in (sdlSymbols, genericSymbols, ogAST, ogParser, Lander, modules = (
AdaGenerator, undoCommands, Renderer, Clipboard, Statechart, sdlSymbols,
Helper, LlvmGenerator, Asn1scc, Connectors, Pr): genericSymbols,
ogAST,
ogParser,
Lander,
AdaGenerator,
undoCommands,
Renderer,
Clipboard,
Statechart,
Helper,
LlvmGenerator,
Asn1scc,
Connectors,
Pr
)
for module in modules:
module.LOG.addHandler(handler_console) module.LOG.addHandler(handler_console)
module.LOG.setLevel(level) module.LOG.setLevel(level)
except NameError: except NameError:
# Some modules may not be loaded (like llvm on Windows) # Some modules may not be loaded (like llvm on Windows)
pass; pass;
# Initialize the clipboard
Clipboard.CLIPBOARD = SDL_Scene(context='clipboard')
LOG.debug('Starting OpenGEODE version ' + __version__)
if(options.check or options.toAda or options.png or def parse(files):
options.pdf or options.svg or options.llvm): ''' Parse files '''
LOG.info('Checking ' + str(options.files)) LOG.info('Checking ' + str(files))
ast, warnings, errors = ogParser.parse_pr(files=files)
LOG.info(
'Parsing complete. Summary, found %d warnings and %d errors' % (len(warnings), len(errors))
)
for warning in warnings:
LOG.warning(warning[0])
for error in errors:
LOG.error(error[0])
return ast, warnings, errors
def generate(process, options):
''' Generate code '''
if options.toAda:
LOG.info('Generating Ada code')
try: try:
ast, warnings, errors = ogParser.parse_pr(files=options.files) AdaGenerator.generate(process)
except IOError: except (TypeError, ValueError, NameError) as err:
LOG.error('Aborting due to parsing error (check input file)') LOG.error(str(err))
return -1 LOG.debug(str(traceback.format_exc()))
LOG.error('Ada code generation failed')
if options.llvm:
LOG.info('Generating LLVM code')
try: try:
process, = ast.processes LlvmGenerator.generate(process)
except ValueError: except (TypeError, ValueError, NameError) as err:
LOG.error('Only one process at a time is supported') LOG.error(str(err))
return -1 LOG.debug(str(traceback.format_exc()))
LOG.info('Parsing complete. Summary, found ' + LOG.error('LLVM IR generation failed')
str(len(warnings)) +
' warnings and ' +
str(len(errors)) + def export(process, options):
' errors') ''' Export process '''
for warning in warnings: # Qt must be initialized before using SDL_Scene
LOG.warning(warning[0]) init_qt()
for error in errors:
LOG.error(error[0]) export_fmt = []
if errors: if options.png:
ret = -1 export_fmt.append('png')
if options.toAda or options.llvm: if options.pdf:
LOG.error('Too many errors, cannot generate code') export_fmt.append('pdf')
if options.toAda and not errors: if options.svg:
LOG.info('Generating Ada code') export_fmt.append('svg')
try: if not export_fmt:
AdaGenerator.generate(process) return
except (TypeError, ValueError, NameError) as err:
LOG.error(str(err)) name = process.processName
LOG.debug(str(traceback.format_exc())) scene = SDL_Scene(context='process')
LOG.error('Code generation failed') scene.render_everything(process)
if options.llvm and not errors: # Update connections, placements:
LOG.info('Generating LLVM code') scene.refresh()
try:
LlvmGenerator.generate(process) for doc_fmt in export_fmt:
except (TypeError, ValueError, NameError) as err: LOG.info('Saving {ext} file: {name}.{ext}'.format(ext=doc_fmt, name=name))
LOG.error(str(err)) scene.export_img(name, doc_format=doc_fmt, split=options.split)
LOG.debug(str(traceback.format_exc()))
LOG.error('LLVM Code generation failed')
export_fmt = [] def cli(options):
if options.png: ''' Run CLI App '''
export_fmt.append('png') try:
if options.pdf: ast, warnings, errors = parse(options.files)
export_fmt.append('pdf') except IOError:
if options.svg: LOG.error('Aborting due to parsing error (check input file)')
export_fmt.append('svg') return 1
if export_fmt:
scene = SDL_Scene(context='process') if len(ast.processes) != 1:
scene.render_everything(process) LOG.error('Only one process at a time is supported')
# Update connections, placements: return 1
scene.refresh()
for doc_fmt in export_fmt: if options.png or options.pdf or options.svg:
LOG.info('Saving {ext} file: {name}.{ext}'.format( export(ast.processes[0], options)
ext=doc_fmt, name=process.processName))
scene.export_img(process.processName, if options.toAda or options.llvm:
doc_format=doc_fmt, if not errors:
split=options.split) generate(ast.processes[0], options)
else:
LOG.error('Too many errors, cannot generate code')
return 0 if not errors else 1
def init_qt():
''' Initialize QT '''
app = QtGui.QApplication.instance()
if app is None:
app = QtGui.QApplication(sys.argv)
return app
def gui(options):
''' Run GUI App '''
LOG.debug('Running the GUI')
LOG.info('Model backup enabled - auto-saving every 2 minutes')
app = init_qt()
app.setApplicationName('OpenGEODE')
app.setWindowIcon(QtGui.QIcon(':icons/input.png'))
# Set all encodings to utf-8 in Qt
QtCore.QTextCodec.setCodecForCStrings(
QtCore.QTextCodec.codecForName('UTF-8')
)
# Bypass system-default font, to harmonize size on all platforms
font_database = QtGui.QFontDatabase()
font_database.addApplicationFont(':fonts/Ubuntu-RI.ttf')
font_database.addApplicationFont(':fonts/Ubuntu-R.ttf')
font_database.addApplicationFont(':fonts/Ubuntu-B.ttf')
font_database.addApplicationFont(':fonts/Ubuntu-BI.ttf')
app.setFont(QtGui.QFont('Ubuntu', 10))
# Initialize the clipboard
Clipboard.CLIPBOARD = SDL_Scene(context='clipboard')
# Load the application layout from the .ui file
loader = QUiLoader()
loader.registerCustomWidget(OG_MainWindow)
loader.registerCustomWidget(SDL_View)
ui_file = QFile(':/opengeode.ui')
ui_file.open(QFile.ReadOnly)
my_widget = loader.load(ui_file)
ui_file.close()
my_widget.start(options.files)
return app.exec_()
def opengeode():
''' Tool entry point '''
# Catch Ctrl-C to stop the app from the console
signal.signal(signal.SIGINT, signal.SIG_DFL)
options = parse_args()
init_logging(options)
LOG.debug('Starting OpenGEODE version ' + __version__)
if any((options.check, options.toAda, options.png, options.pdf, options.svg, options.llvm)):
return cli(options)
else: else:
LOG.debug('Running the GUI') return gui(options)
LOG.info('Model backup enabled - auto-saving every 2 minutes')
# Load the application layout from the .ui file
loader = QUiLoader()
loader.registerCustomWidget(OG_MainWindow)
loader.registerCustomWidget(SDL_View)
ui_file = QFile(':/opengeode.ui')
ui_file.open(QFile.ReadOnly)
my_widget = loader.load(ui_file)
ui_file.close()
my_widget.start(options.files)
ret = app.exec_()
return ret
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(opengeode()) sys.exit(opengeode())
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment