Skip to content

Python3-Conversion, Switch to use argparse, Display all eMail-Addresses, Use multiple VCard-Directories, Interpret PATTERN as Regex, Remove '--starting-matches', Update Readme#2

Open
mageta wants to merge 16 commits into
marvinthepa:masterfrom
mageta:marvinthepa-pullreq-initial-v2

Conversation

@mageta

@mageta mageta commented Oct 8, 2018

Copy link
Copy Markdown

Hey Martin, this comes probably out of the blue, but I started using your script in my mutt mail-setup (together with KAddressBook to manage contacts and pull contacts from LDAP), and thought I might as well share my changes, if you are interested.

This is the second version, after I messed up some things in version 1. There is a lot in the one request. If you want them smaller, I could split them up.

I tried to summarize everything in the Subject, but the changes are better explained in the individual commits, I tried to make them as topical as possible:

  • Python3-Conversion
  • Switch to use argparse instead of getopt
  • Display all eMail-Addresses of a VCard
  • Use multiple VCard-Directories, Get rid of duplicates
  • Interpret PATTERN as Regex
  • Remove '-s'/'--starting-matches'; like i mention in some commit-notes, I feel like this option is necessary, and covered already by sorting and pattern use
  • Fix issues found by the static code checker pylint
  • Update the README so it is a bit more verbose - and mentions some requirements and general usage

mageta added 2 commits October 2, 2018 20:32
Signed-off-by: Benjamin Block <bebl@mageta.org>
Signed-off-by: Benjamin Block <bebl@mageta.org>
@mageta mageta force-pushed the marvinthepa-pullreq-initial-v2 branch from 5259974 to 84074ef Compare October 8, 2018 16:36
mageta added 5 commits October 9, 2018 14:26
* Use python3 as default interpreter
* Convert print-statements into function-calls
* Use binary files for pickle; and don't use the try-cPickle-or-pickle
  pattern - simply use pickle (python will do the rest)
* Change dictionary-lookup to use "x in dict" syntax
* Converted string concatenations to use Format Strings, use
  logger-function's own string-formation where appropriate
* Force VCard-Files to be opened with UTF-8
  * Seems to be the reasonable standard/expectation
  * allows us to drop the remaining encoding handling, as the rest will
    just be handled by python. The VCard-files will be read as UTF-8
    encoded file, but the actual program-output will be encoded in
    whatever locale is set by the user.
  * FIXME: maybe try to guess encoding of the files
* Drop remaining encoding-handling
* Use functools.cmp_to_key() to mechanically fix use of get_sortfunc()
  when the option -s is passed
  * FIXME: this works now, but tbh., I have no idea what it is really supposed
    to do practically.

Signed-off-by: Benjamin Block <bebl@mageta.org>
Can re-introduce this properly later. For now just drop it, its
unnecessary.

Signed-off-by: Benjamin Block <bebl@mageta.org>
This is a bit more modern and it automatically generates useful
help- and error-messages, of which the former didn't exist yet.

Signed-off-by: Benjamin Block <bebl@mageta.org>
Signed-off-by: Benjamin Block <bebl@mageta.org>
It is possible to store more than one eMail-Address for a contact in a
VCard. This adds a new option '-a'/'--all-addresses' that will cause
the script to print all addresses instead of only the first one.

In mutt this will cause a selection-list to be displayed if more than
one address is printed for a query.

Signed-off-by: Benjamin Block <bebl@mageta.org>
@mageta mageta force-pushed the marvinthepa-pullreq-initial-v2 branch from 84074ef to 17f344a Compare October 9, 2018 12:42
mageta added 4 commits October 9, 2018 14:49
Because the script always only used one cache-file, and invalidated all
paths that where found in a previous run but not in the current run, it
was effectively only possible to cache the content of one VCard-Directory.

Improve this by simply appending a hash of the VCard-Directory path to the
cache-path, so that each directory will automatically get its unique
cache-file.

New paths for caches look like:
    ~/.cache/<XDIGIT*64>.vcs_query

Signed-off-by: Benjamin Block <bebl@mageta.org>
Make it possible to pass the option '-d'/'--vcard-dir' multiple times, to
search more than one directory for VCards.

In tools like kaddressbook you can manage multiple address-books in
different locations. To query them all in one go from mutt, one can now
just pass all directories in one go, instead of having to add scaffolding
scripts.

Caveat: there is no checking for duplicates, and the sort will yield
        lightly strange results, if a VCard is in more than one directory,
        and the option '--all-addresses' is active.

Signed-off-by: Benjamin Block <bebl@mageta.org>
Till now there was no mechanism that would filter out duplicates in the
read contact-data (a contact being the tuple of eMail-Address, Name,
and Description). Especially if multiple VCard-Directories are given
and/or the option '--all-addresses' is used, this can lead to unnecessary
verbose output.

Fixing this is not completely straight-forward, as duplicates can be
found in different VCards, while the same VCards also contain different
contacts form the duplicate. For example:

  VCard M:
    Contact A (eMail X, Name A, Description A)
    Contact B (eMail Y, Name A, Description A)
  VCard N:
    Contact A (eMail X, Name A, Description A)
    Contact C (eMail Z, Name A, Description A)

We can not simply dismiss VCard N completely, because it contains
Contact C, but we want to only print one version of Contact A, because
the other is a duplicate.

To fix this, we first collect all VCards from all given VCard-Directories.
While collecting these, we add all contained contacts into a set, which
will automatically discard all duplicates. Afterwards we create a sorted
list from the contact-set and do the filtering/printing on that list.

Further, the VCard class doesn't do the data-formatting anymore, but only
returns a named tuple. This way we don't have to handle "string-blobs" in
the main functions.

Signed-off-by: Benjamin Block <bebl@mageta.org>
The default is to sort the contacts first according to the eMail-Address,
then name, and last description. With the new option '-n'/'--sort-names'
the order is changed to name, eMail, and description. Especially if
'--all-addresses' is used this is better because it keeps the addresses
by the same person ordered together.

The sort when '--starting-matches' is used is not changed.

Signed-off-by: Benjamin Block <bebl@mageta.org>
@mageta mageta force-pushed the marvinthepa-pullreq-initial-v2 branch from 17f344a to 2068e93 Compare October 9, 2018 12:50
With the new option '-r'/'--regex' the PATTERN is handled as regular
expression (with the same syntax that the standard python module re uses
- this is somewhat different from tools like grep or sed). As with regular
PATTERN, the regex matches case independent.

Signed-off-by: Benjamin Block <bebl@mageta.org>
It is unclear what this option should accomplish with regard to its
implementation: it only does a special sort, but filtering is still done
separately; and with no filter, no special sort is done.

The filtered pattern will always be displayed first anyway - there will be
nothing else displayed.

Signed-off-by: Benjamin Block <bebl@mageta.org>
These two __str__() methods are not used throughout the code. Remove them.

Signed-off-by: Benjamin Block <bebl@mageta.org>
Fix some issues found by the static checker pylint (version 1.7.5):
 * global variables should be all capital letters
 * standard library import should come before external imports
 * all import should be done before the first module-code
 * TODO's should show what is open to do
 * variables should not be only one letter
 * line-breaks for 80 character lines
 * no separate space between [()] and the encased code

Signed-off-by: Benjamin Block <bebl@mageta.org>
Signed-off-by: Benjamin Block <bebl@mageta.org>
@mageta mageta force-pushed the marvinthepa-pullreq-initial-v2 branch from 2068e93 to 415c422 Compare October 10, 2018 13:16
@marvinthepa

Copy link
Copy Markdown
Owner

Hey Benjamin, I wouldn't have thought that somebody is using this.

I should maybe look into using it myself again :-).

I might have a look if I find the time, but I cannot promise anything.

Happy hacking.

@mageta

mageta commented Oct 17, 2018

Copy link
Copy Markdown
Author

No problem at all. I keep extending stuff on my repo. I have an extension locally that makes it possible to use it with auto-completion in vim (for mails, patchfiles and git-commit-messages); I'll get that into my github-repo once I had a bit more time later this week. Feel free to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants