:py:mod:`khard.carddav_object`
==============================

.. py:module:: khard.carddav_object

.. autoapi-nested-parse::

   Classes and logic to handle vCards in khard.

   This module explicitly supports the vCard specifications version 3.0 and 4.0
   which can be found here:
   - version 3.0: https://tools.ietf.org/html/rfc2426
   - version 4.0: https://tools.ietf.org/html/rfc6350



Module Contents
---------------

Classes
~~~~~~~

.. autoapisummary::

   khard.carddav_object.VCardWrapper
   khard.carddav_object.YAMLEditable
   khard.carddav_object.CarddavObject



Functions
~~~~~~~~~

.. autoapisummary::

   khard.carddav_object.multi_property_key



Attributes
~~~~~~~~~~

.. autoapisummary::

   khard.carddav_object.logger


.. py:data:: logger
   

   

.. py:function:: multi_property_key(item: Union[str, Dict]) -> List

   key function to pass to sorted(), allowing sorting of dicts with lists
   and strings. Dicts will be sorted by their label, after other types.

   :param item: member of the list being sorted
   :type item: a dict with a single entry or any sortable type
   :returns: a list with two members. The first is int(isinstance(item, dict).
       The second is either the key from the dict or the unchanged item if it
       is not a dict.
   :rtype: list(int, type(item)) or list(int, str)


.. py:class:: VCardWrapper(vcard: vobject.vCard, version: Optional[str] = None)

   Wrapper class around a vobject.vCard object.

   This class can wrap a single vCard and presents its data in a manner
   suitable for khard.  Additionally some details of the vCard specifications
   in RFC 2426 (version 3.0) and RFC 6350 (version 4.0) that are not enforced
   by the vobject library are enforced here.

   .. py:property:: version
      :type: str


   .. py:property:: uid
      :type: str


   .. py:property:: birthday
      :type: Optional[khard.helpers.typing.Date]

      Return the birthday as a datetime object or a string depending on
      whether it is of type text or not.  If no birthday is present in the
      vcard None is returned.

      :returns: contacts birthday or None if not available


   .. py:property:: anniversary
      :type: Optional[khard.helpers.typing.Date]

      :returns: contacts anniversary or None if not available


   .. py:property:: kind
      :type: str


   .. py:property:: formatted_name
      :type: str


   .. py:property:: first_name
      :type: str


   .. py:property:: last_name
      :type: str


   .. py:property:: organisations
      :type: List[Union[List[str], Dict[str, List[str]]]]

      :returns: list of organisations, sorted alphabetically


   .. py:property:: titles
      :type: List[Union[str, Dict[str, str]]]


   .. py:property:: roles
      :type: List[Union[str, Dict[str, str]]]


   .. py:property:: nicknames
      :type: List[Union[str, Dict[str, str]]]


   .. py:property:: notes
      :type: List[Union[str, Dict[str, str]]]


   .. py:property:: webpages
      :type: List[Union[str, Dict[str, str]]]


   .. py:property:: categories
      :type: Union[List[str], List[List[str]]]


   .. py:property:: phone_numbers
      :type: Dict[str, List[str]]

      :returns: dict of type and phone number list


   .. py:property:: emails
      :type: Dict[str, List[str]]

      :returns: dict of type and email address list


   .. py:property:: post_addresses
      :type: Dict[str, List[Dict[str, Union[List, str]]]]

      :returns: dict of type and post address list


   .. py:attribute:: _default_kind
      :annotation: = individual

      

   .. py:attribute:: _default_version
      :annotation: = 3.0

      

   .. py:attribute:: _supported_versions
      :annotation: = ['3.0', '4.0']

      

   .. py:attribute:: phone_types_v3
      :annotation: = ['bbs', 'car', 'cell', 'fax', 'home', 'isdn', 'msg', 'modem', 'pager', 'pcs', 'video', 'voice', 'work']

      

   .. py:attribute:: email_types_v3
      :annotation: = ['home', 'internet', 'work', 'x400']

      

   .. py:attribute:: address_types_v3
      :annotation: = ['dom', 'intl', 'home', 'parcel', 'postal', 'work']

      

   .. py:attribute:: phone_types_v4
      :annotation: = ['text', 'voice', 'fax', 'cell', 'video', 'pager', 'textphone', 'home', 'work']

      

   .. py:attribute:: email_types_v4
      :annotation: = ['home', 'internet', 'work']

      

   .. py:attribute:: address_types_v4
      :annotation: = ['home', 'work']

      

   .. py:method:: __str__() -> str

      Return str(self).


   .. py:method:: _get_string_field(field: str) -> str

      Get a string field from the underlying vCard.

      :param field: the field value to get
      :returns: the field value or the empty string


   .. py:method:: _get_multi_property(name: str) -> List

      Get a vCard property that can exist more than once.

      It does not matter what the individual vcard properties store as their
      value.  This function returnes them untouched inside an agregating
      list.

      If the property is part of a group containing exactly two items, with
      exactly one ABLABEL. the property will be prefixed with that ABLABEL.

      :param name: the name of the property (should be UPPER case)
      :returns: the values from all occurences of the named property


   .. py:method:: _delete_vcard_object(name: str) -> None

      Delete all fields with the given name from the underlying vCard.

      If a field that will be deleted is in a group with an X-ABLABEL field,
      that X-ABLABEL field will also be deleted.  These fields are commonly
      added by the Apple address book to attach custom labels to some fields.

      :param name: the name of the fields to delete


   .. py:method:: _parse_type_value(types: Sequence[str], supported_types: Sequence[str]) -> Tuple[List[str], List[str], int]
      :staticmethod:

      Parse type value of phone numbers, email and post addresses.

      :param types: list of type values
      :param supported_types: all allowed standard types
      :returns: tuple of standard and custom types and pref integer


   .. py:method:: _get_types_for_vcard_object(object: vobject.base.ContentLine, default_type: str) -> List[str]

      get list of types for phone number, email or post address

      :param object: vcard class object
      :param default_type: use if the object contains no type
      :returns: list of type labels


   .. py:method:: _update_revision() -> None

      Generate a new REV field for the vCard, replace any existing

      All vCards should only always have one revision, this is a
      requirement for version 4 but also makes sense for all other
      versions.

      :rtype: NoneType


   .. py:method:: _get_ablabel(item: vobject.base.ContentLine) -> str

      Get an ABLABEL for a specified item in the vCard.
      Will return the ABLABEL only if the item is part of a group with exactly
      two items, exactly one of which is an ABLABEL.

      :param item: the item to be labelled
      :returns: the ABLABEL in the circumstances above or an empty string


   .. py:method:: _get_new_group(group_type: str = '') -> str

      Get an unused group name for adding new groups. Uses the form item123
       or itemgroup_type123 if a grouptype is specified.

      :param group_type: (Optional) a string to add between "item" and
          the number
      :returns: the name of the first unused group of the specified form


   .. py:method:: _add_labelled_object(obj_type: str, user_input, name_groups: bool = False, allowed_object_type: khard.helpers.typing.ObjectType = ObjectType.str) -> None

      Add an object to the VCARD. If user_input is a dict, the object will
       be added to a group with an ABLABEL created from the key of the dict.

      :param obj_type: type of object to add to the VCARD.
      :param user_input: Contents of the object to add. If a dict
      :type user_input: str or list(str) or dict(str) or dict(list(str))
      :param name_groups: (Optional) If True, use the obj_type in the
          group name for labelled objects.
      :param allowed_object_type: (Optional) set the accepted return type
          for vcard attribute


   .. py:method:: _prepare_birthday_value(date: khard.helpers.typing.Date) -> Tuple[Optional[str], bool]

      Prepare a value to be stored in a BDAY or ANNIVERSARY attribute.

      :param date: the date like value to be stored
      :returns: the object to set as the .value for the attribute and whether
          it should be stored as plain text
      :rtype: tuple(str,bool)


   .. py:method:: _get_names_part(part: str) -> List[str]

      Get some part of the "N" entry in the vCard as a list

      :param part: the name to get e.g. "prefix" or "given"
      :returns: a list of entries for this name part


   .. py:method:: _get_name_prefixes() -> List[str]


   .. py:method:: _get_first_names() -> List[str]


   .. py:method:: _get_additional_names() -> List[str]


   .. py:method:: _get_last_names() -> List[str]


   .. py:method:: _get_name_suffixes() -> List[str]


   .. py:method:: get_first_name_last_name() -> str

      Compute the full name of the contact by joining first, additional
      and last names together


   .. py:method:: get_last_name_first_name() -> str

      Compute the full name of the contact by joining the last names and
      then after a comma the first and additional names together


   .. py:method:: _add_name(prefix: khard.helpers.typing.StrList, first_name: khard.helpers.typing.StrList, additional_name: khard.helpers.typing.StrList, last_name: khard.helpers.typing.StrList, suffix: khard.helpers.typing.StrList) -> None

      Add an N entry to the vCard. No old entries are affected.

      :param prefix:
      :param first_name:
      :param additional_name:
      :param last_name:
      :param suffix:


   .. py:method:: _add_organisation(organisation: khard.helpers.typing.StrList) -> None

      Add one ORG entry to the underlying vcard

      :param organisation: the value to add


   .. py:method:: _add_title(title) -> None


   .. py:method:: _add_role(role) -> None


   .. py:method:: _add_nickname(nickname) -> None


   .. py:method:: _add_note(note) -> None


   .. py:method:: _add_webpage(webpage) -> None


   .. py:method:: _add_category(categories: List[str]) -> None

      Add categories to the vCard

      :param categories:


   .. py:method:: _add_phone_number(type: str, number: str) -> None


   .. py:method:: add_email(type: str, address: str) -> None


   .. py:method:: get_formatted_post_addresses() -> Dict[str, List[str]]


   .. py:method:: _add_post_address(type, box, extended, street, code, city, region, country)



.. py:class:: YAMLEditable(vcard: vobject.vCard, supported_private_objects: Optional[List[str]] = None, version: Optional[str] = None, localize_dates: bool = False)

   Bases: :py:obj:`VCardWrapper`

   Conversion of vcards to YAML and updateing the vcard from YAML

   .. py:method:: _get_private_objects() -> Dict[str, List[str]]


   .. py:method:: _add_private_object(key: str, value) -> None


   .. py:method:: get_formatted_anniversary() -> str


   .. py:method:: get_formatted_birthday() -> str


   .. py:method:: _format_date_object(date: Optional[khard.helpers.typing.Date], localize: bool) -> str
      :staticmethod:


   .. py:method:: _filter_invalid_tags(contents: str) -> str
      :staticmethod:


   .. py:method:: _parse_yaml(input: str) -> Dict
      :staticmethod:

      Parse a YAML document into a dictinary and validate the data to some
      degree.

      :param str input: the YAML document to parse
      :returns: the parsed datastructure
      :rtype: dict


   .. py:method:: _set_string_list(setter: Callable[[Union[str, List]], None], key: str, data: Dict) -> None
      :staticmethod:

      Prepocess a string or list and set each value with the given setter

      :param setter: the setter method to add a value to a card
      :param key:
      :param data:


   .. py:method:: _set_date(target: str, key: str, data: Dict) -> None


   .. py:method:: update(input: str) -> None

      Update this vcard with some yaml input

      :param input: a yaml string to parse and then use to update self


   .. py:method:: to_yaml() -> str

      Convert this contact to a YAML string

      The conversion follows the implicit schema that is given by the
      internal YAML template of khard.

      :returns: a YAML representation of this contact



.. py:class:: CarddavObject(vcard: vobject.vCard, address_book: CarddavObject.__init__.address_book, filename: str, supported_private_objects: Optional[List[str]] = None, vcard_version: Optional[str] = None, localize_dates: bool = False)

   Bases: :py:obj:`YAMLEditable`

   Conversion of vcards to YAML and updateing the vcard from YAML

   .. py:method:: new(address_book: CarddavObject.new.address_book, supported_private_objects: Optional[List[str]] = None, version: Optional[str] = None, localize_dates: bool = False) -> CarddavObject
      :classmethod:

      Create a new CarddavObject from scratch


   .. py:method:: from_file(address_book: CarddavObject.from_file.address_book, filename: str, query: khard.query.Query = AnyQuery(), supported_private_objects: Optional[List[str]] = None, localize_dates: bool = False) -> Optional[CarddavObject]
      :classmethod:

      Load a CarddavObject object from a .vcf file if the plain file
      matches the query.

      :param address_book: the address book where this contact is stored
      :param filename: the file name of the .vcf file
      :param query: the query to search in the source file or None to load
          the file unconditionally
      :param supported_private_objects: the list of private property names
          that will be loaded from the actual vcard and represented in this
          pobject
      :param localize_dates: should the formatted output of anniversary
          and birthday be localized or should the isoformat be used instead
      :returns: the loaded CarddavObject or None if the file didn't match


   .. py:method:: from_yaml(address_book: CarddavObject.from_yaml.address_book, yaml: str, supported_private_objects: Optional[List[str]] = None, version: Optional[str] = None, localize_dates: bool = False) -> CarddavObject
      :classmethod:

      Use this if you want to create a new contact from user input.


   .. py:method:: clone_with_yaml_update(contact: CarddavObject, yaml: str, localize_dates: bool = False) -> CarddavObject
      :classmethod:

      Use this if you want to clone an existing contact and replace its data
      with new user input in one step.


   .. py:method:: __eq__(other: object) -> bool

      Return self==value.


   .. py:method:: __ne__(other: object) -> bool

      Return self!=value.


   .. py:method:: pretty(verbose: bool = True) -> str


   .. py:method:: write_to_file(overwrite: bool = False) -> None


   .. py:method:: delete_vcard_file() -> None


   .. py:method:: get_properties() -> List[str]
      :classmethod:

      Return the property names that are defined on this class.



