command shell
This commit is contained in:
parent
00db142df7
commit
66e9da31f5
|
@ -0,0 +1,86 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""armcCtl.py: control your robot arm."""
|
||||
|
||||
__author__ = "Kevin Matz"
|
||||
__copyright__ = "Copyright 2019, Company 235, LLC"
|
||||
__credits__ = ["Kevin Matz"]
|
||||
|
||||
__license__ = "MIT"
|
||||
__version__ = "0.1"
|
||||
__maintainer__ = "Kevin Matz"
|
||||
__email__ = "kevin@company235.com"
|
||||
|
||||
__status__ = "Prototype"
|
||||
|
||||
import configparser
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from antlr4 import CommonTokenStream
|
||||
from antlr4 import InputStream
|
||||
from antlr4 import ParseTreeWalker
|
||||
from antlr4.error.ErrorListener import ErrorListener
|
||||
from ArmControlLexer import ArmControlLexer
|
||||
from ArmControlParser import ArmControlParser
|
||||
from ArmCtlListener import ArmCtlListener
|
||||
from robot import Robot
|
||||
|
||||
|
||||
# define an error listener that raises SyntaxError exceptions
|
||||
class SyntaxErrorListener(ErrorListener):
|
||||
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
|
||||
raise SyntaxError("line "+str(line)+":"+str(column)+" "+msg)
|
||||
|
||||
|
||||
# instantiate a robot
|
||||
r = Robot()
|
||||
|
||||
# setup logging
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
ch = logging.StreamHandler() # create console handler and
|
||||
ch.setLevel(logging.INFO) # set console log level to INFO
|
||||
logger.addHandler(ch) # add console log handler
|
||||
|
||||
# load configuration
|
||||
config = configparser.ConfigParser(allow_no_value=True)
|
||||
config.read('robot.cfg') # open config file
|
||||
r.config = config['robot'] # section of config file
|
||||
r.start() # open port, start read thread
|
||||
|
||||
script = ArmCtlListener(r)
|
||||
walker = ParseTreeWalker()
|
||||
|
||||
|
||||
def command(text):
|
||||
input_stream = InputStream(text)
|
||||
lexer = ArmControlLexer(input_stream)
|
||||
stream = CommonTokenStream(lexer)
|
||||
parser = ArmControlParser(stream)
|
||||
parser._listeners.append(SyntaxErrorListener())
|
||||
try:
|
||||
tree = parser.prog()
|
||||
walker.walk(script, tree)
|
||||
except SyntaxError as e:
|
||||
logger.debug(e) # antlr internal listener prints the error
|
||||
# # log it to the debug logger anyway
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) > 1:
|
||||
logger.debug("found macro at argv[1]")
|
||||
comment(sys.argv[1])
|
||||
else:
|
||||
import readline # for input history and line editing
|
||||
while True:
|
||||
try:
|
||||
text = input("command# ")
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
text = 'exit'
|
||||
print(text)
|
||||
if text == 'exit' or text == 'quit':
|
||||
break
|
||||
else:
|
||||
command(text)
|
Loading…
Reference in New Issue