Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
open_wui cmd for tui :)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvonrocketstein committed Feb 2, 2013
1 parent 6628f30 commit 9db13ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
34 changes: 18 additions & 16 deletions lib/cortex/core/notation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from cortex.services import Service
from cortex.core.util import report


class UniverseNotation:
""" """

Expand All @@ -15,10 +16,24 @@ def __xor__(self, other):
(universe ^ '~cortex/etc/master.conf')
"""


def __pow__(self, other):
return self.agents[other].obj

def _retrieve_string_from_services(self, name):
try:
out = self.services[name]
return out and out.obj
except self.services.NotFound:
return None

def _retrieve_string_from_agents(self, name):
try:
out = self.agents[name]
return out and out.obj
except self.agents.NotFound:
return None

# TODO: multimethods
def __or__(self, other):
""" syntactic sugar for grabbing a service by name,
given an actual service, getting it's name. example
Expand All @@ -29,24 +44,11 @@ def __or__(self, other):
>>> postoffice = ( universe | "postoffice" )
>>> ( universe | postoffice )
"postoffice"
"""
def get_string_from_services(name):
try:
out = self.services[name]
return out and out.obj
except self.services.NotFound:
return None
def get_string_from_agents(name):
try:
out = self.agents[name]
return out and out.obj
except self.agents.NotFound:
return None

if type(other) in types.StringTypes:
result = get_string_from_services(other) or \
get_string_from_agents(other)
result = self._retrieve_string_from_services(other) #or \
#self._retrieve_string_from_agents(other)
if result is None:
report('no such service/agent', other)
return None
Expand Down
26 changes: 21 additions & 5 deletions lib/cortex/services/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import copy
import urllib
import webbrowser

from nevow import appserver
from twisted.web import static, server
Expand Down Expand Up @@ -33,6 +34,12 @@
class WebRoot(Agent, PortChooser):
""" abstraction for / """

def open_page_in_browser(self, url):
report('opening: ',url)
webbrowser.open_new_tab(url)

def open_wui(self):
return self.open_page_in_browser(self.url)

def iterate(self):
""" WebRoot is a trivial Agent with no true concurrency
Expand Down Expand Up @@ -69,14 +76,17 @@ def make_data_stream(self, endpoint, fxn):
self.putChild(endpoint, stream)
return stream

@property
def url(self):
return 'http://{0}:{1}'.format(self.universe.host,
self.universe.port_for(self))


def make_redirect(self, _from, b):
""" """
rsrc = Redirect(b)
self.putChild(_from, rsrc)
url = 'http://{0}:{1}/{2}'.\
format(self.universe.host,
self.universe.port_for(self),
_from)
url = '{0}/{1}'.format(self.url, _from)
return rsrc, url

def setup(self):
Expand Down Expand Up @@ -146,6 +156,12 @@ def __getattr__(self,name):
else:
return getattr(self.filter_by_type(WebRoot)[0], name)

@constraint(boot_first='postoffice')
@constraint(boot_first='postoffice terminal'.split())
def start(self):
super(Web, self).start()
terminal = (self.universe|'terminal')
# keeping *args just so terminal can use
# comma-style calling: ", open_wui"
namespace = dict(open_wui = lambda *args: self.open_wui())
terminal.contribute_to_api(**namespace)
report('type open_wui() to see the web user interface.')
5 changes: 2 additions & 3 deletions lib/cortex/services/web/pchoose.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""
"""
from goulash.net import is_port_open
from cortex.core.util import report
from cortex.core.util import report, report_if_verbose
from cortex.core.data import CORTEX_PORT_RANGE,LOOPBACK_HOST
class PortChooser(object):
@property
def port(self):
if hasattr(self, '_port'):
return self._port ##= 1338
report("Choosing port")
for x in range(*CORTEX_PORT_RANGE):
report("Trying: ",x)
report_if_verbose("{0} looking for port; trying: {1}".format(self,x))
if not is_port_open(x, LOOPBACK_HOST):
self._port = x
return x

0 comments on commit 9db13ab

Please sign in to comment.