<commit_before>#!/usr/bin/env python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

import cmd
import readline
import sys
import os
import traceback
from src.console_controller import ConsoleController

def try_catch(command, args):
    try:
        if args is not None:
            return command(args)
        else:
            return command()
    except:
        print("Sorry, that command raised an exception. Here's what I know:\n")
        print(traceback.format_exc())

class GagCmd(cmd.Cmd):

## Setup, loading and saving sessions, exit, etc.

    def __init__(self):
        cmd.Cmd.__init__(self)
        self.prompt = "GAG> "
        readline.set_history_length(1000)
        try:
            readline.read_history_file('.gaghistory')
        except IOError:
            sys.stderr.write("No .gaghistory file available...")
        self.controller = ConsoleController()

    def precmd(self, line):
        readline.write_history_file('.gaghistory')
        return cmd.Cmd.precmd(self, line)

    def postcmd(self, stop, line):
        if hasattr(self, 'output') and self.output:
            print self.output
            self.output = None
        return stop

    def parseline(self, line):
        if '|' in line:
            commands = ['']
            quotes = ''
            for c in line:
                if c == '|' and quotes == '':
                    commands[-1] = commands[-1].strip()
                    commands.append('')
                else:
                    if quotes == '' and (c == "'" or c == '"'):
                        quotes = c
                    elif quotes == c:
                        quotes = ''
                    commands[-1] += c
            commands[-1] = commands[-1].strip()
            if len(commands) > 1:
                return 'pipe', commands, line
        return cmd.Cmd.parseline(self, line)

    def do_pipe(self, args):
        buf = None
        for arg in args:
            if buf:
                self.controller.input = buf
            else:
                self.controller.input = ''
            self.output = ''
            self.onecmd(arg)
            if hasattr(self, 'output') and self.output:
                buf = self.output

    def help_barffolder(self):
        print("Usage: barffolder <directory>\n")
        print("Writes gff, fasta and trinotate files to the specified directory.\n")

    def do_barffolder(self, line):
        try_catch(self.controller.barf_folder, line)

    def help_loadfolder(self):
        print("Usage: loadfolder <directory>\n")
        print("Reads in a gff, fasta and trinotate file from the specified directory.\n")

    def do_loadfolder(self, line):
        try_catch(self.controller.load_folder, line)

    def help_exit(self):
        print("Exit this console.\n")

    def do_exit(self, line):
        return True

    def do_ls(self, line):
        self.output = self.controller.ls(line)

    def do_cat(self, line):
        self.output = self.controller.cat(line)

    def do_grep(self, line):
        self.output = self.controller.grep(line)

    def do_sed(self, line):
        self.output = self.controller.sed(line)

    def do_sort(self, line):
        self.output = self.controller.sort(line)

    def do_uniq(self, line):
        self.output = self.controller.uniq(line)

    def do_barf(self, line):
        self.output = self.controller.barf(line)

## Assorted utilities

    def help_status(self):
        print("Usage: status\n")
        print("Gives a brief summary of what's in memory\n")

    def do_status(self, line):
        print(try_catch(self.controller.status, None))

    def do_barftofile(self, line):
        try_catch(self.controller.barftofile, line)

## Manipulate genome

    def help_ducttape(self):
        print("Usage: ducttape\n")
        print("For the genome in memory, does the following:\n")
        print("\t*Renames all 'maker' mRNAs, starting with 'BDOR_1000000'\n")
        print("\t*Removes each first CDS segment if its length is less than 4\n")
        print("\t*Verifies all start and stop codons by checking against the actual sequence; creates new features as necessary\n")
        print("\t*Verifies all 'frame' information for coding sequences by running a six-frame translation and checking the\n")
        print("\t protein sequence against the one stored in the annotation file\n")
        print("\t*Removes invalid features -- first eliminating any mRNA with no CDS, then any gene with no mRNAs.\n")

    def do_ducttape(self, line):
        try_catch(self.controller.ducttape, None)

    def do_removemrna(self, line):
        try_catch(self.controller.removemrna, line)

    def help_removegene(self):
        print("Usage: removegene <gene_id_prefix>\n")
        print("Removes from genome all genes with said gene_id prefix.\n")

    def do_removegene(self, line):
        try_catch(self.controller.remove_gene, line)

    def help_trimregion(self):
        print("Usage: trimregion <seq_id> <start_index> <stop_index>\n")
        print("Removes subsequence from fasta and adjusts indices of features which follow region.\n")

    def do_trimregion(self, line):
        try_catch(self.controller.trim_region, line)

    def help_removeseq(self):
        print("Usage: removeseq <seq_id> [-F]\n")
        print("Removes sequence from fasta, only if the sequence contains no features or if the -F option is used.\n")

    def do_removeseq(self, line):
        try_catch(self.controller.remove_seq, line)

    def help_invalidateregion(self):
        print("Usage: invalidateregion <seq_id> <start> <stop>\n")
        print("Truncates or removes any feature located on region to be invalidated\n")

    def do_invalidateregion(self, line):
        try_catch(self.controller.invalidate_region, line)

## Output info to console

    def help_barfgenegff(self):
        print("Usage: barfgenegff <gene_id>\n")
        print("Prints gff entry for corresponding gene to console.\n")

    def do_barfgenegff(self, line):
        self.output = try_catch(self.controller.barf_gff, line)

    def help_barfseq(self):
        print("Usage: barfseq <seq_id> <start_index> <end_index>\n")
        print("Prints (sub)sequence to console.\n")

    def do_barfseq(self, line):
        self.output = try_catch(self.controller.barf_seq, line)

    def help_barfcdsseq(self, line):
        print("Usage: barfcdsseq <mrna>\n")
        print("Prints CDS's whole sequence\n")

    def do_barfcdsseq(self, line):
        self.output = try_catch(self.controller.barf_cds_seq, line)

    def help_barfgenetbl(self):
        print("TODO")   # TODO

    def do_barfgenetbl(self, line):
        self.output = try_catch(self.controller.barf_gene_tbl, line)

## Output info to file

    def help_writetbl(self):
        print("Usage: writetbl <file_name>\n")
        print("Write a sweet feature table to the specified file.\n")

    def do_writetbl(self, line):
        try_catch(self.controller.write_tbl, line)

########################################################################

if __name__ == '__main__':
    GagCmd().cmdloop("Welcome to the GAG console!")
<commit_msg>fixed help barfcdsseq<commit_after>#!/usr/bin/env python
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

import cmd
import readline
import sys
import os
import traceback
from src.console_controller import ConsoleController

def try_catch(command, args):
    try:
        if args is not None:
            return command(args)
        else:
            return command()
    except:
        print("Sorry, that command raised an exception. Here's what I know:\n")
        print(traceback.format_exc())

class GagCmd(cmd.Cmd):

## Setup, loading and saving sessions, exit, etc.

    def __init__(self):
        cmd.Cmd.__init__(self)
        self.prompt = "GAG> "
        readline.set_history_length(1000)
        try:
            readline.read_history_file('.gaghistory')
        except IOError:
            sys.stderr.write("No .gaghistory file available...")
        self.controller = ConsoleController()

    def precmd(self, line):
        readline.write_history_file('.gaghistory')
        return cmd.Cmd.precmd(self, line)

    def postcmd(self, stop, line):
        if hasattr(self, 'output') and self.output:
            print self.output
            self.output = None
        return stop

    def parseline(self, line):
        if '|' in line:
            commands = ['']
            quotes = ''
            for c in line:
                if c == '|' and quotes == '':
                    commands[-1] = commands[-1].strip()
                    commands.append('')
                else:
                    if quotes == '' and (c == "'" or c == '"'):
                        quotes = c
                    elif quotes == c:
                        quotes = ''
                    commands[-1] += c
            commands[-1] = commands[-1].strip()
            if len(commands) > 1:
                return 'pipe', commands, line
        return cmd.Cmd.parseline(self, line)

    def do_pipe(self, args):
        buf = None
        for arg in args:
            if buf:
                self.controller.input = buf
            else:
                self.controller.input = ''
            self.output = ''
            self.onecmd(arg)
            if hasattr(self, 'output') and self.output:
                buf = self.output

    def help_barffolder(self):
        print("Usage: barffolder <directory>\n")
        print("Writes gff, fasta and trinotate files to the specified directory.\n")

    def do_barffolder(self, line):
        try_catch(self.controller.barf_folder, line)

    def help_loadfolder(self):
        print("Usage: loadfolder <directory>\n")
        print("Reads in a gff, fasta and trinotate file from the specified directory.\n")

    def do_loadfolder(self, line):
        try_catch(self.controller.load_folder, line)

    def help_exit(self):
        print("Exit this console.\n")

    def do_exit(self, line):
        return True

    def do_ls(self, line):
        self.output = self.controller.ls(line)

    def do_cat(self, line):
        self.output = self.controller.cat(line)

    def do_grep(self, line):
        self.output = self.controller.grep(line)

    def do_sed(self, line):
        self.output = self.controller.sed(line)

    def do_sort(self, line):
        self.output = self.controller.sort(line)

    def do_uniq(self, line):
        self.output = self.controller.uniq(line)

    def do_barf(self, line):
        self.output = self.controller.barf(line)

## Assorted utilities

    def help_status(self):
        print("Usage: status\n")
        print("Gives a brief summary of what's in memory\n")

    def do_status(self, line):
        print(try_catch(self.controller.status, None))

    def do_barftofile(self, line):
        try_catch(self.controller.barftofile, line)

## Manipulate genome

    def help_ducttape(self):
        print("Usage: ducttape\n")
        print("For the genome in memory, does the following:\n")
        print("\t*Renames all 'maker' mRNAs, starting with 'BDOR_1000000'\n")
        print("\t*Removes each first CDS segment if its length is less than 4\n")
        print("\t*Verifies all start and stop codons by checking against the actual sequence; creates new features as necessary\n")
        print("\t*Verifies all 'frame' information for coding sequences by running a six-frame translation and checking the\n")
        print("\t protein sequence against the one stored in the annotation file\n")
        print("\t*Removes invalid features -- first eliminating any mRNA with no CDS, then any gene with no mRNAs.\n")

    def do_ducttape(self, line):
        try_catch(self.controller.ducttape, None)

    def do_removemrna(self, line):
        try_catch(self.controller.removemrna, line)

    def help_removegene(self):
        print("Usage: removegene <gene_id_prefix>\n")
        print("Removes from genome all genes with said gene_id prefix.\n")

    def do_removegene(self, line):
        try_catch(self.controller.remove_gene, line)

    def help_trimregion(self):
        print("Usage: trimregion <seq_id> <start_index> <stop_index>\n")
        print("Removes subsequence from fasta and adjusts indices of features which follow region.\n")

    def do_trimregion(self, line):
        try_catch(self.controller.trim_region, line)

    def help_removeseq(self):
        print("Usage: removeseq <seq_id> [-F]\n")
        print("Removes sequence from fasta, only if the sequence contains no features or if the -F option is used.\n")

    def do_removeseq(self, line):
        try_catch(self.controller.remove_seq, line)

    def help_invalidateregion(self):
        print("Usage: invalidateregion <seq_id> <start> <stop>\n")
        print("Truncates or removes any feature located on region to be invalidated\n")

    def do_invalidateregion(self, line):
        try_catch(self.controller.invalidate_region, line)

## Output info to console

    def help_barfgenegff(self):
        print("Usage: barfgenegff <gene_id>\n")
        print("Prints gff entry for corresponding gene to console.\n")

    def do_barfgenegff(self, line):
        self.output = try_catch(self.controller.barf_gff, line)

    def help_barfseq(self):
        print("Usage: barfseq <seq_id> <start_index> <end_index>\n")
        print("Prints (sub)sequence to console.\n")

    def do_barfseq(self, line):
        self.output = try_catch(self.controller.barf_seq, line)

    def help_barfcdsseq(self):
        print("Usage: barfcdsseq <mrna>\n")
        print("Prints CDS's whole sequence\n")

    def do_barfcdsseq(self, line):
        self.output = try_catch(self.controller.barf_cds_seq, line)

    def help_barfgenetbl(self):
        print("TODO")   # TODO

    def do_barfgenetbl(self, line):
        self.output = try_catch(self.controller.barf_gene_tbl, line)

## Output info to file

    def help_writetbl(self):
        print("Usage: writetbl <file_name>\n")
        print("Write a sweet feature table to the specified file.\n")

    def do_writetbl(self, line):
        try_catch(self.controller.write_tbl, line)

########################################################################

if __name__ == '__main__':
    GagCmd().cmdloop("Welcome to the GAG console!")
<|endoftext|>


<commit_before>"""
Copyright 2016, 2017 Kevin Kahn

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
import importlib
import os
import sys
import signal
import time
import cgitb
import datetime
import pygame

# noinspection PyProtectedMember
from configobj import ConfigObj, Section
import isyeventmonitor

import config
import configobjects
import ast
import debug
import displayscreen
import globalparams
import isy
import logsupport
import maintscreen
import utilities
import requests
from logsupport import ConsoleWarning

import json

def handler(signum, frame):
	if signum in (signal.SIGTERM, signal.SIGINT):
		config.Logs.Log("Console received a termination signal ", str(signum), " - Exiting")
		time.sleep(1)
		pygame.display.quit()
		pygame.quit()
		os._exit(0)
	else:
		config.Logs.Log("Console received signal " + str(signum) + " Ignoring")

signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)

sectionget = Section.get

def CO_get(self, key, default, delkey=True):
	rtn = sectionget(self, key, default)
	if key in self and delkey:
		del self[key]
	return rtn

Section.get = CO_get

def LogBadParams(section, name):
	for nm, s in section.iteritems():
		if isinstance(s, Section):
			LogBadParams(s, nm)
		else:
			config.Logs.Log("Bad (unused) parameter name in: ", name, " (", nm, "=", str(s), ")",
							severity=ConsoleWarning)

config.exdir = os.path.dirname(os.path.abspath(__file__))
config.homedir = os.path.dirname(config.exdir)

earlylog = open(config.homedir + '/Console/earlylog.log', 'w', 0)
earlylog.write("Console start at " + time.strftime('%m-%d-%y %H:%M:%S') + ' on ' + config.hostname + '\n')

if os.getegid() <> 0:
	# Not running as root
	earlylog.write("Not running as root - exit\n")
	print "Must run as root"
	exit(999)

utilities.InitializeEnvironment()

earlylog.write('Environment initialized\n')

lastfn = ""
lastmod = 0
config.Console_pid = os.getpid()

earlylog.write('Exdir: ' + config.exdir + '  Pid: ' + str(config.Console_pid) + '\n')

for root, dirs, files in os.walk(config.exdir):
	for fname in files:
		if fname.endswith(".py"):
			fn = os.path.join(root, fname)
			if os.path.getmtime(fn) > lastmod:
				lastmod = os.path.getmtime(fn)
				lastfn = fn

try:
	with open(config.exdir + '/' + 'versioninfo') as f:
		config.versionname = f.readline()[:-1]
		config.versionsha = f.readline()[:-1]
		config.versiondnld = f.readline()[:-1]
		config.versioncommit = f.readline()[:-1]
except:
	config.versionname = 'none'
	config.versionsha = 'none'
	config.versiondnld = 'none'
	config.versioncommit = 'none'

earlylog.write(
	'Version/Sha/Dnld/Commit: ' + config.versionname + ' ' + config.versionsha + ' ' + config.versiondnld + ' ' + config.versioncommit + '\n')

"""
Dynamically load class definitions for all defined screen types and link them to how configuration happens
"""
for screentype in os.listdir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/screens'):
	if '__' not in screentype:
		splitname = os.path.splitext(screentype)
		if splitname[1] == '.py':
			importlib.import_module('screens.' + splitname[0])

earlylog.write("Screen types imported \n")

for alertproctype in os.listdir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/alerts'):
	if '__' not in alertproctype:
		splitname = os.path.splitext(alertproctype)
		if splitname[1] == '.py':
			importlib.import_module('alerts.' + splitname[0])

earlylog.write("Alert Proc types imported\n")

for n in config.alertprocs:
	config.alertprocs[n] = config.alertprocs[n]()  # instantiate an instance of each alert class

earlylog.write("Alert classes instantiated\n")

"""
Initialize the Console
"""

with open(config.exdir + '/termshortenlist', 'r') as f:
	try:
		config.TermShortener = json.load(f)
	except:
		config.TermShortener = {}

if len(sys.argv) == 2:
	config.configfile = sys.argv[1]
elif os.path.isfile(config.configfilebase + "config.txt"):
	config.configfile = config.configfilebase + "config.txt"
else:
	config.configfile = config.configfilebase + "config-" + config.hostname + ".txt"

if not os.path.isfile(config.configfile):
	earlylog.write("Abort - no configuratio file ('+config.hostname+')'\n")
	utilities.EarlyAbort('No Configuration File (' + config.hostname + ')')

config.ParsedConfigFile = ConfigObj(config.configfile)  # read the config.txt file

earlylog.write("Parsed base config file\n")

configdir = os.path.dirname(config.configfile)

config.configfilelist[config.configfile] = os.path.getmtime(config.configfile)

cfiles = []
pfiles = []
cfglib = config.ParsedConfigFile.get('cfglib', '')
if cfglib <> '':
	cfglib += '/'
if cfglib[0] <> '/':
	cfglib = configdir + '/' + cfglib
includes = config.ParsedConfigFile.get('include', [])
while includes:
	f = includes.pop(0)
	if f[0] <> '/':
		pfiles.append('+' + f)
		f = cfglib + f
	else:
		pfiles.append(f)
	cfiles.append(f)
	tmpconf = ConfigObj(f)
	includes = includes + tmpconf.get('include', [])
	config.ParsedConfigFile.merge(tmpconf)
	earlylog.write("Merged config file " + f + "\n")
	config.configfilelist[f] = os.path.getmtime(f)

debug.Flags = debug.InitFlags()

utilities.ParseParam(globalparams)  # add global parameters to config file

earlylog.write("Parsed globals\n")

config.Logs = logsupport.Logs(config.screen, os.path.dirname(config.configfile))
cgitb.enable(format='text')
config.Logs.Log(u"Soft ISY Console")
earlylog.write("Switched to real log\n")
earlylog.close()
os.remove(config.homedir + '/Console/earlylog.log')
config.Logs.Log(u"  \u00A9 Kevin Kahn 2016, 2017")
config.Logs.Log("Software under Apache 2.0 License")
config.Logs.Log("Version Information:")
config.Logs.Log(" Run from: ", config.exdir)
config.Logs.Log(" Last mod: ", lastfn)
config.Logs.Log(" Mod at: ", time.ctime(lastmod))
config.Logs.Log(" Tag: ", config.versionname)
config.Logs.Log(" Sha: ", config.versionsha)
config.Logs.Log(" How: ", config.versiondnld)
config.Logs.Log(" Version date: ", config.versioncommit)
config.Logs.Log("Start time: ", time.ctime(config.starttime))
with open(config.homedir + "/.ConsoleStart", "w") as f:
	f.write(str(config.starttime) + '\n')
config.Logs.Log("Console Starting  pid: ", config.Console_pid)
config.Logs.Log("Host name: ", config.hostname)
config.Logs.Log("Screen type: ", config.screentype)
config.Logs.Log("Screen Orientation: ", ("Landscape", "Portrait")[config.portrait])
if config.personalsystem:
	config.Logs.Log("Personal System")
if config.previousup > 0:
	config.Logs.Log("Previous Console Lifetime: ", str(datetime.timedelta(seconds=config.previousup)))
if config.lastup > 0:
	config.Logs.Log("Console Last Running at: ", time.ctime(config.lastup))
	config.Logs.Log("Previous Console Downtime: ", str(datetime.timedelta(seconds=(config.starttime - config.lastup))))
config.Logs.Log("Main config file: ", config.configfile,
				time.strftime(' %c', time.localtime(config.configfilelist[config.configfile])))
config.Logs.Log("Including config files:")
for p, f in zip(pfiles, cfiles):
	config.Logs.Log("  ", p, time.strftime(' %c', time.localtime(config.configfilelist[f])))
for flg, fval in debug.Flags.iteritems():
	if fval:
		config.Logs.Log('Debug flag ', flg, '=', fval, severity=logsupport.ConsoleWarning)
		config.LogLevel = 0  # if a debug flag is set force Logging unless explicitly overridden
config.LogLevel = int(config.ParsedConfigFile.get('LogLevel', config.LogLevel))
config.Logs.Log("Log level: ", config.LogLevel)
config.DS = displayscreen.DisplayScreen()  # create the actual device screen and touch manager

utilities.LogParams()

import alerttasks

"""
Set up for ISY access
"""
if config.ISYaddr != '':
	if config.ISYaddr.startswith( 'http' ) :
	  config.ISYprefix = config.ISYaddr + '/rest/'
	else:
	  config.ISYprefix = 'http://' + config.ISYaddr + '/rest/'
	config.ISYrequestsession = requests.session()
	config.ISYrequestsession.auth = (config.ISYuser, config.ISYpassword)

	config.ISY = isy.ISY(config.ISYrequestsession)
	config.Logs.Log("Enumerated ISY Structure")
	# todo seems odd that the following code is here and has to be skipped for null ISY case - should be in the ISY definition
	cmdvar = 'Command.' + config.hostname.replace('-', '.')
	if cmdvar in config.ISY.varsInt:
		alertspeclist = ConfigObj()
		alertspeclist['RemoteCommands2'] = {
			'Type': 'IntVarChange', 'Var': cmdvar, 'Test': 'NE', 'Value': '0',
			'Invoke': 'NetCmd.Command'}
	else:
		alertspeclist = None
else:
	config.ISY = isy.ISY(None)
	alertspeclist = None # todo see comment above
	config.Logs.Log("No ISY Specified", severity=ConsoleWarning)

"""
Set up alerts
"""
if 'Alerts' in config.ParsedConfigFile:
	alertspec = config.ParsedConfigFile['Alerts']
	if alertspeclist is not None:
		alertspec.merge(alertspeclist)
	del config.ParsedConfigFile['Alerts']
else:
	alertspec = ConfigObj()
	if alertspeclist is not None:
		alertspec.merge(alertspeclist)

"""
Build the ISY object structure and connect the configured screens to it
"""

if 'Variables' in config.ParsedConfigFile:
	i = 0
	for nm, val in config.ParsedConfigFile['Variables'].iteritems():
		config.ISY.LocalVars.append(int(val))
		config.ISY.varsLocal[nm] = i
		config.ISY.varsLocalInv[i] = nm
		config.Logs.Log("Local variable: " + nm + "(" + str(i) + ") = " + str(val))
		i += 1
	del config.ParsedConfigFile['Variables']
configobjects.MyScreens()
config.Logs.Log("Linked config to ISY")

"""
Set up the websocket thread to handle ISY stream
"""
isyeventmonitor.CreateWSThread()

"""
Build the alerts structures
"""
config.Alerts = alerttasks.Alerts(alertspec)
config.Logs.Log("Alerts established")

"""
Set up the Maintenance Screen
"""
config.Logs.Log("Built Maintenance Screen")
maintscreen.SetUpMaintScreens()

config.Logs.livelog = False  # turn off logging to the screen and give user a moment to scan
time.sleep(2)

LogBadParams(config.ParsedConfigFile, "Globals")
LogBadParams(alertspec, "Alerts")
"""
Dump documentation if development version
"""
if config.versionname == 'development':
	utilities.DumpDocumentation()

"""
Run the main console loop
"""
config.DS.MainControlLoop(config.HomeScreen)

# This never returns

	# humidity, temperature = Adafruit_DHT.read_retry(22,4)
	# tempF = temperature*9/5.0 +32
	# if humidity is not None and temperature is not None:
	#	print 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(tempF, humidity)

<commit_msg>Prep for systemd changes<commit_after>#!/usr/bin/python -u
# above assumes it points at python 2.7 and may not be portable
"""
Copyright 2016, 2017 Kevin Kahn

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
"""
import importlib
import os
import sys
import signal
import time
import cgitb
import datetime
import pygame

# noinspection PyProtectedMember
from configobj import ConfigObj, Section
import isyeventmonitor

import config
import configobjects
import exitutils
import debug
import displayscreen
import globalparams
import isy
import logsupport
import maintscreen
import utilities
import requests
from logsupport import ConsoleWarning

import json

def handler(signum, frame):
	if signum in (signal.SIGTERM, signal.SIGINT):
		config.Logs.Log("Console received a termination signal ", str(signum), " - Exiting")
		time.sleep(1)
		pygame.display.quit()
		pygame.quit()
		os._exit(0)
	else:
		config.Logs.Log("Console received signal " + str(signum) + " Ignoring")

signal.signal(signal.SIGTERM, handler)
signal.signal(signal.SIGINT, handler)

sectionget = Section.get

print("Console starting in "+os.getcwd())

def CO_get(self, key, default, delkey=True):
	rtn = sectionget(self, key, default)
	if key in self and delkey:
		del self[key]
	return rtn

Section.get = CO_get

def LogBadParams(section, name):
	for nm, s in section.iteritems():
		if isinstance(s, Section):
			LogBadParams(s, nm)
		else:
			config.Logs.Log("Bad (unused) parameter name in: ", name, " (", nm, "=", str(s), ")",
							severity=ConsoleWarning)

config.exdir = os.path.dirname(os.path.abspath(__file__))
config.homedir = os.path.dirname(config.exdir)

earlylog = open(config.homedir + '/Console/earlylog.log', 'w', 0)
earlylog.write("Console start at " + time.strftime('%m-%d-%y %H:%M:%S') + ' on ' + config.hostname + '\n')

if os.getegid() <> 0:
	# Not running as root
	earlylog.write("Not running as root - exit\n")
	print ("Must run as root")
	sys.exit(exitutils.EARLYABORT)

utilities.InitializeEnvironment()

earlylog.write('Environment initialized\n')

lastfn = ""
lastmod = 0
config.Console_pid = os.getpid()

earlylog.write('Exdir: ' + config.exdir + '  Pid: ' + str(config.Console_pid) + '\n')

for root, dirs, files in os.walk(config.exdir):
	for fname in files:
		if fname.endswith(".py"):
			fn = os.path.join(root, fname)
			if os.path.getmtime(fn) > lastmod:
				lastmod = os.path.getmtime(fn)
				lastfn = fn

try:
	with open(config.exdir + '/' + 'versioninfo') as f:
		config.versionname = f.readline()[:-1]
		config.versionsha = f.readline()[:-1]
		config.versiondnld = f.readline()[:-1]
		config.versioncommit = f.readline()[:-1]
except:
	config.versionname = 'none'
	config.versionsha = 'none'
	config.versiondnld = 'none'
	config.versioncommit = 'none'

earlylog.write(
	'Version/Sha/Dnld/Commit: ' + config.versionname + ' ' + config.versionsha + ' ' + config.versiondnld + ' ' + config.versioncommit + '\n')

"""
Dynamically load class definitions for all defined screen types and link them to how configuration happens
"""
#for screentype in os.listdir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/screens'):
for screentype in os.listdir(os.getcwd() + '/screens'):
	if '__' not in screentype:
		splitname = os.path.splitext(screentype)
		if splitname[1] == '.py':
			importlib.import_module('screens.' + splitname[0])

earlylog.write("Screen types imported \n")

#for alertproctype in os.listdir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/alerts'):
for alertproctype in os.listdir(os.getcwd() + '/alerts'):
	if '__' not in alertproctype:
		splitname = os.path.splitext(alertproctype)
		if splitname[1] == '.py':
			importlib.import_module('alerts.' + splitname[0])

earlylog.write("Alert Proc types imported\n")

for n in config.alertprocs:
	config.alertprocs[n] = config.alertprocs[n]()  # instantiate an instance of each alert class

earlylog.write("Alert classes instantiated\n")

"""
Initialize the Console
"""

with open(config.exdir + '/termshortenlist', 'r') as f:
	try:
		config.TermShortener = json.load(f)
	except:
		config.TermShortener = {}

if len(sys.argv) == 2:
	config.configfile = sys.argv[1]
elif os.path.isfile(config.configfilebase + "config.txt"):
	config.configfile = config.configfilebase + "config.txt"
else:
	config.configfile = config.configfilebase + "config-" + config.hostname + ".txt"

print ("Configuration file: ",config.configfile)

if not os.path.isfile(config.configfile):
	print ("Abort - no configuration file found")
	earlylog.write("Abort - no configuration file ('+config.hostname+')'\n")
	exitutils.EarlyAbort('No Configuration File (' + config.hostname + ')')

config.ParsedConfigFile = ConfigObj(config.configfile)  # read the config.txt file

earlylog.write("Parsed base config file\n")

configdir = os.path.dirname(config.configfile)

config.configfilelist[config.configfile] = os.path.getmtime(config.configfile)

cfiles = []
pfiles = []
cfglib = config.ParsedConfigFile.get('cfglib', '')
if cfglib <> '':
	cfglib += '/'
if cfglib[0] <> '/':
	cfglib = configdir + '/' + cfglib
includes = config.ParsedConfigFile.get('include', [])
while includes:
	f = includes.pop(0)
	if f[0] <> '/':
		pfiles.append('+' + f)
		f = cfglib + f
	else:
		pfiles.append(f)
	cfiles.append(f)
	tmpconf = ConfigObj(f)
	includes = includes + tmpconf.get('include', [])
	config.ParsedConfigFile.merge(tmpconf)
	earlylog.write("Merged config file " + f + "\n")
	config.configfilelist[f] = os.path.getmtime(f)

debug.Flags = debug.InitFlags()

utilities.ParseParam(globalparams)  # add global parameters to config file

earlylog.write("Parsed globals\n")

config.Logs = logsupport.Logs(config.screen, os.path.dirname(config.configfile))
cgitb.enable(format='text')
config.Logs.Log(u"Soft ISY Console")
earlylog.write("Switched to real log\n")
earlylog.close()
os.remove(config.homedir + '/Console/earlylog.log')
config.Logs.Log(u"  \u00A9 Kevin Kahn 2016, 2017")
config.Logs.Log("Software under Apache 2.0 License")
config.Logs.Log("Version Information:")
config.Logs.Log(" Run from: ", config.exdir)
config.Logs.Log(" Last mod: ", lastfn)
config.Logs.Log(" Mod at: ", time.ctime(lastmod))
config.Logs.Log(" Tag: ", config.versionname)
config.Logs.Log(" Sha: ", config.versionsha)
config.Logs.Log(" How: ", config.versiondnld)
config.Logs.Log(" Version date: ", config.versioncommit)
config.Logs.Log("Start time: ", time.ctime(config.starttime))
with open(config.homedir + "/.ConsoleStart", "w") as f:
	f.write(str(config.starttime) + '\n')
config.Logs.Log("Console Starting  pid: ", config.Console_pid)
config.Logs.Log("Host name: ", config.hostname)
config.Logs.Log("Screen type: ", config.screentype)
config.Logs.Log("Screen Orientation: ", ("Landscape", "Portrait")[config.portrait])
if config.personalsystem:
	config.Logs.Log("Personal System")
if config.previousup > 0:
	config.Logs.Log("Previous Console Lifetime: ", str(datetime.timedelta(seconds=config.previousup)))
if config.lastup > 0:
	config.Logs.Log("Console Last Running at: ", time.ctime(config.lastup))
	config.Logs.Log("Previous Console Downtime: ", str(datetime.timedelta(seconds=(config.starttime - config.lastup))))
config.Logs.Log("Main config file: ", config.configfile,
				time.strftime(' %c', time.localtime(config.configfilelist[config.configfile])))
config.Logs.Log("Including config files:")
for p, f in zip(pfiles, cfiles):
	config.Logs.Log("  ", p, time.strftime(' %c', time.localtime(config.configfilelist[f])))
for flg, fval in debug.Flags.iteritems():
	if fval:
		config.Logs.Log('Debug flag ', flg, '=', fval, severity=logsupport.ConsoleWarning)
		config.LogLevel = 0  # if a debug flag is set force Logging unless explicitly overridden
config.LogLevel = int(config.ParsedConfigFile.get('LogLevel', config.LogLevel))
config.Logs.Log("Log level: ", config.LogLevel)
config.DS = displayscreen.DisplayScreen()  # create the actual device screen and touch manager

utilities.LogParams()

import alerttasks

"""
Set up for ISY access
"""
if config.ISYaddr != '':
	if config.ISYaddr.startswith( 'http' ) :
	  config.ISYprefix = config.ISYaddr + '/rest/'
	else:
	  config.ISYprefix = 'http://' + config.ISYaddr + '/rest/'
	config.ISYrequestsession = requests.session()
	config.ISYrequestsession.auth = (config.ISYuser, config.ISYpassword)

	config.ISY = isy.ISY(config.ISYrequestsession)
	config.Logs.Log("Enumerated ISY Structure")
	# todo seems odd that the following code is here and has to be skipped for null ISY case - should be in the ISY definition
	cmdvar = 'Command.' + config.hostname.replace('-', '.')
	if cmdvar in config.ISY.varsInt:
		alertspeclist = ConfigObj()
		alertspeclist['RemoteCommands2'] = {
			'Type': 'IntVarChange', 'Var': cmdvar, 'Test': 'NE', 'Value': '0',
			'Invoke': 'NetCmd.Command'}
	else:
		alertspeclist = None
else:
	config.ISY = isy.ISY(None)
	alertspeclist = None # todo see comment above
	config.Logs.Log("No ISY Specified", severity=ConsoleWarning)

"""
Set up alerts
"""
if 'Alerts' in config.ParsedConfigFile:
	alertspec = config.ParsedConfigFile['Alerts']
	if alertspeclist is not None:
		alertspec.merge(alertspeclist)
	del config.ParsedConfigFile['Alerts']
else:
	alertspec = ConfigObj()
	if alertspeclist is not None:
		alertspec.merge(alertspeclist)

"""
Build the ISY object structure and connect the configured screens to it
"""

if 'Variables' in config.ParsedConfigFile:
	i = 0
	for nm, val in config.ParsedConfigFile['Variables'].iteritems():
		config.ISY.LocalVars.append(int(val))
		config.ISY.varsLocal[nm] = i
		config.ISY.varsLocalInv[i] = nm
		config.Logs.Log("Local variable: " + nm + "(" + str(i) + ") = " + str(val))
		i += 1
	del config.ParsedConfigFile['Variables']
configobjects.MyScreens()
config.Logs.Log("Linked config to ISY")

"""
Set up the websocket thread to handle ISY stream
"""
isyeventmonitor.CreateWSThread()

"""
Build the alerts structures
"""
config.Alerts = alerttasks.Alerts(alertspec)
config.Logs.Log("Alerts established")

"""
Set up the Maintenance Screen
"""
config.Logs.Log("Built Maintenance Screen")
maintscreen.SetUpMaintScreens()

config.Logs.livelog = False  # turn off logging to the screen and give user a moment to scan
time.sleep(2)

LogBadParams(config.ParsedConfigFile, "Globals")
LogBadParams(alertspec, "Alerts")
"""
Dump documentation if development version
"""
if config.versionname == 'development':
	utilities.DumpDocumentation()

"""
Run the main console loop
"""
config.DS.MainControlLoop(config.HomeScreen)

# This never returns

<|endoftext|>


<commit_before>import action
from player import Player
from game   import GameState

import random
import os

Players = []
PlayersAlive = []
CurrentPlayer = 0

AvailableActions = []
def SetupActions():
    global AvailableActions
    for action in GameState.CommonActions:
        AvailableActions.append(action)
    for action in GameState.CardsAvailable:
        AvailableActions.append(action)

def Setup():
    # How many people are playing?
    # Generate the player list
    # Shuffle the player list

    GameState.reset()
    #PlayerCount = int(input("How many players? "))
    PlayerCount = 2

    def CreatePlayer(Number):
        player = Player()

        print("Creating Player #%i" % (Number + 1))
        #player.name = input(" What is your name? ")
        player.name = random.choice(["A", "B", "C"]) + str(Number)

        return player

    for i in range(PlayerCount):
        Players.append(CreatePlayer(i))

    random.shuffle(Players)

    global PlayersAlive
    PlayersAlive = [player for player in Players if player.alive]

    SetupActions()

def PrintTurnOrder():
    print ("Turn order:")
    for i, player in enumerate(Players):
        print(" %i: %s" % (i + 1, player.name))

def PrintDeckList():
    print ("Cards in Court Deck (%i cards):" % (len(GameState.Deck)))
    deck = [card.name for card in GameState.Deck]
    deck.sort()
    for card in deck:
        print(" ", card)

def MainLoop():
    # Infinite loop until one player remains
    global PlayersAlive, CurrentPlayer

    while len(PlayersAlive) > 1:
        player = Players[CurrentPlayer]

        def PrintInfo():
            os.system("cls")
            print("%s's turn" % player.name)
            print(" Coins: %i" % player.coins)
            print("=================\n ")
            PrintDeckList()
            print("\nCards in hand of %s: " % (player.name), end = "")
            print(" and ".join([card.name for card in player.influence]))
            print()

        def PrintActions():
            print("Available actions:")
            for i, action in enumerate(AvailableActions):
                print (" %i: %s" % (i + 1, action.name))

        def Cleanup():
            global CurrentPlayer
            CurrentPlayer += 1
            if CurrentPlayer >= len(Players): CurrentPlayer = 0
            PlayersAlive = [player for player in Players if player.alive]

        def ChooseAction():
            action = input ("Action > ")
            if not action.isnumeric():
                ChooseAction()
                return
            action = int(action) - 1

            if not (action >= 0 and action < len(AvailableActions)):
                ChooseAction()
                return

            print("Playing %s" % AvailableActions[action].name)
            status, response = player.play(AvailableActions[action])
            if status == False:
                print (response)
                ChooseAction()

        PrintInfo()
        PrintActions()
        ChooseAction()
        Cleanup()
        input("\nPress enter key to continue...")

    print("\nThe winner is %s" % (PlayersAlive[0].name))

os.system("cls")
Setup()
PrintTurnOrder()
input("\nPress enter key to start...")
MainLoop()<commit_msg>added playing action and targetting<commit_after>import action
from player import Player
from game   import GameState

import random
import os

Players = []
PlayersAlive = []
CurrentPlayer = 0

AvailableActions = []
def SetupActions():
    global AvailableActions
    for action in GameState.CommonActions:
        AvailableActions.append(action)
    for action in GameState.CardsAvailable:
        AvailableActions.append(action)

def Setup():
    # How many people are playing?
    # Generate the player list
    # Shuffle the player list

    GameState.reset()
    #PlayerCount = int(input("How many players? "))
    PlayerCount = 2

    def CreatePlayer(Number):
        player = Player()

        print("Creating Player #%i" % (Number + 1))
        #player.name = input(" What is your name? ")
        player.name = random.choice(["A", "B", "C"]) + str(Number)

        return player

    for i in range(PlayerCount):
        Players.append(CreatePlayer(i))

    random.shuffle(Players)

    global PlayersAlive
    PlayersAlive = [player for player in Players if player.alive]

    SetupActions()

def PrintTurnOrder():
    print ("Turn order:")
    for i, player in enumerate(Players):
        print(" %i: %s" % (i + 1, player.name))

def PrintDeckList():
    print ("Cards in Court Deck (%i cards):" % (len(GameState.Deck)))
    deck = [card.name for card in GameState.Deck]
    deck.sort()
    for card in deck:
        print(" ", card)

def MainLoop():
    # Infinite loop until one player remains
    global PlayersAlive, CurrentPlayer

    while len(PlayersAlive) > 1:
        player = Players[CurrentPlayer]

        def PrintInfo():
            os.system("cls")
            print("%s's turn" % player.name)
            print(" Coins: %i" % player.coins)
            print("=================\n ")
            PrintDeckList()
            print("\nCards in hand of %s: " % (player.name), end = "")
            print(" and ".join([card.name for card in player.influence]))
            print()

        def PrintActions():
            print("Available actions:")
            for i, action in enumerate(AvailableActions):
                print (" %i: %s" % (i + 1, action.name))

        def Cleanup():
            global CurrentPlayer
            CurrentPlayer += 1
            if CurrentPlayer >= len(Players): CurrentPlayer = 0
            PlayersAlive = [player for player in Players if player.alive]

        def ChooseAction():
            move = input ("Action> ")
            if not move.isnumeric():
                ChooseAction()
                return
            move = int(move) - 1

            if not (move >= 0 and move < len(AvailableActions)):
                ChooseAction()
                return

            print("Playing %s" % AvailableActions[move].name)
            try:
                status, response = player.play(AvailableActions[move])
            except action.TargetRequired:
                def ChooseTarget():
                    for i, player in enumerate(Players):
                        print(" %i: %s" % (i + 1, player.name))
                    target = input ("Choose a target>")

                    if not target.isnumeric():
                        return ChooseTarget()
                    target = int(target) - 1
                    if target < 0 or target > len(Players):
                        return ChooseTarget()

                    return target

                target = ChooseTarget()
                status, response = player.play(AvailableActions[move], Players[target])

            if status == False:
                print (response)
                ChooseAction()

        PrintInfo()
        PrintActions()
        ChooseAction()
        Cleanup()
        input("\nPress enter key to continue...")

    print("\nThe winner is %s" % (PlayersAlive[0].name))

os.system("cls")
Setup()
PrintTurnOrder()
input("\nPress enter key to start...")
MainLoop()<|endoftext|>


<commit_before>#!/usr/bin/env python

# add owa to quickdetect - look for function IsOwaPremiumBrowser

import curses
from libs.AngularUtil import *
from libs.WordPressUtil import *
from libs.DrupalUtil import *
from libs.JQueryUtil import *
from libs.WebDriverUtil import *
from libs.gui.cursesutil import *

import time

from libs.gui.javascriptmenu import *
from libs.gui.angularmenu import *

from libs.QuickDetect import *
from libs.JSConsole import *

from libs.spider.spiderscreen import *
from libs.spider.spidercommands import *

from libs.logger import *

from webnukeproxy import *

javascript_to_inject = """
	theParent = document.getElementsByTagName("body")[0];
	theKid = document.createElement("div");
	theKid.innerHTML = '
WebNuke V0.1 BETA

heh
';
	theParent.insertBefore(theKid, theParent.firstChild)

"""

class webnuke_api_server:
	def __init__(self, logger):
		self.version = 0.1
		self.logger = logger

	def start(self):
		self.apiserver = WebAPIServer(8002, logger)
		self.apiserver.startServer()
		self.logger.log("Web server started")

class JavascriptInjector:
	def __init__(self):
		self.version = 0.1
		self.jsfunctions=[]
		self.javascript_block=""
		self.help_block=[]

	def inject_js(self, javascript):
		self.javascript_block+=javascript

	def add_help_topic(self, jsfunction, description):
		self.jsfunctions.append(jsfunction)
		self.help_block.append({'function': jsfunction, 'description': description})

	def get_js_block(self):
		wnhelp_block = """window.wn_help = function(){
	console.log('webnuke: HELP!!');
	console.log('-=-=-=-=-=-=-=-');"""
		for x in self.help_block:
			wnhelp_block+="console.log('"+x['function']+" - "+x['description']+"');"
		wnhelp_block+='};'

		return self.javascript_block+wnhelp_block

class mainframe:
	def __init__(self, logger, proxy_port):
		self.debug = True
		self.proxy_host = 'localhost'
		self.proxy_port = proxy_port
		self.driver = 'notset'
		self.current_url = "NONE"
		self.warning = ''
		self.curses_util = CursesUtil()
		self.logger = logger
		self.jsinjector = JavascriptInjector()
		# load plugin javascript
		self.plugins = [JSConsoleScript(self.jsinjector), JavascriptScript(self.jsinjector)]

	def run_main(self):
		self.logger.log("run_main")
		mystr = 'startup'
		mystr_elements = mystr.split()
		firstelement=mystr_elements[0]

		while firstelement != 'quit':
			 self.screen = self.curses_util.get_screen()

			 self.screen.addstr(2, 2,  "Please enter a command...")
			 self.screen.addstr(4, 4,  "goto <url>   - opens url")
			 self.screen.addstr(5, 4,  "quickdetect  - detect technologies in use on url...")
			 self.screen.addstr(6, 4,  "jsconsole    - opens a console bound to a browser javascript console...")
			 self.screen.addstr(7, 4,  "debug        - toggle debug on/off")
			 self.screen.addstr(8, 4,  "proxy        - set proxy settings...")
			 self.screen.addstr(9, 4,  "!sh          - escape to unix land...")
			 self.screen.addstr(11, 4, "javascript   - Javascript tools menu...")
			 self.screen.addstr(12, 4, "angularjs    - AngularJS tools menu...")
			 self.screen.addstr(14, 4, "spider       - Spider tools menu...")
			 self.screen.addstr(18, 4, "quit         - Exit webnuke")

			 if self.warning is not '':
				 self.screen.addstr(22, 2, self.warning, curses.color_pair(1))
				 self.warning=''

			 if self.proxy_host is not '':
				 self.screen.addstr(0, 1, "PROXY ENABLED", curses.color_pair(1))
			 if self.debug:
				 self.screen.addstr(0, 71, "DEBUG ON", curses.color_pair(1))
			 self.screen.refresh()

			 mystr = self.screen.getstr(20,4).decode(encoding="utf-8")
			 mystr_elements = mystr.split()
			 firstelement='notset'
			 if len(mystr_elements) >= 1:
				firstelement=mystr_elements[0]

			 if firstelement == 'd':
				 self.debug = True
				 self.current_url = "http://bugbound.co.uk"
				 self.open_url(self.current_url)
				 firstelement="bah"

			 if firstelement == 'goto':
				 if len(mystr_elements) >= 2:
					 url = mystr_elements[1]
				 else:
					 url = self.curses_util.get_param("Enter the url")

				 self.open_url(url)

			 if firstelement == 'debug':
				  self.debug = not self.debug

			 if firstelement == 'proxy':
				  self.proxy_host = self.curses_util.get_param("Enter Proxy Server Hostname or IP, Leave BLANK for no proxy")
				  self.proxy_port = self.curses_util.get_param("Enter Proxy Server Port Number")

			 if firstelement == 'quickdetect':
				 if len(mystr_elements) >= 2:
					 url = mystr_elements[1]
					 self.open_url(url)

				 if self.driver == 'notset':
					 self.warning = "QUICKDETECT requires a url is loaded, please set a url using GOTO"
					 return

				 QuickDetect(self.screen, self.driver, self.curses_util).run()

			 if firstelement == 'jsconsole':
				 self.curses_util.close_screen()
				 JSConsole(self.driver, self.jsinjector).run()

			 if firstelement == '!sh':
				  self.curses_util.execute_cmd("bash")

			 if firstelement == 'javascript':
				 JavascriptScreen(self.screen, self.driver, self.curses_util, self.jsinjector).show()

			 if firstelement == 'angularjs':
				 AngularScreen(self.screen, self.driver, self.curses_util).show()

			 if firstelement == 'spider':
				 SpiderScreen(self.screen, self.curses_util, self.current_url, self.proxy_host, self.proxy_port).show()

			 #if firstelement == 'javascript':
				# ResendScreen(self.screen, self.curses_util)

		self.curses_util.close_screen()
	def create_browser_instance(self):
		self.webdriver_util = WebDriverUtil()
		self.webdriver_util.setDebug(self.debug)
		if self.proxy_host is not '' and int(self.proxy_port) is not 0:
			return self.webdriver_util.getDriverWithProxySupport(self.proxy_host, int(self.proxy_port))
		else:
			return self.webdriver_util.getDriver(self.logger)

	def open_url(self, url):
		if self.driver == 'notset':
			self.driver = self.create_browser_instance()
		self.current_url = url
		#self.curses_util.set_footer_url(self.current_url)

		self.driver.get(url)
		self.current_url = self.driver.current_url
		#self.curses_util.set_footer_url(self.current_url)

		#time.sleep(30)
		#self.driver.execute_script(javascript_to_inject)

logger = FileLogger()
logger.log('app started')

try:
	#webserver = webnuke_api_server(logger)
	#webserver.start()
	proxy_port = 8008
	webnukeproxy = ProxySupport("bah1", proxy_port, logger)

	mainwin = mainframe(logger, proxy_port)
	mainwin.run_main()
except:
	logger.log('app error')
	raise
finally:
	webnukeproxy.stop()

logger.log('app ended')

<commit_msg>removed local proxy for now<commit_after>#!/usr/bin/env python

# add owa to quickdetect - look for function IsOwaPremiumBrowser

import curses
from libs.AngularUtil import *
from libs.WordPressUtil import *
from libs.DrupalUtil import *
from libs.JQueryUtil import *
from libs.WebDriverUtil import *
from libs.gui.cursesutil import *

import time

from libs.gui.javascriptmenu import *
from libs.gui.angularmenu import *

from libs.QuickDetect import *
from libs.JSConsole import *

from libs.spider.spiderscreen import *
from libs.spider.spidercommands import *

from libs.logger import *

from webnukeproxy import *

javascript_to_inject = """
	theParent = document.getElementsByTagName("body")[0];
	theKid = document.createElement("div");
	theKid.innerHTML = '
WebNuke V0.1 BETA

heh
';
	theParent.insertBefore(theKid, theParent.firstChild)

"""

class webnuke_api_server:
	def __init__(self, logger):
		self.version = 0.1
		self.logger = logger

	def start(self):
		self.apiserver = WebAPIServer(8002, logger)
		self.apiserver.startServer()
		self.logger.log("Web server started")

class JavascriptInjector:
	def __init__(self):
		self.version = 0.1
		self.jsfunctions=[]
		self.javascript_block=""
		self.help_block=[]

	def inject_js(self, javascript):
		self.javascript_block+=javascript

	def add_help_topic(self, jsfunction, description):
		self.jsfunctions.append(jsfunction)
		self.help_block.append({'function': jsfunction, 'description': description})

	def get_js_block(self):
		wnhelp_block = """window.wn_help = function(){
	console.log('webnuke: HELP!!');
	console.log('-=-=-=-=-=-=-=-');"""
		for x in self.help_block:
			wnhelp_block+="console.log('"+x['function']+" - "+x['description']+"');"
		wnhelp_block+='};'

		return self.javascript_block+wnhelp_block

class mainframe:
	def __init__(self, logger, proxy_port):
		self.debug = True
		#self.proxy_host = 'localhost'
		#self.proxy_port = proxy_portv
		self.proxy_host = ''
		self.proxy_port = 0
		self.driver = 'notset'
		self.current_url = "NONE"
		self.warning = ''
		self.curses_util = CursesUtil()
		self.logger = logger
		self.jsinjector = JavascriptInjector()
		# load plugin javascript
		self.plugins = [JSConsoleScript(self.jsinjector), JavascriptScript(self.jsinjector)]

	def run_main(self):
		self.logger.log("run_main")
		mystr = 'startup'
		mystr_elements = mystr.split()
		firstelement=mystr_elements[0]

		while firstelement != 'quit':
			 self.screen = self.curses_util.get_screen()

			 self.screen.addstr(2, 2,  "Please enter a command...")
			 self.screen.addstr(4, 4,  "goto <url>   - opens url")
			 self.screen.addstr(5, 4,  "quickdetect  - detect technologies in use on url...")
			 self.screen.addstr(6, 4,  "jsconsole    - opens a console bound to a browser javascript console...")
			 self.screen.addstr(7, 4,  "debug        - toggle debug on/off")
			 self.screen.addstr(8, 4,  "proxy        - set proxy settings...")
			 self.screen.addstr(9, 4,  "!sh          - escape to unix land...")
			 self.screen.addstr(11, 4, "javascript   - Javascript tools menu...")
			 self.screen.addstr(12, 4, "angularjs    - AngularJS tools menu...")
			 self.screen.addstr(14, 4, "spider       - Spider tools menu...")
			 self.screen.addstr(18, 4, "quit         - Exit webnuke")

			 if self.warning is not '':
				 self.screen.addstr(22, 2, self.warning, curses.color_pair(1))
				 self.warning=''

			 if self.proxy_host is not '':
				 self.screen.addstr(0, 1, "PROXY ENABLED", curses.color_pair(1))
			 if self.debug:
				 self.screen.addstr(0, 71, "DEBUG ON", curses.color_pair(1))
			 self.screen.refresh()

			 mystr = self.screen.getstr(20,4).decode(encoding="utf-8")
			 mystr_elements = mystr.split()
			 firstelement='notset'
			 if len(mystr_elements) >= 1:
				firstelement=mystr_elements[0]

			 if firstelement == 'd':
				 self.debug = True
				 self.current_url = "http://bugbound.co.uk"
				 self.open_url(self.current_url)
				 firstelement="bah"

			 if firstelement == 'goto':
				 if len(mystr_elements) >= 2:
					 url = mystr_elements[1]
				 else:
					 url = self.curses_util.get_param("Enter the url")

				 self.open_url(url)

			 if firstelement == 'debug':
				  self.debug = not self.debug

			 if firstelement == 'proxy':
				  self.proxy_host = self.curses_util.get_param("Enter Proxy Server Hostname or IP, Leave BLANK for no proxy")
				  self.proxy_port = self.curses_util.get_param("Enter Proxy Server Port Number")

			 if firstelement == 'quickdetect':
				 if len(mystr_elements) >= 2:
					 url = mystr_elements[1]
					 self.open_url(url)

				 if self.driver == 'notset':
					 self.warning = "QUICKDETECT requires a url is loaded, please set a url using GOTO"
					 return

				 QuickDetect(self.screen, self.driver, self.curses_util).run()

			 if firstelement == 'jsconsole':
				 self.curses_util.close_screen()
				 JSConsole(self.driver, self.jsinjector).run()

			 if firstelement == '!sh':
				  self.curses_util.execute_cmd("bash")

			 if firstelement == 'javascript':
				 JavascriptScreen(self.screen, self.driver, self.curses_util, self.jsinjector).show()

			 if firstelement == 'angularjs':
				 AngularScreen(self.screen, self.driver, self.curses_util).show()

			 if firstelement == 'spider':
				 SpiderScreen(self.screen, self.curses_util, self.current_url, self.proxy_host, self.proxy_port).show()

			 #if firstelement == 'javascript':
				# ResendScreen(self.screen, self.curses_util)

		self.curses_util.close_screen()
	def create_browser_instance(self):
		self.webdriver_util = WebDriverUtil()
		self.webdriver_util.setDebug(self.debug)
		if self.proxy_host is not '' and int(self.proxy_port) is not 0:
			return self.webdriver_util.getDriverWithProxySupport(self.proxy_host, int(self.proxy_port))
		else:
			return self.webdriver_util.getDriver(self.logger)

	def open_url(self, url):
		if self.driver == 'notset':
			self.driver = self.create_browser_instance()
		self.current_url = url
		#self.curses_util.set_footer_url(self.current_url)

		self.driver.get(url)
		self.current_url = self.driver.current_url
		#self.curses_util.set_footer_url(self.current_url)

		#time.sleep(30)
		#self.driver.execute_script(javascript_to_inject)

logger = FileLogger()
logger.log('app started')

try:
	#webserver = webnuke_api_server(logger)
	#webserver.start()
	currently_not_used_local_proxy_port = 8008
	#webnukeproxy = ProxySupport("bah1", proxy_port, logger)

	mainwin = mainframe(logger, currently_not_used_local_proxy_port)
	mainwin.run_main()
except:
	logger.log('app error')
	raise
finally:
	webnukeproxy.stop()

logger.log('app ended')

<|endoftext|>


<commit_before>#!/usr/bin/env python3
''' Interactive prompt to inspect specified wordlist
'''

import json
import argparse
from collections import OrderedDict

parser = argparse.ArgumentParser(__doc__)
parser.add_argument('-l', '--list-path',
                    help='Path to wordlist JSON file')

FLAGS = parser.parse_args()

# TODO: support multiple wordlists
obj = OrderedDict(json.load(open(FLAGS.list_path)))

while True:
    print('> ', end='')
    query = input()
    if query in obj:
        for k, v in obj[query].items():
            print(k, ': ', v)
    elif query == '--':
        break
    else:
        print('"{}" not found in wordlist {}'.format(
            query, FLAGS.list_path
        ))
<commit_msg>refactor console with cmd.Cmd<commit_after>#!/usr/bin/env python3
''' Interactive prompt to inspect specified wordlist
'''

import json
import argparse
from cmd import Cmd
import readline
from collections import OrderedDict

readline.set_completer_delims(' \t\n')

class WordlistConsole(Cmd):
    def __init__(self, worddict, dict_name=None):
        super(WordlistConsole, self).__init__()
        self._worddict = worddict
        self._dict_name = dict_name or 'dict'

    @property
    def prompt(self):
        return '({}) '.format(self._dict_name)

    def do_l(self, query):
        # TODO: remove the required 'l' command token
        if query in self._worddict:
            if isinstance(self._worddict[query], dict):
                for k, v in self._worddict[query].items():
                    print(k, ': ', v)
            elif isinstance(self._worddict[query], str):
                print(self._worddict[query])
        else:
            print('"{}" not found in wordlist {}'.format(
                query, self._dict_name
            ))

    def complete_l(self, query, line, start_index, end_index):
        return sorted([word for word in self._worddict
                       if word.startswith(query)])

if __name__ == '__main__':
    parser = argparse.ArgumentParser(__doc__)
    parser.add_argument('-l', '--list-path',
                        help='Path to wordlist JSON file')

    FLAGS = parser.parse_args()

    # TODO: support multiple wordlists
    obj = OrderedDict(json.load(open(FLAGS.list_path)))

    console = WordlistConsole(obj)
    console.cmdloop()

# while True:
#     print('> ', end='')
#     query = input()
#     if query in obj:
#         for k, v in obj[query].items():
#             print(k, ': ', v)
#     elif query == '--':
#         break
#     else:
#         print('"{}" not found in wordlist {}'.format(
#             query, FLAGS.list_path
#         ))
<|endoftext|>


<commit_before>#!/usr/bin/env python
#   -*- encoding: UTF-8 -*-

# This file is part of Addison Arches.
#
# Addison Arches is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Addison Arches is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Addison Arches.  If not, see <http://www.gnu.org/licenses/>.

import cmd
from collections import namedtuple
import datetime
import itertools

class Console(cmd.Cmd):
    clock = (
        t for t in (
            datetime.datetime(year=2015, month=5, day=11) +
            datetime.timedelta(seconds=i)
            for i in itertools.count(0, 30 * 60))
        if 8 <= t.hour <= 19)
    prompt = "{:%A %H:%M} > ".format(next(clock))

    def postcmd(self, stop, line):
        self.prompt = "{:%A %H:%M} > ".format(next(self.clock))
        return stop

if __name__ == "__main__":
    console = Console()
    console.cmdloop()
<commit_msg>First sketch of asyncio event loop.<commit_after>#!/usr/bin/env python
#   -*- encoding: UTF-8 -*-

# This file is part of Addison Arches.
#
# Addison Arches is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Addison Arches is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Addison Arches.  If not, see <http://www.gnu.org/licenses/>.

import asyncio
import cmd
from collections import namedtuple
import concurrent.futures
import datetime
import itertools

import scenario

@asyncio.coroutine
def game_loop(intervals, commands, executor, loop=None):
    yield from intervals.put(5)
    while True:
        line = yield from commands.get()
        yield from intervals.put(30)

@asyncio.coroutine
def user_input(intervals, commands, executor, loop=None, prompt="?"):
    yield from asyncio.sleep(0, loop=loop)
    while True:
        interval = yield from intervals.get()
        try:
            line = yield from asyncio.wait_for(
                    loop.run_in_executor(
                        executor,
                        input,
                        prompt
                    ),
                    interval,
                    loop=loop)
        except asyncio.TimeoutError:
            line = "wait"
        yield from commands.put(line)

class Game:

    def __init__(self, name, console):
        self.name = name
        self.console = console

class Console(cmd.Cmd):
    clock = (
        t for t in (
            datetime.datetime(year=2015, month=5, day=11) +
            datetime.timedelta(seconds=i)
            for i in itertools.islice(
                itertools.count(0, 30 * 60),
                7 * 24 * 60 // 30)
            )
        if 8 <= t.hour <= 19)
    prompt = "{:%A %H:%M} > ".format(next(clock))

    def postcmd(self, stop, line):
        try:
            self.prompt = "{:%A %H:%M} > ".format(next(self.clock))
        except StopIteration:
            stop = True
        return stop

    def do_go(self, arg):
        """
        Boo.
        """
        line = arg.strip()
        if not line:
            print(*["{0:01}: {1.name}".format(n, i)
                    for n, i in enumerate(scenario.locations)],
                  sep="\n")

    def do_quit(self, arg):
        """
        End the game.
        """
        return True

if __name__ == "__main__":
    name = input("Please enter your name: ")
    game = Game(name=name, console=Console())
    loop = asyncio.get_event_loop()
    intervals = asyncio.Queue()
    commands = asyncio.Queue()
    routines = [user_input, game_loop]
    executor = concurrent.futures.ThreadPoolExecutor(len(routines))
    tasks = [
        asyncio.Task(routine(intervals, commands, executor, loop=loop))
        for routine in routines
    ]
    try:
        loop.run_forever()
    finally:
        loop.close()
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>, and others
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

try:
    import json
except ImportError:
    import simplejson as json
import shlex
import os
import subprocess
import sys
import datetime
import traceback
import signal
import time
import syslog

def daemonize_self():
    # daemonizing code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
    # logger.info("cobblerd started")
    try:
        pid = os.fork()
        if pid > 0:
            # exit first parent
            sys.exit(0)
    except OSError, e:
        print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/")
    os.setsid()
    os.umask(022)

    # do second fork
    try:
        pid = os.fork()
        if pid > 0:
            # print "Daemon PID %d" % pid
            sys.exit(0)
    except OSError, e:
        print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
        sys.exit(1)

    dev_null = file('/dev/null','rw')
    os.dup2(dev_null.fileno(), sys.stdin.fileno())
    os.dup2(dev_null.fileno(), sys.stdout.fileno())
    os.dup2(dev_null.fileno(), sys.stderr.fileno())

if len(sys.argv) < 3:
    print json.dumps({
        "failed" : True,
        "msg"    : "usage: async_wrapper <jid> <time_limit> <modulescript> <argsfile>.  Humans, do not call directly!"
    })
    sys.exit(1)

jid = "%s.%d" % (sys.argv[1], os.getpid())
time_limit = sys.argv[2]
wrapped_module = sys.argv[3]
argsfile = sys.argv[4]
cmd = "%s %s" % (wrapped_module, argsfile)

syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % " ".join(sys.argv[1:]))

# setup logging directory
logdir = os.path.expanduser("~/.ansible_async")
log_path = os.path.join(logdir, jid)

if not os.path.exists(logdir):
    try:
        os.makedirs(logdir)
    except:
        print json.dumps({
            "failed" : 1,
            "msg" : "could not create: %s" % logdir
        })

def _run_command(wrapped_cmd, jid, log_path):

    logfile = open(log_path, "w")
    logfile.write(json.dumps({ "started" : 1, "ansible_job_id" : jid }))
    logfile.close()
    logfile = open(log_path, "w")
    result = {}

    outdata = ''
    try:
        cmd = shlex.split(wrapped_cmd)
        script = subprocess.Popen(cmd, shell=False,
            stdin=None, stdout=logfile, stderr=logfile)
        script.communicate()
        outdata = file(log_path).read()
        result = json.loads(outdata)

    except (OSError, IOError), e:
        result = {
            "failed": 1,
            "cmd" : wrapped_cmd,
            "msg": str(e),
        }
        result['ansible_job_id'] = jid
        logfile.write(json.dumps(result))
    except:
        result = {
            "failed" : 1,
            "cmd" : wrapped_cmd,
            "data" : outdata, # temporary debug only
            "msg" : traceback.format_exc()
        }
        result['ansible_job_id'] = jid
        logfile.write(json.dumps(result))
    logfile.close()

# immediately exit this process, leaving an orphaned process
# running which immediately forks a supervisory timing process

#import logging
#import logging.handlers

#logger = logging.getLogger("ansible_async")
#logger.setLevel(logging.WARNING)
#logger.addHandler( logging.handlers.SysLogHandler("/dev/log") )
def debug(msg):
    #logger.warning(msg)
    pass

try:
    pid = os.fork()
    if pid:
        # Notify the overlord that the async process started

        # we need to not return immmediately such that the launched command has an attempt
        # to initialize PRIOR to ansible trying to clean up the launch directory (and argsfile)
        # this probably could be done with some IPC later.  Modules should always read
        # the argsfile at the very first start of their execution anyway
        time.sleep(1)
        debug("Return async_wrapper task started.")
        print json.dumps({ "started" : 1, "ansible_job_id" : jid, "results_file" : log_path })
        sys.stdout.flush()
        sys.exit(0)
    else:
        # The actual wrapper process

        # Daemonize, so we keep on running
        daemonize_self()

        # we are now daemonized, create a supervisory process
        debug("Starting module and watcher")

        sub_pid = os.fork()
        if sub_pid:
            # the parent stops the process after the time limit
            remaining = int(time_limit)

            # set the child process group id to kill all children
            os.setpgid(sub_pid, sub_pid)

            debug("Start watching %s (%s)"%(sub_pid, remaining))
            time.sleep(5)
            while os.waitpid(sub_pid, os.WNOHANG) == (0, 0):
                debug("%s still running (%s)"%(sub_pid, remaining))
                time.sleep(5)
                remaining = remaining - 5
                if remaining <= 0:
                    debug("Now killing %s"%(sub_pid))
                    os.killpg(sub_pid, signal.SIGKILL)
                    debug("Sent kill to group %s"%sub_pid)
                    time.sleep(1)
                    sys.exit(0)
            debug("Done in kid B.")
            os._exit(0)
        else:
            # the child process runs the actual module
            debug("Start module (%s)"%os.getpid())
            _run_command(cmd, jid, log_path)
            debug("Module complete (%s)"%os.getpid())
            sys.exit(0)

except Exception, err:
    debug("error: %s"%(err))
    raise err
<commit_msg>code cleanup and reoorg, renamed vars and functions to actual purpose reneabled logging of steps<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>, and others
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

try:
    import json
except ImportError:
    import simplejson as json
import shlex
import os
import subprocess
import sys
import traceback
import signal
import time
import syslog

syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % " ".join(sys.argv[1:]))

def notice(msg):
    syslog.syslog(syslog.LOG_NOTICE, msg)

def daemonize_self():
    # daemonizing code: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
    try:
        pid = os.fork()
        if pid > 0:
            # exit first parent
            sys.exit(0)
    except OSError, e:
        print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/")
    os.setsid()
    os.umask(022)

    # do second fork
    try:
        pid = os.fork()
        if pid > 0:
            # print "Daemon PID %d" % pid
            sys.exit(0)
    except OSError, e:
        print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
        sys.exit(1)

    dev_null = file('/dev/null','rw')
    os.dup2(dev_null.fileno(), sys.stdin.fileno())
    os.dup2(dev_null.fileno(), sys.stdout.fileno())
    os.dup2(dev_null.fileno(), sys.stderr.fileno())

def _run_module(wrapped_cmd, jid, job_path):

    jobfile = open(job_path, "w")
    jobfile.write(json.dumps({ "started" : 1, "ansible_job_id" : jid }))
    jobfile.close()
    jobfile = open(job_path, "w")
    result = {}

    outdata = ''
    try:
        cmd = shlex.split(wrapped_cmd)
        script = subprocess.Popen(cmd, shell=False, stdin=None, stdout=jobfile, stderr=jobfile)
        script.communicate()
        outdata = file(job_path).read()
        result = json.loads(outdata)

    except (OSError, IOError), e:
        result = {
            "failed": 1,
            "cmd" : wrapped_cmd,
            "msg": str(e),
        }
        result['ansible_job_id'] = jid
        jobfile.write(json.dumps(result))
    except:
        result = {
            "failed" : 1,
            "cmd" : wrapped_cmd,
            "data" : outdata, # temporary notice only
            "msg" : traceback.format_exc()
        }
        result['ansible_job_id'] = jid
        jobfile.write(json.dumps(result))
    jobfile.close()

####################
##      main      ##
####################
if __name__ == '__main__':

    if len(sys.argv) < 3:
        print json.dumps({
            "failed" : True,
            "msg"    : "usage: async_wrapper <jid> <time_limit> <modulescript> <argsfile>.  Humans, do not call directly!"
        })
        sys.exit(1)

    jid = "%s.%d" % (sys.argv[1], os.getpid())
    time_limit = sys.argv[2]
    wrapped_module = sys.argv[3]
    argsfile = sys.argv[4]
    cmd = "%s %s" % (wrapped_module, argsfile)
    step = 5

    # setup job output directory
    jobdir = os.path.expanduser("~/.ansible_async")
    job_path = os.path.join(jobdir, jid)

    if not os.path.exists(jobdir):
        try:
            os.makedirs(jobdir)
        except:
            print json.dumps({
                "failed" : 1,
                "msg" : "could not create: %s" % jobdir
            })
    # immediately exit this process, leaving an orphaned process
    # running which immediately forks a supervisory timing process

    try:
        pid = os.fork()
        if pid:
            # Notify the overlord that the async process started

            # we need to not return immmediately such that the launched command has an attempt
            # to initialize PRIOR to ansible trying to clean up the launch directory (and argsfile)
            # this probably could be done with some IPC later.  Modules should always read
            # the argsfile at the very first start of their execution anyway
            notice("Return async_wrapper task started.")
            print json.dumps({ "started" : 1, "ansible_job_id" : jid, "results_file" : job_path })
            sys.stdout.flush()
            time.sleep(1)
            sys.exit(0)
        else:
            # The actual wrapper process

            # Daemonize, so we keep on running
            daemonize_self()

            # we are now daemonized, create a supervisory process
            notice("Starting module and watcher")

            sub_pid = os.fork()
            if sub_pid:
                # the parent stops the process after the time limit
                remaining = int(time_limit)

                # set the child process group id to kill all children
                os.setpgid(sub_pid, sub_pid)

                notice("Start watching %s (%s)"%(sub_pid, remaining))
                time.sleep(step)
                while os.waitpid(sub_pid, os.WNOHANG) == (0, 0):
                    notice("%s still running (%s)"%(sub_pid, remaining))
                    time.sleep(step)
                    remaining = remaining - step
                    if remaining <= 0:
                        notice("Now killing %s"%(sub_pid))
                        os.killpg(sub_pid, signal.SIGKILL)
                        notice("Sent kill to group %s"%sub_pid)
                        time.sleep(1)
                        sys.exit(0)
                notice("Done in kid B.")
                sys.exit(0)
            else:
                # the child process runs the actual module
                notice("Start module (%s)"%os.getpid())
                _run_module(cmd, jid, job_path)
                notice("Module complete (%s)"%os.getpid())
                sys.exit(0)

    except Exception, err:
        notice("error: %s"%(err))
        print json.dumps({
            "failed" : True,
            "msg"    : "FATAL ERROR: %s" % str(err)
        })
        sys.exit(1)
<|endoftext|>


<commit_before>####    Hierarchy
##      Creators: Adam Stiles and Ryan McKay
##      July 2016
##		Made in Python

## Heirarchy is a game of survival that urges the player to discover what it means to be Human.
<commit_msg>GUI Module work<commit_after>####    Hierarchy
##      Creators: Adam Stiles and Ryan McKay
##      July 2016
##      Made in Python

## Heirarchy is a game of survival that urges the player to discover what it means to be Human.

import pygame
import random
pygame.init()
random.seed()

config = {
    'description': 'Hierarchy',
    'author': 'Adam Stiles and Ryan McKay',
    'url': 'URL to get it at.',
    'download_url': 'Where to download it.',
    'author_email': 'My email.',
    'version': '0.1',
    'install_requires': [''],
    'packages': ['NAME'],
    'scripts': [],
    'name': 'projectname'
}

class GUI(object):

    def __init__(self):
        #set window screen res - pixels
        self.resolution = res_width, res_height = (1280, 720)
        self.screen = pygame.display.set_mode(self.resolution)
        pygame.display.set_caption('Hierarchy')
        #Divide Map
        self.map_screen_dim = map_positionx, map_positiony, map_width, map_height  = (0, 0, 3*res_width/4, res_height)
        self.map_screen_position = pygame.Rect(self.map_screen_dim)
        self.map_sub_screen = self.screen.subsurface(self.map_screen_position)
        # Divide Stats screen
        self.stats_screen_dim = stats_positionx, stats_positiony, stats_width, stats_height = (map_width, 0, res_width/4, res_height)
        self.stats_screen_position = pygame.Rect(self.stats_screen_dim)
        self.stats_sub_screen = self.screen.subsurface(self.stats_screen_position)

        #Initiate map surface - pixels
        self.whole_map_dim = ((10000, 10000))
        self.whole_map = pygame.Surface(self.whole_map_dim)
        #fill black
        black = (0,0,0)
        self.whole_map.fill(black)

        #Mount a section of the map surfacen the map screen
        self.map_segment_dim = segment_posx, segment_posy, segment_width, segment_height = (0, 0, map_width, map_height)
        self.map_segment = pygame.Rect(self.map_segment_dim)
        self.map_sub_screen.blit(self.whole_map.subsurface(self.map_segment), (0,0))

        #Draw to display
        pygame.display.flip()

def main():
    screen = GUI()

if __name__ == "__main__":
    main()<|endoftext|>


<commit_before># Convert NYC building footprints and addresses into importable OSM files.
from fiona import collection
from lxml import etree
from lxml.etree import tostring
from rtree import index
from shapely.geometry import asShape
from shapely import speedups
from sys import argv
from glob import glob
import re
from pprint import pprint

# Converts given building and address shapefiles into corresponding OSM XML
# files.
def convert(buildingIn, addressIn, osmOut):
    # Load all addresses.
    addresses = []

    with collection(addressIn, "r") as input:
        for address in input:
            shape = asShape(address['geometry'])
            shape.original = address
            addresses.append(shape)

    # Load and index all buildings.
    buildingIdx = index.Index()
    buildings = []
    with collection(buildingIn, "r") as input:
        for building in input:
            building['shape'] = asShape(building['geometry'])
            building['properties']['addresses'] = []
            buildings.append(building)
            buildingIdx.add(len(buildings) - 1, building['shape'].bounds)

    # Map addresses to buildings.
    for address in addresses:
        for i in buildingIdx.intersection(address.bounds):
            if buildings[i]['shape'].contains(address):
                buildings[i]['properties']['addresses'].append(
                    address.original)

    # Generates a new osm id.
    osmIds = dict(node = -1, way = -1, rel = -1)
    def newOsmId(type):
        osmIds[type] = osmIds[type] - 1
        return osmIds[type]

    ## Formats multi part house numbers
    def formatHousenumber(p):
        def suffix(part1, part2, hyphen_type=None):
            part1 = stripZeroes(part1)
            if not part2:
                return str(part1)
            part2 = stripZeroes(part2)
            if hyphen_type == 'U': # unit numbers
                return part1 + '-' + part2
            if len(part2) == 1 and part2.isalpha(): # single letter extensions
                return part1 + part2
            return part1 + ' ' + part2 # All others
        def stripZeroes(addr): # strip leading zeroes from numbers
            if addr.isdigit():
                addr = str(int(addr))
            if '-' in addr:
                addr = str(int(addr.split('-')[0])) + '-' + str(int(addr.split('-')[1]))
            return addr
        number = suffix(p['HOUSE_NUMB'], p['HOUSE_NU_1'], p['HYPHEN_TYP'])
        if p['HOUSE_NU_2']:
            number = number + ' - ' + suffix(p['HOUSE_NU_2'], p['HOUSE_NU_3'])
        return number

    # Converts an address
    def convertAddress(address):
        result = dict()
        if all (k in address for k in ('HOUSE_NUMB', 'STREET_NAM')):
            if address['HOUSE_NUMB']:
                result['addr:housenumber'] = formatHousenumber(address)
            if address['STREET_NAM']:
                if re.match('^(\d+)\w\w$', address['STREET_NAM']): # Test for 2ND, 14TH, 21ST
                    streetname = address['STREET_NAM'].lower()
                else:
                    streetname = address['STREET_NAM'].title()
                result['addr:street'] = streetname
            if address['ZIPCODE']:
                result['addr:postcode'] = str(int(address['ZIPCODE']))
        return result

    # Appends new node or returns existing if exists.
    nodes = {}
    def appendNewNode(coords, osmXml):
        rlon = int(float(coords[0]*10**7))
        rlat = int(float(coords[1]*10**7))
        if (rlon, rlat) in nodes:
            return nodes[(rlon, rlat)]
        node = etree.Element('node', visible = 'true', id = str(newOsmId('node')))
        node.set('lon', str(coords[0]))
        node.set('lat', str(coords[1]))
        nodes[(rlon, rlat)] = node
        osmXml.append(node)
        return node

    def appendNewWay(coords, osmXml):
        way = etree.Element('way', visible='true', id=str(newOsmId('way')))
        firstNid = 0
        for i, coord in enumerate(coords):
            if i == 0: continue # the first and last coordinate are the same
            node = appendNewNode(coord, osmXml)
            if i == 1: firstNid = node.get('id')
            way.append(etree.Element('nd', ref=node.get('id')))
        way.append(etree.Element('nd', ref=firstNid)) # close way
        osmXml.append(way)
        return way

    # Appends an address to a given node or way.
    def appendAddress(address, element):
        for k, v in convertAddress(address['properties']).iteritems():
            element.append(etree.Element('tag', k=k, v=v))

    # Appends a building to a given OSM xml document.
    def appendBuilding(building, address, osmXml):
        # Export building, create multipolygon if there are interior shapes.
        interiors = []
        try:
            way = appendNewWay(list(building['shape'].exterior.coords), osmXml)
            for interior in building['shape'].interiors:
                interiors.append(appendNewWay(list(interior.coords), osmXml))
        except AttributeError:
            way = appendNewWay(list(building['shape'][0].exterior.coords), osmXml)
            for interior in building['shape'][0].interiors:
                interiors.append(appendNewWay(list(interior.coords), osmXml))
        if len(interiors) > 0:
            relation = etree.Element('relation', visible='true', id=str(newOsmId('way')))
            relation.append(etree.Element('member', type='way', role='outer', ref=way.get('id')))
            for interior in interiors:
                relation.append(etree.Element('member', type='way', role='inner', ref=interior.get('id')))
            relation.append(etree.Element('tag', k='type', v='multipolygon'))
            osmXml.append(relation)
            way = relation
        way.append(etree.Element('tag', k='building', v='yes'))
        if 'HEIGHT_ROO' in building['properties']:
            height = round(((building['properties']['HEIGHT_ROO'] * 12) * 0.0254), 1)
            way.append(etree.Element('tag', k='height', v=str(height)))
        if 'BIN' in building['properties']:
            way.append(etree.Element('tag', k='nycdoitt:bin', v=str(building['properties']['BIN'])))
        if address: appendAddress(address, way)

    # Export buildings & addresses. Only export address with building if thre is exactly
    # one address per building. Export remaining addresses as individual nodes.
    addresses = []
    osmXml = etree.Element('osm', version='0.6', generator='alex@mapbox.com')
    for building in buildings:
        address = None
        if len(building['properties']['addresses']) == 1:
            address = building['properties']['addresses'][0]
        else:
            addresses.extend(building['properties']['addresses'])
        appendBuilding(building, address, osmXml)
    if (len(addresses) > 0):
        for address in addresses:
            node = appendNewNode(address['geometry']['coordinates'], osmXml)
            appendAddress(address, node)
    with open(osmOut, 'w') as outFile:
        outFile.writelines(tostring(osmXml, pretty_print=True, xml_declaration=True, encoding='UTF-8'))
        print "Exported " + osmOut

# Run conversions. Expects an chunks/addresses-[district id].shp for each
# chunks/buildings-[district id].shp. Optinally convert only one election district.
if (len(argv) == 2):
    convert(
        'chunks/buildings-%s.shp' % argv[1],
        'chunks/addresses-%s.shp' % argv[1],
        'osm/buildings-addresses-%s.osm' % argv[1])
else:
    buildingFiles = glob("chunks/buildings-*.shp")
    for buildingFile in buildingFiles:
        matches = re.match('^.*-(\d+)\.shp$', buildingFile).groups(0)
        convert(
            buildingFile,
            'chunks/addresses-%s.shp' % matches[0],
            'osm/buildings-addresses-%s.osm' % matches[0])
<commit_msg>share nodes between intersecting ways, ref #8<commit_after># Convert NYC building footprints and addresses into importable OSM files.
from fiona import collection
from lxml import etree
from lxml.etree import tostring
from rtree import index
from shapely.geometry import asShape, Point, LineString
from shapely import speedups
from sys import argv
from glob import glob
import re
from pprint import pprint

# Converts given building and address shapefiles into corresponding OSM XML
# files.
def convert(buildingIn, addressIn, osmOut):
    # Load all addresses.
    addresses = []

    with collection(addressIn, "r") as input:
        for address in input:
            shape = asShape(address['geometry'])
            shape.original = address
            addresses.append(shape)

    # Load and index all buildings.
    buildingIdx = index.Index()
    buildings = []
    with collection(buildingIn, "r") as input:
        for building in input:
            building['shape'] = asShape(building['geometry'])
            building['properties']['addresses'] = []
            buildings.append(building)
            buildingIdx.add(len(buildings) - 1, building['shape'].bounds)

    # Map addresses to buildings.
    for address in addresses:
        for i in buildingIdx.intersection(address.bounds):
            if buildings[i]['shape'].contains(address):
                buildings[i]['properties']['addresses'].append(
                    address.original)

    # Generates a new osm id.
    osmIds = dict(node = -1, way = -1, rel = -1)
    def newOsmId(type):
        osmIds[type] = osmIds[type] - 1
        return osmIds[type]

    ## Formats multi part house numbers
    def formatHousenumber(p):
        def suffix(part1, part2, hyphen_type=None):
            part1 = stripZeroes(part1)
            if not part2:
                return str(part1)
            part2 = stripZeroes(part2)
            if hyphen_type == 'U': # unit numbers
                return part1 + '-' + part2
            if len(part2) == 1 and part2.isalpha(): # single letter extensions
                return part1 + part2
            return part1 + ' ' + part2 # All others
        def stripZeroes(addr): # strip leading zeroes from numbers
            if addr.isdigit():
                addr = str(int(addr))
            if '-' in addr:
                try:
                    addr = str(int(addr.split('-')[0])) + '-' + str(int(addr.split('-')[1]))
                except:
                    pass
            return addr
        number = suffix(p['HOUSE_NUMB'], p['HOUSE_NU_1'], p['HYPHEN_TYP'])
        if p['HOUSE_NU_2']:
            number = number + ' - ' + suffix(p['HOUSE_NU_2'], p['HOUSE_NU_3'])
        return number

    # Converts an address
    def convertAddress(address):
        result = dict()
        if all (k in address for k in ('HOUSE_NUMB', 'STREET_NAM')):
            if address['HOUSE_NUMB']:
                result['addr:housenumber'] = formatHousenumber(address)
            if address['STREET_NAM']:
                if re.match('^(\d+)\w\w$', address['STREET_NAM']): # Test for 2ND, 14TH, 21ST
                    streetname = address['STREET_NAM'].lower()
                else:
                    streetname = address['STREET_NAM'].title()
                result['addr:street'] = streetname
            if address['ZIPCODE']:
                result['addr:postcode'] = str(int(address['ZIPCODE']))
        return result

    # Appends new node or returns existing if exists.
    nodes = {}
    def appendNewNode(coords, osmXml):
        rlon = int(float(coords[0]*10**7))
        rlat = int(float(coords[1]*10**7))
        if (rlon, rlat) in nodes:
            return nodes[(rlon, rlat)]
        node = etree.Element('node', visible = 'true', id = str(newOsmId('node')))
        node.set('lon', str(coords[0]))
        node.set('lat', str(coords[1]))
        nodes[(rlon, rlat)] = node
        osmXml.append(node)
        return node

    def appendNewWay(coords, intersects, osmXml):
        way = etree.Element('way', visible='true', id=str(newOsmId('way')))
        firstNid = 0
        for i, coord in enumerate(coords):
            if i == 0: continue # the first and last coordinate are the same
            node = appendNewNode(coord, osmXml)
            if i == 1: firstNid = node.get('id')
            way.append(etree.Element('nd', ref=node.get('id')))

            # Check each way segment for intersecting nodes
            int_nodes = {}
            try:
                line = LineString([coord, coords[i+1]])
            except IndexError:
                line = LineString([coord, coords[1]])
            for idx, c in enumerate(intersects):
                if line.buffer(0.000001).contains(Point(c[0], c[1])) and c not in coords:
                    t_node = appendNewNode(c, osmXml)
                    for n in way.iter('nd'):
                        if n.get('ref') == t_node.get('id'):
                            break
                    else:
                        int_nodes[t_node.get('id')] = Point(c).distance(Point(coord))
            for n in sorted(int_nodes, key=lambda key: int_nodes[key]): # add intersecting nodes in order
                way.append(etree.Element('nd', ref=n))

        way.append(etree.Element('nd', ref=firstNid)) # close way
        osmXml.append(way)
        return way

    # Appends an address to a given node or way.
    def appendAddress(address, element):
        for k, v in convertAddress(address['properties']).iteritems():
            element.append(etree.Element('tag', k=k, v=v))

    # Appends a building to a given OSM xml document.
    def appendBuilding(building, address, osmXml):
        # Check for intersecting buildings
        intersects = []
        for i in buildingIdx.intersection(building['shape'].bounds):
            for c in buildings[i]['shape'].exterior.coords:
                if Point(c[0], c[1]).intersects(building['shape']):
                    intersects.append(c)

        # Export building, create multipolygon if there are interior shapes.
        interiors = []
        try:
            way = appendNewWay(list(building['shape'].exterior.coords), intersects, osmXml)
            for interior in building['shape'].interiors:
                interiors.append(appendNewWay(list(interior.coords), [], osmXml))
        except AttributeError:
            way = appendNewWay(list(building['shape'][0].exterior.coords), intersects, osmXml)
            for interior in building['shape'][0].interiors:
                interiors.append(appendNewWay(list(interior.coords), [], osmXml))
        if len(interiors) > 0:
            relation = etree.Element('relation', visible='true', id=str(newOsmId('way')))
            relation.append(etree.Element('member', type='way', role='outer', ref=way.get('id')))
            for interior in interiors:
                relation.append(etree.Element('member', type='way', role='inner', ref=interior.get('id')))
            relation.append(etree.Element('tag', k='type', v='multipolygon'))
            osmXml.append(relation)
            way = relation
        way.append(etree.Element('tag', k='building', v='yes'))
        if 'HEIGHT_ROO' in building['properties']:
            height = round(((building['properties']['HEIGHT_ROO'] * 12) * 0.0254), 1)
            way.append(etree.Element('tag', k='height', v=str(height)))
        if 'BIN' in building['properties']:
            way.append(etree.Element('tag', k='nycdoitt:bin', v=str(building['properties']['BIN'])))
        if address: appendAddress(address, way)

    # Export buildings & addresses. Only export address with building if thre is exactly
    # one address per building. Export remaining addresses as individual nodes.
    addresses = []
    osmXml = etree.Element('osm', version='0.6', generator='alex@mapbox.com')
    for building in buildings:
        address = None
        if len(building['properties']['addresses']) == 1:
            address = building['properties']['addresses'][0]
        else:
            addresses.extend(building['properties']['addresses'])
        appendBuilding(building, address, osmXml)
    if (len(addresses) > 0):
        for address in addresses:
            node = appendNewNode(address['geometry']['coordinates'], osmXml)
            appendAddress(address, node)

    with open(osmOut, 'w') as outFile:
        outFile.writelines(tostring(osmXml, pretty_print=True, xml_declaration=True, encoding='UTF-8'))
        print "Exported " + osmOut

speedups.enable()
# Run conversions. Expects an chunks/addresses-[district id].shp for each
# chunks/buildings-[district id].shp. Optinally convert only one election district.
if (len(argv) == 2):
    convert(
        'chunks/buildings-%s.shp' % argv[1],
        'chunks/addresses-%s.shp' % argv[1],
        'osm/buildings-addresses-%s.osm' % argv[1])
else:
    buildingFiles = glob("chunks/buildings-*.shp")
    for buildingFile in buildingFiles:
        matches = re.match('^.*-(\d+)\.shp$', buildingFile).groups(0)
        convert(
            buildingFile,
            'chunks/addresses-%s.shp' % matches[0],
            'osm/buildings-addresses-%s.osm' % matches[0])
<|endoftext|>