class Generator: def __init__(self): self.detector = RequirementsDetector(subprocess) self.console = Console(os, "Grammar Generator") self.ToolPath = "ANTLR/antlr-3.1.3.jar" self.OutputPath = "Output" self.OutputFileName = "FiltersLanguageGrammar" self.Output = { "G" : (".g", BufferedFileWriter(self.OutputPath)), "STG" : (".stg", BufferedFileWriter(self.OutputPath)) } self.modules = {} self.parts = {} self.base = {} self.targetsAndRepositories = [ (self.parts, PartsRepository()), (self.base, BaseRepository()), (self.modules, InputRepository()) ] self.ModulesObjects = {} def verifyRequirements(self): if not self.detector.isJavaRuntimeEnvironmentPresent(): raise EnvironmentError("Missing JRE in system - please install at least version 1.6.") def fillDictionaries(self): reader = FileReader() for target, repository in self.targetsAndRepositories: for fileFromRepository in repository.loadAll(): name = self.onlyFileName(fileFromRepository) target[name] = reader.getContent(fileFromRepository) def onlyFileName(self, path): return os.path.splitext(os.path.basename(path))[0] def parseAndValidateModules(self): for name, content in self.modules.iteritems(): stringifiedSettings = JsonContent(content) if stringifiedSettings.validate(): self.ModulesObjects[name] = stringifiedSettings.parse() else: raise AttributeError("Module named '%s' has invalid syntax - JSON validation error." % (name)) def buildG(self): GrammarFileBuilder( self.Output["G"][1], self.ModulesObjects, self.parts, self.base["G"], self.console ).generate() def buildStg(self): StringTemplateGroupFileBuilder( self.Output["STG"][1], self.ModulesObjects, self.parts, self.base["STG"], self.console ).generate() def saveAllFiles(self): for extension, writer in self.Output.values(): writer.saveWithFileName(self.OutputFileName + extension) def getInputFileName(self): extension = self.Output["G"][0] return self.OutputFileName + extension def getToolParameters(self): inputPath = os.path.join(self.OutputPath, self.getInputFileName()) return [ '-cp', self.ToolPath, 'org.antlr.Tool', '-verbose', '-report', '-fo', self.OutputPath, inputPath ] def getToolErrorMessage(self, processResponse): EOL = '\n' parameters = (EOL * 2, processResponse["stderr"]) return "ANTLR exception has ocurred. Details:%s%s" % parameters def invokeTool(self): invoker = Invoker(subprocess) result = invoker.executeCommandWithParams('java', *self.getToolParameters()) if result["ReturnCode"] != 0: raise EnvironmentError(self.getToolErrorMessage(result)) def run(self): try: self.console.writeStartHeader() self.console.writeStep("Verifying environment") self.verifyRequirements() self.console.writeStep("Reading files") self.fillDictionaries() self.console.writeStep("Validating") self.parseAndValidateModules() self.console.writeStep("Building G file") self.buildG() self.console.writeStep("Building STG file") self.buildStg() self.console.writeStep("Saving output files") self.saveAllFiles() self.console.writeStep("Invoking ANTLR") self.invokeTool() self.console.writeStep("Finished!") self.console.writeEndHeader() except AttributeError as (errorMessage): self.console.writeError("ATTRIBUTE ERROR: %s" % (errorMessage)) except EnvironmentError as (errorMessage): self.console.writeError("ENVIRONMENT ERROR: %s" % (errorMessage)) except IOError as (errorNumber, errorMessage): self.console.writeError("I/O ERROR [%d]: %s" % (errorNumber, errorMessage))
class TestClass(unittest.TestCase): def setUp(self): self.outputter = Console(MockedConsole(), "Name") def test_existance_output(self): assert self.outputter.output != None, "Output is None - missing STDOUT." def test_simple_print_message(self): message = "Simple information" self.outputter.printMessage(message) assert self.outputter.output.getLastPrintedMessage() == message, "You cannot print messages." def test_print_line(self): message = "Simple" self.outputter.printLine(message) assert self.outputter.output.getLastPrintedMessage() == "Simple\n", "You cannot print whole line." def test_print_separator(self): self.outputter.writeSeparatorLine() value = self.outputter.separator * self.outputter.separatorLength + "\n" assert self.outputter.output.getLastPrintedMessage() == value, "You can't print separator line." def test_which_checks_printing_headers(self): self.outputter.writeStartHeader() assert self.outputter.output.getLastPrintedMessage().find("Name") != -1, "You can't print start header." self.outputter.writeEndHeader() value = self.outputter.separator * self.outputter.separatorLength + "\n" assert self.outputter.output.getLastPrintedMessage() == value, "You can't print ending header." def test_for_checking_steps(self): self.outputter.writeStep("1") assert self.outputter.output.getLastPrintedMessage() == "%s[ 1]\t1\n" % (self.outputter.defaultTabs) self.outputter.writeStep("2") assert self.outputter.output.getLastPrintedMessage() == "%s[ 2]\t2\n" % (self.outputter.defaultTabs) def test_for_writing_errors(self): self.outputter.writeError("ERROR") assert self.outputter.output.getLastPrintedMessage() == "%sERROR\n" % (self.outputter.defaultTabs) def test_for_increasing_indent(self): self.outputter.writeWithIndent("TEST") assert self.outputter.output.getLastPrintedMessage() == "\t\tTEST\n" self.outputter.increaseIndent() self.outputter.writeWithIndent("TEST") assert self.outputter.output.getLastPrintedMessage() == "\t\t\tTEST\n" def test_for_decreasing_indent(self): self.outputter.writeWithIndent("TEST") assert self.outputter.output.getLastPrintedMessage() == "\t\tTEST\n" self.outputter.decreaseIndent() self.outputter.writeWithIndent("TEST") assert self.outputter.output.getLastPrintedMessage() == "\tTEST\n" def test_for_decreasing_indent_below_zero(self): self.outputter.decreaseIndent() self.outputter.decreaseIndent() self.outputter.decreaseIndent() self.outputter.decreaseIndent() self.outputter.decreaseIndent() self.outputter.writeWithIndent("TEST") assert self.outputter.output.getLastPrintedMessage() == "TEST\n" assert self.outputter.tabsLength == 0