commit 6ca19964b9e8a7866fd7e21a3dac9ccd35f0d434 Author: Ismaƫl Bouya 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)