summaryrefslogtreecommitdiff
path: root/clab
blob: 26f9c77cafab425eb56b7476953fbe8ec23e0f92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/python
#
# Copyright 2013  Lars Wirzenius
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import cliapp
import logging
import os
import sys
import yaml


class AddressBook(object):

    def __init__(self):
        self.entries = []
    
    def find_yaml_files(self, database_root):
        for dirname, subdirs, basenames in os.walk(database_root):
            for basename in basenames:
                if basename.endswith('.yaml'):
                    yield os.path.join(dirname, basename)
    
    def add_from_database(self, database_root):
        logging.info('Adding from database %s' % database_root)
        for yaml_filename in self.find_yaml_files(database_root):
            self.add_from_file(yaml_filename)

    def add_from_file(self, filename):
        logging.info('Adding from file %s' % filename)
        with open(filename) as f:
            record = yaml.safe_load(f)
        self.entries.append(record)

    def find(self, pattern):
        return []
        
    def get_single(self, key, default):
        return default
    
    def get_list(self, key):
        return []


class CommandLineAddressBook(cliapp.Application):

    def add_settings(self):
        self.settings.string_list(
            ['database', 'db', 'd'],
            'add DIR to list of databases to use',
            metavar='DIR')

    def load_address_book(self):
        book = AddressBook()
        for database in self.settings['database']:
            book.add_from_database(database)
        return book

    def format_entry(self, entry):
        return yaml.safe_dump(entry)

    def cmd_list(self, args):
        book = self.load_address_book()
        for entry in book.entries:
            self.output.write(self.format_entry(entry) + '\n')

    def cmd_mutt_query(self, args):
        if len(args) != 1:
            raise cliapp.AppException(
                'mutt-query requires exactly one argument')

        book = self.load_address_book()
        entries = book.find(args[0])
        if not entries:
            self.output.write('No matches\n')
            sys.exit(1)
        self.output.write('%d matching:\n' % len(entries))
        for entry in entries:
            name = entry.get_single('name', '')
            for email in entry.get_list('email'):
                self.output.write('%s\t%s\n' % (email, name))


CommandLineAddressBook().run()