Skip to content

Commit

Permalink
Make things actually work without Voltron installed
Browse files Browse the repository at this point in the history
  • Loading branch information
snare committed Jan 1, 2015
1 parent f8a3166 commit deed088
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 132 deletions.
3 changes: 2 additions & 1 deletion calculon/__init__.py
Expand Up @@ -4,4 +4,5 @@
from .display import *
from .env import *

disp = None
disp = None
V = None
9 changes: 6 additions & 3 deletions calculon/load.py
Expand Up @@ -3,7 +3,7 @@

import calculon
from .env import *
from .voltron_integration import VoltronProxy
from .voltron_integration import *
import repl

if 'bpython' in sys.modules.keys():
Expand All @@ -17,5 +17,8 @@
print("Connected to calculon")

# connect to voltron
calculon.V = VoltronProxy()
calculon.V.disp = calculon.disp
try:
calculon.V = VoltronProxy()
calculon.V.disp = calculon.disp
except NameError:
pass
14 changes: 9 additions & 5 deletions calculon/main.py
Expand Up @@ -10,7 +10,7 @@
import calculon
from .display import *
from .env import *
from .voltron_integration import VoltronProxy
from .voltron_integration import *
from . import repl

def display():
Expand Down Expand Up @@ -47,16 +47,20 @@ def console():
repl.disp = calculon.disp

# connect to voltron
calculon.V = VoltronProxy()
calculon.V.disp = calculon.disp
calculon.V.update_disp()
try:
calculon.V = VoltronProxy()
calculon.V.disp = calculon.disp
calculon.V.update_disp()
except NameError:
pass

# run repl
code.InteractiveConsole.runsource = repl.CalculonInterpreter().runsource
code.interact(local=locals())

# clean up
calculon.V._disconnect()
if calculon.V:
calculon.V._disconnect()
if calculon.disp:
calculon.disp._pyroRelease()

Expand Down
14 changes: 8 additions & 6 deletions calculon/repl.py
Expand Up @@ -16,7 +16,7 @@
import calculon
from .colour import *
from .env import *
from .voltron_integration import VoltronProxy
from .voltron_integration import *
from .display import VALID_FORMATS

if sys.version_info[0] > 2:
Expand Down Expand Up @@ -140,10 +140,11 @@ def update_display_exprs():
self.locals['switch'] = swap
self.locals['_watch_expr'] = watch_expr
self.locals['_unwatch_expr'] = unwatch_expr
calculon.V.callback = update_display_exprs
calculon.V.start_watcher()
calculon.V.update_disp()
self.locals['V'] = calculon.V
if calculon.V:
calculon.V.callback = update_display_exprs
calculon.V.start_watcher()
calculon.V.update_disp()
self.locals['V'] = calculon.V

# make sure there's a valid connection to the display
try:
Expand All @@ -156,7 +157,8 @@ def update_display_exprs():
calculon.disp.are_you_there()
except:
calculon.disp = None
calculon.V.disp = calculon.disp
if calculon.V:
calculon.V.disp = calculon.disp

# update value from last operation
if calculon.disp:
Expand Down
236 changes: 119 additions & 117 deletions calculon/voltron_integration.py
Expand Up @@ -11,123 +11,125 @@
from voltron.api import *
from voltron.plugin import *
voltron.setup_env()
HAS_VOLTRON = True
except ImportError:
voltron = None


class VoltronWatcher(threading.Thread):
def __init__(self, callback=None):
super(VoltronWatcher, self).__init__()
self.callback = callback

def run(self, *args, **kwargs):
self.done = False
self.client = voltron.core.Client()
self.client.connect()
while not self.done:
try:
res = self.client.send_request(api_request('wait', timeout=1))
if res.is_success:
self.callback()
except Exception as e:
done = True

class VoltronProxy(object):
_instance = None
exception = False
connected = False
watcher = None
disp = None

def __init__(self, callback=None):
self.callback = callback
if not self.connected:
self.connect()
self.start_watcher()
calculon.voltron_proxy = self

def __getattr__(self, key):
if self.connected:
try:
req = api_request('registers')
res = self.client.send_request(req)
if res.status == 'success':
return res.registers[key]
else:
print("{} getting register: {}".format(type(res), res.message))
except Exception as e:
print("Exception getting register: {} {}".format(type(e), str(e)))
else:
raise Exception("Not connected")

def __getitem__(self, key):
if self.connected:
try:
req = api_request('memory')
if isinstance(key, slice):
req.address = key.start
req.length = key.stop - key.start
else:
req.address = key
req.length = 1

res = self.client.send_request(req)

if res.status == 'success':
return res.memory
else:
print("{} reading memory: {}".format(type(res), res.message))
except Exception as e:
print("Exception reading memory: {} {}".format(type(e), str(e)))

return resp['value']
else:
raise Exception("Not connected")

def _connect(self):
self.client = voltron.core.Client()
self.client.connect()
self.start_watcher()
self.connected = True
self.update_disp()

def connect(self):
if not self.connected:
if not self.exception:
HAS_VOLTRON = False


if HAS_VOLTRON:
class VoltronWatcher(threading.Thread):
def __init__(self, callback=None):
super(VoltronWatcher, self).__init__()
self.callback = callback

def run(self, *args, **kwargs):
self.done = False
self.client = voltron.core.Client()
self.client.connect()
while not self.done:
try:
res = self.client.send_request(api_request('wait', timeout=1))
if res.is_success:
self.callback()
except Exception as e:
done = True

class VoltronProxy(object):
_instance = None
exception = False
connected = False
watcher = None
disp = None

def __init__(self, callback=None):
self.callback = callback
if not self.connected:
self.connect()
self.start_watcher()
calculon.voltron_proxy = self

def __getattr__(self, key):
if self.connected:
try:
req = api_request('registers')
res = self.client.send_request(req)
if res.status == 'success':
return res.registers[key]
else:
print("{} getting register: {}".format(type(res), res.message))
except Exception as e:
print("Exception getting register: {} {}".format(type(e), str(e)))
else:
raise Exception("Not connected")

def __getitem__(self, key):
if self.connected:
try:
self._connect()
print("Connected to voltron")
except socket.error:
pass
req = api_request('memory')
if isinstance(key, slice):
req.address = key.start
req.length = key.stop - key.start
else:
req.address = key
req.length = 1

res = self.client.send_request(req)

if res.status == 'success':
return res.memory
else:
print("{} reading memory: {}".format(type(res), res.message))
except Exception as e:
raise e
self.exception = True
print("Error loading voltron: " + str(e))
print("Make sure you have the most recent version of voltron")
else:
print("Already connected")

def _disconnect(self):
if self.watcher:
self.watcher.done = True
self.watcher.join()
self.watcher = None
self.client = None
self.connected = False
self.update_disp()

def disconnect(self):
if self.connected:
self._disconnect()
print("Disconnected from voltron")
else:
print("Not connected")

def start_watcher(self):
if not self.watcher and self.callback and self.connected:
self.watcher = VoltronWatcher(self.callback)
self.watcher.start()

def update_disp(self):
if self.disp:
self.disp.set_voltron_status(self.connected)
print("Exception reading memory: {} {}".format(type(e), str(e)))

return resp['value']
else:
raise Exception("Not connected")

def _connect(self):
self.client = voltron.core.Client()
self.client.connect()
self.start_watcher()
self.connected = True
self.update_disp()

def connect(self):
if not self.connected:
if not self.exception:
try:
self._connect()
print("Connected to voltron")
except socket.error:
pass
except Exception as e:
raise e
self.exception = True
print("Error loading voltron: " + str(e))
print("Make sure you have the most recent version of voltron")
else:
print("Already connected")

def _disconnect(self):
if self.watcher:
self.watcher.done = True
self.watcher.join()
self.watcher = None
self.client = None
self.connected = False
self.update_disp()

def disconnect(self):
if self.connected:
self._disconnect()
print("Disconnected from voltron")
else:
print("Not connected")

def start_watcher(self):
if not self.watcher and self.callback and self.connected:
self.watcher = VoltronWatcher(self.callback)
self.watcher.start()

def update_disp(self):
if self.disp:
self.disp.set_voltron_status(self.connected)

0 comments on commit deed088

Please sign in to comment.