cobalt.py

Sun, 19 Apr 2015 22:02:02 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sun, 19 Apr 2015 22:02:02 +0300
changeset 128
bd949c554dd2
parent 124
7b2cd8b1ba86
child 132
a22c50f52a23
permissions
-rwxr-xr-x

Major refactor: moved calculator routines into a Calculator class.
Added attributes to calculator.

#!/usr/bin/env python
'''
	Copyright 2014-2015 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. The name of the author may not be used to endorse or promote products
	   derived from this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
	IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
	OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
	THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''

import asyncore
import sys
import traceback
import os
import modulecore as ModuleCore
import configfile as ConfigFile
from configfile import Config
import hgpoll as HgPoll
import bt as Bt
import irc as Irc
import rest as Rest

if __name__ != '__main__':
	raise ImportError ('cobalt may not be imported as a module')

g_BotActive = False

#
# Exception handling
#
def handle_exception(excType, excValue, trace):
	excepterm (traceback.format_exception(excType, excValue, trace))

def excepterm (data):
	for segment in data:
		for line in segment.splitlines():
			#print line
			Irc.broadcast (line)

	for client in Irc.all_clients:
		if len(data) > 0:
			client.exceptdie()
		else:
			client.quit_irc()

	if g_BotActive:
		restart_self()
	else:
		quit()

def restart_self():
	os.execl (sys.executable, sys.executable, * sys.argv)

def main():
	global g_BotActive
	sys.excepthook = handle_exception
	all_clients = []
	g_BotActive = False

	try:
		uid = os.geteuid()
	except:
		uid = -1

	if uid == 0 and raw_input ('Do you seriously want to run cobalt as root? [y/N] ') != 'y':
		quit()

	ConfigFile.init()
	ModuleCore.init_data()
	Bt.init()
	HgPoll.init()
	
	try:
		autoconnects = Config.get_value ('autoconnect', [])
		
		if len (autoconnects) == 0:
			print "Nowhere to connect."
			quit()
		
		for aconn in autoconnects:
			for conndata in Config.get_nodelist ('connections'):
				if conndata.get_value ('name') == aconn:
					Irc.irc_client (conndata, 0)
					break
			else:
				raise ValueError ("unknown autoconnect entry %s" % (aconn))
		
		# Start the REST server
		Rest.RESTServer()
		
		g_BotActive = True
		asyncore.loop()
	except KeyboardInterrupt:
		for client in Irc.all_clients:
			client.keyboardinterrupt()
		quit()
	except Irc.RestartError as e:
		excepterm (e.message)

if __name__ == '__main__':
	main()

mercurial