Skip to content

Commit

Permalink
Added support for Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
scbtest committed Dec 24, 2014
1 parent a3030db commit 68e1f42
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 84 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
QuickSimplenote
=============

Sublime Text 2 plugin for Simplenote.
Sublime Text plugin for Simplenote.

Overview
---------
Expand Down
28 changes: 14 additions & 14 deletions operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class Operation(Thread):

def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None):
Thread.__init__(self, group, target, name, args, kwargs, Verbose)
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, verbose=None):
super(Operation, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self.callback = None
self.exception_callback = None

Expand Down Expand Up @@ -37,8 +37,8 @@ def get_update_run_text(self):
return None

class NoteCreator(Operation):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, verbose=None, simplenote_instance=None):
super(NoteCreator, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self.simplenote_instance = simplenote_instance

def run(self):
Expand All @@ -59,8 +59,8 @@ def get_result(self):
return self.result

class NoteDownloader(Thread):
def __init__(self, note_id, semaphore, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None):
Thread.__init__(self, group, target, name, args, kwargs, Verbose)
def __init__(self, note_id, semaphore, group=None, target=None, name=None, args=(), kwargs={}, verbose=None, simplenote_instance=None):
super(NoteDownloader, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self.note_id = note_id
self.semaphore = semaphore
self.simplenote_instance = simplenote_instance
Expand All @@ -81,8 +81,8 @@ def join(self):

class MultipleNoteContentDownloader(Operation):

def __init__(self, semaphore, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None, notes=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
def __init__(self, semaphore, group=None, target=None, name=None, args=(), kwargs={}, verbose=None, simplenote_instance=None, notes=None):
super(MultipleNoteContentDownloader, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self.notes = notes
self.semaphore = semaphore
self.simplenote_instance = simplenote_instance
Expand Down Expand Up @@ -116,8 +116,8 @@ def get_update_run_text(self):

class GetNotesDelta(Operation):

def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, verbose=None, simplenote_instance=None):
super(GetNotesDelta, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self.note_resume = []
self.simplenote_instance = simplenote_instance

Expand All @@ -139,8 +139,8 @@ def get_update_run_text(self):
return 'QuickSimplenote: Downloading note list'

class NoteDeleter(Operation):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, note=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, verbose=None, note=None, simplenote_instance=None):
super(NoteDeleter, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self.note = note
self.simplenote_instance = simplenote_instance

Expand All @@ -162,8 +162,8 @@ def get_result(self):
return self.result

class NoteUpdater(Operation):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None, note=None, simplenote_instance=None):
Operation.__init__(self, group, target, name, args, kwargs, Verbose)
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, verbose=None, note=None, simplenote_instance=None):
super(NoteUpdater, self).__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self.note = note
self.simplenote_instance = simplenote_instance

Expand Down
95 changes: 59 additions & 36 deletions quick_simplenote.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sublime, sublime_plugin
from simplenote import Simplenote
from .simplenote import Simplenote

import functools
import time
Expand All @@ -9,7 +9,7 @@
from datetime import datetime
from threading import Semaphore, Lock

from operations import NoteCreator, MultipleNoteContentDownloader, GetNotesDelta, NoteDeleter, NoteUpdater
from .operations import NoteCreator, MultipleNoteContentDownloader, GetNotesDelta, NoteDeleter, NoteUpdater

def cmp_to_key(mycmp):
'Convert a cmp= function into a key= function'
Expand Down Expand Up @@ -38,7 +38,7 @@ def sort_notes(a_note, b_note):
else:
date_a = datetime.fromtimestamp(float(a_note['modifydate']))
date_b = datetime.fromtimestamp(float(b_note['modifydate']))
return cmp(date_a, date_b)
return (date_a > date_b) - (date_a < date_b)

def show_message(message):
if not message:
Expand All @@ -54,7 +54,7 @@ def write_note_to_path(note, filepath):
f = open(filepath, 'wb')
try:
content = note['content']
f.write(content)
f.write(content.encode('utf-8'))
except KeyError:
pass
f.close()
Expand All @@ -64,7 +64,7 @@ def open_note(note, window=None):
window = sublime.active_window()
filepath = get_path_for_note(note)
write_note_to_path(note, filepath)
window.open_file(filepath)
return window.open_file(filepath)

def get_filename_for_note(note):
# Take out invalid characters from title and use that as base for the name
Expand Down Expand Up @@ -96,7 +96,7 @@ def get_note_from_path(view_filepath):
note = [note for note in notes if get_filename_for_note(note) == note_filename]
if not note:
import re
pattern = re.compile(ur'\((.*?)\)')
pattern = re.compile(r'\((.*?)\)')
results = re.findall(pattern, note_filename)
if results:
noteKey = results[ len(results) - 1]
Expand All @@ -110,7 +110,7 @@ def get_note_from_path(view_filepath):
def get_note_name(note):
try:
content = note['content']
except Exception, e:
except Exception as e:
return 'untitled'
index = content.find('\n');
if index > -1:
Expand All @@ -120,22 +120,42 @@ def get_note_name(note):
title = content
else:
title = 'untitled'
title = title.decode('utf-8')
return title

def handle_open_filename_change(old_file_path, updated_note):
new_file_path = get_path_for_note(updated_note)
old_note_window = None
old_note_view = None
new_view = None
# If name changed
if old_file_path != new_file_path:
old_active = sublime.active_window().active_view()
# Save the current active view because we might lose the focus
old_active_view = sublime.active_window().active_view()
# Search for the view of the open note
for view_list in [window.views() for window in sublime.windows()]:
for view in view_list:
if view.file_name() == old_file_path:
old_note_window = window
if old_note_window:
open_note(updated_note, old_note_window)
close_view(view)
sublime.active_window().focus_view(old_active)
old_note_view = view
break
# If found
if old_note_view:
# Open the note in a new view
new_view = open_note(updated_note, old_note_view.window())
# Close the old dirty note
old_note_view_id = old_note_view.id()
old_active_view_id = old_active_view.id()
if old_note_view.window():
old_note_window_id = old_note_view.window().id()
else:
old_note_window_id = sublime.active_window() # Sometimes this happens on Sublime 2...
close_view(old_note_view)
# Focus on the new view or on the previous one depending
# on where we were
if old_note_view_id == old_active_view_id:
old_note_window = [window for window in sublime.windows() if window.id() == old_note_window_id]
if old_note_window:
old_note_window[0].focus_view(new_view)
else:
sublime.active_window().focus_view(old_active_view)
try:
remove(old_file_path)
except OSError as e:
Expand Down Expand Up @@ -165,7 +185,7 @@ def load_notes():
notes = []
try:
with open(path.join(package_path, 'note_cache'),'rb') as cache_file:
notes = pickle.load(cache_file)
notes = pickle.load(cache_file, encoding='utf-8')
except (EOFError, IOError) as e:
pass
return notes
Expand Down Expand Up @@ -274,7 +294,7 @@ def flush_saves():
sublime.set_timeout(flush_saves, debounce_time)

def get_current_content(self, view):
return view.substr(sublime.Region(0, view.size())).encode('utf-8')
return view.substr(sublime.Region(0, view.size()))

def handle_note_changed(self, modified_note_resume, content, old_file_path, open_view):
global notes
Expand Down Expand Up @@ -589,25 +609,28 @@ def reload_if_needed():
sublime.set_timeout(start, 2000) # I know...
print('QuickSimplenote: Autostarting')

def plugin_loaded():
global package_path, temp_path, settings, notes
package_path = path.join(sublime.packages_path(), "QuickSimplenote")
temp_path = path.join(package_path, "temp")

notes = load_notes()
note_files = [note['filename'] for note in notes]
if not path.exists(temp_path):
makedirs(temp_path)
for f in listdir(temp_path):
if f not in note_files:
remove(path.join(temp_path, f))

settings = sublime.load_settings('quick_simplenote.sublime-settings')
settings.clear_on_change('username')
settings.clear_on_change('password')
settings.add_on_change('username', reload_if_needed)
settings.add_on_change('password', reload_if_needed)

reload_if_needed()

reload_calls = -1
simplenote_instance = None
started = False
notes = []
package_path = path.join(sublime.packages_path(), "QuickSimplenote")
temp_path = path.join(package_path, "temp")

notes = load_notes()
note_files = [note['filename'] for note in notes]
if not path.exists(temp_path):
makedirs(temp_path)
for f in listdir(temp_path):
if f not in note_files:
remove(path.join(temp_path, f))

settings = sublime.load_settings('quick_simplenote.sublime-settings')
settings.clear_on_change('username')
settings.clear_on_change('password')
settings.add_on_change('username', reload_if_needed)
settings.add_on_change('password', reload_if_needed)

reload_if_needed()
notes = []
16 changes: 5 additions & 11 deletions simplenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
Python library for accessing the Simplenote API
:copyright: (c) 2011 by Daniel Schauenberg
:ported to python 3 by sickmartian
:license: MIT, see LICENSE for more details.
:ported to python 3 by sickmartian
Changed the way the tags and content fields are
returned, using utf-8 directly instead of bytes
"""

import urllib.request, urllib.parse, urllib.error
Expand Down Expand Up @@ -109,11 +112,6 @@ def get_note(self, noteid, version=None):
except IOError as e:
return e, -1
note = json.loads(response.read().decode('utf-8'))
# use UTF-8 encoding
note["content"] = note["content"]
# For early versions of notes, tags not always available
if "tags" in note:
note["tags"] = [t for t in note["tags"]]
return note, 0

def update_note(self, note):
Expand All @@ -130,6 +128,7 @@ def update_note(self, note):
- status (int): 0 on sucesss and -1 otherwise
"""

# determine whether to create a new note or updated an existing one
if "key" in note:
# set modification timestamp if not set by client
Expand All @@ -147,11 +146,6 @@ def update_note(self, note):
except IOError as e:
return e, -1
note = json.loads(response.read().decode('utf-8'))
if "content" in note:
# use UTF-8 encoding
note["content"] = note["content"]
if "tags" in note:
note["tags"] = [t for t in note["tags"]]
return note, 0

def add_note(self, note):
Expand Down
22 changes: 0 additions & 22 deletions test_simplenote.py

This file was deleted.

0 comments on commit 68e1f42

Please sign in to comment.