forked from facelessuser/RegReplace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrr_plugin.py
80 lines (65 loc) · 2.06 KB
/
rr_plugin.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
"""
Reg Replace.
Licensed under MIT
Copyright (c) 2011 - 2015 Isaac Muse <[email protected]>
"""
import sublime
import imp
import sys
# import traceback
from os.path import join, normpath
import re
def sublime_format_path(pth):
"""Format path for Sublime."""
m = re.match(r"^([A-Za-z]{1}):(?:/|\\)(.*)", pth)
if sublime.platform() == "windows" and m is not None:
pth = m.group(1) + "/" + m.group(2)
return pth.replace("\\", "/")
class Plugin(object):
"""Load plugins for RegReplace."""
loaded = []
@classmethod
def purge(cls):
"""Purge list of loaded plugins."""
cls.loaded = []
@classmethod
def get_module(cls, module_name, path_name):
"""Get the requested module."""
module = None
try:
module = sys.modules[module_name]
except Exception:
module = cls.load_module(module_name, path_name)
return module
@classmethod
def load_module(cls, module_name, path_name):
"""Load the requested module."""
module = imp.new_module(module_name)
sys.modules[module_name] = module
exec(
compile(
sublime.load_resource(sublime_format_path(path_name)),
module_name,
'exec'
),
sys.modules[module_name].__dict__
)
return module
@classmethod
def load(cls, module_name):
"""Load module."""
if module_name.startswith("rr_modules."):
path_name = join("Packages", "RegReplace", normpath(module_name.replace('.', '/')))
else:
path_name = join("Packages", normpath(module_name.replace('.', '/')))
path_name += ".py"
module = None
if module_name in cls.loaded:
module = cls.get_module(module_name, path_name)
else:
module = cls.load_module(module_name, path_name)
return module
@classmethod
def load_from(cls, module_name, attribute):
"""Load from a module."""
return getattr(cls.load(module_name), attribute)