Skip to content

Commit

Permalink
fix: Introduce user-editable settings file
Browse files Browse the repository at this point in the history
This commit introduces a user-editable settings file for the Simplenote plugin, allowing users to customize syncing behavior, autosave options, and file extension support.

The new settings file provides more flexibility and control over the plugin's functionality.
  • Loading branch information
RedAtman committed Aug 1, 2024
1 parent 366a412 commit 777c58e
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 85 deletions.
2 changes: 1 addition & 1 deletion _config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def __init_subclass__(cls, **kwargs):

SIMPLENOTE_INSTALLED_PACKAGE_DIR = os.path.join(installed_packages_path(), SIMPLENOTE_PROJECT_NAME)
SIMPLENOTE_PACKAGE_DIR = os.path.join(packages_path(), SIMPLENOTE_PROJECT_NAME)
os.makedirs(SIMPLENOTE_PACKAGE_DIR, exist_ok=True)
# os.makedirs(SIMPLENOTE_PACKAGE_DIR, exist_ok=True)

SUBLIME_USER_DIR = os.path.join(packages_path(), "User")
SIMPLENOTE_SETTINGS_FILE_PATH = SIMPLENOTE_SETTINGS_FILE
Expand Down
67 changes: 60 additions & 7 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,51 @@
"close_view",
]

settings_content = """
{
// --------------------------------
// Credentials:
// --------------------------------
"username": ""
,"password": ""
// --------------------------------
// Sync settings:
// --------------------------------
// Sync when sublime text starts:
,"autostart": true
// Sync automatically (in seconds)
,"sync_every": 30
// Number of notes synchronized each time
,"sync_note_number": 1000
// Conflict resolution (If a file was edited on another client and also here, on sync..)
// Server Wins (Same as selecting 'Overwrite')
,"on_conflict_use_server": false
// Local is left unchanged (Same as selecting 'Cancel')
,"on_conflict_leave_alone": false
// --------------------------------
// Autosave (beta)
// --------------------------------
// Activate autosave and tell how much (in seconds) to wait
// after you stop typing to send the save
,"autosave_debounce_time": 1
// --------------------------------
// File extension support
// --------------------------------
// Which file extension should the temporal files use?
// This allows you to interact with other plugins such as
// PlainTasks defining an extension for certain note title
,"title_extension_map": [{
"title_regex": "\\[ST\\]"
,"extension": "todo"
},
{
"title_regex": "\\# "
,"extension": "md"
,"systemTags": ["markdown"]
}]
}
"""


def edit_settings():
installed_package_settings_file = os.path.join(
Expand All @@ -34,15 +79,23 @@ def edit_settings():
else:
settings_file = CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH

logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
logger.warning(settings_file)
sublime.run_command("open_file", {"file": settings_file})
# logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))

# settings_file = os.path.join(
# sublime.installed_packages_path(), "Simplenote.sublime-package", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH
# )
# settings_file = (
# """/Users/nut/Library/Application Support/Sublime Text/Packages/Simplenote/Simplenote.sublime-settings"""
# )
# settings_file = os.path.join(os.path.dirname(__file__), settings_file)
logger.warning((type(settings_file), settings_file))
# with open(settings_file) as f:
# settings = f.read()
# logger.warning(settings)
# sublime.run_command("open_file", {"file": settings_file})
sublime.run_command(
"edit_settings",
{
"base_file": settings_file,
"default": "// Simplenote Settings - User\n{\n\t$0\n}\n",
},
{"base_file": settings_file, "default": "// Simplenote Settings - User\n{\n\t$0\n}\n" + settings_content},
)


Expand Down
86 changes: 86 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import logging

import sublime

from ._config import CONFIG
from .gui import edit_settings, remove_status, show_message
from .models import Note
from .operations import OperationManager


logger = logging.getLogger()

SIMPLENOTE_RELOAD_CALLS = -1
SIMPLENOTE_STARTED = False


def sync():
manager = OperationManager()
if not manager.running:
sublime.run_command("simplenote_sync")
else:
logger.debug("Sync omitted")

settings = sublime.load_settings(CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH)
sync_every = settings.get("sync_every", 0)
logger.debug(("Simplenote sync_every", sync_every))
if not isinstance(sync_every, int):
show_message("`sync_every` must be an integer. Please check settings file.")
return

if sync_every > 0:
sublime.set_timeout(sync, sync_every * 1000)


def start():
global SIMPLENOTE_STARTED
logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
username = settings.get("username")
password = settings.get("password")

if username and password:
sync()
SIMPLENOTE_STARTED = True
else:
edit_settings()
show_message("Simplenote: Please configure username/password, Please check settings file.")
sublime.set_timeout(remove_status, 2000)
SIMPLENOTE_STARTED = False
return SIMPLENOTE_STARTED


def reload_if_needed():
global SIMPLENOTE_RELOAD_CALLS

# Sublime calls this twice for some reason :(
SIMPLENOTE_RELOAD_CALLS += 1
if SIMPLENOTE_RELOAD_CALLS % 2 != 0:
logger.debug("Simplenote Reload call %s" % SIMPLENOTE_RELOAD_CALLS)
return

logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
autostart = settings.get("autostart")
if bool(autostart):
autostart = True
logger.debug(("Simplenote Reloading", autostart))
if autostart:
sublime.set_timeout(start, 2000)
logger.debug("Auto Starting")


def plugin_loaded():
# load_notes()
logger.debug(("Loaded notes number: ", Note.tree.count))

logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
# logger.debug(("SETTINGS.__dict__: ", SETTINGS.__dict__))
# logger.debug(("SETTINGS.username: ", SETTINGS.get("username")))
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()
78 changes: 1 addition & 77 deletions simplenotecommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sublime_plugin

from ._config import CONFIG
from .gui import close_view, edit_settings, open_view, remove_status, show_message
from .gui import close_view, open_view, show_message
from .models import Note
from .operations import NoteCreator, NoteDeleter, NotesIndicator, NoteUpdater, OperationManager
from .simplenote import clear_orphaned_filepaths, on_note_changed
Expand All @@ -19,10 +19,6 @@
"SimplenoteSyncCommand",
"SimplenoteCreateCommand",
"SimplenoteDeleteCommand",
"sync",
"start",
"reload_if_needed",
"plugin_loaded",
]


Expand Down Expand Up @@ -225,75 +221,3 @@ def run(self):
note_deleter = NoteDeleter(note=note)
note_deleter.set_callback(self.handle_deletion, {"view": view})
OperationManager().add_operation(note_deleter)


def sync():
manager = OperationManager()
if not manager.running:
sublime.run_command("simplenote_sync")
else:
logger.debug("Sync omitted")

settings = sublime.load_settings(CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH)
sync_every = settings.get("sync_every", 0)
logger.debug(("Simplenote sync_every", sync_every))
if not isinstance(sync_every, int):
show_message("`sync_every` must be an integer. Please check settings file.")
return

if sync_every > 0:
sublime.set_timeout(sync, sync_every * 1000)


def start():
global SIMPLENOTE_STARTED
logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
username = settings.get("username")
password = settings.get("password")

if username and password:
sync()
SIMPLENOTE_STARTED = True
else:
edit_settings()
show_message("Simplenote: Please configure username/password, Please check settings file.")
sublime.set_timeout(remove_status, 2000)
SIMPLENOTE_STARTED = False
return SIMPLENOTE_STARTED


def reload_if_needed():
global SIMPLENOTE_RELOAD_CALLS

# Sublime calls this twice for some reason :(
SIMPLENOTE_RELOAD_CALLS += 1
if SIMPLENOTE_RELOAD_CALLS % 2 != 0:
logger.debug("Simplenote Reload call %s" % SIMPLENOTE_RELOAD_CALLS)
return

logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
autostart = settings.get("autostart")
if bool(autostart):
autostart = True
logger.debug(("Simplenote Reloading", autostart))
if autostart:
sublime.set_timeout(start, 2000)
logger.debug("Auto Starting")


def plugin_loaded():
# load_notes()
logger.debug(("Loaded notes number: ", Note.tree.count))

logger.warning(("CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH", CONFIG.SIMPLENOTE_SETTINGS_FILE_PATH))
settings = sublime.load_settings("Simplenote.sublime-settings")
# logger.debug(("SETTINGS.__dict__: ", SETTINGS.__dict__))
# logger.debug(("SETTINGS.username: ", SETTINGS.get("username")))
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()

0 comments on commit 777c58e

Please sign in to comment.