From 0e21a2bd209ccc3316896abb822ff8ad9294bd63 Mon Sep 17 00:00:00 2001 From: ccremers Date: Sat, 27 Jan 2007 12:53:19 +0000 Subject: [PATCH] - Added scripting support for claim list retrieval and single claim evaluation. --- gui/Scyther/Error.py | 12 +++++++++ gui/Scyther/Scyther.py | 56 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/gui/Scyther/Error.py b/gui/Scyther/Error.py index 4f4910a..1ab5718 100644 --- a/gui/Scyther/Error.py +++ b/gui/Scyther/Error.py @@ -59,5 +59,17 @@ class UnknownPlatformError(Error): def __str__(self): return "The %s platform is currently unsupported." % self.platform +class StringListError(Error): + """Raised when the a string should be a list of strings or a string + + Attributes: + obj -- object that did not fit + """ + + def __init__(self, obj): + self.obj = obj + + def __str__(self): + return "Got %s instead of a (list of) string." % self.obj # vim: set ts=4 sw=4 et list lcs=tab\:>-: diff --git a/gui/Scyther/Scyther.py b/gui/Scyther/Scyther.py index 030f87e..5d1007d 100755 --- a/gui/Scyther/Scyther.py +++ b/gui/Scyther/Scyther.py @@ -79,6 +79,27 @@ def CheckSanity(program): #--------------------------------------------------------------------------- +def EnsureString(x,sep=" "): + """ + Takes a thing that is either a list or a string. + Turns it into a string. If it was a list, is inserted, and the + process iterats. + """ + if type(x) is str: + return x + + elif type(x) is list: + newlist = [] + for el in x: + newlist.append(EnsureString(el,sep)) + return sep.join(newlist) + + else: + raise Error.StringListError, x + + +#--------------------------------------------------------------------------- + def getScytherBackend(): # Where is my executable? # @@ -225,14 +246,24 @@ class Scyther(object): return (output,errors) - def verify(self): + def sanitize(self): + """ Sanitize some of the input """ + self.options = EnsureString(self.options) + + def verify(self,extraoptions=None): """ Should return a list of results """ + + # Cleanup first + self.sanitize() # prepare arguments args = "" if self.xml: args += " --dot-output --xml-output --plain" args += " %s" % self.options + if extraoptions: + # extraoptions might need sanitizing + args += " %s" % EnsureString(extraoptions) # execute (output,errors) = self.doScytherCommand(self.spdl, args) @@ -278,6 +309,29 @@ class Scyther(object): else: return self.output + def verifyOne(self,claimid): + """ + Verify just a single claim with an ID retrieved from the + procedure below, 'scanClaims' + """ + return self.verify("--filter=%s" % claimid) + + def scanClaims(self): + """ + Retrieve the list of claims in a format that can be passed to + --filter=X or 'verifyOne' later. + A result of 'None' means that some errors occurred. + """ + self.verify("--scan-claims") + if self.errorcount > 0: + return None + else: + self.validxml = False # Signal that we should not interpret the output as XML + l = [] + for claim in self.claims: + l.append(claim.id) + return l + def getClaim(self,claimid): if self.claims: for cl in self.claims: