#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Troels Agergaard Jacobsen

SANDBOX = True

import sys
import os
import io
import pickle
import argparse

from __init__ import __description__
from auth import ENoteAuth

class ENote():
    def __init__(self, token):
        self.token = token
        self.client = EvernoteClient(token=token, sandbox=True)

def main():
    parser = argparse.ArgumentParser(prog='enote', description=__description__)            
    subparser = parser.add_subparsers(dest='command')

    # init command
    parser_init = subparser.add_parser('init', help='Initialize directory and authenticate user')

    # list-notebooks command
    parser_list_notebooks = subparser.add_parser('list-notebooks', help='List available notebooks')

    # list-tags command
    parser_list_tags = subparser.add_parser('list-tags', help='List available tags')

    # list-notes command
    parser_list_notes = subparser.add_parser('list-notes', help='List notes')
    parser_list_notes.add_argument('--notebook', type=str, help='Limit search to specified notebook')
    parser_list_notes.add_argument('--tag', type=str, help='Limit search to specified tag')

    # download command
    parser_download = subparser.add_parser('download', help='Download notes')
    parser_download.add_argument('--notebook', type=str, help='Limit search to specified notebook')
    parser_download.add_argument('--tag', type=str, help='Limit search to specified tag')
    parser_download.add_argument('--delete', action='store_true', help='Delete extraneous files')
    parser_download.add_argument('--incremental', action='store_true', help='Only download new notes')

    args = parser.parse_args()
    command = args.command

    path = os.getcwd()
    config_file = os.path.join(path, '.enote')

    if command == 'init':
        enauth = ENoteAuth() 
        token = enauth.get_token()
        #TODO: This will wipe the file. If we store other variables in the future, need to find different way
        f = io.open(config_file, 'wb')
        pickle.dump(token, f)
        f.close()
        sys.exit(0)

    if not os.path.isfile(os.path.join(path, '.enote')):
        #TODO: write to stderr
        print 'Directory not initialized'
        sys.exit(1)

    f = io.open(config_file, 'rb')
    token = pickle.load(f)
    f.close()

    if command == 'list-notebooks':
        raise NotImplementedError('list-notebooks command not implemented')

    if command == 'list-tags':
        raise NotImplementedError('list-tags command not implemented')

    if command == 'list-notes':
        raise NotImplementedError('list-notes command not implemented')

    if command == 'download':
        raise NotImplementedError('download command not implemented')

if __name__ == "__main__":
    main()

@@ -166,16 +166,64 @@
 gparse%0A%0A
+from evernote.api.client import EvernoteClient%0A%0A
 from __i
@@ -415,16 +415,867 @@
 ox=True)
+%0A        self.note_store = self.client.get_note_store()%0A%0A        self.notebooks = None%0A        self.tags = None%0A%0A    def pullNotebooks(self):%0A        if self.notebooks is None:%0A            self.notebooks = %7B%7D%0A            for notebook in self.note_store.listNotebooks():%0A                self.notebooks%5Bnotebook.guid%5D = notebook.name%0A%0A    def listNotebooks(self):%0A        self.pullNotebooks()%0A        notebooks = self.notebooks.values()%0A        notebooks.sort()%0A        for notebook in notebooks:%0A            print notebook%0A%0A    def pullTags(self):%0A        if self.tags is None:%0A            self.tags = %7B%7D%0A            for tag in self.note_store.listTags():%0A                self.tags%5Btag.guid%5D = tag.name%0A%0A    def listTags(self):%0A        self.pullTags()%0A        tags = self.tags.values()%0A        tags.sort()%0A        for tag in tags:%0A            print tag
 %0A%0Adef ma
@@ -3239,24 +3239,50 @@
  f.close()%0A%0A
+    enote = ENote(token)%0A%0A
     if comma
@@ -3317,49 +3317,27 @@
     
-raise NotImplementedError('
+enote.
 list
--n
+N
 otebooks
  com
@@ -3336,33 +3336,9 @@
 ooks
- command not implemented'
+(
 )%0A%0A 
@@ -3379,69 +3379,23 @@
     
-raise NotImplementedError('
+enote.
 list
--tags command not implemented'
+Tags(
 )%0A%0A 

Implement list-tags and list-notebooks