nncli

NextCloud Notes Command Line Interface
git clone git://git.danielmoch.com/nncli.git
Log | Files | Refs | LICENSE

commit fa27b206efc76434e0bf5d75e5fe2e89827bdac6
parent 8a25e0bdf88e1f8e169c5680c5786ef24b5bb23e
Author: Samuel Walladge <samuel@swalladge.id.au>
Date:   Sat, 28 May 2016 18:56:29 +0930

cleanup old utf-8 encode function and related calls

Diffstat:
Msimplenote_cli/simplenote.py | 19++-----------------
Msimplenote_cli/sncli.py | 12++++--------
Msimplenote_cli/temp.py | 9++-------
3 files changed, 8 insertions(+), 32 deletions(-)

diff --git a/simplenote_cli/simplenote.py b/simplenote_cli/simplenote.py @@ -143,18 +143,8 @@ def update_note(self, note): - status (int): 0 on sucesss and -1 otherwise """ - # # use UTF-8 encoding - # # cpbotha: in both cases check if it's not unicode already - # # otherwise you get "TypeError: decoding Unicode is not supported" - # if "content" in note: - # if isinstance(note["content"], str): - # note["content"] = str(note["content"], 'utf-8') - # - # if "tags" in note: - # # if a tag is a string, unicode it, otherwise pass it through - # # unchanged (it's unicode already) - # # using the ternary operator, because I like it: a if test else b - # note["tags"] = [str(t, 'utf-8') if isinstance(t, str) else t for t in note["tags"]] + # Note: all strings in notes stored as type str + # - use s.encode('utf-8') when bytes type needed # determine whether to create a new note or updated an existing one params = {'auth': self.get_token(), @@ -174,11 +164,6 @@ def update_note(self, note): #logging.debug('RESPONSE ERROR: ' + str(e)) return e, -1 note = res.json() - # if "content" in note: - # # use UTF-8 encoding - # note["content"] = note["content"].encode('utf-8') - # if "tags" in note: - # note["tags"] = [t.encode('utf-8') for t in note["tags"]] #logging.debug('RESPONSE OK: ' + str(note)) return note, 0 diff --git a/simplenote_cli/sncli.py b/simplenote_cli/sncli.py @@ -644,8 +644,8 @@ def gui_frame_keypress(self, size, key): if not content: return None - md5_old = hashlib.md5(self.encode_utf_8(note['content'])).digest() - md5_new = hashlib.md5(self.encode_utf_8(content)).digest() + md5_old = hashlib.md5(note['content'].encode('utf-8')).digest() + md5_new = hashlib.md5(content.encode('utf-8')).digest() if md5_old != md5_new: self.log('Note updated') @@ -863,10 +863,6 @@ def gui_frame_keypress(self, size, key): self.gui_update_status_bar() return None - def encode_utf_8(self, string): - # This code also exists in temp.py. Move into an encoding or utility class if other areas need encoding. - return string.encode("utf-8") if isinstance(string, str) else string - def gui_init_view(self, loop, view_note): self.master_frame.keypress = self.gui_frame_keypress self.gui_body_set(self.view_titles) @@ -1093,8 +1089,8 @@ def cli_note_edit(self, key): if not content: return - md5_old = hashlib.md5(self.encode_utf_8(note['content'])).digest() - md5_new = hashlib.md5(self.encode_utf_8(content)).digest() + md5_old = hashlib.md5(note['content'].encode('utf-8')).digest() + md5_new = hashlib.md5(content.encode('utf-8')).digest() if md5_old != md5_new: self.log('Note updated') diff --git a/simplenote_cli/temp.py b/simplenote_cli/temp.py @@ -19,14 +19,10 @@ def tempfile_create(note, raw=False): tf = tempfile.NamedTemporaryFile(suffix=ext, delete=False) if note: contents = note['content'] - tf.write(encode_utf_8(contents)) + tf.write(contents.encode('utf-8')) tf.flush() return tf -def encode_utf_8(string): - # This code also exists in sncli.py. Move into an encoding or utility class if other areas need encoding. - return string.encode("utf-8") if isinstance(string, str) else string - def tempfile_delete(tf): if tf: os.unlink(tf.name) @@ -38,7 +34,6 @@ def tempfile_name(tf): def tempfile_content(tf): # This seems like a hack. When editing with Gedit, tf file contents weren't getting - # updated in memory, even though it successfully saved on disk. updated_tf_contents = open(tf.name, 'r').read() - tf.write(updated_tf_contents.encode("utf-8")) + tf.write(updated_tf_contents.encode('utf-8')) return updated_tf_contents