Skip to content

Commit cde7680

Browse files
committed
Demo finished
1 parent 124d0b0 commit cde7680

File tree

7 files changed

+149
-21
lines changed

7 files changed

+149
-21
lines changed

Diff for: .gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
build/*
22
dist/*
33
LocalNote.egg-info/*
4-
LocalNote/user.cfg
5-
LocalNote/run.py
64
*.pyc
75
*.swp

Diff for: LocalNote/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from controllers import Controller
1+
# from controllers import Controller

Diff for: LocalNote/controllers.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@ def __check_available(self):
2424
return True, ec
2525
except:
2626
return False, None
27-
def log_in(self, interAct = True, config = {}):
28-
if interAct:
29-
pass
30-
else:
31-
if config.get('token') is not None: self.token = config.get('token')
32-
if config.get('isSpecialToken') is not None: self.isSpecialToken = config.get('isSpecialToken')
33-
if config.get('sandbox') is not None: self.sandbox = config.get('sandbox')
34-
if config.get('isInternational') is not None: self.isInternational = config.get('isInternational')
35-
if config.get('expireTime') is not None: self.expireTime = config.get('expireTime')
36-
if config.get('lastUpdate') is not None: self.lastUpdate = config.get('lastUpdate')
27+
def log_in(self, config = {}, **kwargs):
28+
config = dict(config, **kwargs)
29+
if config.get('token') is not None: self.token = config.get('token')
30+
if config.get('isSpecialToken') is not None: self.isSpecialToken = config.get('isSpecialToken')
31+
if config.get('sandbox') is not None: self.sandbox = config.get('sandbox')
32+
if config.get('isInternational') is not None: self.isInternational = config.get('isInternational')
33+
if config.get('expireTime') is not None: self.expireTime = config.get('expireTime')
34+
if config.get('lastUpdate') is not None: self.lastUpdate = config.get('lastUpdate')
3735
available, ec = self.__check_available()
3836
if available:
3937
self.available = True
@@ -79,7 +77,6 @@ def __get_changes(self, update = False): # -1 for need download, 1 for need uplo
7977
for nbName in noteDict.keys(): r.append((nbName, 0))
8078
self.changesList = r
8179
return r
82-
# DEBUG
8380
def get_changes(self):
8481
return self.__get_changes(True)
8582
def download_notes(self, update = True):

Diff for: LocalNote/evernoteapi/oauth.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import requests, getpass
22

3+
CONSUMER_KEY = 'littlecodersh1259'
4+
CONSUMER_SECRET = '39cf81a16bcfb160'
5+
36
class Oauth(object):
4-
def __init__(self, consumerKey, consumerSecret, sandbox = True, isInternational = False):
5-
if sanbox:
7+
def __init__(self, consumerKey = CONSUMER_KEY, consumerSecret = CONSUMER_SECRET, sandbox = True, isInternational = False):
8+
if sandbox:
69
self.host = 'sandbox.evernote.com'
710
elif isInternational:
811
self.host = 'app.evernote.com'

Diff for: LocalNote/local/storage.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from os.path import join, exists
33
import chardet
44

5-
from evernoteapi import EvernoteController
6-
75
CONFIG_DIR = 'user.cfg'
86

97
# fileDictFormat: {
@@ -85,7 +83,7 @@ def write_note(self, noteFullPath, contentDict = {}):
8583
else:
8684
if not exists(join(nbName, nName)): os.mkdir(join(nbName, nName))
8785
for k, v in contentDict.iteritems():
88-
self.write_file(noteFullPath+'/'+k, v, '') # ok, this looks strange
86+
self.write_file(noteFullPath+'/'+k, v, '') # ok, this looks strange, ext is included in k
8987
else:
9088
if contentDict: # create folder
9189
if not exists(self.__str_c2l(noteFullPath)): os.mkdir(self.__str_c2l(noteFullPath))
@@ -99,6 +97,16 @@ def write_note(self, noteFullPath, contentDict = {}):
9997
os.remove(join(noteFullPath, fName, dName))
10098
os.rmdir(join(noteFullPath, fName))
10199
os.rmdir(noteFullPath)
100+
def write_file(self, noteFullPath, content, postfix = '.md'):
101+
if len(noteFullPath.split('/')) < 1: return False
102+
if not exists(self.__str_c2l(noteFullPath.split('/')[0])):
103+
os.mkdir(self.__str_c2l(noteFullPath.split('/')[0]))
104+
try:
105+
noteFullPath += postfix
106+
with open(self.__str_c2l(join(*noteFullPath.split('/'))), 'wb') as f: f.write(content)
107+
return True
108+
except:
109+
return False
102110
def get_file_dict(self):
103111
fileDict = {}
104112
for nbName in os.walk('.').next()[1]: # get folders

Diff for: LocalNote/main.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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()

Diff for: setup.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" A NetEase cloud music api project
1+
""" A command line tool to use evernote locally
22
See:
33
https://github.com/littlecodersh/LocalNote
44
"""
@@ -48,10 +48,15 @@
4848

4949
# You can just specify the packages manually here if your project is
5050
# simple. Or you can use find_packages().
51-
packages=['LocalNote',],
51+
packages=find_packages(),
5252

5353
install_requires=['requests', 'markdown2'],
5454

5555
# List additional groups of dependencies here
5656
extras_require={},
57+
entry_points={
58+
'console_scripts':[
59+
'localnote = LocalNote.main:main'
60+
]
61+
},
5762
)

0 commit comments

Comments
 (0)