forked from HelloZeroNet/Plugin-BackgroundProcessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSitePlugin.py
88 lines (72 loc) · 2.64 KB
/
SitePlugin.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
from Plugin import PluginManager
from .spawner import Spawner
from . import storage
@PluginManager.registerTo("Site")
class SitePlugin(object):
def __init__(self, *args, **kwags):
super(SitePlugin, self).__init__(*args, **kwags)
# Now spawn background process if needed
io = {
"output": self.backgroundOutput,
"input": self.backgroundInput,
"readModule": self.readModule,
"allowed_import": (
"json", "re", "datetime", "base64", "collections", "random"
),
"modules": storage.modules,
"site": self,
"scope0": [],
"import_cache": {}
}
self.spawner = Spawner(self, io=io)
self.spawned_background_processes = False
if "BACKGROUND" in self.settings["permissions"]:
self.spawnBackgroundProcesses()
self.onFileDone.append(self.reloadBackgroundProcess)
def spawnBackgroundProcesses(self):
if self.spawned_background_processes:
return
self.log.debug("Spawning background processes")
self.spawned_background_processes = True
files = self.storage.list("")
for file in files:
# Run every file that starts with 0background.
if file.startswith("0background."):
self.spawnBackgroundProcess(file)
# Spawn background process if needed
def spawnBackgroundProcess(self, file_name):
ext = file_name.replace("0background.", "")
# Read code
code = self.storage.read(file_name)
# Spawn
self.spawner.spawn(ext, code)
# If a background process is changed, reload it
def reloadBackgroundProcess(self, inner_path):
if inner_path.startswith("0background."):
self.spawner.stopAll()
self.spawnBackgroundProcesses()
def delete(self):
# First really delete
super(SitePlugin, self).delete()
# Now stop all threads
self.spawner.stopAll()
def saveSettings(self):
super(SitePlugin, self).saveSettings()
# Spawn if just got the permission
if "BACKGROUND" in self.settings["permissions"]:
self.spawnBackgroundProcesses()
# IO
def backgroundOutput(self, *args):
print(*args)
def backgroundInput(self, *args):
raise NotImplementedError
def readModule(self, path):
if self.storage.isDir(path):
path += "/__init__.py"
else:
path += ".py"
try:
with self.storage.open(path, "r") as f:
return f.read(), path
except IOError:
return None, path