-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnanobot_helpers.py
102 lines (79 loc) · 3.11 KB
/
nanobot_helpers.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
95
96
97
98
99
100
101
102
import threading
import sublime
from .nanobot import NanoBot
from .sublime_helpers import SublimeHelpers
class NanoBotHelpers:
@staticmethod
def config():
settings_keys = [
'NANO_BOTS_API_ADDRESS',
'NANO_BOTS_STREAM',
'NANO_BOTS_END_USER']
def load_settings(source, keys):
if isinstance(source, dict):
return {
key: source.get(key) for key in keys if key in source}
if hasattr(source, "has"):
return {
key: source.get(key) for key in keys if source.has(key)}
return {}
default_settings = sublime.load_settings('Default.sublime-settings')
user_settings = sublime.load_settings('Preferences.sublime-settings')
project_data = sublime.active_window().project_data() or {}
project_settings = project_data.get('settings', {})
view_settings = SublimeHelpers.view().settings()
config = {key: default_settings.get(key) for key in settings_keys}
for settings in [user_settings, project_settings, view_settings]:
config.update(load_settings(settings, settings_keys))
return config
@staticmethod
def stop():
NanoBot.stop()
@staticmethod
def cartridges_as_menu(callback):
def fetch(callback):
cartridges = NanoBotHelpers.cartridges()
items = []
for cartridge in cartridges:
if cartridge['meta']['symbol']:
items.append(
(cartridge['meta']['symbol'] + ' ' +
cartridge['meta']['name'], cartridge))
else:
items.append(cartridge['meta']['name'])
callback(items)
threading.Thread(target=fetch, args=(callback,)).start()
@staticmethod
def cartridges():
return NanoBot.cartridges(NanoBotHelpers.config())
@staticmethod
def cartridge(cartridge_id):
return NanoBot.cartridge(NanoBotHelpers.config(), cartridge_id)
@staticmethod
def evaluate(cartridge, state, input, mode, prefix):
params = {
'cartridge': cartridge, 'state': state,
'input': input, 'mode': mode, 'prefix': prefix}
config = NanoBotHelpers.config()
NanoBot.perform(
config, params,
lambda result:
NanoBotHelpers.on_output(
{'config': config, 'params': params},
result))
@staticmethod
def on_output(environment, result):
if environment['config']['NANO_BOTS_STREAM']:
content = result['fragment']
else:
content = result['output']
selection = SublimeHelpers.selection()
if selection['region'] is not None:
SublimeHelpers.insert_text(
'',
selection['region'],
environment['params']['mode'],
environment['params']['prefix'])
SublimeHelpers.insert_text(
content, None,
environment['params']['mode'], environment['params']['prefix'])