forked from CRImier/pyLCI
-
Notifications
You must be signed in to change notification settings - Fork 23
WIP: import contacts from remote CardDAV server #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Fnux
wants to merge
12
commits into
ZeroPhone:devel
Choose a base branch
from
Fnux:contacts-sync
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6cac24b
Contacts: add basic sync support with vdirsyncer, split AddressBook a…
Fnux 81c73c5
Contacts: allow to reset the address book, rename a few menu items
Fnux 89d091e
Contacts: avoid duplicated entries after CardDAV import
Fnux 1a8557d
Contacts: small refactor in VCard importation
Fnux a9af3b5
Address Book: remove useless app name from log messages
Fnux 7844b30
Contacts App: add vdirsyncer CardDAV remote setup wizard
Fnux bb19598
Contacts App: minor refactoring following initial review of PR #134
Fnux 6a40010
Contacts App: move reusable code to /libs
Fnux ffb8db9
Add 'paths' helper module for consistant cache, config and data dirs
Fnux ce69110
Add minimal documentation to the vdirsycner module
Fnux 123c48b
Add minimal documentation to the address_book module
Fnux a180045
Minor refactor (move a few methods around) of the address_book/contac…
Fnux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import os | ||
| import pickle | ||
|
|
||
| from helpers import Singleton, flatten | ||
| from helpers import setup_logger | ||
|
|
||
| logger = setup_logger(__name__, "warning") | ||
|
|
||
| class Contact(object): | ||
| """ | ||
| >>> c = Contact() | ||
| >>> c.name | ||
| [] | ||
| >>> c = Contact(name="John") | ||
| >>> c.name | ||
| ['John'] | ||
| """ | ||
|
|
||
| def __init__(self, **kwargs): | ||
| self.name = [] | ||
| self.address = [] | ||
| self.telephone = [] | ||
| self.email = [] | ||
| self.url = [] | ||
| self.note = [] | ||
| self.org = [] | ||
| self.photo = [] | ||
| self.title = [] | ||
| self.from_kwargs(kwargs) | ||
|
|
||
| def __eq__(self, other): | ||
| return self.__dict__ == other.__dict__ | ||
|
|
||
| def __str__(self): | ||
| return str(self.__dict__) | ||
|
|
||
| def from_kwargs(self, kwargs): | ||
| provided_attrs = {attr: kwargs[attr] for attr in self.get_all_attributes() if attr in kwargs.keys()} | ||
| for attr_name in provided_attrs: | ||
|
Fnux marked this conversation as resolved.
Outdated
|
||
| attr_value = provided_attrs[attr_name] | ||
|
Fnux marked this conversation as resolved.
Outdated
|
||
| if isinstance(attr_value, list): | ||
| setattr(self, attr_name, attr_value) | ||
| else: | ||
| setattr(self, attr_name, [attr_value]) | ||
|
|
||
| def match_score(self, other): | ||
| # type: (Contact) -> int | ||
| """ | ||
| Computes how many element matches with other and self | ||
| >>> c1 = Contact(name="John", telephone="911") | ||
| >>> c2 = Contact(name="Johnny") | ||
| >>> c1.match_score(c2) | ||
| 0 | ||
| >>> c2.telephone = ["123", "911"] # now the contacts have 911 in common | ||
| >>> c1.match_score(c2) | ||
| 1 | ||
|
|
||
| Now add a common nickname to them, ignoring case | ||
| >>> c1.name.append("deepthroat") | ||
| >>> c2.name.append("DeepThroat") | ||
| >>> c1.match_score(c2) | ||
| 2 | ||
| """ | ||
| common_attrs = set(self.get_filled_attributes()).intersection(other.get_filled_attributes()) | ||
| return sum([self.common_attribute_count(getattr(self, attr), getattr(other, attr)) for attr in common_attrs]) | ||
|
|
||
| def consolidate(self): | ||
| """ | ||
| Merge duplicate attributes | ||
| >>> john = Contact() | ||
| >>> john.name = ['John', 'John Doe', ' John Doe', 'Darling'] | ||
| >>> john.consolidate() | ||
| >>> 'Darling' in john.name | ||
| True | ||
| >>> 'John Doe' in john.name | ||
| True | ||
| >>> len(john.name) | ||
| 2 | ||
| >>> john.org = [['whatever org']] | ||
| >>> john.consolidate() | ||
| >>> john.org | ||
| ['whatever org'] | ||
| """ | ||
| my_attributes = self.get_filled_attributes() | ||
| for name in my_attributes: # removes exact duplicates | ||
| self.consolidate_attribute(name) | ||
|
|
||
| def get_filled_attributes(self): | ||
| """ | ||
| >>> c = Contact() | ||
| >>> c.name = ["John", "Johnny"] | ||
| >>> c.note = ["That's him !"] | ||
| >>> c.get_filled_attributes() | ||
| ['name', 'note'] | ||
| """ | ||
| return [a for a in dir(self) | ||
| if not callable(getattr(self, a)) and not a.startswith("__") and len(getattr(self, a))] | ||
|
|
||
| def get_all_attributes(self): | ||
| return [a for a in dir(self) if not callable(getattr(self, a)) and not a.startswith("__")] | ||
|
|
||
| def consolidate_attribute(self, attribute_name): | ||
| # type: (str) -> None | ||
| attr_value = getattr(self, attribute_name) | ||
| attr_value = flatten(attr_value) | ||
| attr_value = list(set([i.strip() for i in attr_value if isinstance(i, basestring)])) # removes exact duplicates | ||
|
|
||
| attr_value[:] = [x for x in attr_value if not self._is_contained_in_other_element_of_the_list(x, attr_value)] | ||
|
|
||
| setattr(self, attribute_name, list(set(attr_value))) | ||
|
|
||
| def merge(self, other): | ||
| # type: (Contact) -> None | ||
| """ | ||
| >>> c1 = Contact() | ||
| >>> c1.name = ["John"] | ||
| >>> c2 = Contact() | ||
| >>> c2.name = ["John"] | ||
| >>> c2.telephone = ["911"] | ||
| >>> c1.merge(c2) | ||
| >>> c1.telephone | ||
| ['911'] | ||
| """ | ||
| attr_sum = self.get_filled_attributes() + other.get_filled_attributes() | ||
| for attr_name in attr_sum: | ||
| attrs_sum = getattr(self, attr_name) + getattr(other, attr_name) | ||
| setattr(self, attr_name, attrs_sum) | ||
| self.consolidate() | ||
|
|
||
| def short_name(self): | ||
| for attr_name in self.get_filled_attributes(): | ||
| for attribute in getattr(self, attr_name): | ||
| if not isinstance(attribute, basestring) and not isinstance(attribute, list): | ||
| continue | ||
| if isinstance(attribute, list): | ||
| for entry_str in attribute: | ||
| if not isinstance(entry_str, basestring): | ||
| continue | ||
| else: | ||
| return attribute | ||
| return "unknown" | ||
|
|
||
| @staticmethod | ||
| def common_attribute_count(a1, a2): | ||
| # type: (list, list) -> int | ||
| a1_copy = [i.lower() for i in a1 if isinstance(i, basestring)] | ||
| a2_copy = [i.lower() for i in a2 if isinstance(i, basestring)] | ||
| return len(set(a1_copy).intersection(a2_copy)) | ||
|
|
||
| @staticmethod | ||
| def _is_contained_in_other_element_of_the_list(p_element, the_list): | ||
| """ | ||
| """ | ||
| # type: (object, list) -> bool | ||
| copy = list(the_list) | ||
| copy.remove(p_element) | ||
| for element in copy: | ||
| if p_element in element: | ||
| return True | ||
| return False | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.