From f1a062a5f2cf970b6e91c473ce5e4e54cf353785 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Thu, 7 May 2020 11:56:53 +0200 Subject: [PATCH 1/4] top level init This way, the code could be directly used from git while keeping most source in src. Toplevel config link for the same reason --- __init__.py | 1 + config.json | 1 + 2 files changed, 2 insertions(+) create mode 100644 __init__.py create mode 120000 config.json diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..72dfb0d --- /dev/null +++ b/__init__.py @@ -0,0 +1 @@ +from .src.syntax_highlighting import main diff --git a/config.json b/config.json new file mode 120000 index 0000000..b0f4ea7 --- /dev/null +++ b/config.json @@ -0,0 +1 @@ +src/syntax_highlighting/config.json \ No newline at end of file From 48a66d7b7402b4e7ab6cbcdf6a609458be7a3f82 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Thu, 7 May 2020 11:56:44 +0200 Subject: [PATCH 2/4] use get_conf instead of conf access --- src/syntax_highlighting/config.py | 6 +++--- src/syntax_highlighting/main.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/syntax_highlighting/config.py b/src/syntax_highlighting/config.py index f19f392..8f4e67f 100644 --- a/src/syntax_highlighting/config.py +++ b/src/syntax_highlighting/config.py @@ -28,7 +28,7 @@ ############################################################### # Defaults conf -# - we create a new item in mw.col.conf. This syncs the +# - we create a new item in the collection's configuration. This syncs the # options across machines (but not on mobile) default_conf = {'linenos': True, # show numbers by default 'centerfragments': True, # Use
when generating code fragments @@ -52,9 +52,9 @@ def sync_keys(tosync, ref): def sync_config_with_default(col): if not 'syntax_highlighting_conf' in col.conf: - col.conf['syntax_highlighting_conf'] = default_conf + col.set_config('syntax_highlighting_conf', default_conf) else: - sync_keys(col.conf['syntax_highlighting_conf'], default_conf) + sync_keys(col.get_config('syntax_highlighting_conf'), default_conf) # Mark collection state as modified, else config changes get lost unless # some unrelated action triggers the flush of collection data to db diff --git a/src/syntax_highlighting/main.py b/src/syntax_highlighting/main.py index b6da9f6..8d6ffd9 100644 --- a/src/syntax_highlighting/main.py +++ b/src/syntax_highlighting/main.py @@ -83,7 +83,7 @@ def get_deck_name(mw): def get_default_lang(mw): - addon_conf = mw.col.conf['syntax_highlighting_conf'] + addon_conf = mw.col.get_config('syntax_highlighting_conf') lang = addon_conf['lang'] if addon_conf['defaultlangperdeck']: deck_name = get_deck_name(mw) @@ -93,7 +93,7 @@ def get_default_lang(mw): def set_default_lang(mw, lang): - addon_conf = mw.col.conf['syntax_highlighting_conf'] + addon_conf = mw.col.get_config('syntax_highlighting_conf') addon_conf['lang'] = lang # Always update the overall default if addon_conf['defaultlangperdeck']: deck_name = get_deck_name(mw) @@ -125,7 +125,7 @@ def switch_cssclasses(self): self.addon_conf['cssclasses'] = not cssclasses_ def setupUi(self): - self.addon_conf = self.mw.col.conf['syntax_highlighting_conf'] + self.addon_conf = self.mw.col.get_config('syntax_highlighting_conf') linenos_label = QLabel('Line numbers') linenos_checkbox = QCheckBox('') @@ -349,7 +349,7 @@ def onBridgeCmd(ed, cmd, _old): def highlight_code(ed): - addon_conf = mw.col.conf['syntax_highlighting_conf'] + addon_conf = mw.col.get_config('syntax_highlighting_conf') # Do we want line numbers? linenos is either true or false according # to the user's preferences From 74e268fa4c5a62d0414f86dbd282ad238aec53c9 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Thu, 7 May 2020 12:01:16 +0200 Subject: [PATCH 3/4] remove 2.0 code --- src/syntax_highlighting/config.py | 108 ++---------------------------- src/syntax_highlighting/main.py | 37 ++-------- 2 files changed, 9 insertions(+), 136 deletions(-) diff --git a/src/syntax_highlighting/config.py b/src/syntax_highlighting/config.py index 8f4e67f..47fab98 100644 --- a/src/syntax_highlighting/config.py +++ b/src/syntax_highlighting/config.py @@ -69,109 +69,9 @@ def setupSyncedConf(): # Local conf -if anki21: - def getConfig(): - return mw.addonManager.getConfig(__name__) - - def writeConfig(config): - mw.addonManager.writeConfig(__name__, config) - -else: - def _addonMeta(): - """Get meta dictionary - - Reads in meta.json in add-on folder and returns - resulting dictionary of user-defined metadata values. - - Note: - Anki 2.1 stores both add-on meta data and customized - settings in meta.json. In this module we are only dealing - with the settings part. - - Returns: - dict: config dictionary - - """ - - try: - meta = json.load(io.open(meta_path, encoding="utf-8")) - except (IOError, OSError): - meta = None - except json.decoder.JSONDecodeError as e: - print("Could not read meta.json: " + str(e)) - meta = None - - if not meta: - meta = {"config": _addonConfigDefaults()} - _writeAddonMeta(meta) - - return meta - - def _writeAddonMeta(meta): - """Write meta dictionary - - Writes meta dictionary to meta.json in add-on folder. - - Args: - meta (dict): meta dictionary - - """ - - with io.open(meta_path, 'w', encoding="utf-8") as f: - f.write(unicode(json.dumps(meta, indent=4, - sort_keys=True, - ensure_ascii=False))) - - def _addonConfigDefaults(): - """Get default config dictionary - - Reads in config.json in add-on folder and returns - resulting dictionary of default config values. - - Returns: - dict: config dictionary - - Raises: - Exception: If config.json cannot be parsed correctly. - (The assumption being that we would end up in an - inconsistent state if we were to return an empty - config dictionary. This should never happen.) - - """ - - try: - return json.load(io.open(defaults_path, encoding="utf-8")) - except (IOError, OSError, json.decoder.JSONDecodeError) as e: - print("Could not read config.json: " + str(e)) - raise Exception("Config file could not be read: " + str(e)) - - def getConfig(): - """Get user config dictionary - - Merges user's keys into default config dictionary - and returns the result. - - Returns: - dict: config dictionary - - """ - - config = _addonConfigDefaults() - meta = _addonMeta() - userConf = meta.get("config", {}) - config.update(userConf) - return config - - def writeConfig(config): - """Write user config dictionary - - Saves user's config dictionary via meta.json. - - Args: - config (dict): user config dictionary - - """ - - _writeAddonMeta({"config": config}) +def getConfig(): + return mw.addonManager.getConfig(__name__) +def writeConfig(config): + mw.addonManager.writeConfig(__name__, config) local_conf = getConfig() diff --git a/src/syntax_highlighting/main.py b/src/syntax_highlighting/main.py index 8d6ffd9..406eaed 100644 --- a/src/syntax_highlighting/main.py +++ b/src/syntax_highlighting/main.py @@ -38,11 +38,6 @@ from .config import local_conf -if anki21: - string = str -else: - import string - HOTKEY = local_conf["hotkey"] STYLE = local_conf["style"] @@ -186,9 +181,6 @@ def init_highlighter(ed, *args, **kwargs): previous_lang = get_default_lang(mw) ed.codeHighlightLangAlias = LANGUAGES_MAP.get(previous_lang, "") - if not anki21: - addWidgets20(ed, previous_lang) - # Highlighter widgets @@ -256,7 +248,7 @@ def add_code_langs_combobox(self, func, previous_lang): if LIMITED_LANGS: selection = LIMITED_LANGS else: - selection = sorted(LANGUAGES_MAP.keys(), key=string.lower) + selection = sorted(LANGUAGES_MAP.keys(), key=str.lower) for lang in selection: combo.addItem(lang) @@ -270,24 +262,6 @@ def add_code_langs_combobox(self, func, previous_lang): QSplitter.add_code_langs_combobox = add_code_langs_combobox -def addWidgets20(ed, previous_lang): - # Add the buttons to the Icon Box - splitter = QSplitter() - splitter.add_plugin_button_(ed, - "highlight_code", - lambda _: highlight_code(ed), - key=HOTKEY, - text="", - icon=icon_path, - tip=_("Paste highlighted code ({})".format(HOTKEY)), - check=False) - splitter.add_code_langs_combobox( - lambda lang: onCodeHighlightLangSelect(ed, lang), previous_lang) - splitter.setFrameStyle(QFrame.Plain) - rect = splitter.frameRect() - splitter.setFrameRect(rect.adjusted(10, 0, -10, 0)) - ed.iconsBox.addWidget(splitter) - def onCodeHighlightLangSelect(ed, lang): try: @@ -308,7 +282,7 @@ def onCodeHighlightLangSelect(ed, lang): """style='vertical-align: top;'>{}""") -def onSetupButtons21(buttons, ed): +def onSetupButtons(buttons, ed): """Add buttons to Editor for Anki 2.1.x""" # no need for a lambda since onBridgeCmd passes current editor instance # to method anyway (cf. "self._links[cmd](self)") @@ -326,7 +300,7 @@ def onSetupButtons21(buttons, ed): if LIMITED_LANGS: selection = LIMITED_LANGS else: - selection = sorted(LANGUAGES_MAP.keys(), key=string.lower) + selection = sorted(LANGUAGES_MAP.keys(), key=str.lower) options.append(option_str.format(previous_lang)) for lang in selection: @@ -433,8 +407,7 @@ def process_html(html): # Hooks and monkey-patches -if anki21: - addHook("setupEditorButtons", onSetupButtons21) - Editor.onBridgeCmd = wrap(Editor.onBridgeCmd, onBridgeCmd, "around") +addHook("setupEditorButtons", onSetupButtons) +Editor.onBridgeCmd = wrap(Editor.onBridgeCmd, onBridgeCmd, "around") Editor.__init__ = wrap(Editor.__init__, init_highlighter) From 02d1fb384edd90c8edae334c32b72b5de26ceb32 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Thu, 7 May 2020 12:04:46 +0200 Subject: [PATCH 4/4] ignore some useless files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 854a3dd..8d90a2e 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,5 @@ resources/icons/optional # Anki src/*/meta.json src/*/manifest.json +/meta.json +*~