summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2013-03-23 13:49:36 +0000
committerLars Wirzenius <liw@liw.fi>2013-03-23 13:49:36 +0000
commit086b5790c67639423f8ec50f34fa9be320500bc0 (patch)
tree98ad9eb239cd9032a0baa06e2e09875a5c0294a9
downloadclab-086b5790c67639423f8ec50f34fa9be320500bc0.tar.gz
Initial
-rwxr-xr-xclab97
1 files changed, 97 insertions, 0 deletions
diff --git a/clab b/clab
new file mode 100755
index 0000000..26f9c77
--- /dev/null
+++ b/clab
@@ -0,0 +1,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()