Beispiel #1
0
    def test_queryVersionCookie(self):
        version_string = "( (VERSION (FORMAT dash) (FOO (BAR baz))))"
        self.assertEqual(
            util.queryVersionCookie(version_string,
                                    "FORMAT"),
            "dash")
        self.assertEqual(
            util.queryVersionCookie(version_string,
                                    "FOO.BAR"),
            "baz")
        self.assertEqual(
            util.queryVersionCookie(version_string,
                                    "FOO"),
            T.Tree("BAR", ["baz"]))

        self.assertIsNone(util.queryVersionCookie(version_string,
                                                  "ABC"))

        # Invalid version cookie gives null result
        invalid_version_tree = "( (FOO bar))"
        self.assertIsNone(util.queryVersionCookie(invalid_version_tree,
                                                  "foo"))

        # multiple matches gives null result, only for aberrant key
        multiple_matches = "( (VERSION (FOO bar) (FOO baz) (BAR quux)))"
        self.assertIsNone(util.queryVersionCookie(multiple_matches,
                                                  "FOO"))
        self.assertEqual(util.queryVersionCookie(multiple_matches,
                                                 "BAR"),
                         "quux")

        # Empty input gives null result
        self.assertIsNone(util.queryVersionCookie("", "FOO"))
        self.assertIsNone(util.queryVersionCookie(None, "FOO"))
Beispiel #2
0
    def doSave(self, trees=None, startTime=None, force=None, update_md5=None):
        # Save failure reason codes
        NON_MATCHING_ANNOTALDS = 1
        NON_MATCHING_HASHES = 2

        cherrypy.response.headers["Content-Type"] = "application/json"
        if (startTime != self.startTime) and not (force == "true"):
            return json.dumps(
                dict(
                    result="failure",
                    reason="non-matching invocations of Annotald",  # noqa
                    reasonCode=NON_MATCHING_ANNOTALDS,
                    startTime=self.startTime,
                )
            )
        tosave = self.integrateTrees(trees)
        tosave = tosave.replace("-FLAG", "")
        print("self.thefile is: %s" % self.thefile)
        if update_md5:
            self.versionCookie = util.updateVersionCookie(
                self.versionCookie, "HASH.MD5", util.hashTrees(trees, self.versionCookie)
            )
        if util.queryVersionCookie(self.versionCookie, "HASH.MD5"):
            print("checking hash")
            # TODO: document hash function in user manual
            old_hash = util.queryVersionCookie(self.versionCookie, "HASH.MD5")
            new_hash = util.hashTrees(tosave, self.versionCookie)
            if old_hash != new_hash:
                return json.dumps(
                    dict(
                        result="failure",
                        reason=("corpus text has changed" + " (it shouldn't!)"),
                        reasonCode=NON_MATCHING_HASHES,
                        startTime=self.startTime,
                    )
                )
        tosave = tosave.replace("-FLAG", "")
        try:
            util.writeTreesToFile(self.versionCookie, tosave, self.thefile)
            self.doLogEvent(json.dumps({"type": "save"}))
            return json.dumps(dict(result="success"))
        except Exception as e:
            print("something went wrong: %s" % e)
            traceback.print_exc()
            return json.dumps(dict(result="failure", reason="server got an exception"))
Beispiel #3
0
    def treesToHtml(self, trees):
        version = util.queryVersionCookie(self.versionCookie, "FORMAT")
        alltrees = '<div class="snode" id="sn0">'
        for tree in trees:
            tree = tree.strip()
            tree = tree.replace("<", "&lt;")
            tree = tree.replace(">", "&gt;")
            if not tree == "":
                nltk_tree = T.Tree(tree)
                alltrees = alltrees + self.conversionFn(nltk_tree, version)

        alltrees = alltrees + "</div>"
        return alltrees
Beispiel #4
0
    def __init__(self, args, shortfile):
        self.thefile = args.psd[0]
        self.shortfile = shortfile
        self.options = args
        self.readVersionCookie(self.thefile)

        # TODO: after a respawn these will not be right
        self.inidle = False
        self.justexited = False
        self.startTime = str(int(time.time()))
        self.eventLog = None  # Will be initialized when needed

        if util.queryVersionCookie(self.versionCookie, "FORMAT") == "deep":
            self.conversionFn = util.deepTreeToHtml
            self.useMetadata = True
        else:
            self.conversionFn = util.treeToHtml
            self.useMetadata = False
        self.showingPartialFile = self.options.oneTree or self.options.numTrees > 1
        self.pythonOptions = {
            "extraJavascripts": [],
            "debugJs": False,
            "validators": {},
            "colorCSS": False,
            # TODO: this masks a bug in jana's branch
            "colorCSSPath": "/dev/null",
            "corpusSearchValidate": util.corpusSearchValidate,
            "rewriteIndices": True,
            "serverMode": True,
        }
        if args.pythonSettings is not None:
            if (
                sys.version_info[0] == 2
                and sys.version_info[1] < 7
                or sys.version_info[0] == 3
                and sys.version_info[1] < 2
            ):
                print("Specifying python settings requires Python v." + ">2.7 or >3.2.")
                sys.exit(1)
            else:
                self.pythonOptions = runpy.run_path(args.pythonSettings, init_globals=self.pythonOptions)
        cherrypy.engine.autoreload.files.add(args.pythonSettings)

        self.doLogEvent(json.dumps({"type": "program-start", "filename": self.thefile}))