From be81b480600c27eca162581520fc38e210920380 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Fri, 14 Feb 2014 22:11:43 +0100 Subject: [PATCH] Init --- vdirsyncer/cli.py | 29 +++++++++++++++++++++++++++++ vdirsyncer/cli_utils.py | 37 +++++++++++++++++++++++++++++++++++++ vdirsyncer/sync/base.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 vdirsyncer/cli.py create mode 100644 vdirsyncer/cli_utils.py create mode 100644 vdirsyncer/sync/base.py diff --git a/vdirsyncer/cli.py b/vdirsyncer/cli.py new file mode 100644 index 0000000..beaf17b --- /dev/null +++ b/vdirsyncer/cli.py @@ -0,0 +1,29 @@ +import os +import ConfigParser +from vdirsyncer.cli_utils import path +from vdirsyncer.sync import sync_classes + +def get_config_parser(env): + fname = env.get('VDIRSYNCER_CONFIG', path('~/.vdirsyncer/config')) + c = ConfigParser.SafeConfigParser() + c.read(fname) + return dict((c, c.items(c)) for c in c.sections()) + +def main(): + env = os.environ + cfg = get_config_parser(env) + _main(env, cfg) + +def _main(env, file_cfg): + app = argvard.Argvard() + + sync = argvard.Command() + @sync_command.main('[accounts...]') + def sync_main(accounts=None): + if accounts is None: + accounts = list(file_cfg.keys()) + for account in accounts: + account_cfg = dict(file_cfg[account]) + del account_cfg['type'] + syncer = sync_classes[account_cfg['type']](**account_cfg) + syncer.run() diff --git a/vdirsyncer/cli_utils.py b/vdirsyncer/cli_utils.py new file mode 100644 index 0000000..4f23b85 --- /dev/null +++ b/vdirsyncer/cli_utils.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +''' + watdo.cli_utils + ~~~~~~~~~ + + This module contains helper functions that should be useful for arbitrary + CLI interfaces. + + :copyright: (c) 2013 Markus Unterwaditzer + :license: MIT, see LICENSE for more details. +''' + +import os +import sys + + +def bail_out(msg): + print(msg) + sys.exit(1) + + +def check_directory(path): + if not os.path.exists(path): + os.makedirs(path) + + +def path(p): + p = os.path.expanduser(p) + p = os.path.abspath(p) + return p + + +def confirm(message='Are you sure? (Y/n)'): + inp = raw_input(message).lower().strip() + if not inp or inp == 'y': + return True + return False diff --git a/vdirsyncer/sync/base.py b/vdirsyncer/sync/base.py new file mode 100644 index 0000000..228bbec --- /dev/null +++ b/vdirsyncer/sync/base.py @@ -0,0 +1,35 @@ +class Syncer(object): + def list_items(self): + ''' + :returns: list of (href, etag) + ''' + raise NotImplementedError() + + def get_items(self, hrefs): + ''' + :param hrefs: list of hrefs to fetch + :returns: list of (object, href, etag) + ''' + raise NotImplementedError() + + def item_exists(self, href): + ''' + check if item exists + ''' + raise NotImplementedError() + + def upload(self, obj): + ''' + Upload a new object, raise error if it already exists. + :returns: (href, etag) + ''' + raise NotImplementedError() + + def update(self, obj, etag): + ''' + Update the object, raise error if the etag on the server doesn't match + the given etag. + + :returns: etag on the server + ''' + raise NotImplementedError()