Merge branch 'add-cli'

This commit is contained in:
Bartłomiej Pluta
2019-09-09 15:28:56 +02:00
3 changed files with 48 additions and 2 deletions

0
smnp/cli/__init__.py Normal file
View File

23
smnp/cli/parser.py Normal file
View File

@@ -0,0 +1,23 @@
import argparse
VERSION = "0.1"
DESCRIPTION = """
Simple Music Notation Processor is a command line tool enabling you to do some music stuff using custom domain-specific language.
"""
class CliParser(object):
def __init__(self):
self.parser = argparse.ArgumentParser(description=DESCRIPTION)
self.parser.add_argument('file', nargs='*', help='a file containing SMNP code')
self.parser.add_argument('-c', '--code', action='append', default=[], type=str, help='a string with SMNP code')
self.parser.add_argument('-m', '--mic', action='store_true', help='test microphone level')
self.parser.add_argument('-C', '--config', type=argparse.FileType('-r'), help='a file containing settings (not implemented yet)')
self.parser.add_argument('-p', '--params', action='append', help='pass arguments to program (not implemented yet)')
self.parser.add_argument('-v', '--version', action='version')
self.parser.add_argument('--tokens', action='store_true', help='print tokens of parsed code')
self.parser.add_argument('--ast', action='store_true', help='print abstract syntax tree of parsed code')
self.parser.add_argument('--dry-run', action='store_true', help='don\'t execute passed code')
self.parser.version = VERSION
def parse(self):
return self.parser.parse_args()

View File

@@ -1,17 +1,40 @@
import sys import sys
import time
from smnp.error.base import SmnpException from smnp.error.base import SmnpException
from smnp.library.loader import loadStandardLibrary from smnp.library.loader import loadStandardLibrary
from smnp.program.interpreter import Interpreter from smnp.program.interpreter import Interpreter
from smnp.cli.parser import CliParser
from smnp.module.mic.lib.detector.noise import NoiseDetector
def interpretFile(args, file):
stdLibraryEnv = loadStandardLibrary() if not args.dry_run else None
Interpreter.interpretFile(file, printTokens=args.tokens, printAst=args.ast, execute=not args.dry_run, baseEnvironment=stdLibraryEnv)
def interpretString(args, string):
stdLibraryEnv = loadStandardLibrary() if not args.dry_run else None
Interpreter.interpretString(string, printTokens=args.tokens, printAst=args.ast, execute=not args.dry_run, baseEnvironment=stdLibraryEnv, source='<cli>')
def main(): def main():
try: try:
stdLibraryEnv = loadStandardLibrary() parser = CliParser()
Interpreter.interpretFile(sys.argv[1], printTokens=False, printAst=False, execute=True, baseEnvironment=stdLibraryEnv) args = parser.parse()
if args.mic:
nd = NoiseDetector()
nd.test()
for code in args.code:
interpretString(args, code)
for file in args.file:
interpretFile(args, file)
except SmnpException as e: except SmnpException as e:
print(e.message()) print(e.message())
except KeyboardInterrupt: except KeyboardInterrupt:
print("Program interrupted") print("Program interrupted")