Skip to content

Commit

Permalink
[Fix] Use the sublime.(find|load)_resources APIs.
Browse files Browse the repository at this point in the history
This commit fixes issues that cropped up because I had CommandsBrowser
in the packages directory. I was loading JSON files directly from the
folder, but when the package will be installed, that folder will go into
the installed packages and hence not available. So it can't read the files.

So, the above API usage will ensure that it works when both installed &
git cloned.
  • Loading branch information
UltraInstinct05 committed Nov 15, 2021
1 parent e677016 commit 782b89c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
13 changes: 5 additions & 8 deletions commands_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ def plugin_loaded():
"""
load_commands_browser_settings()

browse_33_resource = [a for a in sublime.find_resources("*.py") if a.startswith("Packages/CommandsBrowser/src/py33")][0]

commands_browser_33_path = os.path.join(sublime.packages_path(), "CommandsBrowser33")
file_to_copy_path = os.path.join(sublime.packages_path(), "CommandsBrowser", "src/py33/browse.py")
dest_file_path = os.path.join(commands_browser_33_path, "browse.py")

if not os.path.exists(commands_browser_33_path):
os.makedirs(commands_browser_33_path)
if not os.path.exists(dest_file_path):
with open(dest_file_path, "w"):
pass

try:
shutil.copyfile(file_to_copy_path, dest_file_path)
except Exception as e:
print(f"[CommandsBrowser]: Failed to add CommandsBrowser33 with exception {e}")
with open(dest_file_path, "w") as f:
f.write(sublime.load_resource(browse_33_resource))


def plugin_unloaded():
Expand Down
12 changes: 5 additions & 7 deletions src/utils/core_commands_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ def get_core_commands_data(application = "st"):
Returns:
final_dict (Dict): The final dictionary of commands and their docs.
"""
package_path = os.path.join(sublime.packages_path(), "CommandsBrowser")
metadata_folder = os.path.join(package_path, f"{application}_commands_metadata")
json_file_names = [name + ".json" for name in list(string.ascii_lowercase)]

json_file_names = [a for a in sublime.find_resources("*.json") if a.startswith(f"Packages/CommandsBrowser/{application}_commands_metadata")]
final_dict = {}
for file_name in json_file_names:
with open(os.path.join(metadata_folder, file_name), "r") as file:
data = json.loads(file.read())
if data is not None:
final_dict.update(data)
data = json.loads(sublime.load_resource(file_name))
if data is not None:
final_dict.update(data)
return final_dict

0 comments on commit 782b89c

Please sign in to comment.