aboutsummaryrefslogtreecommitdiff
path: root/patches/terminal_velocity_python3_support.patch
blob: bd4aec7b194284ffef3569f396f9bee71c45f958 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
commit 6ca19964b9e8a7866fd7e21a3dac9ccd35f0d434
Author: Ismaël Bouya <ismael.bouya@normalesup.org>
Date:   Sat Mar 9 20:13:18 2019 +0100

    Add python3 support

diff --git a/terminal_velocity/notebook.py b/terminal_velocity/notebook.py
index b6226dc..11f76de 100644
--- a/terminal_velocity/notebook.py
+++ b/terminal_velocity/notebook.py
@@ -60,51 +60,6 @@ import sys
 import chardet
 
 
-def unicode_or_bust(raw_text):
-    """Return the given raw text data decoded to unicode.
-
-    If the text cannot be decoded, return None.
-
-    """
-    encodings = ["utf-8"]
-    for encoding in (sys.getfilesystemencoding(), sys.getdefaultencoding()):
-        # I would use a set for this, but they don't maintain order.
-        if encoding not in encodings:
-            encodings.append(encoding)
-
-    for encoding in encodings:
-        if encoding:  # getfilesystemencoding() may return None
-            try:
-                decoded = unicode(raw_text, encoding=encoding)
-                return decoded
-            except UnicodeDecodeError:
-                pass
-
-    # If none of those guesses worked, let chardet have a go.
-    encoding = chardet.detect(raw_text)["encoding"]
-    if encoding and encoding not in encodings:
-        try:
-            decoded = unicode(raw_text, encoding=encoding)
-            logger.debug("File decoded with chardet, encoding was {0}".format(
-                encoding))
-            return decoded
-        except UnicodeDecodeError:
-            pass
-        except LookupError:
-            pass
-
-    # I've heard that decoding with cp1252 never fails, so try that last.
-    try:
-        decoded = unicode(raw_text, encoding="cp1252")
-        logger.debug("File decoded with encoding cp1252")
-        return decoded
-    except UnicodeDecodeError:
-        pass
-
-    # If nothing worked then give up.
-    return None
-
-
 class Error(Exception):
     """Base class for exceptions in this module."""
     pass
@@ -192,12 +147,12 @@ class PlainTextNote(object):
         # subdirs) if they don't exist.
         directory = os.path.split(self.abspath)[0]
         if not os.path.isdir(directory):
-            logger.debug(u"'{0} doesn't exist, creating it".format(directory))
+            logger.debug("'{0} doesn't exist, creating it".format(directory))
             try:
                 os.makedirs(directory)
             except os.error as e:
                 raise NewNoteError(
-                        u"{0} could not be created: {1}".format(directory, e))
+                        "{0} could not be created: {1}".format(directory, e))
 
         # Create an empty file if the file doesn't exist.
         open(self.abspath, 'a')
@@ -217,11 +172,11 @@ class PlainTextNote(object):
 
     @property
     def contents(self):
-        contents = unicode_or_bust(open(self.abspath, "r").read())
+        contents = open(self.abspath, "rb").read().decode(errors='ignore')
         if contents is None:
             logger.error(
-                u"Could not decode file contents: {0}".format(self.abspath))
-            return u""
+                "Could not decode file contents: {0}".format(self.abspath))
+            return ""
         else:
             return contents
 
@@ -322,12 +277,12 @@ class PlainTextNoteBook(object):
 
         # Create notebook_dir if it doesn't exist.
         if not os.path.isdir(self.path):
-            logger.debug(u"'{0} doesn't exist, creating it".format(self.path))
+            logger.debug("'{0} doesn't exist, creating it".format(self.path))
             try:
                 os.makedirs(self.path)
             except os.error as e:
                 raise NewNoteBookError(
-                        u"{0} could not be created: {1}".format(self.path, e))
+                        "{0} could not be created: {1}".format(self.path, e))
         else:
             # TODO: Check that self.path is a directory, if not raise.
             pass
@@ -358,13 +313,12 @@ class PlainTextNoteBook(object):
                 abspath = os.path.join(root, filename)
                 relpath = os.path.relpath(abspath, self.path)
                 relpath, ext = os.path.splitext(relpath)
-                unicode_relpath = unicode_or_bust(relpath)
                 if relpath is None:
                     # The filename could not be decoded.
                     logger.error(
                             "Could not decode filename: {0}".format(relpath))
                 else:
-                    self.add_new(title=unicode_relpath, extension=ext)
+                    self.add_new(title=relpath, extension=ext)
 
     @property
     def path(self):
@@ -418,7 +372,7 @@ class PlainTextNoteBook(object):
         for note in self._notes:
             if note.title == title and note.extension == extension:
                 raise NoteAlreadyExistsError(
-                        u"Note already in NoteBook: {0}".format(note.title))
+                        "Note already in NoteBook: {0}".format(note.title))
 
         # Ok, add the note.
         note = PlainTextNote(title, self, extension)
diff --git a/terminal_velocity/terminal_velocity.py b/terminal_velocity/terminal_velocity.py
index 5f0e213..9234bea 100755
--- a/terminal_velocity/terminal_velocity.py
+++ b/terminal_velocity/terminal_velocity.py
@@ -1,7 +1,7 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 """A fast note-taking app for the UNIX terminal"""
 from __future__ import print_function
-import ConfigParser
+import configparser
 import argparse
 import os
 import logging
@@ -9,9 +9,9 @@ import logging.handlers
 import sys
 
 #import terminal_velocity.urwid_ui as urwid_ui
-import urwid_ui
+from . import urwid_ui
 
-from git import get_git_project_config, git_project_is_configured, fetch_changes, push_changes
+from .git import get_git_project_config, git_project_is_configured, fetch_changes, push_changes
 
 
 def startup():
@@ -37,7 +37,7 @@ def main():
 
     # Parse the config file.
     config_file = os.path.abspath(os.path.expanduser(args.config))
-    config = ConfigParser.SafeConfigParser()
+    config = configparser.ConfigParser()
     config.read(config_file)
     defaults = dict(config.items('DEFAULT'))
 
diff --git a/terminal_velocity/urwid_ui.py b/terminal_velocity/urwid_ui.py
index caebcb9..89bab35 100644
--- a/terminal_velocity/urwid_ui.py
+++ b/terminal_velocity/urwid_ui.py
@@ -10,7 +10,7 @@ import logging
 logger = logging.getLogger(__name__)
 
 import urwid
-import notebook
+from . import notebook
 
 
 palette = [
@@ -27,8 +27,6 @@ def system(cmd, loop):
 
     loop.screen.stop()
 
-    cmd = u"{0}".format(cmd)
-    cmd = cmd.encode("utf-8")  # FIXME: Correct encoding?
     safe_cmd = shlex.split(cmd)
 
     logger.debug("System command: {0}".format(safe_cmd))
@@ -114,7 +112,7 @@ class AutocompleteWidget(urwid.Edit):
 
         # When search bar is empty show placeholder text.
         if not self.edit_text and not self.autocomplete_text:
-            placeholder_text = u"Find or Create"
+            placeholder_text = "Find or Create"
             return (placeholder_text,
                     [("placeholder", len(placeholder_text))])
 
@@ -186,7 +184,7 @@ class NoteFilterListBox(urwid.ListBox):
 
     def render(self, size, focus=False):
         if len(self.list_walker) == 0:
-            placeholder = placeholder_text(u"No matching notes, press Enter "
+            placeholder = placeholder_text("No matching notes, press Enter "
                 "to create a new note")
             return placeholder.render(size)
         return super(NoteFilterListBox, self).render(size, self.fake_focus)
@@ -399,7 +397,7 @@ class MainFrame(urwid.Frame):
         # If the user has no notes yet show some placeholder text, otherwise
         # show the note list.
         if len(self.notebook) == 0:
-            self.body = placeholder_text(u"You have no notes yet, to create "
+            self.body = placeholder_text("You have no notes yet, to create "
                 "a note type a note title then press Enter")
         else:
             self.body = urwid.Padding(self.list_box, left=1, right=1)