def testDeleteAnnotationByName(self): self.test_doc.SetAnnotation(document.Range(0, 1), 'key', 'value') self.test_doc.SetAnnotation(document.Range(0, 1), 'key2', 'value') self.test_doc.SetAnnotation(document.Range(10, 11), 'key', 'value') self.test_doc.SetAnnotation(document.Range(00, 11), 'key2', 'value') self.test_doc.SetAnnotation(document.Range(20, 21), 'key', 'value') self.test_doc.DeleteAnnotationsByName('key') self.assertFalse(self.test_doc.HasAnnotation('key'))
def testDeleteAnnotationInRange(self): self.test_doc.SetAnnotation(document.Range(0, 10), 'key', 'value') self.test_doc.DeleteAnnotationsInRange(document.Range(2, 6), 'key') self.assertTrue(self.test_doc.HasAnnotation('key')) l = [x for x in self.test_doc.RangesForAnnotation('key')] self.assertEqual(len(l), 2) self.test_doc.DeleteAnnotationsInRange(document.Range(0, 2), 'key') l = [x for x in self.test_doc.RangesForAnnotation('key')] print [str(x) for x in l] self.assertEqual(len(l), 1) self.test_doc.DeleteAnnotationsInRange(document.Range(5, 8), 'key') l = [x for x in self.test_doc.RangesForAnnotation('key')] self.assertEqual(len(l), 1) self.test_doc.DeleteAnnotationsInRange(document.Range(7, 12), 'key') self.assertFalse(self.test_doc.HasAnnotation('key'))
def DeleteAnnotationsInRange(self, r, name): """Clears all of the annotations within a given range with a given key. Args: r: A Range specifying the range to delete. name: Annotation key type to clear. """ self.__context.builder.DocumentAnnotationDelete( self._blip.waveId, self._blip.waveletId, self._blip.blipId, r.start, r.end, name) res = [] for a in self._blip.annotations: if a.name != name or r.start > a.range.end or r.end < a.range.start: res.append(a) elif r.start < a.range.start and r.end > a.range.end: continue else: if a.range.start < r.start: res.append( document.Annotation( name, a.value, document.Range(a.range.start, r.start))) if a.range.end > r.end: a.range.start = r.end res.append(a) self._blip.annotations = res
def __init__(self, json): """Inits this blip with JSON data. Args: json: JSON data dictionary from Wave server. """ self.blipId = json.get('blipId') self.childBlipIds = set(json.get('childBlipIds', [])) self.content = json.get('content', '') self.contributors = set(json.get('contributors', [])) self.creator = json.get('creator') self.lastModifiedTime = json.get('lastModifiedTime', 0) self.parentBlipId = json.get('parentBlipId') self.waveId = json.get('waveId') self.waveletId = json.get('waveletId') self.annotations = [] for annotation in json.get('annotations', []): r = document.Range(annotation['range']['start'], annotation['range']['end']) self.annotations.append( document.Annotation(annotation['name'], annotation['value'], r=r)) self.document = Document(self) self.elements = {} json_elements = json.get('elements', {}) for elem in json_elements: self.elements[elem] = document.ElementFromJson(json_elements[elem]) self.raw_data = json
def CreateBlipData(data): """Construct blip data from the raw incoming wire protocol. TODO(davidbyttow): Automate this based on naming like the Serialize methods. Args: data: Serialized data from server. Returns: Instance of BlipData based on the fields. """ blip_data = BlipData() blip_data.annotations = [] for annotation in data['annotations']: r = document.Range(annotation['range']['start'], annotation['range']['end']) blip_data.annotations.append(document.Annotation(annotation['name'], annotation['value'], r=r)) blip_data.child_blip_ids = set(data['childBlipIds']) blip_data.content = data['content'] blip_data.contributors = set(data['contributors']) blip_data.creator = data['creator'] blip_data.elements = data['elements'] blip_data.last_modified_time = data['lastModifiedTime'] blip_data.parent_blip_id = data['parentBlipId'] blip_data.blip_id = data['blipId'] blip_data.version = data['version'] blip_data.wave_id = data['waveId'] blip_data.wavelet_id = data['waveletId'] return blip_data
def AnnotateDocument(self, name, value): """Annotates the entire document. Args: name: A string as the key for this annotation. value: The value of this annotation. """ b = self.__context.builder b.DocumentAnnotationSetNoRange(self._blip.waveId, self._blip.waveletId, self._blip.blipId, name, value) r = document.Range(0, len(self._blip.content)) self._blip.annotations.append(document.Annotation(name, value, r))
def ClipRange(r, clip_range): """Clips one range to another. Given a range to be clipped and a clipping range, will result in a list of 0-2 new ranges. If the range is completely inside of the clipping range then an empty list will be returned. If it is completely outside, then a list with only the same range will be returned. Otherwise, other permutations may result in a single clipped range or two ranges that were the result of a split. Args: r: The range to be clipped. clip_range: The range that is clipping the other. Returns: A list of 0-2 ranges as a result of performing the clip. """ # Check if completely outside the clipping range. if r.end <= clip_range.start or r.start >= clip_range.end: return [r] # Check if completely clipped. if r.start >= clip_range.start and r.end <= clip_range.end: return [] # Check if split. if clip_range.start >= r.start and clip_range.end <= r.end: splits = [] if r.start < clip_range.start: splits.append(document.Range(r.start, clip_range.start)) if clip_range.end < r.end: splits.append(document.Range(clip_range.end, r.end)) return splits # Just a trim. if clip_range.start < r.start: return [document.Range(clip_range.end, r.end)] return [document.Range(r.start, clip_range.start)]
def DocumentDelete(self, wave_id, wavelet_id, blip_id, start=None, end=None): """Requests to delete content in a given range. Args: wave_id: The wave id owning that this operation is applied to. wavelet_id: The wavelet id that this operation is applied to. blip_id: The blip id that this operation is applied to. start: Start of the range. end: End of the range. """ if start is None or end is None: range = None else: range = document.Range(start, end) self.AddNewOperation(DOCUMENT_DELETE, wave_id, wavelet_id, blip_id, prop=range)
def DocumentAnnotationDelete(self, wave_id, wavelet_id, blip_id, start, end, name): """Deletes a specified annotation of a given range with a specific key. Not yet implemented. Args: wave_id: The wave id owning that this operation is applied to. wavelet_id: The wavelet id that this operation is applied to. blip_id: The blip id that this operation is applied to. start: Start position of the range. end: End position of the range. name: Annotation key name to clear. """ self.AddNewOperation(DOCUMENT_ANNOTATION_DELETE, wave_id, wavelet_id, blip_id=blip_id, prop=document.Range(start, end))
def DocumentDelete(self, wave_id, wavelet_id, blip_id, start, end): """Requests to delete content in a given range. Args: wave_id: The wave id owning that this operation is applied to. wavelet_id: The wavelet id that this operation is applied to. blip_id: The blip id that this operation is applied to. start: Start of the range. end: End of the range. """ range = None if start != end: range = document.Range(start, end) op = Operation(DOCUMENT_DELETE, wave_id, wavelet_id, blip_id, prop=range) self.__context.AddOperation(op)
def DocumentAnnotationSet(self, wave_id, wavelet_id, blip_id, start, end, name, value): """Set a specified annotation of a given range with a specific key. Args: wave_id: The wave id owning that this operation is applied to. wavelet_id: The wavelet id that this operation is applied to. blip_id: The blip id that this operation is applied to. start: Start position of the range. end: End position of the range. name: Annotation key name to clear. value: The value of the annotation across this range. """ annotation = document.Annotation(name, value, document.Range(start, end)) self.AddNewOperation(DOCUMENT_ANNOTATION_SET, wave_id, wavelet_id, blip_id=blip_id, prop=annotation)
def testDefaults(self): r = document.Range() self.assertEquals(0, r.start) self.assertEquals(1, r.end)
def testDefaults(self): annotation = document.Annotation('key', 'value') self.assertEquals(document.Range().start, annotation.range.start) self.assertEquals(document.Range().end, annotation.range.end)
def testSetTextInRange(self): text = 'abc' self.test_doc.SetTextInRange(document.Range(0, 2), text) self.assertEquals('abc456', self.test_doc.GetText()) self.test_doc.SetTextInRange(document.Range(2, 2), text) self.assertEquals('ababc456', self.test_doc.GetText())
def testRangesForAnnotation(self): self.assertEqual([x for x in self.test_doc.RangesForAnnotation('key')], []) self.test_doc.SetAnnotation(document.Range(1, 10), 'key', 'value') l = [x for x in self.test_doc.RangesForAnnotation('key')] self.assertTrue(l[0].start, 1)
def testValidRanges(self): r = document.Range(1, 2) self.assertEquals(1, r.start) self.assertEquals(2, r.end)
def testCollapsedRanges(self): self.assertTrue(document.Range(0, 0).IsCollapsed()) self.assertTrue(document.Range(1, 1).IsCollapsed())
def testDeleteRange(self): self.test_doc.DeleteRange(document.Range(0, 1)) self.assertEquals('3456', self.test_doc.GetText()) self.test_doc.DeleteRange(document.Range(0, 0)) self.assertEquals('456', self.test_doc.GetText())
def testFields(self): annotation = document.Annotation('key', 'value', document.Range(2, 3)) self.assertEquals('key', annotation.name) self.assertEquals('value', annotation.value) self.assertEquals(2, annotation.range.start) self.assertEquals(3, annotation.range.end)
def testSetAnnotation(self): self.test_doc.SetAnnotation(document.Range(0, 1), 'key', 'value') self.assertTrue(self.test_doc.HasAnnotation('key'))
def R(x, y): return document.Range(x, y)
def testDeleteAnnotationInRange(self): self.assertRaises(NotImplementedError, self.test_doc.DeleteAnnotationsInRange, document.Range(0, 1), 'key')