From 777c58ec5d74465df2e4b81ac52322e8b41bb5da Mon Sep 17 00:00:00 2001 From: redatman Date: Thu, 1 Aug 2024 18:57:19 +0800 Subject: [PATCH] fix: Introduce user-editable settings file 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. --- _config.py | 2 +- gui.py | 67 +++++++++++++++++++++++++++++---- main.py | 86 +++++++++++++++++++++++++++++++++++++++++++ simplenotecommands.py | 78 +-------------------------------------- 4 files changed, 148 insertions(+), 85 deletions(-) create mode 100644 main.py diff --git a/_config.py b/_config.py index cfc83c2..343a418 100644 --- a/_config.py +++ b/_config.py @@ -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 diff --git a/gui.py b/gui.py index b71be37..be99fa9 100644 --- a/gui.py +++ b/gui.py @@ -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( @@ -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}, ) diff --git a/main.py b/main.py new file mode 100644 index 0000000..b1a7103 --- /dev/null +++ b/main.py @@ -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() diff --git a/simplenotecommands.py b/simplenotecommands.py index 8fb4be9..7a2cda7 100644 --- a/simplenotecommands.py +++ b/simplenotecommands.py @@ -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 @@ -19,10 +19,6 @@ "SimplenoteSyncCommand", "SimplenoteCreateCommand", "SimplenoteDeleteCommand", - "sync", - "start", - "reload_if_needed", - "plugin_loaded", ] @@ -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()