+ return str(self.asLinesList())
+
+ def __repr__(self):
+ s = "<Snippet: %s>" % self.name
+ return s
+
+ def __cmp__(self, other):
+ ret = cmp(self.name, other.name)
+ if ret != 0: return ret
+ ret = cmp(self.legal, other.legal)
+ if ret != 0: return ret
+ ret = cmp(self.doc, other.doc)
+ if ret != 0: return ret
+ ret = cmp(self.code, other.code)
+ return ret
+
+ def __getitem__(self, key):
+ return {
+ 'legal': self.legal,
+ 'doc' : self.doc,
+ 'code' : self.code
+ }[key]
+
+ def getDiff(self, other):
+ return self.asLinesList().getDiff(other.asLinesList())
+
+class Snippets:
+ """Collection of snippets, where snippets can be accessed
+ individually by name (like dictionary) or sequentionaly in the
+ order they were added."""
+ def __init__(self):
+ self._snippets = dict()
+ self._order = list()
+
+ def __iadd__(self, snippet):
+ assert isinstance(snippet, Snippet)
+ self._snippets[snippet.name] = snippet
+ self._order += [snippet]
+ return self
+
+ def __getitem__(self, key):
+ return self._snippets[key]
+
+ def __contains__(self, item):
+ return item in self._snippets
+
+ def __iter__(self):
+ return iter(self._order)
+
+ def __cmp__(self, other):
+ return cmp(self._snippets, other._snippets)
+
+ def loadFromFiles(self, fnames):
+ """Reads the snippets from several files and adds them to itself."""
+ for fn in fnames:
+ self += Snippet(fn)
+
+ def loadFromDict(self, snipDict):
+ """Adds snippets to itself from dictionary of LineLists."""
+ for s in snipDict:
+ snip = Snippet()
+ snip.name = s
+ snip.readLines(snipDict[s])
+ self += snip
+
+ def getDiff(self, other):
+ assert isinstance(other, Snippets)
+ s = ''
+ for snip in self:
+ if (snip.name[0:2] == '__'): continue
+ if (snip != other[snip.name]):
+ s += "Snippet %s:\n" % snip.name
+ s += snip.getDiff(other[snip.name])