def setUp(self): super(TestTimezoneDB, self).setUp() # Standard components explicitly added for vtz in StandardTZs: cal = Calendar() TimezoneDatabase.getTimezoneDatabase()._addStandardTimezone(cal.parseComponent(StringIO(vtz))) # Just parsing will add as non-standard for vtz in NonStandardTZs: Calendar.parseData(vtz)
def execute(self, cmdname, options): longlist = False path = None rtype = False displayname = False ctag = False etag = False supported_components = False synctoken = False details = False opts, args = getopt.getopt(shlex.split(options), 'acdDeilrs') for name, _ignore_value in opts: if name == "-a": pass elif name == "-c": ctag = True longlist = True elif name == "-d": displayname = True longlist = True elif name == "-D": details = True elif name == "-e": etag = True longlist = True elif name == "-i": supported_components = True longlist = True elif name == "-l": longlist = True elif name == "-r": rtype = True longlist = True elif name == "-s": synctoken = True longlist = True else: print "Unknown option: %s" % (name, ) print self.usage(cmdname) raise WrongOptions if len(args) > 1: print "Wrong number of arguments: %d" % (len(args), ) print self.usage(cmdname) raise WrongOptions elif args: path = args[0] if not path.startswith("/"): path = os.path.join(self.shell.wd, path) else: path = self.shell.wd if not path.endswith("/"): path += "/" resource = URL(url=path) props = (davxml.resourcetype, ) if longlist: props += ( davxml.getcontentlength, davxml.getlastmodified, ) if ctag: props += (csxml.getctag, ) if displayname: props += (davxml.displayname, ) if etag: props += (davxml.getetag, ) if supported_components: props += (caldavxml.supported_calendar_component_set, ) if synctoken: props += (davxml.synctoken, ) results = self.shell.account.session.getPropertiesOnHierarchy( resource, props) items = results.keys() items.sort() lines = [] for rurl in items: summaries = [] if details: data = self.shell.account.session.readData(URL(url=rurl)) try: cobject = Calendar.parseData(data[0]) for comp in cobject.getComponents(): for summary in [ p.getValue().getValue() for p in comp.getProperties("SUMMARY") ]: if summary not in summaries: summaries.append(summary) except: summaries = ["<Could not parse>"] rurl = urllib.unquote(rurl) if rurl == path: continue line = [] if longlist: props = results[urllib.quote(rurl)] size = props.get(davxml.getcontentlength, "-") if not size: size = "0" line.append("%s" % (size, )) modtime = props.get(davxml.getlastmodified, "-") line.append(modtime) line.append(rurl[len(path):]) if rtype: if isinstance(props.get(davxml.resourcetype), str): line.append("type:-") else: line.append("type:%s" % (",".join([ child.tag.split("}")[1] for child in props.get( davxml.resourcetype).getchildren() ]))) if displayname: line.append("name:'%s'" % (props.get(davxml.displayname, '-'), )) if ctag: line.append("ctag:'%s'" % (props.get(csxml.getctag, '-'), )) if etag: line.append("etag:'%s'" % (props.get(davxml.getetag, '-'), )) if supported_components and props.get( caldavxml.supported_calendar_component_set ) is not None: line.append("comp:%s" % (",".join([ child.get("name", "") for child in props.get( caldavxml.supported_calendar_component_set). getchildren() ]))) if synctoken: line.append("sync:'%s'" % (props.get(davxml.synctoken, '-'), )) else: line.append(rurl[len(path):]) if details: line.append("summary: {}".format("|".join(summaries))) lines.append(line) if lines: # Get column widths widths = [0] * len(lines[0]) for line in lines: for ctr, col in enumerate(line): widths[ctr] = max(widths[ctr], len(col)) # Write out each one for line in lines: for ctr, col in enumerate(line): if ctr in (0, 1) and longlist: print col.rjust(widths[ctr] + 2), else: print col.ljust(widths[ctr] + 2), print return True
def verify(self, manager, uri, response, respdata, args, is_json=False): #@UnusedVariable # Get arguments files = args.get("filepath", []) if manager.data_dir: files = map(lambda x: os.path.join(manager.data_dir, x), files) caldata = args.get("data", []) filters = args.get("filter", []) statusCode = args.get("status", ["200", "201", "207"]) doTimezones = args.get("doTimezones", None) if "EMAIL parameter" not in manager.server_info.features: filters.append("ATTENDEE:EMAIL") filters.append("ORGANIZER:EMAIL") filters.append("ATTENDEE:X-CALENDARSERVER-DTSTAMP") filters.append("ATTENDEE:X-CALENDARSERVER-AUTO") filters.append("ATTENDEE:X-CALENDARSERVER-RESET-PARTSTAT") filters.append("CALSCALE") filters.append("PRODID") filters.append("DTSTAMP") filters.append("CREATED") filters.append("LAST-MODIFIED") filters.append("X-WR-CALNAME") for afilter in tuple(filters): if afilter[0] == "!" and afilter[1:] in filters: filters.remove(afilter[1:]) filters = filter(lambda x: x[0] != "!", filters) if doTimezones is None: doTimezones = "timezones-by-reference" not in manager.server_info.features else: doTimezones = doTimezones == "true" # status code must be 200, 201, 207 or explicitly specified code if str(response.status) not in statusCode: return False, " HTTP Status Code Wrong: %d" % (response.status,) # look for response data if not respdata: return False, " No response body" # look for one file if len(files) != 1 and len(caldata) != 1: return False, " No file to compare response to" # read in all data from specified file or use provided data if len(files): fd = open(files[0], "r") try: try: data = fd.read() finally: fd.close() except: data = None else: data = caldata[0] if len(caldata) else None if data is None: return False, " Could not read data file" data = manager.server_info.extrasubs(manager.server_info.subs(data)) def removePropertiesParameters(component): if not doTimezones: for subcomponent in tuple(component.getComponents()): if subcomponent.getType() == "VTIMEZONE": component.removeComponent(subcomponent) for subcomponent in component.getComponents(): removePropertiesParameters(subcomponent) allProps = [] for properties in component.getProperties().itervalues(): allProps.extend(properties) for property in allProps: # Always reset DTSTAMP on these properties if property.getName() in ("ATTENDEE", "X-CALENDARSERVER-ATTENDEE-COMMENT"): if property.hasParameter("X-CALENDARSERVER-DTSTAMP"): property.replaceParameter(Parameter("X-CALENDARSERVER-DTSTAMP", "20080101T000000Z")) for filter in filters: if ":" in filter: propname, parameter = filter.split(":") if property.getName() == propname: if property.hasParameter(parameter): property.removeParameters(parameter) else: if property.getName() == filter: component.removeProperty(property) def reconcileRecurrenceOverrides(calendar1, calendar2): """ Make sure that the same set of overridden components appears in both calendar objects. """ def _getRids(calendar): """ Get all the recurrence ids of the specified calendar. """ results = set() master = None for subcomponent in calendar.getComponents(): if isinstance(subcomponent, ComponentRecur): rid = subcomponent.getRecurrenceID() if rid: results.add(rid.duplicateAsUTC()) else: master = subcomponent return results, master def _addOverrides(calendar, master, missing_rids): """ Derive instances for the missing overrides in the specified calendar object. """ if master is None or not missing_rids: return for rid in missing_rids: # If we were fed an already derived component, use that, otherwise make a new one newcomp = calendar.deriveComponent(rid) if newcomp is not None: calendar.addComponent(newcomp) rids1, master1 = _getRids(calendar1) rids2, master2 = _getRids(calendar2) _addOverrides(calendar1, master1, rids2 - rids1) _addOverrides(calendar2, master2, rids1 - rids2) try: format = Calendar.sFormatJSON if is_json else Calendar.sFormatText resp_calendar = Calendar.parseData(respdata, format=format) removePropertiesParameters(resp_calendar) data_calendar = Calendar.parseData(data, format=format) removePropertiesParameters(data_calendar) reconcileRecurrenceOverrides(resp_calendar, data_calendar) respdata = resp_calendar.getText(includeTimezones=Calendar.NO_TIMEZONES, format=format) data = data_calendar.getText(includeTimezones=Calendar.NO_TIMEZONES, format=format) result = resp_calendar == data_calendar if result: return True, "" else: error_diff = "\n".join([line for line in unified_diff(data.split("\n"), respdata.split("\n"))]) return False, " Response data does not exactly match file data%s" % (error_diff,) except Exception, e: return False, " Response data is not calendar data: %s" % (e,)
def verify(self, manager, uri, response, respdata, args, is_json=False): # @UnusedVariable # Get arguments files = args.get("filepath", []) if manager.data_dir: files = map(lambda x: os.path.join(manager.data_dir, x), files) caldata = args.get("data", []) filters = args.get("filter", []) statusCode = args.get("status", ["200", "201", "207"]) doTimezones = args.get("doTimezones", None) if "EMAIL parameter" not in manager.server_info.features: filters.append("ATTENDEE:EMAIL") filters.append("ORGANIZER:EMAIL") filters.extend(manager.server_info.calendardatafilters) for afilter in tuple(filters): if afilter[0] == "!" and afilter[1:] in filters: filters.remove(afilter[1:]) filters = filter(lambda x: x[0] != "!", filters) if doTimezones is None: doTimezones = "timezones-by-reference" not in manager.server_info.features else: doTimezones = doTimezones == "true" # status code must be 200, 201, 207 or explicitly specified code if str(response.status) not in statusCode: return False, " HTTP Status Code Wrong: %d" % ( response.status, ) # look for response data if not respdata: return False, " No response body" # look for one file if len(files) != 1 and len(caldata) != 1: return False, " No file to compare response to" # read in all data from specified file or use provided data if len(files): fd = open(files[0], "r") try: try: data = fd.read() finally: fd.close() except: data = None else: data = caldata[0] if len(caldata) else None if data is None: return False, " Could not read data file" data = manager.server_info.extrasubs(manager.server_info.subs(data)) def removePropertiesParameters(component): if not doTimezones: for subcomponent in tuple(component.getComponents()): if subcomponent.getType() == "VTIMEZONE": component.removeComponent(subcomponent) for subcomponent in component.getComponents(): removePropertiesParameters(subcomponent) if component.getType() == "VEVENT": if component.hasEnd(): component.editTimingStartDuration( component.getStart(), component.getEnd() - component.getStart()) allProps = [] for properties in component.getProperties().itervalues(): allProps.extend(properties) for property in allProps: # Always reset DTSTAMP on these properties if property.getName() in ("ATTENDEE", "X-CALENDARSERVER-ATTENDEE-COMMENT"): if property.hasParameter("X-CALENDARSERVER-DTSTAMP"): property.replaceParameter( Parameter("X-CALENDARSERVER-DTSTAMP", "20080101T000000Z")) for filter in filters: if ":" in filter: propname, parameter = filter.split(":") if property.getName() == propname: if property.hasParameter(parameter): property.removeParameters(parameter) else: if "=" in filter: filter_name, filter_value = filter.split("=") if property.getName( ) == filter_name and property.getValue().getValue( ) == filter_value: component.removeProperty(property) else: if property.getName() == filter: component.removeProperty(property) def reconcileRecurrenceOverrides(calendar1, calendar2): """ Make sure that the same set of overridden components appears in both calendar objects. """ def _getRids(calendar): """ Get all the recurrence ids of the specified calendar. """ results = set() master = None for subcomponent in calendar.getComponents(): if isinstance(subcomponent, ComponentRecur): rid = subcomponent.getRecurrenceID() if rid: results.add(rid.duplicateAsUTC()) else: master = subcomponent return results, master def _addOverrides(calendar, master, missing_rids): """ Derive instances for the missing overrides in the specified calendar object. """ if master is None or not missing_rids: return for rid in missing_rids: # If we were fed an already derived component, use that, otherwise make a new one newcomp = calendar.deriveComponent(rid) if newcomp is not None: calendar.addComponent(newcomp) rids1, master1 = _getRids(calendar1) rids2, master2 = _getRids(calendar2) _addOverrides(calendar1, master1, rids2 - rids1) _addOverrides(calendar2, master2, rids1 - rids2) try: format = Calendar.sFormatJSON if is_json else Calendar.sFormatText resp_calendar = Calendar.parseData(respdata, format=format) removePropertiesParameters(resp_calendar) data_calendar = Calendar.parseData(data, format=format) removePropertiesParameters(data_calendar) reconcileRecurrenceOverrides(resp_calendar, data_calendar) respdata = resp_calendar.getText( includeTimezones=Calendar.NO_TIMEZONES, format=format) data = data_calendar.getText( includeTimezones=Calendar.NO_TIMEZONES, format=format) result = resp_calendar == data_calendar if not result: respdata2 = respdata.replace("\r\n ", "") data2 = data.replace("\r\n ", "").replace("urn:x-uid:", "urn:uuid:") result = respdata2 == data2 if result: return True, "" else: error_diff = "\n".join([ line for line in unified_diff(data.split("\n"), respdata.split("\n")) ]) return False, " Response data does not exactly match file data%s" % ( error_diff, ) except Exception, e: return False, " Response data is not calendar data: %s" % ( e, )
def execute(self, cmdname, options): timerange = False start = None end = None expand = False data = False details = False opts, args = getopt.getopt(shlex.split(options), 'ts:e:xdD') for name, value in opts: if name == "-t": timerange = True elif name == "-s": start = value elif name == "-e": end = value elif name == "-x": expand = True elif name == "-d": data = True elif name == "-D": details = True else: print "Unknown option: %s" % (name, ) print self.usage(cmdname) raise WrongOptions if len(args) > 1: print "Wrong number of arguments: %d" % (len(args), ) print self.usage(cmdname) raise WrongOptions path = args[0] if len(args) else self.shell.wd if not path.startswith("/"): path = os.path.join(self.shell.wd, path) if not path.endswith("/"): path += "/" resource = URL(url=path) now = time.time() if timerange and start is None: now_tm = time.gmtime(now) start = "%04d%02d%02dT000000Z" % ( now_tm.tm_year, now_tm.tm_mon, now_tm.tm_mday, ) if timerange and end is None: tomorrow_tm = time.gmtime(now + 24 * 60 * 60) end = "%04d%02d%02dT000000Z" % ( tomorrow_tm.tm_year, tomorrow_tm.tm_mon, tomorrow_tm.tm_mday, ) props = (davxml.getetag, ) if data and not expand: props += (caldavxml.calendar_data, ) results = self.shell.account.session.queryCollection(resource, timerange, start, end, expand, props=props) for href in results: if details: summaries = [] data = self.shell.account.session.readData(href) try: cobject = Calendar.parseData(data[0]) for comp in cobject.getComponents(): for summary in [ p.getValue().getValue() for p in comp.getProperties("SUMMARY") ]: if summary not in summaries: summaries.append(summary) except: summaries = ["<Could not parse>"] print href, "summary: {}".format("|".join(summaries)) else: print href return True
def verify(self, manager, uri, response, respdata, args, is_json=False): #@UnusedVariable # Get arguments files = args.get("filepath", []) caldata = args.get("data", []) filters = args.get("filter", []) statusCode = args.get("status", ["200", "201", "207"]) doTimezones = args.get("doTimezones", None) if "EMAIL parameter" not in manager.server_info.features: filters.append("ATTENDEE:EMAIL") filters.append("ORGANIZER:EMAIL") filters.append("ATTENDEE:X-CALENDARSERVER-DTSTAMP") filters.append("ATTENDEE:X-CALENDARSERVER-AUTO") filters.append("CALSCALE") filters.append("PRODID") filters.append("DTSTAMP") filters.append("CREATED") filters.append("LAST-MODIFIED") filters.append("X-WR-CALNAME") for afilter in tuple(filters): if afilter[0] == "!" and afilter[1:] in filters: filters.remove(afilter[1:]) filters = filter(lambda x: x[0] != "!", filters) if doTimezones is None: doTimezones = "timezones-by-reference" not in manager.server_info.features else: doTimezones = doTimezones == "true" # status code must be 200, 201, 207 or explicitly specified code if str(response.status) not in statusCode: return False, " HTTP Status Code Wrong: %d" % (response.status,) # look for response data if not respdata: return False, " No response body" # look for one file if len(files) != 1 and len(caldata) != 1: return False, " No file to compare response to" # read in all data from specified file or use provided data if len(files): fd = open(files[0], "r") try: try: data = fd.read() finally: fd.close() except: data = None else: data = caldata[0] if len(caldata) else None if data is None: return False, " Could not read data file" data = manager.server_info.extrasubs(manager.server_info.subs(data)) def removePropertiesParameters(component): if not doTimezones: for subcomponent in tuple(component.getComponents()): if subcomponent.getType() == "VTIMEZONE": component.removeComponent(subcomponent) for subcomponent in component.getComponents(): removePropertiesParameters(subcomponent) allProps = [] for properties in component.getProperties().itervalues(): allProps.extend(properties) for property in allProps: # Always reset DTSTAMP on these properties if property.getName() in ("ATTENDEE", "X-CALENDARSERVER-ATTENDEE-COMMENT"): if property.hasParameter("X-CALENDARSERVER-DTSTAMP"): property.replaceParameter(Parameter("X-CALENDARSERVER-DTSTAMP", "20080101T000000Z")) for filter in filters: if ":" in filter: propname, parameter = filter.split(":") if property.getName() == propname: if property.hasParameter(parameter): property.removeParameters(parameter) else: if property.getName() == filter: component.removeProperty(property) try: format = Calendar.sFormatJSON if is_json else Calendar.sFormatText resp_calendar = Calendar.parseData(respdata, format=format) removePropertiesParameters(resp_calendar) respdata = resp_calendar.getText(includeTimezones=Calendar.NO_TIMEZONES, format=format) data_calendar = Calendar.parseData(data, format=format) removePropertiesParameters(data_calendar) data = data_calendar.getText(includeTimezones=Calendar.NO_TIMEZONES, format=format) result = resp_calendar == data_calendar if result: return True, "" else: error_diff = "\n".join([line for line in unified_diff(data.split("\n"), respdata.split("\n"))]) return False, " Response data does not exactly match file data%s" % (error_diff,) except Exception, e: return False, " Response data is not calendar data: %s" % (e,)