Exemple #1
0
    def test_pml_to_json(self):
        test_dir = "pml_testfiles"
        for i in os.listdir(test_dir):
            if i.endswith(".pml"):
                path = os.path.join(test_dir, i)
                try:
                    print "Testing parser can load file: " + i
                    pml_to_json.parse(path)
                except:
                    self.fail("Parser broken on parsing file: " + i)

        output = pml_to_json.parse(os.path.join(test_dir, "test_parser.pml"))
        print "Testing that the parser output matches expected output"
        try:
            assert "cells" in output.keys()
            assert output["cells"][0]["type"] == "html.Element"
            assert output["cells"][0]["nameIn"] == "a1"
            assert output["cells"][0]["RequiresIn"][0] == {
                "attribute": "",
                "operator": "",
                "relOp": "",
                "resource": "require1",
                "value": ""
            }
            assert output["cells"][0]["ProvidesIn"][0] == {
                "resource": "provide1",
                "attribute": "attr",
                "operator": "==",
                "value": "val",
                "relOp": ""
            }
            assert output["cells"][0]["ProvidesIn"][1] == {
                "resource": "provide2",
                "attribute": "attr",
                "operator": "==",
                "value": "val2",
                "relOp": "&&"
            }

            #test that it has only one child
            assert len(output["cells"][1]["embeds"]) == 1
            #test that embeds embeds the right element id
            assert output["cells"][1]["embeds"][0] == output["cells"][2]["id"]
            #test that nested items have the right children
            assert len(output["cells"][3]["embeds"]) == 2
            assert output["cells"][3]["embeds"][0] == output["cells"][4]["id"]
            assert output["cells"][4]["attrs"]["name"] == "branch"

            #test that last action is correct
            assert output["cells"][-1]["nameIn"] == "a5"
            assert output["cells"][-1]["RequiresIn"][0]["resource"] == "require2"
            assert output["cells"][-1]["AgentsIn"][0]["resource"] == "carer"
        except:
            self.fail("Parser output does not match expected output")
Exemple #2
0
def load_graphical_file():
    filename = secure_filename(request.form["data"])
    checkIfUserDirectoryExists()

    tmp_filename = os.path.join('.' + app.config["UPLOAD_DIR"], current_user.get_id())
    tmp_filename = os.path.join(tmp_filename, filename)

    try:
        p = Popen(["peos/pml/check/pmlcheck", tmp_filename], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    except OSError as e:
        return jsonify(output="Error", reason = str(e))
    prog_out, error = p.communicate()
    if p.returncode > 0:
        return jsonify(output="Error", reason = error)
    
    #continue and parse      
    try:
        arr = pml_to_json.parse(tmp_filename)
        arr = pml_to_json.arr_to_json(arr["cells"])
        return jsonify(output = 'Success', source=arr)
    except Exception, e:
        # flash("Unable to Parse File", "danger")
        return jsonify(output = 'Error', reason = str(e) + ":" + "PML to JSON parser broke")