diff --git a/vobject/__init__.py b/vobject/__init__.py index 516df75..d684511 100644 --- a/vobject/__init__.py +++ b/vobject/__init__.py @@ -80,12 +80,15 @@ from .base import newFromBehavior, readComponents, readOne +# Master package version number. __version__ = "0.9.8" def iCalendar(): + """Return an iCalendar 2.0 VCALENDAR component.""" return newFromBehavior("vcalendar", "2.0") def vCard(): + """Return a vCard 3.0 VCARD component.""" return newFromBehavior("vcard", "3.0") diff --git a/vobject/behavior.py b/vobject/behavior.py index 790656f..4cce669 100644 --- a/vobject/behavior.py +++ b/vobject/behavior.py @@ -107,8 +107,6 @@ def validate(cls, obj, raiseException=False, complainUnrecognized=False): @classmethod def lineValidate(cls, line, raiseException, complainUnrecognized): """Examine a line's parameters and values, return True if valid.""" - # todo: remove used param line, raiseException, complainUnrecognized - print(line, raiseException, complainUnrecognized) return True @classmethod diff --git a/vobject/icalendar.py b/vobject/icalendar.py index c8038d8..d432252 100644 --- a/vobject/icalendar.py +++ b/vobject/icalendar.py @@ -545,7 +545,6 @@ def getrruleset(self, addRDate=False): except IndexError: # it's conceivable that an rrule has 0 datetimes added = False - print(added) # todo: remove unused vars return rruleset @@ -1032,7 +1031,7 @@ def serialize(cls, obj, buf, lineLength, validate=True): transformed = obj # FIXME: never used undoTransform = False out = None - print(transformed, out) # todo: remove unused vars + outbuf = buf or six.StringIO() if obj.group is None: groupString = "" diff --git a/vobject/ics_diff.py b/vobject/ics_diff.py index f3e4baa..ca556a1 100644 --- a/vobject/ics_diff.py +++ b/vobject/ics_diff.py @@ -1,16 +1,18 @@ +""" +Compare VTODOs and VEVENTs in two iCalendar sources. +""" + from __future__ import print_function -from optparse import OptionParser +from optparse import OptionParser # pylint: disable=W4901 from vobject import __version__ -from vobject.base import Component, getBehavior, newFromBehavior, readOne - -""" -Compare VTODOs and VEVENTs in two iCalendar sources. -""" +from vobject.base import Component, newFromBehavior, readOne def getSortKey(component): + """Return a string sort-key for the component.""" + def getUID(component): return component.getChildValue("uid", "") @@ -25,13 +27,14 @@ def getRecurrenceID(component): recurrence_id = component.getChildValue("recurrence_id", None) if recurrence_id is None: return "0000-00-00" - else: - return recurrence_id.isoformat() + + return recurrence_id.isoformat() return getUID(component) + getSequence(component) + getRecurrenceID(component) def sortByUID(components): + """Sort the components by UID.""" return sorted(components, key=getSortKey) @@ -81,9 +84,9 @@ def processComponentLists(leftList, rightList): if rightIndex >= rightListSize: output.append((comp, None)) break - else: - rightComp = rightList[rightIndex] - rightKey = getSortKey(rightComp) + + rightComp = rightList[rightIndex] + rightKey = getSortKey(rightComp) if leftKey < rightKey: output.append((comp, None)) @@ -95,15 +98,6 @@ def processComponentLists(leftList, rightList): return output - def newComponent(name, body): - if body is None: - return None - else: - c = Component(name) - c.behavior = getBehavior(name) - c.isNative = True - return c - def processComponentPair(leftComp, rightComp): """ Return None if a match, or a pair of components including UIDs and @@ -135,34 +129,34 @@ def processComponentPair(leftComp, rightComp): if len(differentContentLines) == 0 and len(differentComponents) == 0: return None - else: - left = newFromBehavior(leftComp.name) - right = newFromBehavior(leftComp.name) - # add a UID, if one existed, despite the fact that they'll always be - # the same - uid = leftComp.getChildValue("uid") - if uid is not None: - left.add("uid").value = uid - right.add("uid").value = uid - - for name, childPairList in differentComponents.items(): - leftComponents, rightComponents = zip(*childPairList) - if len(leftComponents) > 0: - # filter out None - left.contents[name] = filter(None, leftComponents) - if len(rightComponents) > 0: - # filter out None - right.contents[name] = filter(None, rightComponents) - - for leftChildLine, rightChildLine in differentContentLines: - nonEmpty = leftChildLine or rightChildLine - name = nonEmpty[0].name - if leftChildLine is not None: - left.contents[name] = leftChildLine - if rightChildLine is not None: - right.contents[name] = rightChildLine - - return left, right + + left = newFromBehavior(leftComp.name) + right = newFromBehavior(leftComp.name) + # add a UID, if one existed, despite the fact that they'll always be + # the same + uid = leftComp.getChildValue("uid") + if uid is not None: + left.add("uid").value = uid + right.add("uid").value = uid + + for name, childPairList in differentComponents.items(): + leftComponents, rightComponents = zip(*childPairList) + if len(leftComponents) > 0: + # filter out None + left.contents[name] = filter(None, leftComponents) + if len(rightComponents) > 0: + # filter out None + right.contents[name] = filter(None, rightComponents) + + for leftChildLine, rightChildLine in differentContentLines: + nonEmpty = leftChildLine or rightChildLine + name = nonEmpty[0].name + if leftChildLine is not None: + left.contents[name] = leftChildLine + if rightChildLine is not None: + right.contents[name] = rightChildLine + + return left, right vevents = processComponentLists( sortByUID(getattr(left, "vevent_list", [])), sortByUID(getattr(right, "vevent_list", [])) @@ -176,6 +170,7 @@ def processComponentPair(leftComp, rightComp): def prettyDiff(leftObj, rightObj): + """Print prettily-formatted differences between left and right objects.""" for left, right in diff(leftObj, rightObj): print("<<<<<<<<<<<<<<<") if left is not None: @@ -184,28 +179,17 @@ def prettyDiff(leftObj, rightObj): if right is not None: right.prettyPrint() print(">>>>>>>>>>>>>>>") - print - - -def main(): - options, args = getOptions() - if args: - ignore_dtstamp = options.ignore - ics_file1, ics_file2 = args - with open(ics_file1) as f, open(ics_file2) as g: - cal1 = readOne(f) - cal2 = readOne(g) - deleteExtraneous(cal1, ignore_dtstamp=ignore_dtstamp) - deleteExtraneous(cal2, ignore_dtstamp=ignore_dtstamp) - prettyDiff(cal1, cal2) + print("") def getOptions(): - # Configuration options + """Parse command line options. + + @returns (options, remaining_args)""" usage = "usage: %prog [options] ics_file1 ics_file2" parser = OptionParser(usage=usage, version=__version__) - parser.set_description("ics_diff will print a comparison of two iCalendar files ") + parser.set_description("Print a comparison of two iCalendar files") parser.add_option( "-i", @@ -219,13 +203,28 @@ def getOptions(): (cmdline_options, args) = parser.parse_args() if len(args) < 2: print("error: too few arguments given") - print + print("") print(parser.format_help()) return False, False return cmdline_options, args +def main(): + """Main function.""" + + options, args = getOptions() + if args: + ignore_dtstamp = options.ignore + ics_file1, ics_file2 = args + with open(ics_file1) as f, open(ics_file2) as g: + cal1 = readOne(f) + cal2 = readOne(g) + deleteExtraneous(cal1, ignore_dtstamp=ignore_dtstamp) + deleteExtraneous(cal2, ignore_dtstamp=ignore_dtstamp) + prettyDiff(cal1, cal2) + + if __name__ == "__main__": try: main()