-
Notifications
You must be signed in to change notification settings - Fork 14
/
medulla.py
executable file
·136 lines (104 loc) · 3.77 KB
/
medulla.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import sys
import cortex
import thalamus
import cellphone
from config import load_config
from time import sleep, mktime, localtime
from hyperthymesia import Hyperthymesia
from cybernetics import metacortex
# Welcome to the beginning of a very strained brain metaphor!
# This is the shell for running the cortex. Ideally, this will never
# fail and you never have to reboot. Hah! I make funny, yes? More
# important, if you make changes to this file, you have to reboot as
# a reload won't change it. Same goes for changes to thalamus.py
class Medulla:
# Used by thalamus
sock = False
radio = False
def __init__(self):
print '* Becoming self-aware'
self.settings = load_config('/usr/home/peter/bots/mongo/config/settings.yaml')
self.secrets = load_config('/usr/home/peter/bots/mongo/config/secrets.yaml')
self.active = True
self.logger = Hyperthymesia()
try:
self.brain = cortex.Cortex(self)
except Exception as e:
self.logger.warn('Drain bamaged... Stroking... out...')
sys.exit()
self.thalamus = thalamus.Thalamus(self, self.brain)
self.thalamus.connect()
self.brain.thalamus = self.thalamus
self.cellphone = cellphone.Cellphone(self, self.brain)
self.cellphone.connect()
self.brain.cellphone = self.cellphone
self.brain.logger = self.logger
metacortex.cx = self.brain
# The pulse file is set as a measure of how
# long the bot has been spinning its gears
# in a process. If it can't set the pulse
# for too long, a signal kills it and reboots.
# Note: this has become less of an issue
# since all the bot's commands became threaded
print '* Establishing pulse'
self.setpulse()
print '* Running monitor'
while True:
sleep(0.1)
# Slight race condition on reloads
if not self.brain.thalamus: continue
self.brain.monitor()
if mktime(localtime()) - self.lastpulse > 10:
self.setpulse()
# Reload has to be run from here to update the
# cortex.
def reload(self, quiet=False):
if self.brain.values and len(self.brain.values[0]):
quiet = True
if not quiet:
self.brain.act('strokes out.')
else:
self.brain.act('strokes out.', False, self.secrets.owner)
# TODO broken.
#for channel in self.secrets.channels:
# name, attr = channel.popitem()
# if attr.primary:
# continue
# self.brain.brainmeats['channeling'].leave(name)
self.active = False
self.settings = settings = load_config('config/settings.yaml')
self.secrets = secrets = load_config('config/secrets.yaml')
import datastore
import util
import staff
import autonomic
import cortex
import thalamus
import id
reload(datastore)
reload(autonomic)
reload(util)
reload(staff)
reload(cortex)
reload(thalamus)
reload(id)
self.brain = cortex.Cortex(self, True)
self.thalamus = thalamus.Thalamus(self, self.brain)
self.brain.thalamus = self.thalamus
self.active = True
metacortex.cx = self.brain
if not quiet:
self.brain.act('comes to.')
else:
self.brain.act('comes to.', False, self.secrets.owner)
def setpulse(self):
self.lastpulse = mktime(localtime())
pulse = open(self.settings.sys.pulse, 'w')
pulse.write(str(self.lastpulse))
pulse.close()
def die(self, msg=None):
if msg is not None:
print msg
os._exit(1)
connect = Medulla()