insanum / sncli

Simplenote CLI
MIT License
397 stars 34 forks source link

'UTF-8' problem #13

Closed s5unty closed 9 years ago

s5unty commented 9 years ago

When edit the file which content has "UTF-8" charset, something wrong:

...
  File "/usr/local/lib/python2.7/dist-packages/urwid/main_loop.py", line 499, in process_input
    k = self._topmost_widget.keypress(self.screen_size, k)
  File "/usr/local/lib/python2.7/dist-packages/simplenote_cli/sncli.py", line 615, in gui_frame_keypress
    content = self.exec_cmd_on_note(note)
  File "/usr/local/lib/python2.7/dist-packages/simplenote_cli/sncli.py", line 81, in exec_cmd_on_note
    tf = temp.tempfile_create(note if note else None, raw=raw)
  File "/usr/local/lib/python2.7/dist-packages/simplenote_cli/temp.py", line 21, in tempfile_create
    tf.write(note['content'])
UnicodeEncodeError: 'ascii' codec can't encode characters in position 4-5: ordinal not in range(128)

I did a quick patch for myself. Without much more testing. Hope it will works well for other guys ;)

 simplenote_cli/sncli.py |    8 ++++----
 simplenote_cli/temp.py  |    4 ++--
 2 个文件被修改,插入 6 行(+),删除 6 行(-)

diff --git simplenote_cli/sncli.py simplenote_cli/sncli.py
index 0e76a91..bcac3db 100644
--- simplenote_cli/sncli.py
+++ simplenote_cli/sncli.py
@@ -623,8 +623,8 @@ class sncli:
             if not content:
                 return None

-            md5_old = md5.new(note['content']).digest()
-            md5_new = md5.new(content).digest()
+            md5_old = md5.new(note['content'].encode("utf-8")).digest()
+            md5_new = md5.new(content.encode("utf-8")).digest()

             if md5_old != md5_new:
                 self.log(u'Note updated')
@@ -1030,8 +1030,8 @@ class sncli:
         if not content:
             return

-        md5_old = md5.new(note['content']).digest()
-        md5_new = md5.new(content).digest()
+        md5_old = md5.new(note['content'].encode("utf-8")).digest()
+        md5_new = md5.new(content.encode("utf-8")).digest()

         if md5_old != md5_new:
             self.log(u'Note updated')
diff --git simplenote_cli/temp.py simplenote_cli/temp.py
index 46de9c2..f81968f 100644
--- simplenote_cli/temp.py
+++ simplenote_cli/temp.py
@@ -18,7 +18,7 @@ def tempfile_create(note, raw=False):
             ext = '.mkd'
         tf = tempfile.NamedTemporaryFile(suffix=ext, delete=False)
         if note:
-            tf.write(note['content'])
+            tf.write(note['content'].encode("utf-8"))
         tf.flush()
     return tf

@@ -35,6 +35,6 @@ def tempfile_content(tf):
     tf.seek(0)
     lines = []
     for line in tf:
-        lines.append(line)
+        lines.append(line.decode("utf-8"))
     return lines
insanum commented 9 years ago

Can you please supply a pull request? Thanks. https://help.github.com/articles/using-pull-requests/

shawnaxsom commented 9 years ago

I have merged in changes in the latest commit (afafc4969aea2b896c7e3bd54f6c08f42f072052). This encoding problem was also affecting issue #17.

Closing this issue. Please reopen the issue if you find any issues within the commit.