forked from ihdavids/orgextended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orgextension.py
94 lines (84 loc) · 3.15 KB
/
orgextension.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# LOADING Extension modules.
# Dynamic Blocks, Links and the Agenda plan to use this mechanism.
# My extensions can live in subfolder and are loaded dynamically
# YOUR Extensions can live in User and be loaded dynamically.
import sublime
import sublime_plugin
import datetime
import re
from pathlib import Path
import os
import fnmatch
import logging
import sys
import OrgExtended.asettings as sets
import importlib
log = logging.getLogger(__name__)
lastModCache = {}
def load_module(basemodule, folder, filename,force = False):
if sys.version_info[0] < 3:
module_path = folder + '.' + filename.split('.')[0]
name = filename.split('.')[0]
module = __import__(module_path, globals(), locals(), name)
module = reload(module)
else:
module_path = basemodule + '.'+ folder +'.' + filename.split('.')[0]
#for m in sys.modules:
# if(module_path in m):
# print("KEY: " + str(m))
if force and module_path in sys.modules:
del sys.modules[module_path]
module = importlib.import_module(module_path)
return module
def GetUserFolder():
base = sublime.packages_path()
#base = os.path.dirname(os.path.abspath(__file__))
return os.path.join(base,'User')
def find_extension_modules(folder, builtins):
importlib.invalidate_caches()
#base = os.path.dirname(os.path.abspath(__file__))
base = sublime.packages_path()
path = base + '/' + folder
moduleTable = {}
# Built in extensions
#for root, dirnames, filenames in os.walk(path):
# for filename in fnmatch.filter(filenames, '*.py'):
# if '__init__' in filename or 'abstract' in filename:
# continue
for filename in builtins:
filename = filename + ".py"
force = sets.Get("forceLoadInternalExtensions",False)
module = load_module("OrgExtended", folder, filename, force)
moduleTable[filename.split('.')[0]] = module
# User generated extensions
path = os.path.join(base,'User',folder)
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, '*.py'):
if '__init__' in filename or 'abstract' in filename:
continue
# Only reload if the file is newer.
# NOTE: Due to how import works, it will not
# reload the file until sublime reloads
# so we have to track that ourselves
# in the loadModCache.
fullfilename = os.path.join(path,filename)
lastMod = os.path.getmtime(fullfilename)
force = fullfilename not in lastModCache or lastMod > lastModCache[fullfilename] or sets.Get("forceLoadExternalExtensions",False)
lastModCache[fullfilename] = lastMod
module = load_module("User", folder, filename, force)
moduleTable[filename.split('.')[0]] = module
return moduleTable
def find_extension_file(folder,name,extension='.py'):
base = os.path.dirname(os.path.abspath(__file__))
# User generated extensions
path = base + '/../User/' + folder
fname = name + extension
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, fname):
return "Packages/User/" + folder + "/" + filename
# Built in extensions
path = base + '/' + folder
for root, dirnames, filenames in os.walk(path):
for filename in fnmatch.filter(filenames, fname):
return "Packages/OrgExtended/" + folder + "/" + filename
return None