|
| 1 | +#coding=utf8 |
| 2 | +import sys, os, json, time |
| 3 | + |
| 4 | +from controllers import Controller |
| 5 | +from evernoteapi.oauth import Oauth |
| 6 | + |
| 7 | + |
| 8 | +def sys_print(s, level = 'info'): |
| 9 | + print(('[%-4s] %s'%((level+' '*4)[:4].upper(), s)).encode(sys.stdin.encoding)) |
| 10 | +def sys_input(s): |
| 11 | + return raw_input(s.encode(sys.stdin.encoding)) |
| 12 | +def show_help(*args): |
| 13 | + for fn, h in argDict.iteritems(): |
| 14 | + print('%-10s: %s'%(fn, h[1].decode('utf8').encode(sys.stdin.encoding))) |
| 15 | +def init(*args): |
| 16 | + mainController = Controller() |
| 17 | + def clear_dir(): |
| 18 | + if sys_input(u'初始化目录将会清除目录下所有文件,是否继续?[yn]') != 'y': return False |
| 19 | + def _clear_dir(currentDir): |
| 20 | + dirs, files = os.walk(currentDir).next()[1:] |
| 21 | + for d in dirs: |
| 22 | + _clear_dir(os.path.join(currentDir, d)) |
| 23 | + os.rmdir(os.path.join(currentDir, d)) |
| 24 | + for f in files: os.remove(f) |
| 25 | + _clear_dir('.') |
| 26 | + return True |
| 27 | + def _init(*args): |
| 28 | + if not reduce(lambda x,y: x+y, [l for l in os.walk('.').next()[1:]]) or clear_dir(): |
| 29 | + sys_print(u'账户仅需要在第一次使用时设置一次') |
| 30 | + while 1: |
| 31 | + sandbox = sys_input(u'是否是沙盒环境?[yn]') == 'y' |
| 32 | + isInternational = False |
| 33 | + isSpecialToken = sys_input(u'是否使用开发者Token?[yn]') == 'y' |
| 34 | + if isSpecialToken: |
| 35 | + token = sys_input(u'开发者Token: ') |
| 36 | + else: |
| 37 | + sys_print(u'本地删除笔记本将不会同步到云端,但笔记会照常删除') |
| 38 | + if not sandbox: isInternational = sys_input(u'是否是国际用户?[yn]') == 'y' |
| 39 | + token = Oauth(sandbox = sandbox, isInternational = isInternational) |
| 40 | + if mainController.log_in(token=token, isSpecialToken = isSpecialToken, sandbox=sandbox, |
| 41 | + isInternational = isInternational): |
| 42 | + mainController.ls.update_config(token=token, isSpecialToken=isSpecialToken, |
| 43 | + sandbox=sandbox, isInternational=isInternational, |
| 44 | + expireTime=time.time()) |
| 45 | + sys_print(u'登陆成功') |
| 46 | + break |
| 47 | + else: |
| 48 | + sys_print(u'登录失败') |
| 49 | + if sys_input(u'重试登录?[yn]') != 'y': break |
| 50 | + if mainController.available: |
| 51 | + if sys_input(u'已经登录,是否要重新登录?[yn]') == 'y': _init(*args) |
| 52 | + else: |
| 53 | + _init(*args) |
| 54 | + print('Bye~') |
| 55 | +def config(*args): |
| 56 | + mainController = Controller() |
| 57 | + if mainController.available: |
| 58 | + sys_print(u'目前登录用户: ' + mainController.ec.userStore.getUser().username) |
| 59 | + else: |
| 60 | + sys_print(u'尚未登录', 'warn') |
| 61 | +def pull(*args): |
| 62 | + mainController = Controller() |
| 63 | + if mainController.available: |
| 64 | + mainController.fetch_notes() |
| 65 | + # show changes |
| 66 | + for change in mainController.get_changes(): |
| 67 | + if change[1] in (-1, 0): sys_print(change[0].decode('utf8'), 'down') |
| 68 | + # confirm |
| 69 | + if sys_input(u'是否更新本地文件?[yn]') == 'y': |
| 70 | + mainController.download_notes(False) |
| 71 | + print('Bye~') |
| 72 | + else: |
| 73 | + sys_print(u'尚未登录', 'warn') |
| 74 | +def push(*args): |
| 75 | + mainController = Controller() |
| 76 | + if mainController.available: |
| 77 | + mainController.fetch_notes() |
| 78 | + # show changes |
| 79 | + for change in mainController.get_changes(): |
| 80 | + if change[1] in (1, 0): sys_print(change[0].decode('utf8'), 'down') |
| 81 | + # confirm |
| 82 | + if sys_input(u'是否上传本地文件?[yn]') == 'y': |
| 83 | + mainController.upload_files(False) |
| 84 | + print('Bye~') |
| 85 | + else: |
| 86 | + sys_print(u'尚未登录', 'warn') |
| 87 | +def status(*args): |
| 88 | + mainController = Controller() |
| 89 | + if mainController.available: |
| 90 | + mainController.fetch_notes() |
| 91 | + # show changes |
| 92 | + for change in mainController.get_changes(): |
| 93 | + if change[1] == -1: |
| 94 | + sys_print(change[0].decode('utf8'), 'down') |
| 95 | + elif change[1] == 1: |
| 96 | + sys_print(change[0].decode('utf8'), 'uplo') |
| 97 | + elif change[1] == 0: |
| 98 | + sys_print(change[0].decode('utf8'), 'both') |
| 99 | + else: |
| 100 | + sys_print(u'尚未登录', 'warn') |
| 101 | + |
| 102 | +argDict = { |
| 103 | + 'help': (show_help, '显示帮助'), |
| 104 | + 'init': (init, '登陆localnote'), |
| 105 | + 'config': (config, '查看已经登录的账户'), |
| 106 | + 'pull': (pull, '下载云端笔记'), |
| 107 | + 'push': (push, '上传本地笔记'), |
| 108 | + 'status': (status, '查看本地及云端更改'), |
| 109 | +} |
| 110 | + |
| 111 | +def main(): |
| 112 | + del sys.argv[0] |
| 113 | + if not sys.argv: sys.argv.append('help') |
| 114 | + argDict.get(sys.argv[0], show_help)[0](sys.argv[1:]) |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + main() |
0 commit comments