コード例 #1
0
ファイル: util_test.py プロジェクト: Annotald/annotald
 def test_updateVersionCookie(self):
     vc = "( (VERSION (BAR (BAZ quux)) (FOO bar)))"
     self.assertEqual(util.updateVersionCookie(vc, "FOO", "baz"),
                      "( (VERSION (BAR (BAZ quux)) (FOO baz)))")
     self.assertEqual(util.updateVersionCookie(vc, "BAR.BAZ", "baz"),
                      "( (VERSION (BAR (BAZ baz)) (FOO bar)))")
     self.assertEqual(util.updateVersionCookie(vc, "BAR", "baz"),
                      "( (VERSION (BAR baz) (FOO bar)))")
     self.assertEqual(util.updateVersionCookie(
         "( (VERSION (FOO baz) (BAR baz)))", "BAR.BAZ", "baz"),
                      "( (VERSION (BAR (BAZ baz)) (FOO baz)))")
コード例 #2
0
ファイル: treedrawing.py プロジェクト: Annotald/annotald
    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"))