Exemple #1
0
 def testComment(self):
     cmd = "box([1,1,1]);" + os.linesep + " /* comment */ box([1,1,1]); " + os.linesep
     parser = Parser()
     parser.setup()
     parser.parse(cmd)
     result = parser.getStatementsOfType("comment")
     self.assertEqual(len(result), 1)
Exemple #2
0
 def testDisplayText(self):
     cmd = "%mime text/plain" + os.linesep + " %display box([1,1,1]); " + os.linesep
     parser = Parser()
     parser.setup()
     parser.parse(cmd)
     result = parser.renderMime()
     self.assertNotEqual(result, None)
Exemple #3
0
 def testLsMagic(self):
     cmd = os.linesep + " %lsmagic " + os.linesep
     parser = Parser()
     parser.parse(cmd)
     self.assertEqual(parser.getSourceCode().strip(), "")
     self.assertTrue(
         parser.getMessages().startswith("Available Commands: "))
Exemple #4
0
 def testEquals(self):
     p = Parser()
     str = "a=1;" + os.linesep + "b=2;" + os.linesep
     p.parse(str)
     stmts = p.getStatements()
     self.assertEqual(len(stmts), 2)
     self.assertEqual(stmts[0].sourceCode, "a=1;" + os.linesep)
     self.assertEqual(stmts[1].sourceCode, "b=2;" + os.linesep)
Exemple #5
0
 def testSourceCode(self):
     cmd = os.linesep + " box([1,1,1]); " + os.linesep
     parser = Parser()
     parser.parse(cmd)
     resultPermanent = "".join(
         [elem.sourceCode for elem in parser.getStatements()])
     self.assertEqual(self.strip(resultPermanent), "box([1,1,1]);")
     self.assertEqual(self.strip(parser.getSourceCode()), "box([1,1,1]);")
Exemple #6
0
 def testClear(self):
     cmd = os.linesep + "module test(){ box([1,1,1]);} " + os.linesep + "%display test();"
     parser = Parser()
     parser.parse(cmd)
     self.assertTrue(self.strip(parser.getSourceCode()))
     parser.parse("%clear")
     self.assertEqual(self.strip(parser.getSourceCode()), "")
     self.assertTrue("cleared" in parser.getMessages().strip())
Exemple #7
0
 def testClose(self):
     cmd = os.linesep + "module test(){ box([1,1,1]);}" + os.linesep + "%display test();"
     parser = Parser()
     parser.parse(cmd)
     self.assertEqual(self.strip(parser.getSourceCode()),
                      "module test(){ box([1,1,1]);} test();")
     parser.close()
     self.assertEqual(parser.getSourceCode().strip(), "")
     self.assertEqual(parser.getMessages().strip(), "")
Exemple #8
0
 def testModules(self):
     cmd = os.linesep + "module test(){ box([1,1,1]);} " + os.linesep
     parser = Parser()
     parser.parse(cmd)
     result = parser.getStatementsOfType("module")
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0].name, "test")
     self.assertEqual(self.strip(result[0].sourceCode),
                      "module test(){ box([1,1,1]);}")
Exemple #9
0
 def testConvert(self):
     cmd = os.linesep + " box([1,1,1]); " + os.linesep
     parser = Parser()
     parser.setup()
     parser.parse(cmd)
     mime = parser.renderMime()
     f = open(mime, "rb")
     content = f.read()
     print(content)
     f.close()
Exemple #10
0
 def testModulesUpdate(self):
     parser = Parser()
     cmd = os.linesep + "module test(){ box([1,1,1]);} " + os.linesep
     parser.parse(cmd)
     cmd = os.linesep + "module test(){ box([2,2,2]);} " + os.linesep
     parser.parse(cmd)
     result = parser.getStatementsOfType("module")
     self.assertEqual(len(result), 1)
     self.assertEqual(result[0].name, "test")
     self.assertEqual(self.strip(result[0].sourceCode),
                      "module test(){ box([2,2,2]);}")
     self.assertTrue(parser.getMessagesExt().startswith("Number of lines"))
Exemple #11
0
 def testMultipleComment(self):
     parser = Parser()
     cmd = "// test" + os.linesep
     parser.parse(cmd)
     self.assertEqual(len(parser.getStatementsOfType("comment")), 1)
     cmd = "// test" + os.linesep
     parser.parse(cmd)
     self.assertEqual(len(parser.getStatementsOfType("comment")), 1)
     cmd = "// test"
     parser.parse(cmd)
     self.assertEqual(len(parser.getStatementsOfType("comment")), 1)
Exemple #12
0
 def testMultipleStatement(self):
     parser = Parser()
     cmd = "test();" + os.linesep
     parser.parse(cmd)
     self.assertEqual(len(parser.getStatementsOfType("-")), 1)
     cmd = "test();" + os.linesep
     parser.parse(cmd)
     self.assertEqual(len(parser.getStatementsOfType("-")), 1)
     cmd = "test();"
     parser.parse(cmd)
     self.assertEqual(len(parser.getStatementsOfType("-")), 1)
class IOpenSCAD(Kernel):
    banner = "OpenSCAD"
    implementation = 'ipopenscad'
    implementation_version = '1.0'
    language = 'openscad'  # will be used for syntax highlighting
    language_version = '2019'
    language_info = {
        'name': 'OpenSCAD',
        'mimetype': 'application/x-openscad',
        'extension': '.scad'
    }
    complete = [
        "cube(size = [x,y,z], center = true);", "sphere(r = 10);",
        "sphere(d = 20);", "cylinder(h=15, r1=9.5, r2=19.5, center=true);",
        "square([10,10],center=true);", "circle(r=10);", "circle(d=20);",
        "translate([x,y,z])", "rotate([x,y,z])", "scale([x,y,z])",
        "resize([x,y,z])", "mirror([x,y,z])", "hull(){...};",
        "minkowski(){...};", "union() {...};", "difference(){...};",
        "intersection(){...};", "include <file>", "use <file>",
        "module name() { ... }", "function name() = ... ;"
    ]
    parser = None
    isSetup = False

    ##
    # Executes the source code which is defined in a cell
    ##
    def do_execute(self,
                   code,
                   silent,
                   store_history=True,
                   user_expressions=None,
                   allow_stdin=False):

        # Setup parser
        if not self.parser:
            self.parser = Parser()
            self.parser.setup()

        resultObj = None
        self.parser.parse(code)

        if not silent:
            # We send the standard output to the client.
            if self.parser.displayRendered:
                self.displayMessages(self.parser)
                self.parser.clearMessages()
                resultFile = self.parser.renderMime()
                if resultFile:
                    resultObj = open(resultFile, 'rb').read()
                    if self.parser.mime == 'text/plain':
                        resultObj = resultObj.decode('utf-8')
                    else:
                        resultObj = base64.standard_b64encode(
                            resultObj).decode('utf-8')

                if self.parser.getMessages().strip():
                    self.displayMessages(self.parser)
                if resultObj:
                    self.displayImage(resultObj, self.parser.mime)
                else:
                    if self.parser.getSourceCode().strip():
                        self.displayError(os.linesep +
                                          self.parser.getSourceCode())
            else:
                self.displayMessages(self.parser)

        # We return the exection results.
        return {
            'status': 'ok',
            'execution_count': self.execution_count,
            'payload': [],
            'user_expressions': {},
        }

    ##
    # Determine completion result
    ##
    def do_complete(self, code, cursor_pos):
        char = code[cursor_pos]
        result = self.complete + self.parser.getModuleNames(
        ) + self.parser.lsCommands
        result.sort()

        # filter the result if necessary
        if char.strip():
            subset = []
            for cmd in result:
                if cmd.startswith(char):
                    subset.append(cmd)
            result = subset

        content = {
            # The list of all matches to the completion request, such as
            # ['a.isalnum', 'a.isalpha'] for the above example.
            'matches': result,

            # The range of text that should be replaced by the above matches when a completion is accepted.
            # typically cursor_end is the same as cursor_pos in the request.
            'cursor_start': cursor_pos,
            'cursor_end': cursor_pos,

            # status should be 'ok' unless an exception was raised during the request,
            # in which case it should be 'error', along with the usual error message content
            # in other messages.
            'status': 'ok'
        }
        return content

    ##
    # Cleanup
    ##
    def do_shutdown(self, restart):
        self.parser.close()

    def displayInfo(self, info):
        stream_content = {'name': 'stdout', 'text': info}
        self.send_response(self.iopub_socket, 'stream', stream_content)

    def displayError(self, error):
        stream_content = {'name': 'stderr', 'text': error}
        self.send_response(self.iopub_socket, 'stream', stream_content)

    def displayMessages(self, parser):
        if self.parser.isError:
            self.displayError(parser.getMessages())
        else:
            self.displayInfo(parser.getMessagesExt())

    def displayImage(self, resultObj, mime):
        if resultObj:
            # We prepare the response with our rich
            # data (the plot).
            content = {
                'source': 'kernel',

                # This dictionary may contain
                # different MIME representations of
                # the output.
                'data': {
                    mime: resultObj
                },

                # We can specify the image size
                # in the metadata field.
                'metadata': {
                    mime: {
                        'width': 600,
                        'height': 400
                    }
                }
            }

            # We send the display_data message with
            # the contents.
            self.send_response(self.iopub_socket, 'display_data', content)
Exemple #14
0
 def testInclude(self):
     cmd = "%include https://raw.githubusercontent.com/pschatzmann/openscad-models/master/Pig.scad"
     parser = Parser()
     parser.parse(cmd)
     self.assertEqual(len(self.strip(parser.getSourceCode())), 2192)
     self.assertTrue("Included" in parser.getMessages().strip())
Exemple #15
0
 def testUse(self):
     cmd = "%use https://raw.githubusercontent.com/pschatzmann/openscad-models/master/Pig.scad"
     parser = Parser()
     parser.parse(cmd)
     self.assertTrue("Included" in parser.getMessages().strip())
Exemple #16
0
 def testDisplayCode(self):
     cmd = "cube([20,30,50]);" + os.linesep + "%displayCode " + os.linesep
     parser = Parser()
     parser.parse(cmd)
     self.assertTrue(parser.getMessages().startswith("cube([20,30,50])"))
Exemple #17
0
 def testMime(self):
     cmd = os.linesep + " %mime application/openscad " + os.linesep
     parser = Parser()
     parser.parse(cmd)
     self.assertEqual(parser.getSourceCode().strip(), "")
     self.assertEqual(parser.mime, "application/openscad")
Exemple #18
0
 def testCommand(self):
     cmd = os.linesep + " %command testCommand " + os.linesep
     parser = Parser()
     parser.parse(cmd)
     self.assertEqual(parser.getSourceCode().strip(), "")
     self.assertEqual(parser.scadCommand, "testCommand")