<commit_before>#!/usr/bin/env python
# coding: utf-8
from logging.handlers import RotatingFileHandler
from logging import getLogger, StreamHandler, Formatter, Filter, DEBUG, ERROR, INFO, WARN, CRITICAL
from datetime import datetime as datetime_datetime
from time import time as timestlocal, strftime, strptime, localtime, timezone
from threading import Lock
from xmlrpclib import Server
from re import compile as re_compile
from os import path as os_path
from sys import getdefaultencoding
from locale import setlocale, LC_ALL, getlocale
from gettext import install as gettext_install, GNUTranslations, NullTranslations
from urllib2 import quote, unquote
from time import time as timest
from traceback import extract_stack
from sys import _getframe
from json import loads as json_loads, dumps as json_dumps

import __builtin__
import xmlrpclib
import sys
import threading

spinner = "jqmica/images/ajax-loader.gif"

def timest() :
    return timestlocal() - timezone

texts = {}

cwd = re_compile(".*\/").search(os_path.realpath(__file__)).group(0)
sys.path = [cwd] + sys.path

verbose = False
#verbose = True

if getdefaultencoding() != "utf-8" :
    if verbose :
        print "Correcting the default encoding back to UTF-8 from " + getdefaultencoding()
    reload(sys).setdefaultencoding("utf-8")

try :
    from jnius import autoclass
    String = autoclass('java.lang.String')
except ImportError, e :
    String = False
    if verbose :
        print("pyjnius not available. Probably on a server.")

gnutextkwargs = {}
catalogs = threading.local()

if sys.version_info[0] < 3:
    # In Python 2, ensure that the _() that gets installed into built-ins
    # always returns unicodes.  This matches the default behavior under Python
    # 3, although that keyword argument is not present in the Python 3 API.
    gnutextkwargs['unicode'] = True

gettext_install("mica", **gnutextkwargs)

micalogger = False
txnlogger = False
duplicate_logger = False

def get_global_language() :
    global global_language
    return global_language

def dump_all_threads() :
    mdebug("\n*** STACKTRACE - START ***\n")

    for threadId, stack in sys._current_frames().items():
        mdebug("\n# ThreadID: " + str(threadId) + " " + str(stack))
        for filename, lineno, name, line in extract_stack(stack):
            mdebug("File: " + str(filename))
            mdebug("File: %s, line %d, in %s" % (filename, lineno, name))
            if line:
                mdebug("  %s" % (line.strip()))

    mdebug("\n*** STACKTRACE - END ***\n")

def gettext(message):
    global global_language
    try :
        result = texts[catalogs.language].ugettext(message)
        return result
    except AttributeError, e :
        return texts[global_language].ugettext(message)

def pre_init_localization(language, log = False) :
    global global_language
    if String and log :
        log.debug(String("Beginning localization: " + language))

    for l in lang :
       locale = l.split("-")[0]
       try:
           texts[locale] = GNUTranslations(open(cwd + "res/messages_" + locale + ".mo", 'rb'))
       except IOError:
           if l == u"en" :
               texts[locale] = NullTranslations()
           else :
               print("Language translation " + l + " failed. Bailing...")
               exit(1)

    __builtin__.__dict__['_'] = gettext

    if language.count("-") :
        language = language.split("-")[0]
    if language.count("_") :
        language = language.split("_")[0]

    if language in texts :
        global_language = language

    test = "Translation test: " + texts[global_language].ugettext("Please wait...")
    test2 = "Translation test 2: " + _("Please wait...")
    if String and log :
        log.debug(String("language set to: " + global_language))
        log.debug(String(test))
        log.debug(String(test2))
    else :
        if verbose :
            print("Language set to: " + global_language)
        #print(test)
        #print(test2)

lang = {
         u"zh-CHS" : _(u"Chinese Simplified"),
         u"en" : _(u"English"),
         u"py" : _(u"Pinyin"),
         u"es" : _(u"Spanish"),
       }

supported = {
          u"en,zh-CHS" : _(u"English to Chinese"),
          u"zh-CHS,en" : _(u"Chinese to English"),
          u"es,en" : _(u"Spanish to English"),
          u"en,es" : _(u"English to Spanish"),
        }

supported_map = {
        u"zh-CHS" : u"zh-CHS",
        u"zh" : u"zh-CHS",
        u"en" : u"en",
        u"es" : u"es",
        u"py" : u"py",
}

processor_map = {
        u"zh-CHS,en" : u"ChineseSimplifiedToEnglish",
        u"en,zh-CHS" : u"EnglishToChineseSimplified",
        u"es,en" : u"SpanishToEnglish",
        u"en,es" : u"EnglishToSpanish",
        u"py": False,
}

tutorials = {
        u"zh" : "info_template.html",
        u"en" : "info_template.html",
        u"es" : "info_template.html",
        u"py": "info_template.html",
}

def prefix() :
   f = _getframe(2)
   log_prefix = f.f_code.co_filename.split('/')[-1] + '/'
   if 'self' in f.f_locals :
       log_prefix += f.f_locals['self'].__class__.__name__ + '.'
   log_prefix += f.f_code.co_name + ":" + str(f.f_lineno)
   log_prefix += " "
   return log_prefix

def minfo(msg) :
   if micalogger :
       micalogger.info(prefix() + msg)
   else :
       print prefix() + msg
   if duplicate_logger and String :
      duplicate_logger.info(String(prefix() + msg))

def mverbose(msg) :
    if verbose :
        mdebug(prefix() + msg)

def mdebug(msg) :
   if micalogger :
       micalogger.debug(prefix() + msg)
       #micalogger.debug(threading.current_thread().name + ": " + prefix() + msg)
   else :
       print prefix() + msg

   if duplicate_logger and String :
      duplicate_logger.debug(String(prefix() + msg))

def mwarn(msg) :
   if micalogger :
       micalogger.warn(prefix() + msg)
   else :
       print prefix() + msg
   if duplicate_logger and String :
      duplicate_logger.warn(String(prefix() + msg))

def merr(msg) :
   if micalogger :
       micalogger.error(prefix() + msg)
   else :
       print prefix() + msg
   if duplicate_logger and String :
      duplicate_logger.err(String(prefix() + msg))

mobile = True

try :
    from jnius import autoclass
    String = autoclass('java.lang.String')
except ImportError, e :
    try :
        from pyobjus import autoclass, objc_f, objc_str as String, objc_l as Long, objc_i as Integer
    except ImportError, e :
        mverbose("pyjnius and pyobjus not available. Probably on a server.")
        mobile = False

if not mobile :
    from requests_oauthlib import OAuth2Session
    from requests_oauthlib.compliance_fixes import facebook_compliance_fix, weibo_compliance_fix

def mica_init_logging(logfile, duplicate = False) :
    global micalogger
    global duplicate_logger

    if duplicate :
        duplicate_logger = duplicate

    # Reset the logging handlers
    logger = getLogger()
    while len(logger.handlers) != 0 :
        logger.removeHandler(logger.handlers[0])

    restlogger = getLogger("restkit.client")
    restlogger.setLevel(level=INFO)
    txnlogger = getLogger("txn")
    txnlogger.setLevel(level=INFO)
    micalogger = getLogger("")
    micalogger.setLevel(DEBUG)

    formatter = Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    if logfile and logfile != 1 :
        handler = RotatingFileHandler(logfile, maxBytes=(1048576*5), backupCount=7)
        handler.setFormatter(formatter)

        micalogger.addHandler(handler)
        txnlogger.addHandler(handler)
        restlogger.addHandler(handler)

    streamhandler = StreamHandler(sys.stdout)
    streamhandler.setFormatter(formatter)

    micalogger.addHandler(streamhandler)
    txnlogger.addHandler(streamhandler)
    restlogger.addHandler(streamhandler)

def wait_for_port_ready(hostname, port, try_once = False) :
    '''
    TBD
    '''
    while True :
        try:
            s = socket_socket()
            s.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)
            s.bind((hostname, int(port)))
            s.close()
            break
        except socket_error, (value, message) :
            if value == 98 :
                mwarn("Previous port " + str(port) + " taken! ...")
                if try_once :
                    return False
                sleep(30)
                continue
            else :
                merr("Could not test port " + str(port) + " liveness: " +  message)
                raise
    return True

class MICASlaveException(Exception) :
    def __init__(self, status, msg):
        Exception.__init__(self)
        self.msg = msg
        self.status = str(status)
    def __str__(self):
        return self.msg

def makeTimestampNoDate(supplied_epoch_time = False) :
    if not supplied_epoch_time :
        _now = datetime_datetime.now()
    else :
        _now = datetime_datetime.fromtimestamp(supplied_epoch_time)

    _date = _now.date()

    result = ""

    result += strftime(" %I:%M:%S %p",
                        strptime(str(_now.hour) + ":" + str(_now.minute) + ":" + \
                                 str(_now.second), "%H:%M:%S"))
    return result

def makeTimestamp(supplied_epoch_time = False) :
    if not supplied_epoch_time :
        _now = datetime_datetime.now()
    else :
        _now = datetime_datetime.fromtimestamp(supplied_epoch_time)

    _date = _now.date()

    result = ("%02d" % _date.month) + "/" + ("%02d" % _date.day) + "/" + ("%04d" % _date.year)

    result += strftime(" %I:%M:%S %p",
                        strptime(str(_now.hour) + ":" + str(_now.minute) + ":" + \
                                 str(_now.second), "%H:%M:%S"))

    result += strftime(" %Z", localtime(time()))
    return result

mutex = Lock()

class MICASlaveClient(Server):
    def slave_error_check(self, func):
        def wrapped(*args, **kwargs):
            try :
                mutex.acquire()
                resp = func(*args, **kwargs)
                mutex.release()
            except Exception, e :
                mutex.release()
                raise e
            if int(resp["status"]) :
                raise MICASlaveException(str(resp["status"]), resp["msg"])
            if self.print_message :
                print resp["msg"]
            return resp["result"]
        return wrapped

    def __init__ (self, service_url, print_message = False):

        '''
         This rewrites the xmlrpc function bindings to use a
         decorator so that we can check the return status of the Slave
         functions before returning them back to the client
         It allows the client object to directly inherit all
         of the Slave calls exposed on the server side to the
         client side without writing ANOTHER lookup table.
        '''

        _orig_Method = xmlrpclib._Method

        '''
        XML-RPC doesn't support keyword arguments,
        so we have to do it ourselves...
        '''
        class KeywordArgMethod(_orig_Method):
            def __call__(self, *args, **kwargs):
                args = list(args)
                if kwargs:
                    args.append(("kwargs", kwargs))
                return _orig_Method.__call__(self, *args)

        xmlrpclib._Method = KeywordArgMethod

        Server.__init__(self, service_url)

        setattr(self, "_ServerProxy__request", self.slave_error_check(self._ServerProxy__request))
        self.vms = {}
        self.msattrs = None
        self.msci = None
        self.username = None
        self.print_message = print_message
        self.last_refresh = datetime_datetime.now()

def myquote(val):
    if isinstance(val, unicode) :
        val_unquoted = quote(val.encode("utf-8"))
    else :
        val_unquoted = quote(val)

    if isinstance(val_unquoted, str) :
        val_unquoted = val_unquoted.decode("utf-8")

    return val_unquoted

times = {}
tree = []

def tracefunc(frame, event, arg, indent=[0]):
    global times
    if event == "call":
        tree.append(frame.f_code.co_name)
        name = ".".join(tree)
        indent[0] += 1
        if name not in times :
            times[name] = {"level" : indent[0], "time" : 0, "calls" : 0}
        times[name]["start"] = timest()
        #mdebug("-" * indent[0] + "> call function: " + name)
    elif event == "return":
        name = ".".join(tree)
        if name in times :
            times[name]["time"] = times[name]["time"] + (timest() - times[name]["start"])
            times[name]["calls"] += 1

        indent[0] -= 1
        #mdebug("<" + "-" * indent[0] + " exit function: " + name)
        del tree[-1]

    return tracefunc

def call_report() :
    global times
    mdebug("Times length: " + str(len(times)))
    fulist = []
    for fname, fdata in times.iteritems() :
        fdata["fname"] = fname
        fulist.append(fdata)

    def by_time( a ):
        return a["time"]

    fulist.sort(key=by_time, reverse = True)

    for fidx in range(0, min(50, len(fulist))) :
        fdata = fulist[fidx]
        mdebug("Function: " + fdata["fname"] + " calls: " + str(fdata["calls"]) + " level: " + str(fdata["level"]) + " time: " + str(fdata["time"]))
    times = {}

def sdict(**kwargs) :
    return json_dumps(kwargs)

def test_log(test, **kwargs) :
    if test :
        logentry = sdict(**kwargs)
        mdebug("Logging: " + logentry)
        fh = open(test + "translator.log", 'a')
        fh.write(logentry + "\n")
        fh.close()

def getFromDict(dataDict, mapList):
    return reduce(lambda d, k: d[k], mapList, dataDict)

def setInDict(dataDict, mapList, value):
    getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value

def recursiveSetInDict(dataDict, mapList, value) :
    for idx in range(0, len(mapList)) :
        sublist = mapList[:(idx + 1)]
        try :
            getFromDict(dataDict, sublist)
        except KeyError, e :
            if idx == (len(mapList) - 1) :
                setInDict(dataDict, sublist, value)
            else :
                setInDict(dataDict, sublist, {})
<commit_msg>fixed missing language<commit_after>#!/usr/bin/env python
# coding: utf-8
from logging.handlers import RotatingFileHandler
from logging import getLogger, StreamHandler, Formatter, Filter, DEBUG, ERROR, INFO, WARN, CRITICAL
from datetime import datetime as datetime_datetime
from time import time as timestlocal, strftime, strptime, localtime, timezone
from threading import Lock
from xmlrpclib import Server
from re import compile as re_compile
from os import path as os_path
from sys import getdefaultencoding
from locale import setlocale, LC_ALL, getlocale
from gettext import install as gettext_install, GNUTranslations, NullTranslations
from urllib2 import quote, unquote
from time import time as timest
from traceback import extract_stack
from sys import _getframe
from json import loads as json_loads, dumps as json_dumps

import __builtin__
import xmlrpclib
import sys
import threading

spinner = "jqmica/images/ajax-loader.gif"

def timest() :
    return timestlocal() - timezone

texts = {}

cwd = re_compile(".*\/").search(os_path.realpath(__file__)).group(0)
sys.path = [cwd] + sys.path

verbose = False
#verbose = True

if getdefaultencoding() != "utf-8" :
    if verbose :
        print "Correcting the default encoding back to UTF-8 from " + getdefaultencoding()
    reload(sys).setdefaultencoding("utf-8")

try :
    from jnius import autoclass
    String = autoclass('java.lang.String')
except ImportError, e :
    String = False
    if verbose :
        print("pyjnius not available. Probably on a server.")

gnutextkwargs = {}
catalogs = threading.local()

if sys.version_info[0] < 3:
    # In Python 2, ensure that the _() that gets installed into built-ins
    # always returns unicodes.  This matches the default behavior under Python
    # 3, although that keyword argument is not present in the Python 3 API.
    gnutextkwargs['unicode'] = True

gettext_install("mica", **gnutextkwargs)

micalogger = False
txnlogger = False
duplicate_logger = False

def get_global_language() :
    global global_language
    return global_language

def dump_all_threads() :
    mdebug("\n*** STACKTRACE - START ***\n")

    for threadId, stack in sys._current_frames().items():
        mdebug("\n# ThreadID: " + str(threadId) + " " + str(stack))
        for filename, lineno, name, line in extract_stack(stack):
            mdebug("File: " + str(filename))
            mdebug("File: %s, line %d, in %s" % (filename, lineno, name))
            if line:
                mdebug("  %s" % (line.strip()))

    mdebug("\n*** STACKTRACE - END ***\n")

def gettext(message):
    global global_language
    try :
        result = texts[catalogs.language].ugettext(message)
        return result
    except KeyError, e :
        return texts[global_language].ugettext(message)
    except AttributeError, e :
        return texts[global_language].ugettext(message)

def pre_init_localization(language, log = False) :
    global global_language
    if String and log :
        log.debug(String("Beginning localization: " + language))

    for l in lang :
       locale = l.split("-")[0]
       try:
           texts[locale] = GNUTranslations(open(cwd + "res/messages_" + locale + ".mo", 'rb'))
       except IOError:
           if l == u"en" :
               texts[locale] = NullTranslations()
           else :
               print("Language translation " + l + " failed. Bailing...")
               exit(1)

    __builtin__.__dict__['_'] = gettext

    if language.count("-") :
        language = language.split("-")[0]
    if language.count("_") :
        language = language.split("_")[0]

    if language in texts :
        global_language = language

    test = "Translation test: " + texts[global_language].ugettext("Please wait...")
    test2 = "Translation test 2: " + _("Please wait...")
    if String and log :
        log.debug(String("language set to: " + global_language))
        log.debug(String(test))
        log.debug(String(test2))
    else :
        if verbose :
            print("Language set to: " + global_language)
        #print(test)
        #print(test2)

lang = {
         u"zh-CHS" : _(u"Chinese Simplified"),
         u"en" : _(u"English"),
         u"py" : _(u"Pinyin"),
         u"es" : _(u"Spanish"),
       }

supported = {
          u"en,zh-CHS" : _(u"English to Chinese"),
          u"zh-CHS,en" : _(u"Chinese to English"),
          u"es,en" : _(u"Spanish to English"),
          u"en,es" : _(u"English to Spanish"),
        }

supported_map = {
        u"zh-CHS" : u"zh-CHS",
        u"zh" : u"zh-CHS",
        u"en" : u"en",
        u"es" : u"es",
        u"py" : u"py",
}

processor_map = {
        u"zh-CHS,en" : u"ChineseSimplifiedToEnglish",
        u"en,zh-CHS" : u"EnglishToChineseSimplified",
        u"es,en" : u"SpanishToEnglish",
        u"en,es" : u"EnglishToSpanish",
        u"py": False,
}

tutorials = {
        u"zh" : "info_template.html",
        u"en" : "info_template.html",
        u"es" : "info_template.html",
        u"py": "info_template.html",
}

def prefix() :
   f = _getframe(2)
   log_prefix = f.f_code.co_filename.split('/')[-1] + '/'
   if 'self' in f.f_locals :
       log_prefix += f.f_locals['self'].__class__.__name__ + '.'
   log_prefix += f.f_code.co_name + ":" + str(f.f_lineno)
   log_prefix += " "
   return log_prefix

def minfo(msg) :
   if micalogger :
       micalogger.info(prefix() + msg)
   else :
       print prefix() + msg
   if duplicate_logger and String :
      duplicate_logger.info(String(prefix() + msg))

def mverbose(msg) :
    if verbose :
        mdebug(prefix() + msg)

def mdebug(msg) :
   if micalogger :
       micalogger.debug(prefix() + msg)
       #micalogger.debug(threading.current_thread().name + ": " + prefix() + msg)
   else :
       print prefix() + msg

   if duplicate_logger and String :
      duplicate_logger.debug(String(prefix() + msg))

def mwarn(msg) :
   if micalogger :
       micalogger.warn(prefix() + msg)
   else :
       print prefix() + msg
   if duplicate_logger and String :
      duplicate_logger.warn(String(prefix() + msg))

def merr(msg) :
   if micalogger :
       micalogger.error(prefix() + msg)
   else :
       print prefix() + msg
   if duplicate_logger and String :
      duplicate_logger.err(String(prefix() + msg))

mobile = True

try :
    from jnius import autoclass
    String = autoclass('java.lang.String')
except ImportError, e :
    try :
        from pyobjus import autoclass, objc_f, objc_str as String, objc_l as Long, objc_i as Integer
    except ImportError, e :
        mverbose("pyjnius and pyobjus not available. Probably on a server.")
        mobile = False

if not mobile :
    from requests_oauthlib import OAuth2Session
    from requests_oauthlib.compliance_fixes import facebook_compliance_fix, weibo_compliance_fix

def mica_init_logging(logfile, duplicate = False) :
    global micalogger
    global duplicate_logger

    if duplicate :
        duplicate_logger = duplicate

    # Reset the logging handlers
    logger = getLogger()
    while len(logger.handlers) != 0 :
        logger.removeHandler(logger.handlers[0])

    restlogger = getLogger("restkit.client")
    restlogger.setLevel(level=INFO)
    txnlogger = getLogger("txn")
    txnlogger.setLevel(level=INFO)
    micalogger = getLogger("")
    micalogger.setLevel(DEBUG)

    formatter = Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    if logfile and logfile != 1 :
        handler = RotatingFileHandler(logfile, maxBytes=(1048576*5), backupCount=7)
        handler.setFormatter(formatter)

        micalogger.addHandler(handler)
        txnlogger.addHandler(handler)
        restlogger.addHandler(handler)

    streamhandler = StreamHandler(sys.stdout)
    streamhandler.setFormatter(formatter)

    micalogger.addHandler(streamhandler)
    txnlogger.addHandler(streamhandler)
    restlogger.addHandler(streamhandler)

def wait_for_port_ready(hostname, port, try_once = False) :
    '''
    TBD
    '''
    while True :
        try:
            s = socket_socket()
            s.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)
            s.bind((hostname, int(port)))
            s.close()
            break
        except socket_error, (value, message) :
            if value == 98 :
                mwarn("Previous port " + str(port) + " taken! ...")
                if try_once :
                    return False
                sleep(30)
                continue
            else :
                merr("Could not test port " + str(port) + " liveness: " +  message)
                raise
    return True

class MICASlaveException(Exception) :
    def __init__(self, status, msg):
        Exception.__init__(self)
        self.msg = msg
        self.status = str(status)
    def __str__(self):
        return self.msg

def makeTimestampNoDate(supplied_epoch_time = False) :
    if not supplied_epoch_time :
        _now = datetime_datetime.now()
    else :
        _now = datetime_datetime.fromtimestamp(supplied_epoch_time)

    _date = _now.date()

    result = ""

    result += strftime(" %I:%M:%S %p",
                        strptime(str(_now.hour) + ":" + str(_now.minute) + ":" + \
                                 str(_now.second), "%H:%M:%S"))
    return result

def makeTimestamp(supplied_epoch_time = False) :
    if not supplied_epoch_time :
        _now = datetime_datetime.now()
    else :
        _now = datetime_datetime.fromtimestamp(supplied_epoch_time)

    _date = _now.date()

    result = ("%02d" % _date.month) + "/" + ("%02d" % _date.day) + "/" + ("%04d" % _date.year)

    result += strftime(" %I:%M:%S %p",
                        strptime(str(_now.hour) + ":" + str(_now.minute) + ":" + \
                                 str(_now.second), "%H:%M:%S"))

    result += strftime(" %Z", localtime(time()))
    return result

mutex = Lock()

class MICASlaveClient(Server):
    def slave_error_check(self, func):
        def wrapped(*args, **kwargs):
            try :
                mutex.acquire()
                resp = func(*args, **kwargs)
                mutex.release()
            except Exception, e :
                mutex.release()
                raise e
            if int(resp["status"]) :
                raise MICASlaveException(str(resp["status"]), resp["msg"])
            if self.print_message :
                print resp["msg"]
            return resp["result"]
        return wrapped

    def __init__ (self, service_url, print_message = False):

        '''
         This rewrites the xmlrpc function bindings to use a
         decorator so that we can check the return status of the Slave
         functions before returning them back to the client
         It allows the client object to directly inherit all
         of the Slave calls exposed on the server side to the
         client side without writing ANOTHER lookup table.
        '''

        _orig_Method = xmlrpclib._Method

        '''
        XML-RPC doesn't support keyword arguments,
        so we have to do it ourselves...
        '''
        class KeywordArgMethod(_orig_Method):
            def __call__(self, *args, **kwargs):
                args = list(args)
                if kwargs:
                    args.append(("kwargs", kwargs))
                return _orig_Method.__call__(self, *args)

        xmlrpclib._Method = KeywordArgMethod

        Server.__init__(self, service_url)

        setattr(self, "_ServerProxy__request", self.slave_error_check(self._ServerProxy__request))
        self.vms = {}
        self.msattrs = None
        self.msci = None
        self.username = None
        self.print_message = print_message
        self.last_refresh = datetime_datetime.now()

def myquote(val):
    if isinstance(val, unicode) :
        val_unquoted = quote(val.encode("utf-8"))
    else :
        val_unquoted = quote(val)

    if isinstance(val_unquoted, str) :
        val_unquoted = val_unquoted.decode("utf-8")

    return val_unquoted

times = {}
tree = []

def tracefunc(frame, event, arg, indent=[0]):
    global times
    if event == "call":
        tree.append(frame.f_code.co_name)
        name = ".".join(tree)
        indent[0] += 1
        if name not in times :
            times[name] = {"level" : indent[0], "time" : 0, "calls" : 0}
        times[name]["start"] = timest()
        #mdebug("-" * indent[0] + "> call function: " + name)
    elif event == "return":
        name = ".".join(tree)
        if name in times :
            times[name]["time"] = times[name]["time"] + (timest() - times[name]["start"])
            times[name]["calls"] += 1

        indent[0] -= 1
        #mdebug("<" + "-" * indent[0] + " exit function: " + name)
        del tree[-1]

    return tracefunc

def call_report() :
    global times
    mdebug("Times length: " + str(len(times)))
    fulist = []
    for fname, fdata in times.iteritems() :
        fdata["fname"] = fname
        fulist.append(fdata)

    def by_time( a ):
        return a["time"]

    fulist.sort(key=by_time, reverse = True)

    for fidx in range(0, min(50, len(fulist))) :
        fdata = fulist[fidx]
        mdebug("Function: " + fdata["fname"] + " calls: " + str(fdata["calls"]) + " level: " + str(fdata["level"]) + " time: " + str(fdata["time"]))
    times = {}

def sdict(**kwargs) :
    return json_dumps(kwargs)

def test_log(test, **kwargs) :
    if test :
        logentry = sdict(**kwargs)
        mdebug("Logging: " + logentry)
        fh = open(test + "translator.log", 'a')
        fh.write(logentry + "\n")
        fh.close()

def getFromDict(dataDict, mapList):
    return reduce(lambda d, k: d[k], mapList, dataDict)

def setInDict(dataDict, mapList, value):
    getFromDict(dataDict, mapList[:-1])[mapList[-1]] = value

def recursiveSetInDict(dataDict, mapList, value) :
    for idx in range(0, len(mapList)) :
        sublist = mapList[:(idx + 1)]
        try :
            getFromDict(dataDict, sublist)
        except KeyError, e :
            if idx == (len(mapList) - 1) :
                setInDict(dataDict, sublist, value)
            else :
                setInDict(dataDict, sublist, {})
<|endoftext|>


<commit_before>SIMDEM_VERSION = "0.8.0-dev"
SIMDEM_TEMP_DIR = "~/.simdem/tmp"

# When in demo mode we insert a small random delay between characters.
# TYPING DELAY is the upper bound of this delay.
TYPING_DELAY = 0.08

# Prompt to use in the console
console_prompt = "$ "

# Port for web server when running with '--webui true' optios
port = 8080

# ------------------------------------------------------------------ #
# Danger zone
#
# Do not change anything after this notice,
# unless you know what you are doing
# ------------------------------------------------------------------ #

# Set is_debug to True if you want to run in debug mode. This setting
# can be overriden in the command like with the `--debug true` option.
is_debug = True

# Available modes of execution
modes = [ "tutorial", "demo", "learn", "test", "script" ]

<commit_msg>turn debug off again<commit_after>SIMDEM_VERSION = "0.8.0-dev"
SIMDEM_TEMP_DIR = "~/.simdem/tmp"

# When in demo mode we insert a small random delay between characters.
# TYPING DELAY is the upper bound of this delay.
TYPING_DELAY = 0.08

# Prompt to use in the console
console_prompt = "$ "

# Port for web server when running with '--webui true' optios
port = 8080

# ------------------------------------------------------------------ #
# Danger zone
#
# Do not change anything after this notice,
# unless you know what you are doing
# ------------------------------------------------------------------ #

# Set is_debug to True if you want to run in debug mode. This setting
# can be overriden in the command like with the `--debug true` option.
is_debug = False

# Available modes of execution
modes = [ "tutorial", "demo", "learn", "test", "script" ]

<|endoftext|>


<commit_before>from SDWLE.constants import CHARACTER_CLASS

card_abbreviations = {
    'Mark of the Wild': 'Mrk Wild',
    'Power of the Wild': 'Pow Wild',
    'Wild Growth': 'Wld Grth',
    'Healing Touch': 'Hlng Tch',
    'Mark of Nature': 'Mrk Ntr',
    'Savage Roar': 'Svg Roar',
    'Soul of the Forest': 'Sol Frst',
    'Force of Nature': 'Frce Nat',
    'Keeper of the Grove': 'Kpr Grve',
    'Druid of the Claw': 'Drd Claw',
    'Stonetusk Boar': 'Stntsk Br',
    'Raging Worgen': 'Rgng Wrgn',
}

def abbreviate(card_name):
    return card_abbreviations.get(card_name, card_name)

def game_to_string(game):
    pass

class console:
    @classmethod
    def log(self, message, source=None, color=0):
        print(message)

class ConsoleGameRender:
    def __init__(self, window, game, viewing_player):
        if viewing_player is game.players[0]:
            self.top_player = game.players[1]
            self.bottom_player = game.players[0]
        else:
            self.top_player = game.players[0]
            self.bottom_player = game.players[1]

        # curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
        # curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_WHITE)
        # curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLUE)
        # curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_YELLOW)

        # self.top_minion_window = window.derwin(3, 80, 4, 0)
        # self.bottom_minion_window = window.derwin(3, 80, 8, 0)
        # self.card_window = window.derwin(5, 80, 16, 0)
        # self.card_window = window.derwin(5, 80, 16, 0)
        self.top_minion_window = None
        self.bottom_minion_window = None
        self.card_window = None
        self.card_window = None
        self.window = window
        self.game = game
        self.targets = None
        self.selected_target = None
        self.selection_index = -1

        self.lines = []

    def console_printline(self):
        for line in self.lines:
            console.log(line)

    def draw_minion(self, minion, window, y, x, main=True):
        status_array = []
        color = 0
        if minion.can_attack():
            status_array.append("*")
            if not self.targets:
                 color = 2
        else:
            if not self.targets:
                color = 1
        if "attack" in minion.events:
            status_array.append("a")
        if "turn_start" in minion.events:
            status_array.append("b")
        if minion.charge:
            status_array.append("c")
        if minion.deathrattle is not None:
            status_array.append("d")
        if minion.enraged:
            status_array.append("e")
        if minion.frozen:
            status_array.append("f")
        if minion.immune:
            status_array.append("i")
        if minion.stealth:
            status_array.append("s")
        if minion.taunt:
            status_array.append("t")
        if minion.exhausted and not minion.charge:
            status_array.append("z")

        # if self.targets:
        #     if minion is self.selected_target:
        #         color = curses.color_pair(4)
        #     elif minion in self.targets:
        #         color = curses.color_pair(3)

        name = abbreviate(minion.card.name)[:10]
        status = ''.join(status_array)
        power_line = "({0}) ({1})".format(minion.calculate_attack(), minion.health)
        facedown = ''

        if minion.card.is_facedown():
            status = name
            name = 'facedown'

        #console(y, x, "{0}{1}:{2} {3:^9} {4:^9} {5:^9}".format(spaces, minion.index, facedown, name, power_line, status), color)
        #console(y+1, x,"{0:^9}".format(power_line), color)
        #console(y+2, x, "{0:^9}".format(status), color)
        # window.addstr(y + 2, x, "{0}".format(minion.index), color

        self.lines[0] += '[{0:^10}]'.format(name)
        self.lines[1] += '[{0:^10}]'.format(power_line)
        self.lines[2] += '[{0:^1} {1:^8}]'.format(minion.index, status[:8])

    def draw_card(self, card, player, index, window, y, x):
        color = 0
        if card.can_use(player, player.game):
            status = "*"
            if not self.targets:
                color = 2
        else:
            status = " "
            if not self.targets:
                color = 1
        if self.targets:
            if card is self.selected_target:
                color = 4
            elif card in self.targets:
                color = 3

        name = card.name[:15]

        #console(y + 0, x, "{0}:{1:>2} mana ({2}) {3:^15}   ".format(index, card.mana_cost(), status, name), color)
        # console(y + 1, x, "{0:^15}".format(name), color)

        self.lines[0] += '+' + '-'*10 + '+'
        self.lines[1] += '|{0:^10}|'.format(name[:10])
        self.lines[2] += '|{0:^10}|'.format(status[:10])
        self.lines[3] += '|{0:^10}|'.format(index)

    def draw_hero(self, player, window, x, y):
        # color = curses.color_pair(0)
        # if self.targets:
        #     if player.hero is self.selected_target:
        #         color = curses.color_pair(4)
        #     elif player.hero in self.targets:
        #         color = curses.color_pair(3)
        # if player.weapon is not None:
        #     weapon_power = "({0}) ({1})".format(player.weapon.base_attack, player.weapon.durability)
        #     window.addstr(y, x, "{0:^20}".format(player.weapon.card.name))
        #     window.addstr(y + 1, x, "{0:^20}".format(weapon_power))
        #
        # hero_power = "({0}) ({1}+{4}) -- {2}/{3}".format(player.hero.calculate_attack(), player.hero.health,
        #                                                  player.mana, player.max_mana, player.hero.armor)
        # window.addstr(y, x + 20, "{0:^20}".format(CHARACTER_CLASS.to_str(player.hero.character_class)), color)
        # window.addstr(y + 1, x + 20, "{0:^20}".format(hero_power), color)
        #
        # window.addstr(y, x + 40, "{0:^20}".format("Hero Power"))
        # if player.hero.power.can_use():
        #     window.addstr(y + 1, x + 40, "{0:^20}".format("*"))
        pass

    def draw_game(self):
        # console(0,0,'draw_game Turn:{0}'.format(self.game._turns_passed))
        # self.window.clear()
        # self.bottom_minion_window.clear()
        # self.top_minion_window.clear()
        # self.card_window.clear()

        def draw_minions(minions, window, main):
            self.lines = ['','','','']
            l_offset = int((80 - 10 * len(minions)) / 2)
            index = 0
            for minion in minions:
                # if main and index == self.selection_index:
                #     window.addstr(2, l_offset + index * 10 - 1, "^")
                self.draw_minion(minion, window, 0, l_offset + index * 10, main)
                index += 1
            # if main and len(minions) == self.selection_index:
            #     window.addstr(2, l_offset + index * 10 - 1, "^")
            self.console_printline()

        def draw_cards(cards, player, window, y):
            self.lines = ['','','','']
            l_offset = int((80 - 16 * len(cards)) / 2)
            index = 0
            for card in cards:
                self.draw_card(card, player, index, window, y, l_offset + index * 16)
                index += 1
                if not index % 5:
                    self.console_printline()
                    self.lines = ['','','','']

            if index % 5:
                self.console_printline()

        top_player_info = ''.join('deck:{0} hand:{1} base:{2} black-hole:{3}'.format(self.top_player.deck.left,
                                                                len(self.top_player.hand),
                                                                   len(self.top_player.graveyard),
                                                                   len(self.top_player.graveyard_blackhole)))
        console.log(top_player_info)

        draw_minions(self.top_player.minions, self.top_minion_window, False)
        draw_minions(self.bottom_player.minions, self.bottom_minion_window, True)

        draw_cards(self.bottom_player.hand, self.bottom_player, self.card_window, 0)

        player_info = ''.join('turn:{} player {} deck:{} base:{}, black-hole:{}'.format(self.game._turns_passed,
                                                                        self.bottom_player.name,
                                                                        self.bottom_player.deck.left,
                                                                        len(self.bottom_player.graveyard),
                                                                        len(self.bottom_player.graveyard_blackhole)))

        console.log(player_info)

        # draw_cards(self.bottom_player.hand[:5], self.bottom_player, self.card_window, 0)
        # draw_cards(self.bottom_player.hand[5:], self.bottom_player, self.card_window, 3)

        # self.draw_hero(self.top_player, self.window, 10, 0)
        # self.draw_hero(self.bottom_player, self.window, 10, 12)
        # self.window.refresh()
        # self.bottom_minion_window.refresh()
        # self.top_minion_window.refresh()
        # self.card_window.refresh()
<commit_msg>0813 commit all<commit_after>from SDWLE.constants import CHARACTER_CLASS

card_abbreviations = {
    'Mark of the Wild': 'Mrk Wild',
    'Power of the Wild': 'Pow Wild',
    'Wild Growth': 'Wld Grth',
    'Healing Touch': 'Hlng Tch',
    'Mark of Nature': 'Mrk Ntr',
    'Savage Roar': 'Svg Roar',
    'Soul of the Forest': 'Sol Frst',
    'Force of Nature': 'Frce Nat',
    'Keeper of the Grove': 'Kpr Grve',
    'Druid of the Claw': 'Drd Claw',
    'Stonetusk Boar': 'Stntsk Br',
    'Raging Worgen': 'Rgng Wrgn',
}

def abbreviate(card_name):
    return card_abbreviations.get(card_name, card_name)

def game_to_string(game):
    pass

class console:
    @classmethod
    def log(self, message, source=None, color=0):
        print(message)

class ConsoleGameRender:
    def __init__(self, window, game, viewing_player):
        if viewing_player is game.players[0]:
            self.top_player = game.players[1]
            self.bottom_player = game.players[0]
        else:
            self.top_player = game.players[0]
            self.bottom_player = game.players[1]

        # curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
        # curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_WHITE)
        # curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLUE)
        # curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_YELLOW)

        # self.top_minion_window = window.derwin(3, 80, 4, 0)
        # self.bottom_minion_window = window.derwin(3, 80, 8, 0)
        # self.card_window = window.derwin(5, 80, 16, 0)
        # self.card_window = window.derwin(5, 80, 16, 0)
        self.top_minion_window = None
        self.bottom_minion_window = None
        self.card_window = None
        self.card_window = None
        self.window = window

        self.game = game
        self.targets = None
        self.selected_target = None
        self.selection_index = -1

        self.lines = []

    def console_printline(self):
        for line in self.lines:
            console.log(line)

    def draw_minion(self, minion, window, y, x, main=True):
        status_array = []
        color = 0
        if minion.can_attack():
            status_array.append("*")
            if not self.targets:
                 color = 2
        else:
            if not self.targets:
                color = 1
        if "attack" in minion.events:
            status_array.append("a")
        if "turn_start" in minion.events:
            status_array.append("b")
        if minion.charge:
            status_array.append("c")
        if minion.deathrattle is not None:
            status_array.append("d")
        if minion.enraged:
            status_array.append("e")
        if minion.frozen:
            status_array.append("f")
        if minion.immune:
            status_array.append("i")
        if minion.stealth:
            status_array.append("s")
        if minion.taunt:
            status_array.append("t")
        if minion.exhausted and not minion.charge:
            status_array.append("z")

        # if self.targets:
        #     if minion is self.selected_target:
        #         color = curses.color_pair(4)
        #     elif minion in self.targets:
        #         color = curses.color_pair(3)

        name = abbreviate(minion.card.name)[:10]
        status = ''.join(status_array)
        power_line = "({0}) ({1})".format(minion.calculate_attack(), minion.health)
        facedown = ''

        if minion.card.is_facedown():
            status = name
            name = 'facedown'

        #console(y, x, "{0}{1}:{2} {3:^9} {4:^9} {5:^9}".format(spaces, minion.index, facedown, name, power_line, status), color)
        #console(y+1, x,"{0:^9}".format(power_line), color)
        #console(y+2, x, "{0:^9}".format(status), color)
        # window.addstr(y + 2, x, "{0}".format(minion.index), color

        self.lines[0] += '[{0:^10}]'.format(name)
        self.lines[1] += '[{0:^10}]'.format(power_line)
        self.lines[2] += '[{0:^1} {1:^8}]'.format(minion.index, status[:8])

    def draw_card(self, card, player, index, window, y, x):
        color = 0
        if card.can_use(player, player.game):
            status = "*"
            if not self.targets:
                color = 2
        else:
            status = ' '
            if not self.targets:
                color = 1
        if self.targets:
            if card is self.selected_target:
                color = 4
            elif card in self.targets:
                color = 3

        name = card.name[:15]

        #console(y + 0, x, "{0}:{1:>2} mana ({2}) {3:^15}   ".format(index, card.mana_cost(), status, name), color)
        # console(y + 1, x, "{0:^15}".format(name), color)

        self.lines[0] += '+' + '-'*10 + '+'
        self.lines[1] += '|{0:^10}|'.format(name[:10])
        self.lines[2] += '|{0:^10}|'.format(status[:10])
        self.lines[3] += '|{0:^10}|'.format(index)

    def draw_hero(self, player, window, x, y):
        # color = curses.color_pair(0)
        # if self.targets:
        #     if player.hero is self.selected_target:
        #         color = curses.color_pair(4)
        #     elif player.hero in self.targets:
        #         color = curses.color_pair(3)
        # if player.weapon is not None:
        #     weapon_power = "({0}) ({1})".format(player.weapon.base_attack, player.weapon.durability)
        #     window.addstr(y, x, "{0:^20}".format(player.weapon.card.name))
        #     window.addstr(y + 1, x, "{0:^20}".format(weapon_power))
        #
        # hero_power = "({0}) ({1}+{4}) -- {2}/{3}".format(player.hero.calculate_attack(), player.hero.health,
        #                                                  player.mana, player.max_mana, player.hero.armor)
        # window.addstr(y, x + 20, "{0:^20}".format(CHARACTER_CLASS.to_str(player.hero.character_class)), color)
        # window.addstr(y + 1, x + 20, "{0:^20}".format(hero_power), color)
        #
        # window.addstr(y, x + 40, "{0:^20}".format("Hero Power"))
        # if player.hero.power.can_use():
        #     window.addstr(y + 1, x + 40, "{0:^20}".format("*"))
        pass

    def draw_game(self):
        # console(0,0,'draw_game Turn:{0}'.format(self.game._turns_passed))
        # self.window.clear()
        # self.bottom_minion_window.clear()
        # self.top_minion_window.clear()
        # self.card_window.clear()

        def draw_minions(minions, window, main):
            self.lines = ['','','','']
            l_offset = int((80 - 10 * len(minions)) / 2)
            index = 0
            for minion in minions:
                # if main and index == self.selection_index:
                #     window.addstr(2, l_offset + index * 10 - 1, "^")
                self.draw_minion(minion, window, 0, l_offset + index * 10, main)
                index += 1
            # if main and len(minions) == self.selection_index:
            #     window.addstr(2, l_offset + index * 10 - 1, "^")
            self.console_printline()

        def draw_cards(cards, player, window, y):
            self.lines = ['','','','']
            l_offset = int((80 - 16 * len(cards)) / 2)
            index = 0
            for card in cards:
                self.draw_card(card, player, index, window, y, l_offset + index * 16)
                index += 1
                if not index % 5:
                    self.console_printline()
                    self.lines = ['','','','']

            if index % 5:
                self.console_printline()

        top_player_info = ''.join('deck:{0} hand:{1} base:{2} black-hole:{3}'.format(self.top_player.deck.left,
                                                                len(self.top_player.hand),
                                                                                len(self.top_player.graveyard),
                                                                                len(
                                                                                self.top_player.graveyard_blackhole)))
        console.log(top_player_info)

        draw_minions(self.top_player.minions, self.top_minion_window, False)
        draw_minions(self.bottom_player.minions, self.bottom_minion_window, True)

        draw_cards(self.bottom_player.hand, self.bottom_player, self.card_window, 0)

        player_info = ''.join('turn:{} player {} deck:{} base:{}, black-hole:{}'.format(self.game._turns_passed,
                                                                        self.bottom_player.name,
                                                                        self.bottom_player.deck.left,
                                                                        len(self.bottom_player.graveyard),
                                                                        len(self.bottom_player.graveyard_blackhole)))

        console.log(player_info)

        # draw_cards(self.bottom_player.hand[:5], self.bottom_player, self.card_window, 0)
        # draw_cards(self.bottom_player.hand[5:], self.bottom_player, self.card_window, 3)

        # self.draw_hero(self.top_player, self.window, 10, 0)
        # self.draw_hero(self.bottom_player, self.window, 10, 12)
        # self.window.refresh()
        # self.bottom_minion_window.refresh()
        # self.top_minion_window.refresh()
        # self.card_window.refresh()
<|endoftext|>


<commit_before># File  : test_someip
# Who   : jamores
# Some SOME/IP stuff

from test_base import baseTest

from scapy.all import *
from scapy_ext import scapy_helper as helper
from scapy_ext import *

ETH_IFACE = "eth1.10"

class test_somepip(baseTest):

    def __init__(self):
        # call superclass constructor providing test collection category
        baseTest.__init__(self,"SOME/IP")

    # Test 00 : SOME/IP Magic cookie
    def test_00(self):
        """ SOME/IP Magic cookie (client -> server)."""
        err_msg = ""

        # build SOME/IP packet
        sip = helper.newSOMEIP()
        sip.msg_id.srv_id = 0xffff
        sip.msg_id.sub_id = 0x0
        sip.msg_id.mtd_id = 0x0000

        sip.req_id.client_id = 0xdead
        sip.req_id.session_id = 0xbeef

        sip.type = SOMEIP.TYPE_REQUEST_NO_RET
        sip.retcode = 0x0

        # send message
        p = Ether()/IP(dst="10.0.0.11")/TCP(sport=30490,dport=30490)/sip
        ans = helper.sendp(p,iface="eth2.10")

        # log test results
        self.addTestCase("send SOMEIP Magic cookie",error=err_msg)

    # Test 01 : SOME/IP-SD : Find service
    def test_01(self):
        """SOME/IP-SD packet."""
        err_msg = ""

        # build SOME/IP-SD packet
        sip = helper.newSOMEIP()
        sip.msg_id.srv_id = 0xffff
        sip.msg_id.sub_id = 0x1
        sip.msg_id.evt_id = 0x100
        sip.req_id.client_id = 0x00
        sip.req_id.session_id = 0x01
        sip.type = SOMEIP.TYPE_NOTIFICATION

        sd = helper.newSD()
        sd.flags = 0xff
        sd.entry_array = [SDEntry_Service(type=0,srv_id=0x1111,inst_id=0x2222,major_ver=0x01,ttl=0x02)]

        # SEND MESSAGE
        p = Ether()/IP(dst="10.0.0.11")/UDP(sport=30490,dport=30490)/sip/sd
        ans = helper.srp1(p,iface="eth2.10")

        # log test results
        self.addTestCase("send SOME/IP-SD : find service",error=err_msg)
<commit_msg>sender/receiver threading experiments<commit_after># File  : test_someip
# Who   : jamores
# Some SOME/IP stuff

from test_base import baseTest

from scapy.all import *
from scapy_ext import scapy_helper as helper
from scapy_ext import *

import threading
import time

ETH_IFACE = "eth1.10"

class test_somepip(baseTest):

    def __init__(self):
        # call superclass constructor providing test collection category
        baseTest.__init__(self,"SOME/IP")

    # Test 00 : SOME/IP Magic cookie
    def test_00(self):
        """ SOME/IP Magic cookie (client -> server)."""
        err_msg = ""

        # build SOME/IP packet
        sip = helper.newSOMEIP()
        sip.msg_id.srv_id = 0xffff
        sip.msg_id.sub_id = 0x0
        sip.msg_id.mtd_id = 0x0000

        sip.req_id.client_id = 0xdead
        sip.req_id.session_id = 0xbeef

        sip.type = SOMEIP.TYPE_REQUEST_NO_RET
        sip.retcode = 0x0

        # send message
        p = Ether()/IP(dst="10.0.0.11")/TCP(sport=30490,dport=30490)/sip
        ans = helper.sendp(p,iface="eth2.10")

        # log test results
        self.addTestCase("send SOMEIP Magic cookie",error=err_msg)

    # Test 01 : SOME/IP-SD : Find service
    def _test_01_sender(self,p):
        pass
        ans = helper.srp1(p,iface="eth1.10",timeout=5)
    def _test_01_rcv(self,p):
        pass
        time.sleep(1)
        helper.sendp(p,iface="eth2.10")
    def test_01(self):
        """SOME/IP-SD packet."""
        err_msg = ""

        # build SOME/IP-SD packet
        sip = helper.newSOMEIP()
        sip.msg_id.srv_id = 0xffff
        sip.msg_id.sub_id = 0x1
        sip.msg_id.evt_id = 0x100
        sip.req_id.client_id = 0x00
        sip.req_id.session_id = 0x01
        sip.type = SOMEIP.TYPE_NOTIFICATION

        sd = helper.newSD()
        sd.flags = 0xff
        sd.entry_array = [SDEntry_Service(type=0,srv_id=0x1111,inst_id=0x2222,major_ver=0x01,ttl=0x02)]

        # SEND MESSAGE
        p = Ether()/IP(src="10.0.0.11",dst="10.0.0.12")/UDP(sport=30490,dport=30490)/sip/sd
        r = Ether()/IP(src="10.0.0.12",dst="10.0.0.11")/UDP(sport=30490,dport=30490)/sip/sd
        #ans = helper.srp1(p,iface="eth2.10")

        t_send = threading.Thread(name='sender',target=self._test_01_sender,args=(p,))
        t_rcv = threading.Thread(name='receiver',target=self._test_01_rcv,args=(r,))
        t_send.start()
        t_rcv.start()
        t_send.join()
        t_rcv.join()

        # log test results
        self.addTestCase("send SOME/IP-SD : find service",error=err_msg)
<|endoftext|>


<commit_before>VERSION_YEAR = 2017
VERSION_MONTH = 10
VERSION_DAY = 23
VERSION_REV = 0

whos_in = None
twitter = None
users = {}
twilio_client = None
ARGS = {}
smmry_api_key = None

# Variable hold trumps last tweet id
last_id = 0
trump_chance_roll_rdy = False

# Runtime stats
duels_conducted = 0
items_awarded = 0
trump_tweets_seen = 0

# Shot_duel acceptance and active
accepted = False
shot_duel_running = False
vict_name = ""

# News handles to pull from
news_handles = ['mashable', 'cnnbrk', 'whitehouse', 'cnn', 'nytimes',
                'foxnews', 'reuters', 'npr', 'usatoday', 'cbsnews',
                'abc', 'washingtonpost', 'msnbc', 'ap', 'aphealthscience',
                'lifehacker', 'cnnnewsroom', 'theonion']

# Delays for Newsfeed and Check_trump, These are in minutes
# remember that news_del is fuzzed + (0-10)
trump_del = 15
news_del = 55

# Location of db.json and tokens.config
data_dir = "/data"

# Create/Load Local Database
db_file = '{}/db.json'.format(data_dir)
db = {}

# Global toggle for news feed
NEWS_FEED_ON = False
NEWS_FEED_CREATED = False

async def trigger_social(ctx):
    """Triggers a social """
    for m in ctx.bot.get_all_members():
        if m.display_name != 'brochat-bot' and m.status == 'online':
            add_drink(m.display_name)
    glass = ":tumbler_glass:"
    await ctx.bot.say("Ah shit that's three in a row! ITS A SOCIAL! SHOTS! "
                      "SHOTS! SHOTS!\n{}{}{}".format(glass, glass, glass))

def add_drink(user):
    """
    Adds a drink for the user.
    :param user: users display name
    :return:
    """

    if user not in users:
        users[user] = {}

    if "drinks_owed" in users[user]:
        users[user]['drinks_owed'] += 1
    else:
        users[user]['drinks_owed'] = 1

    return users[user]['drinks_owed']
<commit_msg>Open a new release<commit_after>VERSION_YEAR = 2017
VERSION_MONTH = 10
VERSION_DAY = 23
VERSION_REV = 1

whos_in = None
twitter = None
users = {}
twilio_client = None
ARGS = {}
smmry_api_key = None

# Variable hold trumps last tweet id
last_id = 0
trump_chance_roll_rdy = False

# Runtime stats
duels_conducted = 0
items_awarded = 0
trump_tweets_seen = 0

# Shot_duel acceptance and active
accepted = False
shot_duel_running = False
vict_name = ""

# News handles to pull from
news_handles = ['mashable', 'cnnbrk', 'whitehouse', 'cnn', 'nytimes',
                'foxnews', 'reuters', 'npr', 'usatoday', 'cbsnews',
                'abc', 'washingtonpost', 'msnbc', 'ap', 'aphealthscience',
                'lifehacker', 'cnnnewsroom', 'theonion']

# Delays for Newsfeed and Check_trump, These are in minutes
# remember that news_del is fuzzed + (0-10)
trump_del = 15
news_del = 55

# Location of db.json and tokens.config
data_dir = "/data"

# Create/Load Local Database
db_file = '{}/db.json'.format(data_dir)
db = {}

# Global toggle for news feed
NEWS_FEED_ON = False
NEWS_FEED_CREATED = False

async def trigger_social(ctx):
    """Triggers a social """
    for m in ctx.bot.get_all_members():
        if m.display_name != 'brochat-bot' and m.status == 'online':
            add_drink(m.display_name)
    glass = ":tumbler_glass:"
    await ctx.bot.say("Ah shit that's three in a row! ITS A SOCIAL! SHOTS! "
                      "SHOTS! SHOTS!\n{}{}{}".format(glass, glass, glass))

def add_drink(user):
    """
    Adds a drink for the user.
    :param user: users display name
    :return:
    """

    if user not in users:
        users[user] = {}

    if "drinks_owed" in users[user]:
        users[user]['drinks_owed'] += 1
    else:
        users[user]['drinks_owed'] = 1

    return users[user]['drinks_owed']
<|endoftext|>


<commit_before>import os
from dmutils.status import get_version_label

class Config:

    VERSION = get_version_label(
        os.path.abspath(os.path.dirname(__file__))
    )
    DM_SEARCH_API_URL = None
    DM_SEARCH_API_AUTH_TOKEN = None
    DM_API_AUTH_TOKENS = None
    DM_API_CALLBACK_AUTH_TOKENS = None
    ES_ENABLED = True
    AUTH_REQUIRED = True
    DM_HTTP_PROTO = 'http'
    # Logging
    DM_LOG_LEVEL = 'DEBUG'
    DM_PLAIN_TEXT_LOGS = False
    DM_LOG_PATH = None
    DM_APP_NAME = 'api'

    # Feature Flags
    RAISE_ERROR_ON_MISSING_FEATURES = True

    DM_API_SERVICES_PAGE_SIZE = 100
    DM_API_SUPPLIERS_PAGE_SIZE = 100
    DM_API_BRIEFS_PAGE_SIZE = 100
    DM_API_BRIEF_RESPONSES_PAGE_SIZE = 100
    DM_API_BUYER_DOMAINS_PAGE_SIZE = 100
    DM_API_PROJECTS_PAGE_SIZE = 100

    DM_ALLOWED_ADMIN_DOMAINS = ['digital.cabinet-office.gov.uk', 'crowncommercial.gov.uk', 'user.marketplace.team',
                                'notifications.service.gov.uk']

    SQLALCHEMY_COMMIT_ON_TEARDOWN = False
    SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/digitalmarketplace'
    SQLALCHEMY_RECORD_QUERIES = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    DM_FAILED_LOGIN_LIMIT = 5

    VCAP_SERVICES = None

    DM_FRAMEWORK_TO_ES_INDEX = {
        'g-cloud-9': {
            'services': 'g-cloud-9'
        },
        'digital-outcomes-and-specialists': {
            'briefs': 'briefs-digital-outcomes-and-specialists'
        },
        'digital-outcomes-and-specialists-2': {
            'briefs': 'briefs-digital-outcomes-and-specialists'
        },
    }

class Test(Config):
    SERVER_NAME = '127.0.0.1:5000'
    DM_SEARCH_API_AUTH_TOKEN = 'test'
    DM_SEARCH_API_URL = 'http://localhost'
    DEBUG = True
    DM_PLAIN_TEXT_LOGS = True
    ES_ENABLED = False
    SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/digitalmarketplace_test'
    DM_API_AUTH_TOKENS = 'myToken'
    DM_API_CALLBACK_AUTH_TOKENS = 'myCallbackToken'
    DM_API_SERVICES_PAGE_SIZE = 5
    DM_API_SUPPLIERS_PAGE_SIZE = 5
    DM_API_BRIEFS_PAGE_SIZE = 5
    DM_API_BRIEF_RESPONSES_PAGE_SIZE = 5

    DM_API_PROJECTS_PAGE_SIZE = 5

class Development(Config):
    DEBUG = True
    DM_PLAIN_TEXT_LOGS = True

    DM_API_AUTH_TOKENS = 'myToken'
    DM_API_CALLBACK_AUTH_TOKENS = 'myToken'
    DM_SEARCH_API_AUTH_TOKEN = 'myToken'
    DM_SEARCH_API_URL = 'http://localhost:5001'

class Live(Config):
    """Base config for deployed environments"""
    DEBUG = False
    DM_HTTP_PROTO = 'https'
    DM_LOG_PATH = '/var/log/digitalmarketplace/application.log'

class Preview(Live):
    pass

class Staging(Live):
    pass

class Production(Live):
    DM_ALLOWED_ADMIN_DOMAINS = ['digital.cabinet-office.gov.uk', 'crowncommercial.gov.uk']

configs = {
    'development': Development,
    'test': Test,

    'preview': Preview,
    'staging': Staging,
    'production': Production,
}
<commit_msg>Update allowed failed login attempts number from 5 to 10<commit_after>import os
from dmutils.status import get_version_label

class Config:

    VERSION = get_version_label(
        os.path.abspath(os.path.dirname(__file__))
    )
    DM_SEARCH_API_URL = None
    DM_SEARCH_API_AUTH_TOKEN = None
    DM_API_AUTH_TOKENS = None
    DM_API_CALLBACK_AUTH_TOKENS = None
    ES_ENABLED = True
    AUTH_REQUIRED = True
    DM_HTTP_PROTO = 'http'
    # Logging
    DM_LOG_LEVEL = 'DEBUG'
    DM_PLAIN_TEXT_LOGS = False
    DM_LOG_PATH = None
    DM_APP_NAME = 'api'

    # Feature Flags
    RAISE_ERROR_ON_MISSING_FEATURES = True

    DM_API_SERVICES_PAGE_SIZE = 100
    DM_API_SUPPLIERS_PAGE_SIZE = 100
    DM_API_BRIEFS_PAGE_SIZE = 100
    DM_API_BRIEF_RESPONSES_PAGE_SIZE = 100
    DM_API_BUYER_DOMAINS_PAGE_SIZE = 100
    DM_API_PROJECTS_PAGE_SIZE = 100

    DM_ALLOWED_ADMIN_DOMAINS = ['digital.cabinet-office.gov.uk', 'crowncommercial.gov.uk', 'user.marketplace.team',
                                'notifications.service.gov.uk']

    SQLALCHEMY_COMMIT_ON_TEARDOWN = False
    SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/digitalmarketplace'
    SQLALCHEMY_RECORD_QUERIES = True
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    DM_FAILED_LOGIN_LIMIT = 10

    VCAP_SERVICES = None

    DM_FRAMEWORK_TO_ES_INDEX = {
        'g-cloud-9': {
            'services': 'g-cloud-9'
        },
        'digital-outcomes-and-specialists': {
            'briefs': 'briefs-digital-outcomes-and-specialists'
        },
        'digital-outcomes-and-specialists-2': {
            'briefs': 'briefs-digital-outcomes-and-specialists'
        },
    }

class Test(Config):
    SERVER_NAME = '127.0.0.1:5000'
    DM_SEARCH_API_AUTH_TOKEN = 'test'
    DM_SEARCH_API_URL = 'http://localhost'
    DEBUG = True
    DM_PLAIN_TEXT_LOGS = True
    ES_ENABLED = False
    SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/digitalmarketplace_test'
    DM_API_AUTH_TOKENS = 'myToken'
    DM_API_CALLBACK_AUTH_TOKENS = 'myCallbackToken'
    DM_API_SERVICES_PAGE_SIZE = 5
    DM_API_SUPPLIERS_PAGE_SIZE = 5
    DM_API_BRIEFS_PAGE_SIZE = 5
    DM_API_BRIEF_RESPONSES_PAGE_SIZE = 5

    DM_API_PROJECTS_PAGE_SIZE = 5

class Development(Config):
    DEBUG = True
    DM_PLAIN_TEXT_LOGS = True

    DM_API_AUTH_TOKENS = 'myToken'
    DM_API_CALLBACK_AUTH_TOKENS = 'myToken'
    DM_SEARCH_API_AUTH_TOKEN = 'myToken'
    DM_SEARCH_API_URL = 'http://localhost:5001'

class Live(Config):
    """Base config for deployed environments"""
    DEBUG = False
    DM_HTTP_PROTO = 'https'
    DM_LOG_PATH = '/var/log/digitalmarketplace/application.log'

class Preview(Live):
    pass

class Staging(Live):
    pass

class Production(Live):
    DM_ALLOWED_ADMIN_DOMAINS = ['digital.cabinet-office.gov.uk', 'crowncommercial.gov.uk']

configs = {
    'development': Development,
    'test': Test,

    'preview': Preview,
    'staging': Staging,
    'production': Production,
}
<|endoftext|>


<commit_before># Common stuff for the pyWWA ingestors...

from twisted.internet import reactor
from twisted.words.xish import domish
from twisted.python import log
from twisted.mail import smtp
from twisted.web import client
from twisted.words.xish.xmlstream import STREAM_END_EVENT
from twisted.internet.task import LoopingCall

import secret
from twittytwister import twitter
import urllib
import simplejson
import traceback
import sys
import os
import StringIO
from email.MIMEText import MIMEText
import base64
import pg
import socket
from oauth import oauth

mesosite = pg.connect("mesosite", "iemdb", user='nobody')
OAUTH_TOKENS = {}
rs = mesosite.query("SELECT * from oauth_tokens").dictresult()
for i in range(len(rs)):
  OAUTH_TOKENS[ rs[i]['username'] ] = oauth.OAuthToken(rs[i]['token'], rs[i]['secret'])

OAUTH_CONSUMER = oauth.OAuthConsumer(secret.consumer_key, secret.consumer_secret)

BITLY = "http://api.bit.ly/shorten?version=2.0.1&longUrl=%s&login=iembot&apiKey="+ secret.bitly_key

def email_error(exp, message):
    """
    Helper function to generate error emails when necessary and hopefully
    not flood!
    """
    # Always log a message about our fun
    cstr = StringIO.StringIO()
    traceback.print_exc(file=cstr)
    cstr.seek(0)
    tbstr = cstr.read()
    log.msg( tbstr )
    log.msg( exp )
    log.msg( message )

    # Now, we may email....
    if os.environ.get('EMAILS') is None:
        os.environ['EMAILS'] = "10"
    if int(os.environ['EMAILS']) < 0:
        log.msg("NO EMAIL DUE TO LIMIT...")
        return
    os.environ['EMAILS'] = str( int(os.environ["EMAILS"]) - 1 )

    msg = MIMEText("""
Emails Left: %s  Host: %s

Exception:
%s
%s

Message:
%s
""" % (os.environ["EMAILS"], socket.gethostname(), tbstr, exp, message))
    msg['subject'] = '%s Traceback' % (sys.argv[0],)
    msg['From'] = secret.parser_user
    msg['To'] = secret.error_email
    smtp.sendmail("localhost", msg["From"], msg["To"], msg)

def tweet(channels, msg, url, extras={}):
    """
    Method to publish twitter messages
    """
    if secret.DISARM:
        channels = ['TEST',]
        return

    if url:
        url = url.replace("&", "&").replace("#","%23")
        deffer = client.getPage(BITLY % (url, ) )
        deffer.addCallback(reallytweet, channels, msg, extras )
        deffer.addErrback(reallytweet, channels, msg, extras )
    else:
        reallytweet(None, channels, msg, extras)

def reallytweet(json, channels, msg, extras):
    """
    Actually, really publish this time!
    """
    tinyurl = ""
    if json:
        j = simplejson.loads( json )
        if j.has_key('errorMessage'):
            email_error(str(j), "Problem with bitly")
        elif j.has_key('results'):
            tinyurl = j['results'][ j['results'].keys()[0] ]['shortUrl']
    # We are finally ready to tweet!
    for channel in channels:
        tuser = "iembot_%s" % (channel.lower(),)
        if not OAUTH_TOKENS.has_key(tuser):
            print "Unknown Twitter User, %s" % (tuser,)
            continue
        twt = "#%s %s %s" % (channel, msg[:112], tinyurl)
        deffer = twitter.Twitter(consumer=OAUTH_CONSUMER, token=OAUTH_TOKENS[tuser]).update( twt, None, extras)
        deffer.addCallback(tb, channel, twt)
        deffer.addErrback(twitterErrback, channel, twt)

def tb(x, channel, twt):
    print "TWEET [%s] RES: %s" % (twt, x)

def twitterErrback(err, channel, twt):
    print "TWEET [%s] RES: %s" % (twt, err)
    log.msg( dir(err) )

class JabberClient:

    def __init__(self, myJid):
        self.myJid = myJid
        self.xmlstream = None
        self.authenticated = False

    def authd(self,xs):
        log.msg("Logged into Jabber Chat Server!")

        self.xmlstream = xs
        self.xmlstream.rawDataInFn = self.rawDataInFn
        self.xmlstream.rawDataOutFn = self.rawDataOutFn
        presence = domish.Element(('jabber:client','presence'))
        presence.addElement('status').addContent('Online')
        self.xmlstream.send(presence)
        self.authenticated = True

        lc = LoopingCall(self.keepalive)
        lc.start(60)
        self.xmlstream.addObserver(STREAM_END_EVENT, lambda _: lc.stop())

    def keepalive(self):
        self.xmlstream.send(' ')

    def _disconnect(self, xs):
        log.msg("SETTING authenticated to false!")
        self.authenticated = False

    def sendMessage(self, body, html, to_user=secret.iembot_user):
        if (not self.authenticated):
            log.msg("No Connection, Lets wait and try later...")
            reactor.callLater(3, self.sendMessage, body, html, to_user)
            return
        message = domish.Element(('jabber:client','message'))
        message['to'] = '%s@%s' % (to_user, secret.chatserver)
        message['type'] = 'chat'

        # message.addElement('subject',None,subject)
        message.addElement('body',None,body)
        h = message.addElement('html','http://jabber.org/protocol/xhtml-im')
        b = h.addElement('body', 'http://www.w3.org/1999/xhtml')
        b.addRawXml( html or body )
        self.xmlstream.send(message)

    #def findurl(self, message):
    #    """Extract URL from message"""
    #    tokens = message.split(" ")
    #    if len(tokens) == 0:
    #        return None
    #    if tokens[-1][:4] != "http":
    #        return None
    #    return tokens[-1]

    def debug(self, elem):
        log.msg( elem.toXml().encode('utf-8') )

    def rawDataInFn(self, data):
        print 'RECV', unicode(data,'utf-8','ignore').encode('ascii', 'replace')
    def rawDataOutFn(self, data):
        if (data == ' '): return
        print 'SEND', unicode(data,'utf-8','ignore').encode('ascii', 'replace')
<commit_msg>fix bitly<commit_after># Common stuff for the pyWWA ingestors...

from twisted.internet import reactor
from twisted.words.xish import domish
from twisted.python import log
from twisted.mail import smtp
from twisted.web import client
from twisted.words.xish.xmlstream import STREAM_END_EVENT
from twisted.internet.task import LoopingCall

import secret
from twittytwister import twitter
import urllib
import simplejson
import traceback
import sys
import os
import StringIO
from email.MIMEText import MIMEText
import base64
import pg
import socket
from oauth import oauth

mesosite = pg.connect("mesosite", "iemdb", user='nobody')
OAUTH_TOKENS = {}
rs = mesosite.query("SELECT * from oauth_tokens").dictresult()
for i in range(len(rs)):
  OAUTH_TOKENS[ rs[i]['username'] ] = oauth.OAuthToken(rs[i]['token'], rs[i]['secret'])

OAUTH_CONSUMER = oauth.OAuthConsumer(secret.consumer_key, secret.consumer_secret)

BITLY = "http://api.bit.ly/shorten?version=2.0.1&longUrl=%s&login=iembot&apiKey="+ secret.bitly_key

def email_error(exp, message):
    """
    Helper function to generate error emails when necessary and hopefully
    not flood!
    """
    # Always log a message about our fun
    cstr = StringIO.StringIO()
    traceback.print_exc(file=cstr)
    cstr.seek(0)
    tbstr = cstr.read()
    log.msg( tbstr )
    log.msg( exp )
    log.msg( message )

    # Now, we may email....
    if os.environ.get('EMAILS') is None:
        os.environ['EMAILS'] = "10"
    if int(os.environ['EMAILS']) < 0:
        log.msg("NO EMAIL DUE TO LIMIT...")
        return
    os.environ['EMAILS'] = str( int(os.environ["EMAILS"]) - 1 )

    msg = MIMEText("""
Emails Left: %s  Host: %s

Exception:
%s
%s

Message:
%s
""" % (os.environ["EMAILS"], socket.gethostname(), tbstr, exp, message))
    msg['subject'] = '%s Traceback' % (sys.argv[0],)
    msg['From'] = secret.parser_user
    msg['To'] = secret.error_email
    smtp.sendmail("localhost", msg["From"], msg["To"], msg)

def tweet(channels, msg, url, extras={}):
    """
    Method to publish twitter messages
    """
    if secret.DISARM:
        channels = ['TEST',]
        return

    if url:
        url = url.replace("&", "&").replace("#","%23")
        deffer = client.getPage(BITLY % (url, ) )
        deffer.addCallback(reallytweet, channels, msg, extras )
        deffer.addErrback(reallytweet, channels, msg, extras )
    else:
        reallytweet(None, channels, msg, extras)

def reallytweet(json, channels, msg, extras):
    """
    Actually, really publish this time!
    """
    tinyurl = ""
    if json:
        j = simplejson.loads( json )
        if j.has_key('errorCode') and j['errorCode'] != 0 and j.has_key('errorMessage'):
            email_error(str(j), "Problem with bitly")
        elif j.has_key('results'):
            tinyurl = j['results'][ j['results'].keys()[0] ]['shortUrl']
    # We are finally ready to tweet!
    for channel in channels:
        tuser = "iembot_%s" % (channel.lower(),)
        if not OAUTH_TOKENS.has_key(tuser):
            print "Unknown Twitter User, %s" % (tuser,)
            continue
        twt = "#%s %s %s" % (channel, msg[:112], tinyurl)
        deffer = twitter.Twitter(consumer=OAUTH_CONSUMER, token=OAUTH_TOKENS[tuser]).update( twt, None, extras)
        deffer.addCallback(tb, channel, twt)
        deffer.addErrback(twitterErrback, channel, twt)

def tb(x, channel, twt):
    print "TWEET [%s] RES: %s" % (twt, x)

def twitterErrback(err, channel, twt):
    print "TWEET [%s] RES: %s" % (twt, err)
    log.msg( dir(err) )

class JabberClient:

    def __init__(self, myJid):
        self.myJid = myJid
        self.xmlstream = None
        self.authenticated = False

    def authd(self,xs):
        log.msg("Logged into Jabber Chat Server!")

        self.xmlstream = xs
        self.xmlstream.rawDataInFn = self.rawDataInFn
        self.xmlstream.rawDataOutFn = self.rawDataOutFn
        presence = domish.Element(('jabber:client','presence'))
        presence.addElement('status').addContent('Online')
        self.xmlstream.send(presence)
        self.authenticated = True

        lc = LoopingCall(self.keepalive)
        lc.start(60)
        self.xmlstream.addObserver(STREAM_END_EVENT, lambda _: lc.stop())

    def keepalive(self):
        self.xmlstream.send(' ')

    def _disconnect(self, xs):
        log.msg("SETTING authenticated to false!")
        self.authenticated = False

    def sendMessage(self, body, html, to_user=secret.iembot_user):
        if (not self.authenticated):
            log.msg("No Connection, Lets wait and try later...")
            reactor.callLater(3, self.sendMessage, body, html, to_user)
            return
        message = domish.Element(('jabber:client','message'))
        message['to'] = '%s@%s' % (to_user, secret.chatserver)
        message['type'] = 'chat'

        # message.addElement('subject',None,subject)
        message.addElement('body',None,body)
        h = message.addElement('html','http://jabber.org/protocol/xhtml-im')
        b = h.addElement('body', 'http://www.w3.org/1999/xhtml')
        b.addRawXml( html or body )
        self.xmlstream.send(message)

    #def findurl(self, message):
    #    """Extract URL from message"""
    #    tokens = message.split(" ")
    #    if len(tokens) == 0:
    #        return None
    #    if tokens[-1][:4] != "http":
    #        return None
    #    return tokens[-1]

    def debug(self, elem):
        log.msg( elem.toXml().encode('utf-8') )

    def rawDataInFn(self, data):
        print 'RECV', unicode(data,'utf-8','ignore').encode('ascii', 'replace')
    def rawDataOutFn(self, data):
        if (data == ' '): return
        print 'SEND', unicode(data,'utf-8','ignore').encode('ascii', 'replace')
<|endoftext|>


<commit_before>__version__ = "0.0.0"

import socket
import os

# The following need to be set in server.conf
HOST = ""
PORT = 80
REQ_BUFFSIZE = 4096
BASE_DIR = "www"

def test():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST,PORT))
    s.listen(1)
    print "* Serving at http://127.0.0.1:{0}/ (Press CTRL+C to quit)".format(PORT)
    while True:
        conn, addr = s.accept()
        data = conn.recv(REQ_BUFFSIZE)
        http_response = b"HTTP/1.1 200 OK\r\n\r\nHello, World!\r\n"
        conn.sendall(http_response)
        conn.close()

if __name__ == "__main__":
    test()
    print "Bye"
<commit_msg>serves the file<commit_after>__version__ = "0.0.0"

import socket
import os

# The following need to be set in server.conf
HOST = ""
PORT = 80
REQ_BUFFSIZE = 4096
BASE_DIR = "www"

def test():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((HOST,PORT))
    s.listen(1)
    print "* Serving at http://127.0.0.1:{0}/ (Press CTRL+C to quit)".format(PORT)
    while True:
        conn, addr = s.accept()
        data = conn.recv(REQ_BUFFSIZE)
        http_response = b"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n"
        with open(BASE_DIR + "/index.html") as f:
            http_response += f.read() + "\r\n"
        conn.sendall(http_response)
        conn.close()

if __name__ == "__main__":
    test()
    print "Bye"
<|endoftext|>


<commit_before>#!/usr/bin/python
#
# common.py -- Methods used by the cloud-ha scripts.
# Created: Marcus Butler <marcusb@marcusb.org>, 05-April-2017.
#

from boto3 import client
from botocore.exceptions import ClientError
import sys
import json

def get_rtb_assoc(subnet):
    ec2 = client('ec2')
    res = ec2.describe_route_tables()

    for table in res['RouteTables']:
        for assoc in table['Associations']:
            if assoc.has_key('SubnetId') and assoc['SubnetId'] == subnet:
                return assoc['RouteTableAssociationId']

    return None

def change_rtb(old_assoc, rtb):
    ec2 = client('ec2')
    res = ec2.replace_route_table_association(AssociationId = old_assoc,
                                              RouteTableId = rtb)

    return True

def fatal_error(errmsg):
    return {
            'statusCode': 500,
            'headers': { 'Content-Type': 'application/json' },
            'body': json.dumps({'errorMessage': errmsg})
    }
<commit_msg>Added S3 support for config storage<commit_after>#!/usr/bin/python
#
# common.py -- Methods used by the cloud-ha scripts.
# Created: Marcus Butler <marcusb@marcusb.org>, 05-April-2017.
#

from boto3 import client
from botocore.exceptions import ClientError
import sys
import json

def get_rtb_assoc(subnet):
    ec2 = client('ec2')
    res = ec2.describe_route_tables()

    for table in res['RouteTables']:
        for assoc in table['Associations']:
            if assoc.has_key('SubnetId') and assoc['SubnetId'] == subnet:
                return assoc['RouteTableAssociationId']

    return None

def change_rtb(old_assoc, rtb):
    ec2 = client('ec2')
    res = ec2.replace_route_table_association(AssociationId = old_assoc,
                                              RouteTableId = rtb)

    return True

def get_config(bucket, file):
    s3 = client('s3')

    obj = s3.get_object(Bucket=bucket, Key=file)
    dict = json.loads(obj['Body'].read())

    return dict

def fatal_error(errmsg):
    return {
            'statusCode': 500,
            'headers': { 'Content-Type': 'application/json' },
            'body': json.dumps({'errorMessage': errmsg})
    }
<|endoftext|>


<commit_before>#
# server.py
#
# functions for obtaining data about discord servers
#

import discord

from database import Server, Channel

async def on_server_join(server)

	server_id = session.query(Server).filter(Server.name == server.name).first()
	if ( server_id == None ):
		the_server = Server(name=server.name)
		session.add(the_server)
	else:
		the_server = server_id

	for channel in server.channels:
		channel_id = session.query(Channel).filter(Channel.name == channel.name).first()
		if ( channel_id == None ):
			new_channel = Channel(name=channel.name, squelch=False)
			new_channel.server = the_server
			session.add(new_server)

	session.commit()<commit_msg>Fix typo<commit_after>#
# server.py
#
# functions for obtaining data about discord servers
#

import discord

from database import Server, Channel

async def on_server_join(server):

	server_id = session.query(Server).filter(Server.name == server.name).first()
	if ( server_id == None ):
		the_server = Server(name=server.name)
		session.add(the_server)
	else:
		the_server = server_id

	for channel in server.channels:
		channel_id = session.query(Channel).filter(Channel.name == channel.name).first()
		if ( channel_id == None ):
			new_channel = Channel(name=channel.name, squelch=False)
			new_channel.server = the_server
			session.add(new_server)

	session.commit()<|endoftext|>


<commit_before>#!/usr/bin/python2.3
# -*- mode: python -*-
#
# Copyright (c) 2004 Specifix, Inc.
# All rights reserved
#

import branch
import changeset
import commit
import cook
import database
import display
import fsrepos
import log
import os
import repository
import rollbacks
import srscfg
import srcctl
import sys
import updatecmd
import util

if sys.version_info < (2, 3):
    print "error: python 2.3 or greater is requried"
    sys.exit(1)

sys.excepthook = util.excepthook

try:
    cfg = srscfg.SrsConfiguration()
except srscfg.ParseError, e:
    log.error(str(e))
    sys.exit(1)

def usage(rc = 1):
    print "usage: srs branch <newbranch> <branchfrom> [<trove>]"
    print "       srs changeset <pkg> [<oldver>] <newver> <outfile>"
    print "       srs cook    [--prep] [--macros file] <first.recipe> <second.recipe> ..."
    print "       srs commit       <changeset>"
    print "       srs erase        <pkgname> [<version>]"
    print "       srs localcs      <pkg> <outfile>"
    print "       srs localcommit  <changeset>"
    print "       srs pkglist      <pkgname> [<version>]"
    print "       srs replist      <pkgname> [<version>]"
    print "       srs rblist"
    print "       srs rollback     <rollback>"
    print "       srs source       [usage]"
    print "       srs update	   <pkgname> <version>"
    print "           update       <changeset>"
    print "       srs usage"
    print ""
    print "commit flags:  --target-branch <branch>"
    print ""
    print "common flags:  --branch <defaultbranch>"
    print '               --config "<item> <value>"'
    print "               --reppath <repository-path>"
    print "               --root <root>"
    print ""
    print "pkglist flags: --all"
    print "               --ids"
    print "               --ls"
    print ""
    print "update flags: --replace-files"
    return rc

def openRepository(path, mode):
    try:
        repos = fsrepos.FilesystemRepository(path, mode)
    except repository.OpenError, e:
        log.error('Unable to open repository %s: %s', path, str(e))
        sys.exit(1)
    return repos

def openDatabase(root, path, mode):
    try:
        db = database.Database(root, path, mode)
    except repository.OpenError, e:
        log.error('Unable to open database %s%s%s: %s', root, os.sep, path, str(e))
        sys.exit(1)
    return db

def main():
    otherArgs = [ sys.argv[0] ]
    argSet = {}
    argDef = {}
    cfgMap = {}
    # 0 - arg may occur, no parameter
    # 1 - arg may occur once, w/ parameter
    # 2 - arg may occur N times, w/ parameter

    cfgMap["branch"] = "defaultbranch"
    cfgMap["reppath"] = "reppath"
    cfgMap["root"] = "root"

    argDef["all"] = 0
    argDef["config"] = 2
    argDef["ids"] = 0
    argDef["ls"] = 0
    argDef["macros"] = 1
    argDef["prep"] = 0
    argDef["profile"] = 0
    argDef["replace-files"] = 0
    argDef["target-branch"] = 1

    argDef.update(srcctl.argDef)

    for arg in cfgMap.keys():
	argDef[arg] = 1

    i = 1
    while i < len(sys.argv):
	if sys.argv[i][:2] != "--":
	    otherArgs.append(sys.argv[i])
	else:
	    arg = sys.argv[i][2:]
	    if not argDef.has_key(arg): return usage()

	    if not argDef[arg]:
		argSet[arg] = 1
	    else:
		# the argument takes a parameter
		i = i + 1
		if i >= len(sys.argv): return usage()

		if argDef[arg] == 1:
		    # exactly one parameter is allowd
		    if argSet.has_key(arg): return usage()
		    argSet[arg] = sys.argv[i]
		else:
		    # multiple parameters may occur
		    if argSet.has_key(arg):
			argSet[arg].append(sys.argv[i])
		    else:
			argSet[arg] = [sys.argv[i]]

	i = i + 1

    if '-v' in otherArgs:
	otherArgs.remove('-v')
	log.setVerbosity(1)
    else:
	log.setVerbosity(0)

    profile = False
    if argSet.has_key('profile'):
	import hotshot
	prof = hotshot.Profile('srs.prof')
	prof.start()
	profile = True
	del argSet['profile']

    if argSet.has_key('config'):
	for param in argSet['config']:
	    cfg.configLine(param)

	del argSet['config']

    for (arg, name) in cfgMap.items():
	if argSet.has_key(arg):
	    cfg.configLine("%s %s" % (name, argSet[arg]))
	    del argSet[arg]

    if (len(otherArgs) < 2):
	return usage()
    elif (otherArgs[1] == "branch"):
	if argSet: return usage
	if len(otherArgs) < 4 or len(otherArgs) > 5: return usage()
	repos = openRepository(cfg.reppath, "c")

	args = [repos, ] + otherArgs[2:]
	branch.branch(*args)
    elif (otherArgs[1] == "changeset"):
	# current usage is "package file oldversion newversion"
	if len(otherArgs) != 5 and len(otherArgs) != 6:
	    return usage()

	name = otherArgs[2]
	if len(otherArgs) == 6:
	    (old, new) = (otherArgs[3], otherArgs[4])
	    outFile = otherArgs[5]
	else:
	    (old, new) = (None, otherArgs[3])
	    outFile = otherArgs[4]

	repos = openRepository(cfg.reppath, "r")

	changeset.ChangeSetCommand(repos, cfg, name, outFile, old, new)
    elif (otherArgs[1] == "commit"):
	targetBranch = None
	if argSet.has_key('target-branch'):
	    targetBranch  = argSet['target-branch']
	    del argSet['target-branch']
	if len(otherArgs) < 3: return usage()
	repos = openRepository(cfg.reppath, "c")
	for changeSet in otherArgs[2:]:
	    commit.doCommit(repos, changeSet, targetBranch)
    elif (otherArgs[1] == "config"):
	if argSet: return usage
	if (len(otherArgs) > 2):
	    return usage()
	else:
	    cfg.display()
    elif (otherArgs[1] == "cook"):
	log.setVerbosity(1)
	macros = ()
	prep = 0
	if argSet.has_key('prep'):
	    del argSet['prep']
	    prep = 1
	if argSet.has_key('macros'):
	    argSet['macros']
	    f = open(argSet['macros'])
	    # XXX sick hack
	    macroSrc = "macros =" + f.read()
	    exec macroSrc
	    del f
	    del argSet['macros']
	if argSet: return usage()

	cook.cookCommand(cfg, otherArgs[2:], prep, macros)
    elif (otherArgs[1] == "erase"):
	if argSet: return usage
	if len(otherArgs) >= 3 and len(otherArgs) <=4:
	    db = openDatabase(cfg.root, cfg.dbpath, "c")

	    args = [db, cfg] + otherArgs[2:]
	    updatecmd.doErase(*args)
	else:
	    return usage()
    elif (otherArgs[1] == "import"):
	print "this is broken"
    elif (otherArgs[1] == "localcs"):
	if len(otherArgs) != 4 and len(otherArgs) != 4:
	    return usage()

	name = otherArgs[2]
	outFile = otherArgs[3]

	db = database.Database(cfg.root, cfg.dbpath, "r")
	changeset.LocalChangeSetCommand(db, cfg, name, outFile)
    elif (otherArgs[1] == "localcommit"):
	if len(otherArgs) < 3: return usage()
	db = database.Database(cfg.root, cfg.dbpath, "c")
	for changeSet in otherArgs[2:]:
	    commit.doLocalCommit(db, changeSet)
    elif (otherArgs[1] == "pkglist" or otherArgs[1] == "replist"):
	all = argSet.has_key('all')
	if all: del argSet['all']

	ls = argSet.has_key('ls')
	if ls: del argSet['ls']

	ids = argSet.has_key('ids')
	if ids: del argSet['ids']

	if otherArgs[1] == "replist":
	    queryRepos = openRepository(cfg.reppath, "r")
	else:
	    queryRepos = openDatabase(cfg.root, cfg.dbpath, "r")

	if argSet: return usage()

	if len(otherArgs) >= 2 and len(otherArgs) <= 4:
	    args = [queryRepos, cfg, all, ls, ids] + otherArgs[2:]
	    try:
		display.displayPkgs(*args)
	    except IOError, msg:
		sys.stderr.write(msg.strerror + '\n')
		sys.exit(1)
	else:
	    return usage()
    elif (otherArgs[1] == "rblist"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbpath, "r")
	rollbacks.listRollbacks(db, cfg)
    elif (otherArgs[1] == "rollback"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbpath, "c")
	args = [db, cfg] + otherArgs[2:]
	rollbacks.apply(*args)
    elif (otherArgs[1] == "source" or otherArgs[1] == "src"):
	return srcctl.sourceCommand(cfg, otherArgs[2:], argSet)
    elif (otherArgs[1] == "update"):
	kwargs = {}
	replaceFiles = argSet.has_key('replace-files')
	if replaceFiles:
	    kwargs['replaceFiles'] = True
	    del argSet['replace-files']
	if argSet: return usage
	if len(otherArgs) >=3 and len(otherArgs) <= 4:
	    repos = openRepository(cfg.reppath, "r")
	    db = openDatabase(cfg.root, cfg.dbpath, "c")

	    args = [repos, db, cfg] + otherArgs[2:]
	    updatecmd.doUpdate(*args, **kwargs)
	else:
	    return usage()
    elif (otherArgs[1] == "return usage"):
	return usage(rc = 0)
    else:
	return usage()

    if profile:
	prof.stop()

    if log.errorOccurred():
	sys.exit(1)

if __name__ == "__main__":
    sys.exit(main())
<commit_msg>added remove<commit_after>#!/usr/bin/python2.3
# -*- mode: python -*-
#
# Copyright (c) 2004 Specifix, Inc.
# All rights reserved
#

import branch
import changeset
import commit
import cook
import database
import display
import fsrepos
import log
import os
import repository
import rollbacks
import srscfg
import srcctl
import sys
import updatecmd
import util

if sys.version_info < (2, 3):
    print "error: python 2.3 or greater is requried"
    sys.exit(1)

sys.excepthook = util.excepthook

try:
    cfg = srscfg.SrsConfiguration()
except srscfg.ParseError, e:
    log.error(str(e))
    sys.exit(1)

def usage(rc = 1):
    print "usage: srs branch <newbranch> <branchfrom> [<trove>]"
    print "       srs changeset <pkg> [<oldver>] <newver> <outfile>"
    print "       srs cook    [--prep] [--macros file] <first.recipe> <second.recipe> ..."
    print "       srs commit       <changeset>"
    print "       srs erase        <pkgname> [<version>]"
    print "       srs localcs      <pkg> <outfile>"
    print "       srs localcommit  <changeset>"
    print "       srs pkglist      <pkgname> [<version>]"
    print "       srs remove       <path>"
    print "       srs replist      <pkgname> [<version>]"
    print "       srs rblist"
    print "       srs rollback     <rollback>"
    print "       srs source       [usage]"
    print "       srs update	   <pkgname> <version>"
    print "           update       <changeset>"
    print "       srs usage"
    print ""
    print "commit flags:  --target-branch <branch>"
    print ""
    print "common flags:  --branch <defaultbranch>"
    print '               --config "<item> <value>"'
    print "               --reppath <repository-path>"
    print "               --root <root>"
    print ""
    print "pkglist flags: --all"
    print "               --ids"
    print "               --ls"
    print ""
    print "update flags: --replace-files"
    return rc

def openRepository(path, mode):
    try:
        repos = fsrepos.FilesystemRepository(path, mode)
    except repository.OpenError, e:
        log.error('Unable to open repository %s: %s', path, str(e))
        sys.exit(1)
    return repos

def openDatabase(root, path, mode):
    try:
        db = database.Database(root, path, mode)
    except repository.OpenError, e:
        log.error('Unable to open database %s%s%s: %s', root, os.sep, path, str(e))
        sys.exit(1)
    return db

def main():
    otherArgs = [ sys.argv[0] ]
    argSet = {}
    argDef = {}
    cfgMap = {}
    # 0 - arg may occur, no parameter
    # 1 - arg may occur once, w/ parameter
    # 2 - arg may occur N times, w/ parameter

    cfgMap["branch"] = "defaultbranch"
    cfgMap["reppath"] = "reppath"
    cfgMap["root"] = "root"

    argDef["all"] = 0
    argDef["config"] = 2
    argDef["ids"] = 0
    argDef["ls"] = 0
    argDef["macros"] = 1
    argDef["prep"] = 0
    argDef["profile"] = 0
    argDef["replace-files"] = 0
    argDef["target-branch"] = 1

    argDef.update(srcctl.argDef)

    for arg in cfgMap.keys():
	argDef[arg] = 1

    i = 1
    while i < len(sys.argv):
	if sys.argv[i][:2] != "--":
	    otherArgs.append(sys.argv[i])
	else:
	    arg = sys.argv[i][2:]
	    if not argDef.has_key(arg): return usage()

	    if not argDef[arg]:
		argSet[arg] = 1
	    else:
		# the argument takes a parameter
		i = i + 1
		if i >= len(sys.argv): return usage()

		if argDef[arg] == 1:
		    # exactly one parameter is allowd
		    if argSet.has_key(arg): return usage()
		    argSet[arg] = sys.argv[i]
		else:
		    # multiple parameters may occur
		    if argSet.has_key(arg):
			argSet[arg].append(sys.argv[i])
		    else:
			argSet[arg] = [sys.argv[i]]

	i = i + 1

    if '-v' in otherArgs:
	otherArgs.remove('-v')
	log.setVerbosity(1)
    else:
	log.setVerbosity(0)

    profile = False
    if argSet.has_key('profile'):
	import hotshot
	prof = hotshot.Profile('srs.prof')
	prof.start()
	profile = True
	del argSet['profile']

    if argSet.has_key('config'):
	for param in argSet['config']:
	    cfg.configLine(param)

	del argSet['config']

    for (arg, name) in cfgMap.items():
	if argSet.has_key(arg):
	    cfg.configLine("%s %s" % (name, argSet[arg]))
	    del argSet[arg]

    if (len(otherArgs) < 2):
	return usage()
    elif (otherArgs[1] == "branch"):
	if argSet: return usage
	if len(otherArgs) < 4 or len(otherArgs) > 5: return usage()
	repos = openRepository(cfg.reppath, "c")

	args = [repos, ] + otherArgs[2:]
	branch.branch(*args)
    elif (otherArgs[1] == "changeset"):
	# current usage is "package file oldversion newversion"
	if len(otherArgs) != 5 and len(otherArgs) != 6:
	    return usage()

	name = otherArgs[2]
	if len(otherArgs) == 6:
	    (old, new) = (otherArgs[3], otherArgs[4])
	    outFile = otherArgs[5]
	else:
	    (old, new) = (None, otherArgs[3])
	    outFile = otherArgs[4]

	repos = openRepository(cfg.reppath, "r")

	changeset.ChangeSetCommand(repos, cfg, name, outFile, old, new)
    elif (otherArgs[1] == "commit"):
	targetBranch = None
	if argSet.has_key('target-branch'):
	    targetBranch  = argSet['target-branch']
	    del argSet['target-branch']
	if len(otherArgs) < 3: return usage()
	repos = openRepository(cfg.reppath, "c")
	for changeSet in otherArgs[2:]:
	    commit.doCommit(repos, changeSet, targetBranch)
    elif (otherArgs[1] == "config"):
	if argSet: return usage
	if (len(otherArgs) > 2):
	    return usage()
	else:
	    cfg.display()
    elif (otherArgs[1] == "cook"):
	log.setVerbosity(1)
	macros = ()
	prep = 0
	if argSet.has_key('prep'):
	    del argSet['prep']
	    prep = 1
	if argSet.has_key('macros'):
	    argSet['macros']
	    f = open(argSet['macros'])
	    # XXX sick hack
	    macroSrc = "macros =" + f.read()
	    exec macroSrc
	    del f
	    del argSet['macros']
	if argSet: return usage()

	cook.cookCommand(cfg, otherArgs[2:], prep, macros)
    elif (otherArgs[1] == "erase"):
	if argSet: return usage
	if len(otherArgs) >= 3 and len(otherArgs) <=4:
	    db = openDatabase(cfg.root, cfg.dbpath, "c")

	    args = [db, cfg] + otherArgs[2:]
	    updatecmd.doErase(*args)
	else:
	    return usage()
    elif (otherArgs[1] == "import"):
	print "this is broken"
    elif (otherArgs[1] == "localcs"):
	if len(otherArgs) != 4 and len(otherArgs) != 4:
	    return usage()

	name = otherArgs[2]
	outFile = otherArgs[3]

	db = database.Database(cfg.root, cfg.dbpath, "r")
	changeset.LocalChangeSetCommand(db, cfg, name, outFile)
    elif (otherArgs[1] == "localcommit"):
	if len(otherArgs) < 3: return usage()
	db = database.Database(cfg.root, cfg.dbpath, "c")
	for changeSet in otherArgs[2:]:
	    commit.doLocalCommit(db, changeSet)
    elif (otherArgs[1] == "pkglist" or otherArgs[1] == "replist"):
	all = argSet.has_key('all')
	if all: del argSet['all']

	ls = argSet.has_key('ls')
	if ls: del argSet['ls']

	ids = argSet.has_key('ids')
	if ids: del argSet['ids']

	if otherArgs[1] == "replist":
	    queryRepos = openRepository(cfg.reppath, "r")
	else:
	    queryRepos = openDatabase(cfg.root, cfg.dbpath, "r")

	if argSet: return usage()

	if len(otherArgs) >= 2 and len(otherArgs) <= 4:
	    args = [queryRepos, cfg, all, ls, ids] + otherArgs[2:]
	    try:
		display.displayPkgs(*args)
	    except IOError, msg:
		sys.stderr.write(msg.strerror + '\n')
		sys.exit(1)
	else:
	    return usage()
    elif (otherArgs[1] == "rblist"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbpath, "r")
	rollbacks.listRollbacks(db, cfg)
    elif (otherArgs[1] == "remove"):
	if len(otherArgs) != 3: return usage()
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbpath, "c")
	fullPath = util.joinPaths(cfg.root, otherArgs[2])
	if os.path.exists(fullPath):
	    os.unlink(fullPath)
	else:
	    log.warning("%s has already been removed", fullPath)
	db.removeFile(otherArgs[2])
    elif (otherArgs[1] == "rollback"):
	if argSet: return usage
	db = openDatabase(cfg.root, cfg.dbpath, "c")
	args = [db, cfg] + otherArgs[2:]
	rollbacks.apply(*args)
    elif (otherArgs[1] == "source" or otherArgs[1] == "src"):
	return srcctl.sourceCommand(cfg, otherArgs[2:], argSet)
    elif (otherArgs[1] == "update"):
	kwargs = {}
	replaceFiles = argSet.has_key('replace-files')
	if replaceFiles:
	    kwargs['replaceFiles'] = True
	    del argSet['replace-files']
	if argSet: return usage
	if len(otherArgs) >=3 and len(otherArgs) <= 4:
	    repos = openRepository(cfg.reppath, "r")
	    db = openDatabase(cfg.root, cfg.dbpath, "c")

	    args = [repos, db, cfg] + otherArgs[2:]
	    updatecmd.doUpdate(*args, **kwargs)
	else:
	    return usage()
    elif (otherArgs[1] == "return usage"):
	return usage(rc = 0)
    else:
	return usage()

    if profile:
	prof.stop()

    if log.errorOccurred():
	sys.exit(1)

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