<commit_before>#!/usr/bin/env python
"""
__init__.py - Jenni Init Module
Copyright 2008, <NAME>, inamidst.com
Licensed under the Eiffel Forum License 2.

http://inamidst.com/phenny/
"""

import sys, os, time, threading, signal
import bot

class Watcher(object):
    # Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
    def __init__(self):
        self.child = os.fork()
        if self.child != 0:
            self.watch()

    def watch(self):
        try: os.wait()
        except KeyboardInterrupt:
            self.kill()
        sys.exit()

    def kill(self):
        try: os.kill(self.child, signal.SIGKILL)
        except OSError: pass

def run_jenni(config):
    if hasattr(config, 'delay'):
        delay = config.delay
    else: delay = 20

    def connect(config):
        p = bot.Jenni(config)
        p.run(config.host, config.port)

    try: Watcher()
    except Exception, e:
        print >> sys.stderr, 'Warning:', e, '(in __init__.py)'

    while True:
        try: connect(config)
        except KeyboardInterrupt:
            sys.exit()

        if not isinstance(delay, int):
            break

        warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
        print >> sys.stderr, warning
        time.sleep(delay)

def run(config):
    t = threading.Thread(target=run_jenni, args=(config,))
    if hasattr(t, 'run'):
        t.run()
    else: t.start()

if __name__ == '__main__':
    print __doc__<commit_msg>[core] Add debugging to core exceptions, remove useless thread<commit_after>#!/usr/bin/env python
# coding=utf-8
"""
__init__.py - Willie Init Module
Copyright 2008, <NAME>, inamidst.com
Copyright 2012, <NAME>, http://embolalia.net
Copyright © 2012, <NAME> <<EMAIL>>

Licensed under the Eiffel Forum License 2.

http://willie.dftba.net/
"""

import sys, os, time, threading, signal
import bot

class Watcher(object):
    # Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
    def __init__(self):
        self.child = os.fork()
        if self.child != 0:
            self.watch()

    def watch(self):
        try: os.wait()
        except KeyboardInterrupt:
            self.kill()
        sys.exit()

    def kill(self):
        try: os.kill(self.child, signal.SIGKILL)
        except OSError: pass

def run(config):
    if hasattr(config, 'delay'):
        delay = config.delay
    else: 
        delay = 20

    try: Watcher()
    except Exception, e:
        print >> sys.stderr, 'Warning:', e, '(in __init__.py)'

    while True:
        try: 
            p = bot.Jenni(config)
            p.run(config.host, config.port)
        except KeyboardInterrupt:
            sys.exit()
        except Exception, e:
            import traceback
            trace = traceback.format_exc()
            try:
                print trace
            except:
                pass
            logfile = open('logs/exceptions.log', 'a') #todo: make not hardcoded
            logfile.write('Critical exception in core')
            logfile.write(e)
            logfile.write(trace)
            logfile.write('----------------------------------------\n\n')
            logfile.close()
            raise e

        if not isinstance(delay, int):
            break

        warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
        try:
            print >> sys.stderr, warning
        except:
            pass
        time.sleep(delay)

if __name__ == '__main__':
    print __doc__
{'fraction_non_alphanumeric': 0.13138267355134825, 'fraction_numerical': 0.010613884107860011, 'mean_word_length': 1.9626168224299065, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3565258', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1055, 'n_words': 277}