Skip to content

Commit

Permalink
Fixed #72.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rimco committed Feb 21, 2015
1 parent 9f63e20 commit 9d1f961
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 69 deletions.
19 changes: 11 additions & 8 deletions ospy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def reboot(wait=1, block=False):
stations.clear()
time.sleep(wait)
logging.info("Rebooting...")
# Stop the web server

# Stop the web server first:
from ospy import server
server.stop()

Expand All @@ -66,7 +67,8 @@ def poweroff(wait=1, block=False):
stations.clear()
time.sleep(wait)
logging.info("Powering off...")
# Stop the web server

# Stop the web server first:
from ospy import server
server.stop()

Expand All @@ -86,14 +88,15 @@ def restart(wait=1, block=False):
stations.clear()
time.sleep(wait)
logging.info("Restarting...")
if determine_platform() == 'nt':
# Stop the web server first:
from ospy import server
server.stop()

# Stop the web server first:
from ospy import server
server.stop()

if determine_platform() == 'nt':
# Use this weird construction to start a separate process that is not killed when we stop the current one
import sys
subprocess.check_call(['cmd.exe', '/c', 'start', sys.executable] + sys.argv)
subprocess.Popen(['cmd.exe', '/c', 'start', sys.executable] + sys.argv)
else:
# No need to stop web server, the service will do this for us:
subprocess.Popen('service ospy restart'.split())
Expand Down Expand Up @@ -322,7 +325,7 @@ def check_login(redirect=False):
if redirect:
if not web.url().endswith('json'): #TODO: remove when JS is cleaned-up?
server.session.login_to = web.url()
raise web.seeother('/login')
raise web.seeother('/login', True)
return False


Expand Down
28 changes: 16 additions & 12 deletions ospy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import plugins

server = None
__server = None
session = None


Expand Down Expand Up @@ -59,7 +59,7 @@ def __call__(self, environ, start_response):


def start():
global server
global __server
global session

##############################
Expand All @@ -75,27 +75,31 @@ def start():
wsgifunc = web.httpserver.StaticMiddleware(wsgifunc)
wsgifunc = PluginStaticMiddleware(wsgifunc)
wsgifunc = DebugLogMiddleware(wsgifunc)
server = web.httpserver.WSGIServer(("0.0.0.0", options.web_port), wsgifunc)
__server = web.httpserver.WSGIServer(("0.0.0.0", options.web_port), wsgifunc)

sessions = shelve.open(os.path.join('ospy', 'data', 'sessions.db'))
session = web.session.Session(app, web.session.ShelfStore(sessions),
initializer={'validated': False,
'login_to': '/',
'last_page': '/'})

import atexit
atexit.register(sessions.close)

def exit_msg():
print 'OSPy is closing, saving sessions.'
atexit.register(exit_msg)

scheduler.start()
plugins.start_enabled_plugins()

try:
server.start()
__server.start()
except (KeyboardInterrupt, SystemExit):
server.stop()
sessions.close()
session = None
server = None
stop()

def stop():
global server
if server is not None:
server.stop()
server = None
global __server
if __server is not None:
__server.stop()
__server = None
29 changes: 13 additions & 16 deletions ospy/webpages.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@

class WebPage(object):
def __init__(self):
if self.__module__.startswith('plugins'):
directory = os.path.join(*self.__module__.split('.'))
else:
directory = self.__module__.split('.')[0]

self.base_render = lambda page: web.template.render('ospy/templates', globals=template_globals()).base(page)
self.template_render = web.template.render(os.path.join(directory, 'templates'), globals=template_globals(), base=self.base_render)

self.core_render = web.template.render(os.path.join('ospy', 'templates'), globals=template_globals(), base=self.base_render)
self.plugin_render = web.template.render(os.path.join(os.path.join(*self.__module__.split('.')), 'templates'), globals=template_globals(), base=self.base_render)

# If you want to profile the get requests, uncomment these lines:
# self.orig_get = self.GET
Expand Down Expand Up @@ -71,13 +68,13 @@ def GET(self):
if check_login(False):
raise web.seeother('/')
else:
return self.template_render.login(signin_form())
return self.core_render.login(signin_form())

def POST(self):
my_signin = signin_form()

if not my_signin.validates():
return self.template_render.login(my_signin)
return self.core_render.login(my_signin)
else:
from ospy import server
server.session.validated = True
Expand Down Expand Up @@ -105,7 +102,7 @@ def GET(self):
stations.clear()
raise web.seeother('/')

return self.template_render.home()
return self.core_render.home()

def POST(self):
qdict = web.input()
Expand Down Expand Up @@ -134,7 +131,7 @@ def GET(self):
if 'delete' in qdict and qdict['delete'] == '1':
while programs.count() > 0:
programs.remove_program(programs.count()-1)
return self.template_render.programs()
return self.core_render.programs()


class program_page(ProtectedPage):
Expand Down Expand Up @@ -162,7 +159,7 @@ def GET(self, index):
program = programs.create_program()
program.set_days_simple(6*60, 30, 30, 0, [])

return self.template_render.program(program)
return self.core_render.program(program)

def POST(self, index):
qdict = web.input()
Expand Down Expand Up @@ -222,7 +219,7 @@ class runonce_page(ProtectedPage):
"""Open a page to view and edit a run once program."""

def GET(self):
return self.template_render.runonce()
return self.core_render.runonce()

def POST(self):
qdict = web.input()
Expand All @@ -242,7 +239,7 @@ class log_page(ProtectedPage):
"""View Log"""

def GET(self):
return self.template_render.log()
return self.core_render.log()

def POST(self):
qdict = web.input()
Expand All @@ -258,7 +255,7 @@ def GET(self):
qdict = web.input()
errorCode = qdict.get('errorCode', 'none')

return self.template_render.options(errorCode)
return self.core_render.options(errorCode)

def POST(self):
qdict = web.input()
Expand Down Expand Up @@ -296,7 +293,7 @@ class stations_page(ProtectedPage):
"""Stations page"""

def GET(self):
return self.template_render.stations()
return self.core_render.stations()

def POST(self):
qdict = web.input()
Expand All @@ -322,7 +319,7 @@ def GET(self):
return get_help_file(id)

docs = get_help_files()
return self.template_render.help(docs)
return self.core_render.help(docs)


################################################################################
Expand Down
4 changes: 2 additions & 2 deletions plugins/email_notifications/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ class settings_page(ProtectedPage):
"""Load an html page for entering email adjustments."""

def GET(self):
return self.template_render.email_notifications(email_options, log.events(NAME))
return self.plugin_render.email_notifications(email_options, log.events(NAME))

def POST(self):
email_options.web_update(web.input())

if email_sender is not None:
email_sender.update()
raise web.seeother(plugin_url(settings_page))
raise web.seeother(plugin_url(settings_page), True)


class settings_json(ProtectedPage):
Expand Down
4 changes: 2 additions & 2 deletions plugins/lcd_display/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ class settings_page(ProtectedPage):
"""Load an html page for entering lcd adjustments."""

def GET(self):
return self.template_render.lcd_display(lcd_options, log.events(NAME))
return self.plugin_render.lcd_display(lcd_options, log.events(NAME))

def POST(self):
lcd_options.web_update(web.input())

lcd_sender.update()
raise web.seeother(plugin_url(settings_page))
raise web.seeother(plugin_url(settings_page), True)


class settings_json(ProtectedPage):
Expand Down
2 changes: 1 addition & 1 deletion plugins/mobile_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,5 @@ class info_page(ProtectedPage):
"""Load an html page"""

def GET(self):
return self.template_render.mobile_app()
return self.plugin_render.mobile_app()

4 changes: 2 additions & 2 deletions plugins/monthly_water_level/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class settings_page(ProtectedPage):
"""Load an html page for entering monthly irrigation time adjustments"""

def GET(self):
return self.template_render.monthly_water_level(plugin_options)
return self.plugin_render.monthly_water_level(plugin_options)

def POST(self):
qdict = web.input()
Expand All @@ -92,4 +92,4 @@ def POST(self):
plugin_options.web_update(vals)
if checker is not None:
checker.update()
raise web.seeother(plugin_url(settings_page))
raise web.seeother(plugin_url(settings_page), True)
4 changes: 2 additions & 2 deletions plugins/pressure_monitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,14 @@ class settings_page(ProtectedPage):
"""Load an html page for entering pressure adjustments."""

def GET(self):
return self.template_render.pressure_monitor(pressure_options, log.events(NAME))
return self.plugin_render.pressure_monitor(pressure_options, log.events(NAME))

def POST(self):
pressure_options.web_update(web.input())

if pressure_sender is not None:
pressure_sender.update()
raise web.seeother(plugin_url(settings_page))
raise web.seeother(plugin_url(settings_page), True)


class settings_json(ProtectedPage):
Expand Down
4 changes: 2 additions & 2 deletions plugins/sms_adj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,13 @@ class settings_page(ProtectedPage):
"""Load an html page for entering lcd adjustments."""

def GET(self):
return self.template_render.sms_adj(sms_options, log.events(NAME))
return self.plugin_render.sms_adj(sms_options, log.events(NAME))

def POST(self):
sms_options.web_update(web.input())

sms_sender.update()
raise web.seeother(plugin_url(settings_page))
raise web.seeother(plugin_url(settings_page), True)


class settings_json(ProtectedPage):
Expand Down
4 changes: 2 additions & 2 deletions plugins/system_debug/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class status_page(ProtectedPage):
"""Load an html page"""

def GET(self):
return self.template_render.system_debug(get_overview())
return self.plugin_render.system_debug(get_overview())


class delete_page(ProtectedPage):
Expand All @@ -53,6 +53,6 @@ def POST(self):
except Exception:
pass

raise web.seeother(plugin_url(status_page))
raise web.seeother(plugin_url(status_page), True)


2 changes: 1 addition & 1 deletion plugins/system_info/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ class status_page(ProtectedPage):
"""Load an html page"""

def GET(self):
return self.template_render.system_info(get_overview())
return self.plugin_render.system_info(get_overview())
11 changes: 7 additions & 4 deletions plugins/system_update/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ def perform_update():
command = "git config core.filemode false" # http://superuser.com/questions/204757/git-chmod-problem-checkout-screws-exec-bit
subprocess.check_output(command.split())

command = "git reset --hard"
subprocess.check_output(command.split())

command = "git pull"
output = subprocess.check_output(command.split())

Expand Down Expand Up @@ -155,28 +158,28 @@ class status_page(ProtectedPage):

def GET(self):
checker.started.wait(10) # Make sure we are initialized
return self.template_render.system_update(checker.status, log.events(NAME))
return self.plugin_render.system_update(checker.status, log.events(NAME))


class refresh_page(ProtectedPage):
"""Refresh status and show it."""

def GET(self):
checker.update_wait()
raise web.seeother(plugin_url(status_page))
raise web.seeother(plugin_url(status_page), True)


class update_page(ProtectedPage):
"""Update OSPi from github and return text message from comm line."""

def GET(self):
perform_update()
return self.template_render.restarting(plugin_url(status_page))
return self.core_render.restarting(plugin_url(status_page))


class restart_page(ProtectedPage):
"""Restart system."""

def GET(self):
restart(3)
return self.template_render.restarting(plugin_url(status_page))
return self.core_render.restarting(plugin_url(status_page))
4 changes: 2 additions & 2 deletions plugins/ups_adj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ class settings_page(ProtectedPage):
"""Load an html page for entering USP adjustments."""

def GET(self):
return self.template_render.ups_adj(ups_options, ups_sender.status, log.events(NAME))
return self.plugin_render.ups_adj(ups_options, ups_sender.status, log.events(NAME))

def POST(self):
ups_options.web_update(web.input())

if ups_sender is not None:
ups_sender.update()
raise web.seeother(plugin_url(settings_page))
raise web.seeother(plugin_url(settings_page), True)


class settings_json(ProtectedPage):
Expand Down
Loading

0 comments on commit 9d1f961

Please sign in to comment.