Skip to content
This repository
tree: fcf4db3849
Fetching contributors…

Cannot retrieve contributors at this time

file 1110 lines (906 sloc) 40.182 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
#!/usr/bin/python2.4
#
# Copyright (C) 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Support for operations that can be applied to the server.

Contains classes and utilities for creating operations that are to be
applied on the server.
"""

__author__ = 'davidbyttow@google.com (David Byttow)'


import document
import logging
import model
import util


# Operation Types
WAVELET_APPEND_BLIP = 'WAVELET_APPEND_BLIP'
WAVELET_ADD_PARTICIPANT = 'WAVELET_ADD_PARTICIPANT'
WAVELET_CREATE = 'WAVELET_CREATE'
WAVELET_REMOVE_SELF = 'WAVELET_REMOVE_SELF'
WAVELET_DATADOC_SET = 'WAVELET_DATADOC_SET'
WAVELET_SET_TITLE = 'WAVELET_SET_TITLE'
BLIP_CREATE_CHILD = 'BLIP_CREATE_CHILD'
BLIP_DELETE = 'BLIP_DELETE'
DOCUMENT_ANNOTATION_DELETE = 'DOCUMENT_ANNOTATION_DELETE'
DOCUMENT_ANNOTATION_SET = 'DOCUMENT_ANNOTATION_SET'
DOCUMENT_ANNOTATION_SET_NORANGE = 'DOCUMENT_ANNOTATION_SET_NORANGE'
DOCUMENT_APPEND = 'DOCUMENT_APPEND'
DOCUMENT_APPEND_MARKUP = 'DOCUMENT_APPEND_MARKUP'
DOCUMENT_APPEND_STYLED_TEXT = 'DOCUMENT_APPEND_STYLED_TEXT'
DOCUMENT_INSERT = 'DOCUMENT_INSERT'
DOCUMENT_DELETE = 'DOCUMENT_DELETE'
DOCUMENT_REPLACE = 'DOCUMENT_REPLACE'
DOCUMENT_ELEMENT_APPEND = 'DOCUMENT_ELEMENT_APPEND'
DOCUMENT_ELEMENT_DELETE = 'DOCUMENT_ELEMENT_DELETE'
DOCUMENT_ELEMENT_MODIFY_ATTRS = 'DOCUMENT_ELEMENT_MODIFY_ATTRS'
DOCUMENT_ELEMENT_INSERT = 'DOCUMENT_ELEMENT_INSERT'
DOCUMENT_ELEMENT_INSERT_AFTER = 'DOCUMENT_ELEMENT_INSERT_AFTER'
DOCUMENT_ELEMENT_INSERT_BEFORE = 'DOCUMENT_ELEMENT_INSERT_BEFORE'
DOCUMENT_ELEMENT_REPLACE = 'DOCUMENT_ELEMENT_REPLACE'
DOCUMENT_INLINE_BLIP_APPEND = 'DOCUMENT_INLINE_BLIP_APPEND'
DOCUMENT_INLINE_BLIP_DELETE = 'DOCUMENT_INLINE_BLIP_DELETE'
DOCUMENT_INLINE_BLIP_INSERT = 'DOCUMENT_INLINE_BLIP_INSERT'
DOCUMENT_INLINE_BLIP_INSERT_AFTER_ELEMENT = ('DOCUMENT_INLINE_BLIP_INSERT_'
                                             'AFTER_ELEMENT')


class Operation(object):
  """Represents a generic operation applied on the server.

This operation class contains data that is filled in depending on the
operation type.

It can be used directly, but doing so will not result
in local, transient reflection of state on the blips. In other words,
creating a "delete blip" operation will not remove the blip from the local
context for the duration of this session. It is better to use the OpBased
model classes directly instead.
"""

  java_class = 'com.google.wave.api.impl.OperationImpl'

  def __init__(self, op_type, wave_id, wavelet_id, blip_id='', index=-1,
               prop=None):
    """Initializes this operation with contextual data.

Args:
op_type: Type of operation.
wave_id: The id of the wave that this operation is to be applied.
wavelet_id: The id of the wavelet that this operation is to be applied.
blip_id: The optional id of the blip that this operation is to be applied.
index: Optional integer index for content-based operations.
prop: A weakly typed property object is based on the context of this
operation.
"""
    self.type = op_type
    self.wave_id = wave_id
    self.wavelet_id = wavelet_id
    self.blip_id = blip_id
    self.index = index
    self.property = prop


class OpBasedWave(model.Wave):
  """Subclass of the wave model capable of generating operations.

Any mutation-based methods will likely result in one or more operations
being applied locally and sent to the server.
"""

  def __init__(self, json, context):
    """Initializes this wave with the session context."""
    super(OpBasedWave, self).__init__(json)
    self.__context = context

  def CreateWavelet(self, participants=None):
    """Creates a new wavelet on this wave."""
    return self.__context.builder.WaveletCreate(
        self.GetId(), '', participants)


class OpBasedWavelet(model.Wavelet):
  """Subclass of the wavelet model capable of generating operations.

Any mutation-based methods will likely result in one or more operations
being applied locally and sent to the server.
"""

  def __init__(self, json, context):
    """Initializes this wavelet with the session context."""
    super(OpBasedWavelet, self).__init__(json)
    self.__context = context

  def CreateBlip(self):
    """Creates and appends a blip to this wavelet and returns it.

Returns:
A transient version of the blip that was created.
"""
    blip_data = self.__context.builder.WaveletAppendBlip(self.GetWaveId(),
                                                         self.GetId())
    return self.__context.AddBlip(blip_data)

  def AddParticipant(self, participant_id):
    """Adds a participant to a wavelet.

Args:
participant_id: Id of the participant that is to be added.
"""
    self.__context.builder.WaveletAddParticipant(self.GetWaveId(), self.GetId(),
                                                 participant_id)
    self.participants.add(participant_id)

  def RemoveSelf(self):
    """Removes this robot from the wavelet."""
    self.__context.builder.WaveletRemoveSelf(self.GetWaveId(), self.GetId())
    # TODO(davidbyttow): Locally remove the robot.

  def SetDataDocument(self, name, data):
    """Sets a key/value pair on the wavelet data document.

Args:
name: The string key.
data: The value associated with this key.
"""
    self.__context.builder.WaveletSetDataDoc(self.GetWaveId(), self.GetId(),
                                             name, data)
    self.dataDocuments[name] = data

  def SetTitle(self, title):
    """Sets the title of this wavelet.

Args:
title: String title to for this wave.
"""
    self.__context.builder.WaveletSetTitle(self.GetWaveId(), self.GetId(),
                                           title)
    self.title = title


class OpBasedBlip(model.Blip):
  """Subclass of the blip model capable of generating operations.

Any mutation-based methods will likely result in one or more operations
being applied locally and sent to the server.
"""

  def __init__(self, json, context):
    """Initializes this blip with the session context."""
    super(OpBasedBlip, self).__init__(json)
    self.__context = context
    self.document = OpBasedDocument(self, context)

  def CreateChild(self):
    """Creates a child blip of this blip."""
    blip_data = self.__context.builder.BlipCreateChild(self.GetWaveId(),
                                                       self.GetWaveletId(),
                                                       self.GetId())
    return self.__context.AddBlip(blip_data)

  def Delete(self):
    """Deletes this blip from the wavelet."""
    self.__context.builder.BlipDelete(self.GetWaveId(),
                                      self.GetWaveletId(),
                                      self.GetId())
    return self.__context.RemoveBlip(self.GetId())


class OpBasedDocument(model.Document):
  """Subclass of the document model capable of generating operations.

Any mutation-based methods will likely result in one or more operations
being applied locally and sent to the server.

TODO(davidbyttow): Manage annotations and elements as content is updated.
"""

  def __init__(self, blip, context):
    """Initializes this document with its owning blip and session context."""
    super(OpBasedDocument, self).__init__(blip)
    self.__context = context

  def HasAnnotation(self, name):
    """Determines if given named annotation is anywhere on this document.

Args:
name: The key name of the annotation.

Returns:
True if the annotation exists.
"""
    for annotation in self._blip.annotations:
      if annotation.name == name:
        return True
    return False

  def RangesForAnnotation(self, name):
    """Iterate through the ranges defined for name.

Args:
name: The name of the annotation.

Returns:
the matching ranges.
"""
    for annotation in self._blip.annotations:
      if annotation.name == name:
        yield annotation.range

  def SetText(self, text):
    """Clears and sets the text of this document.

Args:
text: The text content to replace this document with.
"""
    self.Clear()
    self.__context.builder.DocumentInsert(self._blip.waveId,
                                          self._blip.waveletId,
                                          self._blip.blipId,
                                          text)
    self._blip.content = text

  def SetTextInRange(self, r, text):
    """Deletes text within a range and sets the supplied text in its place.

Args:
r: Range to delete and where to set the new text.
text: The text to set at the range start position.
"""
    self.DeleteRange(r)
    self.InsertText(r.start, text)

  def InsertText(self, start, text):
    """Inserts text at a specific position.

Args:
start: The index position where to set the text.
text: The text to set.
"""
    self.__context.builder.DocumentInsert(self._blip.waveId,
                                          self._blip.waveletId,
                                          self._blip.blipId,
                                          text, index=start)
    left = self._blip.content[:start]
    right = self._blip.content[start:]
    self._blip.content = left + text + right

  def AppendText(self, text):
    """Appends text to the end of this document.

Args:
text: The text to append.
"""
    self.__context.builder.DocumentAppend(self._blip.waveId,
                                          self._blip.waveletId,
                                          self._blip.blipId,
                                          text)
    self._blip.content += text

  def Clear(self):
    """Clears the content of this document."""
    self.__context.builder.DocumentDelete(self._blip.waveId,
                                          self._blip.waveletId,
                                          self._blip.blipId)
    self._blip.content = ''

  def DeleteRange(self, r):
    """Deletes the content in the specified range.

Args:
r: A Range instance specifying the range to delete.
"""
    self.__context.builder.DocumentDelete(self._blip.waveId,
                                          self._blip.waveletId,
                                          self._blip.blipId,
                                          r.start, r.end)
    left = self._blip.content[:r.start]
    right = self._blip.content[r.end + 1:]
    self._blip.content = left + right

  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 SetAnnotation(self, r, name, value):
    """Sets an annotation on a given range.

Args:
r: A Range specifying the range to set the annotation.
name: A string as the key for this annotation.
value: The value of this annotaton.
"""
    self.__context.builder.DocumentAnnotationSet(self._blip.waveId,
                                                 self._blip.waveletId,
                                                 self._blip.blipId,
                                                 r.start, r.end,
                                                 name, value)
    self._blip.annotations.append(document.Annotation(name, value, r))

  def DeleteAnnotationsByName(self, name):
    """Deletes all annotations with a given key name.

Args:
name: A string as the key for the annotation to delete.
"""
    size = len(self._blip.content)
    self.__context.builder.DocumentAnnotationDelete(self._blip.waveId,
                                                    self._blip.waveletId,
                                                    self._blip.blipId,
                                                    0, size, name)
    self._blip.annotations = [a
        for a in self._blip.annotations if a.name != name]

  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 AppendInlineBlip(self):
    """Appends an inline blip to this blip.

Returns:
The local blip that was appended.
"""
    blip_data = self.__context.builder.DocumentInlineBlipAppend(
        self._blip.waveId, self._blip.waveletId,
        self._blip.blipId)
    return self.__context.AddBlip(blip_data)

  def DeleteInlineBlip(self, inline_blipId):
    """Deletes an inline blip from this blip.

Args:
inline_blipId: The id of the blip to remove.
"""
    self.__context.builder.DocumentInlineBlipDelete(self._blip.waveId,
                                                    self._blip.waveletId,
                                                    self._blip.blipId,
                                                    inline_blipId)
    self.__context.RemoveBlip(inline_blipId)

  def InsertInlineBlip(self, position):
    """Inserts an inline blip into this blip at a specific position.

Args:
position: Position to insert the blip at.

Returns:
The JSON data of the blip that was created.
"""
    blip_data = self.__context.builder.DocumentInlineBlipInsert(
        self._blip.waveId,
        self._blip.waveletId,
        self._blip.blipId,
        position)
    # TODO(davidbyttow): Add local blip element.
    return self.__context.AddBlip(blip_data)

  def DeleteElement(self, position):
    """Deletes an Element at a given position.

Args:
position: Position of the Element to delete.
"""
    self.__context.builder.DocumentElementDelete(self._blip.waveId,
                                                 self._blip.waveletId,
                                                 self._blip.blipId,
                                                 position)

  def InsertElement(self, position, element):
    """Inserts an Element at a given position.

Args:
position: Position of the element to replace.
element: The Element to replace with.
"""
    self.__context.builder.DocumentElementInsert(self._blip.waveId,
                                                 self._blip.waveletId,
                                                 self._blip.blipId,
                                                 position, element)

  def ReplaceElement(self, position, element):
    """Replaces an Element at a given position with a new element.

Args:
position: Position of the element to replace.
element: The Element to replace with.
"""
    self.__context.builder.DocumentElementReplace(self._blip.waveId,
                                                  self._blip.waveletId,
                                                  self._blip.blipId,
                                                  position, element)

  def AppendElement(self, element):
    self.__context.builder.DocumentElementAppend(self._blip.waveId,
                                                 self._blip.waveletId,
                                                 self._blip.blipId,
                                                 element)

  def GadgetSubmitDelta(self, gadget, delta):
    """Submit a delta for the specified gadget.

Currently submit delta is keyed on the url of the gadget and
won't work if two gadgets with the same url are present in the
document.

Args:
gadget: the gadget to submit the delta too
delta: a dictionary with the key/values that need to be updated.
"""
    dummy = document.Gadget(url=gadget.url, props=delta)
    self.__context.builder.DocumentModifyAttributes(self._blip.waveId,
                                                    self._blip.waveletId,
                                                    self._blip.blipId,
                                                    dummy)
    gadget.SubmitDelta(delta)


class _ContextImpl(model.Context):
  """An internal implementation of the Context class.

This implementation of the context is capable of adding waves, wavelets
and blips to itself. This is useful when applying operations locally
in a single session. Through this, clients can access waves, wavelets and
blips and add operations to be applied to those objects by the server.

Operations are applied in the order that they are received. Adding
operations manually will not be reflected in the state of the context.
"""

  def __init__(self):
    super(_ContextImpl, self).__init__()
    self.builder = OpBuilder(self)

  def AddOperation(self, op):
    """Adds an operation to the list of operations to applied by the server.

After all events are handled, the operation list is sent back to the server
and applied in order. Adding an operation this way will have no effect
on the state of the context or its entities.

Args:
op: An instance of an Operation.
"""
    self._operations.append(op)

  def AddWave(self, wave_data):
    """Adds a transient wave based on the data supplied.

Args:
wave_data: JSON data describing this wave.

Returns:
An OpBasedWave that may have operations applied to it.
"""
    wave = OpBasedWave(wave_data, self)
    self.waves[wave.GetId()] = wave
    return wave

  def AddWavelet(self, wavelet_data):
    """Adds a transient wavelet based on the data supplied.

Args:
wavelet_data: JSON data describing this wavelet.

Returns:
An OpBasedWavelet that may have operations applied to it.
"""
    wavelet = OpBasedWavelet(wavelet_data, self)
    self.wavelets[wavelet.GetId()] = wavelet
    return wavelet

  def AddBlip(self, blip_data):
    """Adds a transient blip based on the data supplied.

Args:
blip_data: JSON data describing this blip.

Returns:
An OpBasedBlip that may have operations applied to it.
"""
    blip = OpBasedBlip(blip_data, self)
    self.blips[blip.GetId()] = blip
    return blip

  def RemoveWave(self, wave_id):
    """Removes a wave locally."""
    if wave_id in self.waves:
      del self.waves[wave_id]

  def RemoveWavelet(self, wavelet_id):
    """Removes a wavelet locally."""
    if wavelet_id in self.wavelets:
      del self.wavelets[wavelet_id]

  def RemoveBlip(self, blip_id):
    """Removes a blip locally."""
    if blip_id in self.blips:
      del self.blips[blip_id]

  def Serialize(self):
    """Serialize the operation bundle.

Returns:
Dict representing this object.
"""
    data = {
        'javaClass': 'com.google.wave.api.impl.OperationMessageBundle',
        'operations': util.Serialize(self._operations)
    }
    return data


def CreateContext(data):
  """Creates a Context instance from raw data supplied by the server.

Args:
data: Raw data decoded from JSON sent by the server.

Returns:
A Context instance for this session.
"""
  context = _ContextImpl()
  for raw_blip_data in data['blips'].values():
    context.AddBlip(raw_blip_data)

  # Currently only one wavelet is sent.
  context.AddWavelet(data['wavelet'])

  # Waves are not sent over the wire, but we can build the list based on the
  # wave ids of the wavelets.
  wave_wavelet_map = {}
  wavelets = context.GetWavelets()
  for wavelet in wavelets:
    wave_id = wavelet.GetWaveId()
    wavelet_id = wavelet.GetId()
    if wave_id not in wave_wavelet_map:
      wave_wavelet_map[wave_id] = []
    wave_wavelet_map[wave_id].append(wavelet_id)

  for wave_id, wavelet_ids in wave_wavelet_map.iteritems():
    wave_data = {
        'waveId': wave_id,
        'waveletIds': wavelet_ids,
    }
    context.AddWave(wave_data)

  return context


class BlipData(dict):
  """Temporary class for storing ephemeral blip data.

This should be removed once the Java API no longer requires javaClass
objects, at which point, this method should just return a dict.
"""
  java_class = 'com.google.wave.api.impl.BlipData'

  def __init__(self, wave_id, wavelet_id, blip_id):
    super(BlipData, self).__init__()
    self.waveId = wave_id
    self.waveletId = wavelet_id
    self.blipId = blip_id
    self['waveId'] = wave_id
    self['waveletId'] = wavelet_id
    self['blipId'] = blip_id


class WaveletData(dict):
  """Temporary class for storing ephemeral blip data.

This should be removed once the Java API no longer requires javaClass
objects, at which point, this method should just return a dict.
"""
  java_class = 'com.google.wave.api.impl.WaveletData'

  def __init__(self, wave_id, wavelet_id, participants):
    super(WaveletData, self).__init__()
    self.waveId = wave_id
    self.waveletId = wavelet_id
    self.participants = participants
    self['waveId'] = wave_id
    self['waveletId'] = wavelet_id
    self['participants'] = participants

  def SetRootBlipId(self, blip_id):
    self['rootBlipId'] = blip_id
    self.rootBlipId = blip_id


class OpBuilder(object):
  """Wraps all currently supportable operations as functions.

The operation builder wraps single operations as functions and generates
operations in-order on its context. This should only be used when the context
is not available on a specific entity. For example, to modify a blip that
does not exist in the current context, you might specify the wave, wavelet
and blip id to generate an operation.

Any calls to this will not reflect the local context state in any way.
For example, calling WaveletAppendBlip will not result in a new blip
being added to the local context, only an operation to be applied on the
server.
"""

  def __init__(self, context):
    """Initializes the op builder with the context.

Args:
context: A Context instance to generate operations on.
"""
    self.__context = context
    self.__nextBlipId = 1
    self.__nextWaveId = 1

  def __CreateNewBlipData(self, wave_id, wavelet_id):
    """Creates JSON of the blip used for this session."""
    temp_blip_id = 'TBD_' + wavelet_id + '_' + str(self.__nextBlipId)
    self.__nextBlipId += 1
    return BlipData(wave_id, wavelet_id, temp_blip_id)

  def __CreateNewWaveletData(self, participants):
    """Creates an ephemeral BlipData instance used for this session."""
    wave_id = 'TBD_' + str(self.__nextWaveId)
    self.__nextWaveId += 1
    wavelet_id = "conv+root"
    participants = set(participants)
    return WaveletData(wave_id, wavelet_id, participants)

  def AddNewOperation(self, op_type, wave_id, wavelet_id, blip_id='', index=-1,
                      prop=None):
    """Creates and adds a new operation to the operation list."""
    self.__context.AddOperation(
        Operation(op_type, wave_id, wavelet_id,
                  blip_id=blip_id,
                  index=index,
                  prop=prop))

  def WaveletAppendBlip(self, wave_id, wavelet_id):
    """Requests to append a blip to a wavelet.

Args:
wave_id: The wave id owning the containing wavelet.
wavelet_id: The wavelet id that this blip should be appended to.

Returns:
JSON representing the id information of the new blip.
"""
    blip_data = self.__CreateNewBlipData(wave_id, wavelet_id)
    self.AddNewOperation(WAVELET_APPEND_BLIP, wave_id, wavelet_id,
                         prop=blip_data)
    return blip_data

  def WaveletAddParticipant(self, wave_id, wavelet_id, participant_id):
    """Requests to add a participant to a wavelet.

Args:
wave_id: The wave id owning that this operation is applied to.
wavelet_id: The wavelet id that this operation is applied to.
participant_id: Id of the participant to add.
"""
    self.AddNewOperation(WAVELET_ADD_PARTICIPANT, wave_id, wavelet_id,
                         prop=participant_id)

  def WaveletCreate(self, wave_id, wavelet_id, participants=None):
    """Requests to create a wavelet in a wave.

Not yet implemented.

Args:
participants: initial participants on this wavelet or None if none

"""
    if participants is None:
      participants = []
    wavelet_data = self.__CreateNewWaveletData(participants)
    blip_data = self.__CreateNewBlipData(
        wavelet_data.waveId, wavelet_data.waveletId)
    self.__context.AddBlip(blip_data)
    wavelet_data.SetRootBlipId(blip_data.blipId)
    logging.info('rootblip=' + blip_data.blipId)
    wavelet = self.__context.AddWavelet(wavelet_data)
    op = Operation(WAVELET_CREATE, wave_id, wavelet_id, prop=wavelet_data)
    self.__context.AddOperation(op)
    return wavelet

  def WaveletRemoveSelf(self, wave_id, wavelet_id):
    """Requests to remove this robot from a wavelet.

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.

Raises:
NotImplementedError: Function not yet implemented.
"""
    raise NotImplementedError()

  def WaveletSetDataDoc(self, wave_id, wavelet_id, name, data):
    """Requests set a key/value pair on the data document of a wavelet.

Args:
wave_id: The wave id owning that this operation is applied to.
wavelet_id: The wavelet id that this operation is applied to.
name: The key name for this data.
data: The value of the data to set.
"""
    self.AddNewOperation(WAVELET_DATADOC_SET, wave_id, wavelet_id,
                         blip_id=name, prop=data)

  def WaveletSetTitle(self, wave_id, wavelet_id, title):
    """Requests to set the title of a wavelet.

Args:
wave_id: The wave id owning that this operation is applied to.
wavelet_id: The wavelet id that this operation is applied to.
title: The title to set.
"""
    self.AddNewOperation(WAVELET_SET_TITLE, wave_id, wavelet_id,
                         prop=title)

  def BlipCreateChild(self, wave_id, wavelet_id, blip_id):
    """Requests to create a child blip of another blip.

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.

Returns:
JSON of blip for which further operations can be applied.
"""
    blip_data = self.__CreateNewBlipData(wave_id, wavelet_id)
    self.AddNewOperation(BLIP_CREATE_CHILD, wave_id, wavelet_id,
                         blip_id=blip_id,
                         prop=blip_data)
    return blip_data

  def BlipDelete(self, wave_id, wavelet_id, blip_id):
    """Requests to delete (tombstone) a blip.

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.
"""
    self.AddNewOperation(BLIP_DELETE, wave_id, wavelet_id, blip_id=blip_id)

  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 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 DocumentAnnotationSetNoRange(self, wave_id, wavelet_id, blip_id,
                                   name, value):
    """Requests to set an annotation on an entire document.

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.
name: Annotation key name to clear.
value: The value of the annotation.
"""
    annotation = document.Annotation(name, value, None)
    self.AddNewOperation(DOCUMENT_ANNOTATION_SET_NORANGE, wave_id, wavelet_id,
                         blip_id=blip_id,
                         prop=annotation)

  def DocumentAppend(self, wave_id, wavelet_id, blip_id, content):
    """Requests to append content to a document.

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.
content: The content to append.
"""
    self.AddNewOperation(DOCUMENT_APPEND, wave_id, wavelet_id,
                         blip_id=blip_id,
                         prop=content)

  def DocumentAppendMarkup(self, wave_id, wavelet_id, blip_id, content):
    """Requests to append content with markup to a document.

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.
content: The markup content to append.
"""
    op = Operation(DOCUMENT_APPEND_MARKUP, wave_id, wavelet_id,
                   blip_id=blip_id,
                   prop=content)
    self.__context.AddOperation(op)

  def DocumentAppendStyledText(self, wave_id, wavelet_id, blip_id, text, style):
    """Requests to append styled text to the document.

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.
text: The text ot append..
style: The style to apply.

Raises:
NotImplementedError: Function not yet implemented.
"""
    raise NotImplementedError()

  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 DocumentInsert(self, wave_id, wavelet_id, blip_id, content, index=0):
    """Requests to insert content into a document at a specific location.

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.
content: The content to insert.
index: The position insert the content at in ths document.
"""
    self.AddNewOperation(DOCUMENT_INSERT, wave_id, wavelet_id, blip_id,
                         index=index, prop=content)

  def DocumentReplace(self, wave_id, wavelet_id, blip_id, content):
    """Requests to replace all content in a document.

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.
content: Content that will replace the current document.
"""
    self.AddNewOperation(DOCUMENT_REPLACE, wave_id, wavelet_id, blip_id,
                         prop=content)

  def DocumentElementAppend(self, wave_id, wavelet_id, blip_id, element):
    """Requests to append an element to the document.

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.
element: Element instance to append.
"""
    self.AddNewOperation(DOCUMENT_ELEMENT_APPEND, wave_id, wavelet_id, blip_id,
                         prop=element)

  def DocumentElementDelete(self, wave_id, wavelet_id, blip_id, position):
    """Requests to delete an element from the document at a specific position.

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.
position: Position of the element to delete.
"""
    self.AddNewOperation(DOCUMENT_ELEMENT_DELETE, wave_id, wavelet_id, blip_id,
                         index=position)

  def DocumentElementInsert(self, wave_id, wavelet_id, blip_id, position,
                            element):
    """Requests to insert an element to the document at a specific position.

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.
position: Position of the element to delete.
element: Element instance to insert.
"""
    self.AddNewOperation(DOCUMENT_ELEMENT_INSERT, wave_id, wavelet_id, blip_id,
                         index=position,
                         prop=element)

  def DocumentElementInsertAfter(self):
    """Requests to insert an element after the specified location.

Not yet implemented.

Raises:
NotImplementedError: Function not yet implemented.
"""
    raise NotImplementedError()

  def DocumentElementInsertBefore(self):
    """Requests to insert an element before the specified location.

Not yet implemented.

Raises:
NotImplementedError: Function not yet implemented.
"""
    raise NotImplementedError()

  def DocumentElementReplace(self, wave_id, wavelet_id, blip_id, position,
                             element):
    """Requests to replace an element.

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.
position: Position of the element to replace.
element: Element instance to replace.
"""
    self.AddNewOperation(DOCUMENT_ELEMENT_REPLACE, wave_id, wavelet_id, blip_id,
                         index=position,
                         prop=element)

  def DocumentModifyAttributes(self, wave_id, wavelet_id, blip_id,
                               element):
    """Modifies the attributes of an element.

This is done by passing the a new element that is matched against
existing elements and the attributes are copied without the element
actually being deleted and reinserted. This is especially useful for
gadgets.

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.
element: Element instance to take the attributes from an to
match.
"""
    self.AddNewOperation(DOCUMENT_ELEMENT_MODIFY_ATTRS, wave_id, wavelet_id, blip_id,
                         index=-1,
                         prop=element)

  def DocumentInlineBlipAppend(self, wave_id, wavelet_id, blip_id):
    """Requests to create and append a new inline blip to another blip.

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.

Returns:
JSON of blip containing the id information.
"""
    inline_blip_data = self.__CreateNewBlipData(wave_id, wavelet_id)
    self.AddNewOperation(DOCUMENT_INLINE_BLIP_APPEND, wave_id, wavelet_id,
                         blip_id=blip_id,
                         prop=inline_blip_data)
    inline_blip_data['parentBlipId'] = blip_id
    return inline_blip_data

  def DocumentInlineBlipDelete(self, wave_id, wavelet_id, blip_id,
                               inline_blip_id):
    """Requests to delete an inline blip from its parent.

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.
inline_blip_id: The blip to be deleted.
"""
    self.AddNewOperation(DOCUMENT_INLINE_BLIP_DELETE, wave_id, wavelet_id,
                         blip_id=blip_id,
                         prop=inline_blip_id)

  def DocumentInlineBlipInsert(self, wave_id, wavelet_id, blip_id, position):
    """Requests to insert an inline blip at a specific location.

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.
position: The position in the document to insert the blip.

Returns:
JSON data for the blip that was created for further operations.
"""
    inline_blip_data = self.__CreateNewBlipData(wave_id, wavelet_id)
    inline_blip_data['parentBlipId'] = blip_id
    self.AddNewOperation(DOCUMENT_INLINE_BLIP_INSERT, wave_id, wavelet_id,
                         blip_id=blip_id,
                         index=position,
                         prop=inline_blip_data)
    return inline_blip_data

  def DocumentInlineBlipInsertAfterElement(self):
    """Requests to insert an inline blip after an element.

Raises:
NotImplementedError: Function not yet implemented.
"""
    raise NotImplementedError()
Something went wrong with that request. Please try again.