]>
Commit | Line | Data |
---|---|---|
fcf18de4 IB |
1 | commit 6ca19964b9e8a7866fd7e21a3dac9ccd35f0d434 |
2 | Author: Ismaël Bouya <ismael.bouya@normalesup.org> | |
3 | Date: Sat Mar 9 20:13:18 2019 +0100 | |
4 | ||
5 | Add python3 support | |
6 | ||
7 | diff --git a/terminal_velocity/notebook.py b/terminal_velocity/notebook.py | |
8 | index b6226dc..11f76de 100644 | |
9 | --- a/terminal_velocity/notebook.py | |
10 | +++ b/terminal_velocity/notebook.py | |
11 | @@ -60,51 +60,6 @@ import sys | |
12 | import chardet | |
13 | ||
14 | ||
15 | -def unicode_or_bust(raw_text): | |
16 | - """Return the given raw text data decoded to unicode. | |
17 | - | |
18 | - If the text cannot be decoded, return None. | |
19 | - | |
20 | - """ | |
21 | - encodings = ["utf-8"] | |
22 | - for encoding in (sys.getfilesystemencoding(), sys.getdefaultencoding()): | |
23 | - # I would use a set for this, but they don't maintain order. | |
24 | - if encoding not in encodings: | |
25 | - encodings.append(encoding) | |
26 | - | |
27 | - for encoding in encodings: | |
28 | - if encoding: # getfilesystemencoding() may return None | |
29 | - try: | |
30 | - decoded = unicode(raw_text, encoding=encoding) | |
31 | - return decoded | |
32 | - except UnicodeDecodeError: | |
33 | - pass | |
34 | - | |
35 | - # If none of those guesses worked, let chardet have a go. | |
36 | - encoding = chardet.detect(raw_text)["encoding"] | |
37 | - if encoding and encoding not in encodings: | |
38 | - try: | |
39 | - decoded = unicode(raw_text, encoding=encoding) | |
40 | - logger.debug("File decoded with chardet, encoding was {0}".format( | |
41 | - encoding)) | |
42 | - return decoded | |
43 | - except UnicodeDecodeError: | |
44 | - pass | |
45 | - except LookupError: | |
46 | - pass | |
47 | - | |
48 | - # I've heard that decoding with cp1252 never fails, so try that last. | |
49 | - try: | |
50 | - decoded = unicode(raw_text, encoding="cp1252") | |
51 | - logger.debug("File decoded with encoding cp1252") | |
52 | - return decoded | |
53 | - except UnicodeDecodeError: | |
54 | - pass | |
55 | - | |
56 | - # If nothing worked then give up. | |
57 | - return None | |
58 | - | |
59 | - | |
60 | class Error(Exception): | |
61 | """Base class for exceptions in this module.""" | |
62 | pass | |
63 | @@ -192,12 +147,12 @@ class PlainTextNote(object): | |
64 | # subdirs) if they don't exist. | |
65 | directory = os.path.split(self.abspath)[0] | |
66 | if not os.path.isdir(directory): | |
67 | - logger.debug(u"'{0} doesn't exist, creating it".format(directory)) | |
68 | + logger.debug("'{0} doesn't exist, creating it".format(directory)) | |
69 | try: | |
70 | os.makedirs(directory) | |
71 | except os.error as e: | |
72 | raise NewNoteError( | |
73 | - u"{0} could not be created: {1}".format(directory, e)) | |
74 | + "{0} could not be created: {1}".format(directory, e)) | |
75 | ||
76 | # Create an empty file if the file doesn't exist. | |
77 | open(self.abspath, 'a') | |
78 | @@ -217,11 +172,11 @@ class PlainTextNote(object): | |
79 | ||
80 | @property | |
81 | def contents(self): | |
82 | - contents = unicode_or_bust(open(self.abspath, "r").read()) | |
83 | + contents = open(self.abspath, "rb").read().decode(errors='ignore') | |
84 | if contents is None: | |
85 | logger.error( | |
86 | - u"Could not decode file contents: {0}".format(self.abspath)) | |
87 | - return u"" | |
88 | + "Could not decode file contents: {0}".format(self.abspath)) | |
89 | + return "" | |
90 | else: | |
91 | return contents | |
92 | ||
93 | @@ -322,12 +277,12 @@ class PlainTextNoteBook(object): | |
94 | ||
95 | # Create notebook_dir if it doesn't exist. | |
96 | if not os.path.isdir(self.path): | |
97 | - logger.debug(u"'{0} doesn't exist, creating it".format(self.path)) | |
98 | + logger.debug("'{0} doesn't exist, creating it".format(self.path)) | |
99 | try: | |
100 | os.makedirs(self.path) | |
101 | except os.error as e: | |
102 | raise NewNoteBookError( | |
103 | - u"{0} could not be created: {1}".format(self.path, e)) | |
104 | + "{0} could not be created: {1}".format(self.path, e)) | |
105 | else: | |
106 | # TODO: Check that self.path is a directory, if not raise. | |
107 | pass | |
108 | @@ -358,13 +313,12 @@ class PlainTextNoteBook(object): | |
109 | abspath = os.path.join(root, filename) | |
110 | relpath = os.path.relpath(abspath, self.path) | |
111 | relpath, ext = os.path.splitext(relpath) | |
112 | - unicode_relpath = unicode_or_bust(relpath) | |
113 | if relpath is None: | |
114 | # The filename could not be decoded. | |
115 | logger.error( | |
116 | "Could not decode filename: {0}".format(relpath)) | |
117 | else: | |
118 | - self.add_new(title=unicode_relpath, extension=ext) | |
119 | + self.add_new(title=relpath, extension=ext) | |
120 | ||
121 | @property | |
122 | def path(self): | |
123 | @@ -418,7 +372,7 @@ class PlainTextNoteBook(object): | |
124 | for note in self._notes: | |
125 | if note.title == title and note.extension == extension: | |
126 | raise NoteAlreadyExistsError( | |
127 | - u"Note already in NoteBook: {0}".format(note.title)) | |
128 | + "Note already in NoteBook: {0}".format(note.title)) | |
129 | ||
130 | # Ok, add the note. | |
131 | note = PlainTextNote(title, self, extension) | |
132 | diff --git a/terminal_velocity/terminal_velocity.py b/terminal_velocity/terminal_velocity.py | |
133 | index 5f0e213..9234bea 100755 | |
134 | --- a/terminal_velocity/terminal_velocity.py | |
135 | +++ b/terminal_velocity/terminal_velocity.py | |
136 | @@ -1,7 +1,7 @@ | |
137 | -#!/usr/bin/env python2 | |
138 | +#!/usr/bin/env python3 | |
139 | """A fast note-taking app for the UNIX terminal""" | |
140 | from __future__ import print_function | |
141 | -import ConfigParser | |
142 | +import configparser | |
143 | import argparse | |
144 | import os | |
145 | import logging | |
146 | @@ -9,9 +9,9 @@ import logging.handlers | |
147 | import sys | |
148 | ||
149 | #import terminal_velocity.urwid_ui as urwid_ui | |
150 | -import urwid_ui | |
151 | +from . import urwid_ui | |
152 | ||
153 | -from git import get_git_project_config, git_project_is_configured, fetch_changes, push_changes | |
154 | +from .git import get_git_project_config, git_project_is_configured, fetch_changes, push_changes | |
155 | ||
156 | ||
157 | def startup(): | |
158 | @@ -37,7 +37,7 @@ def main(): | |
159 | ||
160 | # Parse the config file. | |
161 | config_file = os.path.abspath(os.path.expanduser(args.config)) | |
162 | - config = ConfigParser.SafeConfigParser() | |
163 | + config = configparser.ConfigParser() | |
164 | config.read(config_file) | |
165 | defaults = dict(config.items('DEFAULT')) | |
166 | ||
167 | diff --git a/terminal_velocity/urwid_ui.py b/terminal_velocity/urwid_ui.py | |
168 | index caebcb9..89bab35 100644 | |
169 | --- a/terminal_velocity/urwid_ui.py | |
170 | +++ b/terminal_velocity/urwid_ui.py | |
171 | @@ -10,7 +10,7 @@ import logging | |
172 | logger = logging.getLogger(__name__) | |
173 | ||
174 | import urwid | |
175 | -import notebook | |
176 | +from . import notebook | |
177 | ||
178 | ||
179 | palette = [ | |
180 | @@ -27,8 +27,6 @@ def system(cmd, loop): | |
181 | ||
182 | loop.screen.stop() | |
183 | ||
184 | - cmd = u"{0}".format(cmd) | |
185 | - cmd = cmd.encode("utf-8") # FIXME: Correct encoding? | |
186 | safe_cmd = shlex.split(cmd) | |
187 | ||
188 | logger.debug("System command: {0}".format(safe_cmd)) | |
189 | @@ -114,7 +112,7 @@ class AutocompleteWidget(urwid.Edit): | |
190 | ||
191 | # When search bar is empty show placeholder text. | |
192 | if not self.edit_text and not self.autocomplete_text: | |
193 | - placeholder_text = u"Find or Create" | |
194 | + placeholder_text = "Find or Create" | |
195 | return (placeholder_text, | |
196 | [("placeholder", len(placeholder_text))]) | |
197 | ||
198 | @@ -186,7 +184,7 @@ class NoteFilterListBox(urwid.ListBox): | |
199 | ||
200 | def render(self, size, focus=False): | |
201 | if len(self.list_walker) == 0: | |
202 | - placeholder = placeholder_text(u"No matching notes, press Enter " | |
203 | + placeholder = placeholder_text("No matching notes, press Enter " | |
204 | "to create a new note") | |
205 | return placeholder.render(size) | |
206 | return super(NoteFilterListBox, self).render(size, self.fake_focus) | |
207 | @@ -399,7 +397,7 @@ class MainFrame(urwid.Frame): | |
208 | # If the user has no notes yet show some placeholder text, otherwise | |
209 | # show the note list. | |
210 | if len(self.notebook) == 0: | |
211 | - self.body = placeholder_text(u"You have no notes yet, to create " | |
212 | + self.body = placeholder_text("You have no notes yet, to create " | |
213 | "a note type a note title then press Enter") | |
214 | else: | |
215 | self.body = urwid.Padding(self.list_box, left=1, right=1) |