Skip to content

Commit

Permalink
Rename CarddavObject class and module
Browse files Browse the repository at this point in the history
Khard is a vcards based address book and does not directly interact with
the carddav protocol.  The name was long and misleading.
  • Loading branch information
lucc committed Dec 20, 2024
1 parent 799083f commit c6a2575
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 134 deletions.
12 changes: 6 additions & 6 deletions khard/address_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import vobject.base

from . import carddav_object
from . import contacts
from .exceptions import AddressBookParseError
from .query import AnyQuery, Query

Expand All @@ -24,9 +24,9 @@ class AddressBook(metaclass=abc.ABCMeta):
def __init__(self, name: str) -> None:
""":param name: the name to identify the address book"""
self._loaded = False
self.contacts: dict[str, "carddav_object.CarddavObject"] = {}
self.contacts: dict[str, "contacts.Contact"] = {}
self._short_uids: Optional[dict[str,
"carddav_object.CarddavObject"]] = None
"contacts.Contact"]] = None
self.name = name

def __str__(self) -> str:
Expand All @@ -49,7 +49,7 @@ def _compare_uids(uid1: str, uid2: str) -> int:
"""
return len(os.path.commonprefix((uid1, uid2)))

def search(self, query: Query) -> Generator["carddav_object.CarddavObject",
def search(self, query: Query) -> Generator["contacts.Contact",
None, None]:
"""Search this address book for contacts matching the query.
Expand All @@ -66,7 +66,7 @@ def search(self, query: Query) -> Generator["carddav_object.CarddavObject",
yield contact

def get_short_uid_dict(self, query: Query = AnyQuery()) -> dict[
str, "carddav_object.CarddavObject"]:
str, "contacts.Contact"]:
"""Create a dictionary of shortened UIDs for all contacts.
All arguments are only used if the address book is not yet initialized
Expand Down Expand Up @@ -173,7 +173,7 @@ def load(self, query: Query = AnyQuery(),
errors = 0
for filename in glob.glob(os.path.join(self.path, "*.vcf")):
try:
card = carddav_object.CarddavObject.from_file(
card = contacts.Contact.from_file(
self, filename,
query if search_in_source_files else AnyQuery(),
self._private_objects, self._localize_dates)
Expand Down
6 changes: 3 additions & 3 deletions khard/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .actions import Actions
from .exceptions import ConfigError
from .carddav_object import CarddavObject
from .contacts import Contact
from .config import Config
from .query import AndQuery, AnyQuery, parse
from .version import version as khard_version
Expand Down Expand Up @@ -62,7 +62,7 @@ def create_parsers() -> tuple[argparse.ArgumentParser,
# Create the base argument parser. It will be reused for the first and
# second round of argument parsing.
base = argparse.ArgumentParser(
description="Khard is a carddav address book for the console",
description="Khard is a vcard address book for the console",
formatter_class=argparse.RawTextHelpFormatter, add_help=False)
base.add_argument("-c", "--config", help="config file to use")
base.add_argument("--debug", action="store_true",
Expand Down Expand Up @@ -188,7 +188,7 @@ def create_parsers() -> tuple[argparse.ArgumentParser,
help="Machine readable format: uid\\tcontact_name\\taddress_book_name")
field_argument = FieldsArgument(
'index', 'name', 'phone', 'email', 'address_book',
*CarddavObject.get_properties(), nested=True)
*Contact.get_properties(), nested=True)
list_parser.add_argument(
"-F", "--fields", default=[], type=field_argument,
help="Comma separated list of fields to show "
Expand Down
24 changes: 12 additions & 12 deletions khard/carddav_object.py → khard/contacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ def to_yaml(self) -> str:
return stream.getvalue() + "\n"


class CarddavObject(YAMLEditable):
class Contact(YAMLEditable):

def __init__(self, vcard: vobject.base.Component,
address_book: "address_book.VdirAddressBook", filename: str,
Expand Down Expand Up @@ -1364,8 +1364,8 @@ def __init__(self, vcard: vobject.base.Component,
def new(cls, address_book: "address_book.VdirAddressBook",
supported_private_objects: Optional[list[str]] = None,
version: Optional[str] = None, localize_dates: bool = False
) -> "CarddavObject":
"""Create a new CarddavObject from scratch"""
) -> "Contact":
"""Create a new Contact from scratch"""
vcard = vobject.vCard()
uid = helpers.get_random_uid()
filename = os.path.join(address_book.path, uid + ".vcf")
Expand All @@ -1378,8 +1378,8 @@ def new(cls, address_book: "address_book.VdirAddressBook",
def from_file(cls, address_book: "address_book.VdirAddressBook",
filename: str, query: Query = AnyQuery(),
supported_private_objects: Optional[list[str]] = None,
localize_dates: bool = False) -> Optional["CarddavObject"]:
"""Load a CarddavObject object from a .vcf file if the plain file
localize_dates: bool = False) -> Optional["Contact"]:
"""Load a Contact object from a .vcf file if the plain file
matches the query.
:param address_book: the address book where this contact is stored
Expand All @@ -1391,7 +1391,7 @@ def from_file(cls, address_book: "address_book.VdirAddressBook",
object
:param localize_dates: should the formatted output of anniversary
and birthday be localized or should the iso format be used instead
:returns: the loaded CarddavObject or None if the file didn't match
:returns: the loaded Contact or None if the file didn't match
"""
with open(filename, "r") as file:
contents = file.read()
Expand All @@ -1411,17 +1411,17 @@ def from_file(cls, address_book: "address_book.VdirAddressBook",
def from_yaml(cls, address_book: "address_book.VdirAddressBook", yaml: str,
supported_private_objects: Optional[list[str]] = None,
version: Optional[str] = None, localize_dates: bool = False
) -> "CarddavObject":
) -> "Contact":
"""Use this if you want to create a new contact from user input."""
contact = cls.new(address_book, supported_private_objects, version,
localize_dates=localize_dates)
contact.update(yaml)
return contact

@classmethod
def clone_with_yaml_update(cls, contact: "CarddavObject", yaml: str,
def clone_with_yaml_update(cls, contact: "Contact", yaml: str,
localize_dates: bool = False
) -> "CarddavObject":
) -> "Contact":
"""
Use this if you want to clone an existing contact and replace its data
with new user input in one step.
Expand All @@ -1439,7 +1439,7 @@ def clone_with_yaml_update(cls, contact: "CarddavObject", yaml: str,
######################################

def __eq__(self, other: object) -> bool:
return isinstance(other, CarddavObject) and \
return isinstance(other, Contact) and \
self.pretty(False) == other.pretty(False)

def __ne__(self, other: object) -> bool:
Expand Down Expand Up @@ -1566,5 +1566,5 @@ def delete_vcard_file(self) -> None:
@classmethod
def get_properties(cls) -> list[str]:
"""Return the property names that are defined on this class."""
return [name for name in dir(CarddavObject)
if isinstance(getattr(CarddavObject, name), property)]
return [name for name in dir(Contact)
if isinstance(getattr(Contact, name), property)]
8 changes: 4 additions & 4 deletions khard/formatter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Formatting and sorting of contacts"""

from .carddav_object import CarddavObject
from .contacts import Contact


class Formatter:

"""A formatter for CarddavObject.
"""A formatter for Contact.
It receives some settings on initialisation which influence the formatting
of the contact.
Expand Down Expand Up @@ -48,7 +48,7 @@ def format_labeled_field(field: dict[str, list[str]], preferred: list[str]
first_key = sorted(keys, key=lambda k: k.lower())[0]
return "{}: {}".format(first_key, sorted(field.get(first_key, []))[0])

def get_special_field(self, vcard: CarddavObject, field: str) -> str:
def get_special_field(self, vcard: Contact, field: str) -> str:
"""Returns certain fields with specific formatting options
(for support of some list command options)."""
if field == 'name':
Expand All @@ -74,7 +74,7 @@ def get_special_field(self, vcard: CarddavObject, field: str) -> str:
return ""

@staticmethod
def get_nested_field(vcard: CarddavObject, field: str) -> str:
def get_nested_field(vcard: Contact, field: str) -> str:
"""Returns the value of a nested field from a string
get_nested_field(vcard,'emails.home.1') is equivalent to
Expand Down
10 changes: 5 additions & 5 deletions khard/helpers/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from typing import Callable, Generator, Optional, Sequence, TypeVar, Union

from ..exceptions import Cancelled
from ..carddav_object import CarddavObject
from ..contacts import Contact


T = TypeVar("T")
Expand Down Expand Up @@ -162,16 +162,16 @@ def edit_files(self, file1: str, file2: Optional[str] = None) -> EditState:
return EditState.unmodified
return EditState.modified

def edit_templates(self, yaml2card: Callable[[str], CarddavObject],
def edit_templates(self, yaml2card: Callable[[str], Contact],
template1: str, template2: Optional[str] = None
) -> Optional[CarddavObject]:
) -> Optional[Contact]:
"""Edit YAML templates of contacts and parse them back
:param yaml2card: a function to convert the modified YAML templates
into a CarddavObject
into a Contact
:param template1: the first template
:param template2: the second template (optional, for merges)
:returns: the parsed CarddavObject or None
:returns: the parsed Contact or None
"""
templates = [t for t in (template1, template2) if t is not None]
with contextlib.ExitStack() as stack:
Expand Down
Loading

0 comments on commit c6a2575

Please sign in to comment.