
    1                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKJ	r	  SSKJ
r
  SSKJr  SS	KJr   " S
 S\	R                  5      r " S S\5      r " S S\5      r " S S\5      rg)zClasses for common kinds, including Contact, Message, and Event.

Most of these kinds are based on the gd namespace "kinds" from GData:

  https://developers.google.com/gdata/docs/1.0/elements
    )absolute_import)unicode_literals)saxutils)
six_subset)	datastore)datastore_errors)datastore_types)datastore_pbc                   `    \ rS rSrSrSrSr\" 5       r\" 5       r	/ 4S jr
S rS rS rS	 rS
rg)GdKind)   zA base class for gd namespace kinds.

This class contains common logic for all gd namespace kinds. For example,
this class translates datastore (app id, kind, key) tuples to tag:
URIs appropriate for use in <key> tags.
z<entry xmlns:gd='http://schemas.google.com/g/2005'>
  <category scheme='http://schemas.google.com/g/2005#kind'
            term='http://schemas.google.com/g/2005#%s' />z	
</entry>c                 0   [         R                  R                  X5        [        U[        R
                  5      (       d3  [        R                  " SU< S[        R                  " U5      < S35      eX S'   SU S'   [        U5      U l        U R                  R                  [        U R                  5       5      5      (       a   e[        U5      U R                  -
  U l        U R                  R!                  S5        U R                  R!                  S5        g)a  Ctor.

title is the name of this particular entity, e.g. Bob Jones or Mom's
Birthday Party.

kind_properties is a list of property names that should be included in
this entity's XML encoding as first-class XML elements, instead of
<property> elements. 'title' and 'content' are added to kind_properties
automatically, and may not appear in contact_properties.

contact_properties is a list of property names that are Keys that point to
Contact entities, and should be included in this entity's XML encoding as
<gd:who> elements. If a property name is included in both kind_properties
and contact_properties, it is treated as a Contact property.

Args:
kind: string
title: string
kind_properties: list of strings
contact_properties: list of string
z&Expected a string for title; received z (a z).title contentN)r   Entity__init__
isinstancer   string_typesr   BadValueErrorr	   typenameset_contact_propertiesintersectionlistkeys_kind_propertiesadd)selfkindr   kind_propertiescontact_propertiess        6lib/googlecloudsdk/appengine/api/datastore_entities.pyr   GdKind.__init__:   s    , d)eZ4455**	((/	12 2 MDO  ##56D''44T$))+5FGGGG043K3KKDg&i(    c           
         U R                   R                  [        U R                  5       5      5      nSn[	        U5       H  n[
        R                  " U5      SS nX   n[        US5      =(       d&    [        U[        5      =(       a    [        US   S5      nU R                  U5       H&  nU(       a
  USU-  -  nM  USU< SU< S	U< S3-  nM(     M     U$ )
zConvert the properties that are part of this gd kind to XML. For
testability, the XML elements in the output are sorted alphabetically
by property name.

Returns:
string  # the XML representation of the gd kind properties
r      ToXmlr   z
  %sz
  <>z</)r   r   r   r   sortedr   	quoteattrhasattrr   r   _XmlEscapeValues)r   
propertiesxmlpropprop_xmlvalue	has_toxmlvals           r#   _KindPropertiesToXmlGdKind._KindPropertiesToXmla   s     &&33C		4DEJ
Cz"##D)!B/hje5'* JeT*IwuQx/I  &&t,# 
C
#
hX>
># - # Jr%   c           
         U R                   R                  [        U R                  5       5      5      nSn[	        U5       H  nX   n[        U[        5      (       d  U/nU H`  n[        U[        R                  5      (       d   eUSU R                  5       R                  5       < SU< SUR                  5       < S3-  nMb     M     U$ )zConvert this kind's Contact properties kind to XML. For testability,
the XML elements in the output are sorted alphabetically by property name.

Returns:
string  # the XML representation of the Contact properties
r   z1
  <gd:who rel="http://schemas.google.com/g/2005#.z>
    <gd:entryLink href="z" />
  </gd:who>)r   r   r   r   r+   r   r   r	   Keyr    lowerToTagUri)r   r/   r0   r1   valuesr3   s         r#   _ContactPropertiesToXmlGdKind._ContactPropertiesToXml~   s     ))66s499;7GHJ
Cz"zf%%%%!4!45555 ))+##%tU^^-=? 	?  # Jr%   c                     [        U R                  5       5      nXR                  -  nXR                  -  nU(       a#  SSR	                  U R                  U5      5      -   $ g)zConvert all of this entity's properties that *aren't* part of this gd
kind to XML.

Returns:
string  # the XML representation of the leftover properties

  r   )r   r   r   r   join_PropertiesToXml)r   	leftoverss     r#   _LeftoverPropertiesToXmlGdKind._LeftoverPropertiesToXml   sU     DIIK I&&&I)))Ifkk$"7"7	"BCCCr%   c                     [         R                  U R                  5       R                  5       -  nXR	                  5       -  nXR                  5       -  nXR                  5       -  nU[         R                  -  nU$ )z?Returns an XML representation of this entity, as a string.
    )r   HEADERr    r;   r6   r>   rE   FOOTERr   r0   s     r#   r)   GdKind.ToXml   sg     --$))+++-
-C$$&&C''))C((**C6==CJr%   )r   r   N)__name__
__module____qualname____firstlineno____doc__rH   rI   r   r   r   r   r6   r>   rE   r)   __static_attributes__ r%   r#   r   r   )   sD    =&& UFH %)N:2r%   r   c                   2    \ rS rSrSr/ SQr/ SQrSS jrSrg)	Message   aw  A message, such as an email, a discussion group posting, or a comment.

Includes the message title, contents, participants, and other properties.

This is the gd Message kind. See:
https://developers.google.com/gdata/docs/1.0/elements#gdMessageKind

These properties are meaningful. They are all optional.

property name  property type    meaning
-------------------------------------
title          string         message subject
content        string         message body
from           Contact*       sender
to             Contact*       primary recipient
cc             Contact*       CC recipient
bcc            Contact*       BCC recipient
reply-to       Contact*       intended recipient of replies
link           Link*          attachment
category       Category*      tag or label associated with this message
geoPt          GeoPt*         geographic location the message was posted from
rating         Rating*        message rating, as defined by the application

* means this property may be repeated.

The Contact properties should be Keys of Contact entities. They are
represented in the XML encoding as linked <gd:who> elements.
)r   r   linkcategorygeoPtrating)fromtoccbcczreply-toc                 l    [         R                  XU[        R                  [        R                  5        g N)r   r   rT   KIND_PROPERTIESCONTACT_PROPERTIESr   r   r    s      r#   r   Message.__init__   s#    
OODw'>'>..0r%   rR   N)rT   )	rL   rM   rN   rO   rP   r`   ra   r   rQ   rR   r%   r#   rT   rT      s    8 P/>0r%   rT   c                   J    \ rS rSrSr/ SQrS/r " S S5      rSS jrS r	S	r
g
)Event   a(  A calendar event.

Includes the event title, description, location, organizer, start and end
time, and other details.

This is the gd Event kind. See:
https://developers.google.com/gdata/docs/1.0/elements#gdEventKind

These properties are meaningful. They are all optional.

property name  property type    meaning
-------------------------------------
title          string         event name
content        string         event description
author         string         the organizer's name
where          string*        human-readable location (not a GeoPt)
startTime      timestamp      start time
endTime        timestamp      end time
eventStatus    string         one of the Event.Status values
link           Link*          page with more information
category       Category*      tag or label associated with this event
attendee       Contact*       attendees and other related people

* means this property may be repeated.

The Contact properties should be Keys of Contact entities. They are
represented in the XML encoding as linked <gd:who> elements.
)	r   r   authorwhere	startTimeendTimeeventStatusrV   rW   attendeec                        \ rS rSrSrSrSrSrg)Event.Status   	confirmed	tentativecanceledrR   N)rL   rM   rN   rO   	CONFIRMED	TENTATIVECANCELEDrQ   rR   r%   r#   Statusrn      s    IIHr%   rv   c                 l    [         R                  XU[        R                  [        R                  5        g r_   )r   r   re   r`   ra   rb   s      r#   r   Event.__init__   s#    
OODu'<'<,,.r%   c                    [         R                  U R                  5       R                  5       -  n[	        [
        R                  5      U l        XR                  5       -  nSU ;   a  USU S   -  -  nSU ;   a  USU S   -  -  nSU ;   a;  U R                  S5       Vs/ s H  nSU-  PM
     nnUSSR                  U5      -   -  nSnUS	-  nS
 H(  nXP;   d  M
  USU< SX   R                  5       < S3-  nM*     US-  nU R                  R                  / SQ5        XR                  5       -  nXR                  5       -  nU[         R                  -  nU$ s  snf )zWOverride GdKind.ToXml() to special-case author, gd:where, gd:when, and
gd:eventStatus.
rg   z#
  <author><name>%s</name></author>rk   zG
  <gd:eventStatus value="http://schemas.google.com/g/2005#event.%s" />rh   z<gd:where valueString="%s" />rA   z%Y-%m-%dT%H:%M:%Sz
  <gd:when)ri   rj    z=""z />)rg   rh   ri   rj   rk   )r   rH   r    r;   r   Contactr`   r   r6   r.   rB   	isoformatupdater>   rE   rI   )r   r0   r5   lines
iso_formatkeys         r#   r)   Event.ToXml   sz    --$))+++-
-C 7 78D$$&&C 4	 &(,X7 7c 	 J c
 $//8:83 /48  :	Vfkk%(((c %J>C'	S$)"5"5"788 ( 5LC   "1 2''))C((**C6==CJ#:s   Er   N)re   )rL   rM   rN   rO   rP   r`   ra   rv   r   r)   rQ   rR   r%   r#   re   re      s+    8C/"| 
.'r%   re   c                   @    \ rS rSrSrSrSr/ SQr/ SQrSS jr	S r
S	rg
)r|   i*  a  A contact: a person, a venue such as a club or a restaurant, or an
organization.

This is the gd Contact kind. See:
https://developers.google.com/gdata/docs/1.0/elements#gdContactKind

Most of the information about the contact is in the <gd:contactSection>
element; see the reference section for that element for details.

These properties are meaningful. They are all optional.

property name  property type    meaning
-------------------------------------
title          string         contact's name
content        string         notes
email          Email*         email address
geoPt          GeoPt*         geographic location
im             IM*            IM address
phoneNumber    Phonenumber*   phone number
postalAddress  PostalAddress* mailing address
link           Link*          link to more information
category       Category*      tag or label associated with this contact

* means this property may be repeated.
z
  <gd:contactSection>z
  </gd:contactSection>)r   r   rV   rW   )emailrX   imphoneNumberpostalAddressc                 N    [         R                  XU[        R                  5        g r_   )r   r   r|   r`   rb   s      r#   r   Contact.__init__P  s    
OODw'>'>?r%   c                    [         R                  U R                  5       R                  5       -  n[	        [
        R                  5      U l        XR                  5       -  nU[
        R                  -  n[	        [
        R                  5      U l        XR                  5       -  nU[
        R                  -  nU R                  R                  [
        R                  5        XR                  5       -  nU[         R                  -  nU$ )zKOverride GdKind.ToXml() to put some properties inside a
gd:contactSection.
)r   rH   r    r;   r   r|   r`   r   r6   CONTACT_SECTION_HEADERCONTACT_SECTION_PROPERTIESCONTACT_SECTION_FOOTERr~   rE   rI   rJ   s     r#   r)   Contact.ToXmlS  s     --$))+++-
-C   7 78D$$&&C 7)))C B BCD$$&&C7)))C  !8!89((**C6==CJr%   r   N)r|   )rL   rM   rN   rO   rP   r   r   r`   r   r   r)   rQ   rR   r%   r#   r|   r|   *  s1    2 =/ 1@r%   r|   N)rP   
__future__r   r   xml.saxr   "googlecloudsdk.appengine._internalr   googlecloudsdk.appengine.apir   r   r	   "googlecloudsdk.appengine.datastorer
   r   r   rT   re   r|   rR   r%   r#   <module>r      sa    ' '  9 2 9 8 ;EY EP"0f "0JQF Qh<f <r%   