Commits

trunet  committed c63d1ae

refactor do código cmd e rename das classes

  • Participants
  • Parent commits 5223b0e

Comments (0)

Files changed (9)

+import thrift.protocol.TBinaryProtocol as TBinaryProtocol
+import thrift.transport.THttpClient as THttpClient
+import evernote.edam.userstore.UserStore as UserStore
+import evernote.edam.userstore.constants as UserStoreConstants
+import evernote.edam.error.constants as ErrorConstants
+from clConst import clConst
+
+class clAuth:
+	def authenticate(self, username, password):
+		userStoreHttpClient = THttpClient.THttpClient(clConst.userStoreUri)
+		userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
+		userStore = UserStore.Client(userStoreProtocol);
+
+		versionOK = userStore.checkVersion("Python EDAMTest",
+                                           UserStoreConstants.EDAM_VERSION_MAJOR,
+                                           UserStoreConstants.EDAM_VERSION_MINOR)
+
+		if not versionOK:
+			raise ErrorConstants.EDAMSystemException(ErrorConstants.EDAMErrorCode.UNKNOWN,
+								 					 "EDAM protocol version not up to date")
+
+		try:
+			authResult = userStore.authenticate(username, password,
+       											clConst.consumerKey, clConst.consumerSecret)
+		except Exception, e:
+			print 'failed to login, exception:', e
+			return False
+		
+		self.username = username
+		self.user = authResult.user
+		self.authToken = authResult.authenticationToken
+		self.noteStoreUri =  clConst.noteStoreUriBase + self.user.shardId
+		return True
+
+	def getUsername(self):
+		return self.username
+
+	def getUser(self):
+		return self.user
+	
+	def getAuthToken(self):
+		return self.authToken
+	
+	def getNoteStoreUri(self):
+		return self.noteStoreUri
+	
+	def isAuthenticated(self):
+		try:
+			self.user
+		except:
+			return False
+		else:
+			return True
+class clConst:
+		consumerKey = "wsartori"
+		consumerSecret = "d5115ae78bcf31df"
+		userStoreUri = "https://sandbox.evernote.com/edam/user"
+		noteStoreUriBase = "http://sandbox.evernote.com/edam/note/"

File clNotebooks.py

+import thrift.protocol.TBinaryProtocol as TBinaryProtocol
+import thrift.transport.THttpClient as THttpClient
+import evernote.edam.notestore.NoteStore as NoteStore
+import evernote.edam.type.ttypes as Types
+from clConst import clConst
+
+class clNotebooks:
+	def __init__(self, clAuth):
+		self.auth = clAuth
+		noteStoreUri =  clConst.noteStoreUriBase + self.auth.getUser().shardId
+		noteStoreHttpClient = THttpClient.THttpClient(noteStoreUri)
+		noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(noteStoreHttpClient)
+		self.noteStore = NoteStore.Client(noteStoreProtocol)
+
+	def syncBooks(self):
+		self.notebooks = self.noteStore.listNotebooks(self.auth.getAuthToken());
+
+	def getNotebooks(self):
+		try:
+			self.notebooks
+		except:
+			self.syncBooks()
+		return self.notebooks
+
+	def getNotebook(self, notebook):
+		try:
+			self.notebooks
+		except:
+			self.syncBooks()
+		nbList = []
+		[nbList.append(nb.guid) for nb in self.notebooks if nb.name.startswith(notebook)]
+		if len(nbList)>0:
+			return self.noteStore.getNotebook(self.auth.getAuthToken(), nbList[0])
+		else:
+			return False
+
+	def createNotebook(self, notebookName):
+		newNotebook = Types.Notebook()
+		newNotebook.name = notebookName
+		self.noteStore.createNotebook(self.auth.getAuthToken(), newNotebook)
+		self.syncBooks()
+
+	def deleteNotebook(self, notebookName):
+		notebook = self.getNotebook(notebookName)
+		self.noteStore.expungeNotebook(self.auth.getAuthToken(), notebook.guid)
+		self.syncBooks()
+import thrift.protocol.TBinaryProtocol as TBinaryProtocol
+import thrift.transport.THttpClient as THttpClient
+import evernote.edam.notestore.NoteStore as NoteStore
+from clConst import clConst
+
+class clNotes:
+	def __init__(self, clAuth, notebook):
+		self.auth = clAuth
+		self.notebook = notebook
+		noteStoreUri =  clConst.noteStoreUriBase + self.auth.getUser().shardId
+		noteStoreHttpClient = THttpClient.THttpClient(noteStoreUri)
+		noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(noteStoreHttpClient)
+		self.noteStore = NoteStore.Client(noteStoreProtocol)
+
+	def syncNotes(self):
+		self.noteList = self.noteStore.findNotes(self.auth.getAuthToken(), self.notebook, 0, 9999)
+
+	def getNotes(self):
+		try:
+			self.noteList
+		except:
+			self.syncNotes()
+		return self.noteList.notes
+

File clevernote.py

 	else:
 		readline.parse_and_bind("tab: complete")
 
-from cnauth import CNAuth
-from cnbooks import CNBooks
-from cnnotes import CNNotes
-import evernote.edam.type.ttypes as Types
+from clAuth import clAuth
+from clNotebooks import clNotebooks
+from clNotes import clNotes
 
 class Clevernote(cmd.Cmd, object):
 	""" Commandline interface to clevernote. """
 
 	def __init__(self, username=False, password=False, auth=False, notebook=False):
-		cmd.Cmd.__init__(self, completekey="Tab")
+		cmd.Cmd.__init__(self)
 		self.prompt = "(clevernote) "
 		if username and password:
 			self.do_conn(username+" "+password)
 		elif auth and notebook:
 			self.auth = auth
 			self.notebook = notebook
-	
+
 	def help_introduction(self):
 		print "Clevernote is a evernote command line interface"
 		print ""
 		print "Report clevernote bugs to wsartori@gmail.com"
-		print "clevernote home page: <http://lauchpad.net/clevernote>"
+		print "clevernote home page: <http://www.wsartori.com/projects/clevernote>"
 			
 	def do_conn(self,userpass):
 		up = userpass.split()
 		if len(up)==2:
-			self.auth = CNAuth()
-			try:
-				self.username=up[0]
-				self.password=up[1]
-				self.auth.authenticate(up[0],up[1])
+			self.auth = clAuth()
+			if self.auth.authenticate(up[0],up[1]):
 				print "You're successful authenticated with", self.auth.user.username
-			except Exception, e:
-				print 'failed to login', ', exception:', e
 		else:
 			print "conn <username> <password>"
 		
 		print "Usage: conn <username> <password>"
 		print ""
 		print "used to authenticate you in evernote"
-	
+
 	def do_ls(self, line):
 		try:
 			self.notebook
 		except:
 			try:
-				self.nb.notebooks
-			except AttributeError:
-				self.nb = CNBooks(self.auth.user,self.auth.authToken)
-			print "Found", len(self.nb.notebooks), "notebooks:"
-			for notebook in self.nb.notebooks:
+				self.notebooks
+			except:
+				self.notebooks = clNotebooks(self.auth)
+			notebooks = self.notebooks.getNotebooks()
+			print "Found", len(notebooks), "notebooks:"
+			for notebook in notebooks:
 				print "- " + notebook.name
 		else:
 			try:
-				self.notes.noteList
+				self.notes
 			except:
-				self.notes = CNNotes(self.auth.user, self.auth.authToken, self.notebook)
-			print "Found", len(self.notes.noteList.notes), "notes:"
-			for note in self.notes.noteList.notes:
+				self.notes = clNotes(self.auth, self.notebook)
+			notes = self.notes.getNotes()
+			print "Found", len(notes), "notes:"
+			for note in notes:
 				print "- " + note.title
 
 	def complete_ls(self, text, line, begidx, endidx):
 		try:
-			self.nb.notebooks
+			self.notebooks
 		except:
-			self.nb = CNBooks(self.auth.user,self.auth.authToken)
+			self.notebooks = clNotebooks(self.auth)
 		if not text:
 			nbList = []
-			[nbList.append(nb.name) for nb in self.nb.notebooks]
+			[nbList.append(nb.name) for nb in self.notebooks.getNotebooks()]
 			completions = nbList
 		else:
 			nbList = []
-			[nbList.append(nb.name) for nb in self.nb.notebooks if nb.name.startswith(text)]
+			[nbList.append(nb.name) for nb in self.notebooks.getNotebooks() if nb.name.startswith(text)]
 			completions = nbList
 		return completions
-	
+
 	def do_cd(self, line):
 		try:
-			self.nb.notebooks
+			self.notebook
 		except:
-			self.nb = CNBooks(self.auth.user,self.auth.authToken)
-		nb = self.nb.getNotebook(self.auth.authToken, line)
-		notes = Clevernote(auth=self.auth, notebook=nb)
-		notes.prompt = self.prompt[:-2]+":"+nb.name+") "
-		notes.cmdloop()
-		
+			try:
+				self.notebooks
+			except:
+				self.notebooks = clNotebooks(self.auth)
+			notebook = self.notebooks.getNotebook(line)
+			if notebook:
+				notes = Clevernote(auth=self.auth, notebook=notebook)
+				notes.prompt = self.prompt[:-2]+":"+notebook.name+") "
+				notes.cmdloop()
+			else:
+				print "The notebook '" + line + "' does not exists."
+		else:
+			if (line == '..'):
+				return True
+			else:
+				print "You are already in a notebook."
+
 	def complete_cd(self, text, line, begidx, endidx):
-		self.complete_ls(text, line, begidx, endidx)
-	
+		try:
+			self.notebooks
+		except:
+			self.notebooks = clNotebooks(self.auth)
+		if not text:
+			nbList = []
+			[nbList.append(nb.name) for nb in self.notebooks.getNotebooks()]
+			completions = nbList
+		else:
+			nbList = []
+			[nbList.append(nb.name) for nb in self.notebooks.getNotebooks() if nb.name.startswith(text)]
+			completions = nbList
+		return completions
+
 	def help_cd(self):
-		print "Usage: cd <notebook>"
+		print "Usage: cd <notebook>|.."
 		print ""
-		print "change the working notebook"
-	
-	def do_addbook(self,name):
+		print "change the working notebook or back to root directory"
+
+	def do_mkdir(self, line):
 		try:
-			self.nb.notebooks
-		except AttributeError:
-			#create new notebooks object
-			#will need a "sync" command to refresh it
-			self.nb = CNBooks(self.auth.user,self.auth.authToken)
-		newbook = Types.Notebook()
-		newbook.name = name
-		self.nb.noteStore.createNotebook(self.auth.authToken,newbook)
-		self.nb.syncBooks(self.auth.authToken)
+			self.notebooks
+		except:
+			self.notebooks = clNotebooks(self.auth)
+		self.notebooks.createNotebook(line)
 
-	def do_delbook(self,name):
+	def do_rm(self, line):
 		try:
-			self.nb.notebooks
-		except AttributeError:
-			#create new notebooks object
-			#will need a "sync" command to refresh it
-			self.nb = CNBooks(self.auth.user,self.auth.authToken)
-		for notebook in self.nb.notebooks:
-			if notebook.name == name:
-				guid = notebook.guid
-		self.nb.noteStore.expungeNotebook(self.auth.authToken,guid)
-		self.nb.syncBooks(self.auth.authToken)
+			self.notebooks
+		except:
+			self.notebooks = clNotebooks(self.auth)
+		self.notebooks.deleteNotebook(line)
 
 	def onecmd(self, line):
 		if line.strip().startswith("conn") or line.strip().startswith("help"):
 		else:
 			try:
 				self.auth
-				super(Clevernote, self).onecmd(line)
 			except:
 				print "Please, authenticate first"
 				return False
+			else:
+				return super(Clevernote, self).onecmd(line)
 
 	def do_exit(self, goal):
 		return True
 	def help_exit(self):
 		print "Exit clevernote."
 		print "You can also use the Ctrl-D shortcut."
-		
+
+	def do_quit(self, goal):
+		return True
+
+	def help_quit(self):
+		print "Exit clevernote."
+		print "You can also use the Ctrl-D shortcut."
+
 def usage():
 	print "Usage: clevernote [-h|--help][-u|--username]"
 	sys.exit(0)

File cnauth.py

-import thrift.protocol.TBinaryProtocol as TBinaryProtocol
-import thrift.transport.THttpClient as THttpClient
-import evernote.edam.userstore.UserStore as UserStore
-import evernote.edam.userstore.constants as UserStoreConstants
-import evernote.edam.error.constants as ErrorConstants
-from cnconst import CNConst
-
-
-class CNAuth:
-	def authenticate(self,user,passwd):
-		userStoreHttpClient = THttpClient.THttpClient(CNConst.userStoreUri)
-		userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
-		userStore = UserStore.Client(userStoreProtocol);
-
-		versionOK = userStore.checkVersion("Python EDAMTest",
-                                           UserStoreConstants.EDAM_VERSION_MAJOR,
-                                           UserStoreConstants.EDAM_VERSION_MINOR)
-
-		if not versionOK:
-			raise ErrorConstants.EDAMSystemException(ErrorConstants.EDAMErrorCode.UNKNOWN,
-								 					 "EDAM protocol version not up to date")
-
-		authResult = userStore.authenticate(user, passwd,
-						    CNConst.consumerKey, CNConst.consumerSecret)
-
-		self.user = authResult.user
-		self.authToken = authResult.authenticationToken
-		self.noteStoreUri =  CNConst.noteStoreUriBase + self.user.shardId

File cnbooks.py

-import thrift.protocol.TBinaryProtocol as TBinaryProtocol
-import thrift.transport.THttpClient as THttpClient
-import evernote.edam.notestore.NoteStore as NoteStore
-from cnconst import CNConst
-
-
-class CNBooks:
-	
-	def __init__(self,user,authToken):
-		noteStoreUri =  CNConst.noteStoreUriBase + user.shardId
-		noteStoreHttpClient = THttpClient.THttpClient(noteStoreUri)
-		noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(noteStoreHttpClient)
-		self.noteStore = NoteStore.Client(noteStoreProtocol)
-		self.notebooks = self.noteStore.listNotebooks(authToken)
-
-		
-	def syncBooks(self,authToken):
-		self.notebooks = self.noteStore.listNotebooks(authToken);
-
-	def getNotebook(self, authToken, notebook):
-		nbList = []
-		[nbList.append(nb.guid) for nb in self.notebooks if nb.name.startswith(notebook)]
-		if len(nbList)>0:
-			return self.noteStore.getNotebook(authToken, nbList[0])
-		else:
-			return False

File cnconst.py

-class CNConst:
-		consumerKey = "wsartori"
-		consumerSecret = "d5115ae78bcf31df"
-		userStoreUri = "https://sandbox.evernote.com/edam/user"
-		noteStoreUriBase = "http://sandbox.evernote.com/edam/note/"

File cnnotes.py

-import thrift.protocol.TBinaryProtocol as TBinaryProtocol
-import thrift.transport.THttpClient as THttpClient
-import evernote.edam.notestore.NoteStore as NoteStore
-from cnconst import CNConst
-
-
-class CNNotes:
-	def __init__(self,user,authToken,notebook):
-		noteStoreUri =  CNConst.noteStoreUriBase + user.shardId
-		noteStoreHttpClient = THttpClient.THttpClient(noteStoreUri)
-		noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(noteStoreHttpClient)
-		self.noteStore = NoteStore.Client(noteStoreProtocol)
-		self.noteList = self.noteStore.findNotes(authToken, notebook, 0, 9999)