<commit_before>"""
Django settings for bovidecoapp project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
import secrets

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = secrets.SECRET_KEY

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEV = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['.wabarr.com']

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bovidecoapp',
    'axes',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'axes.middleware.FailedLoginMiddleware',
)

ROOT_URLCONF = 'bovidecoapp.urls'

WSGI_APPLICATION = 'bovidecoapp.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

if DEV:
    dbPath = os.path.join(BASE_DIR,'bovidecomorph_fortest.db')
else:
    dbPath = "/home/wabarr/bovidecomorph.db"
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': dbPath,
    }
}

#default URL for user logins (used by @requires_permission() decorator)
LOGIN_URL = "/admin/"

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

TEMPLATE_DIRS = [os.path.join(BASE_DIR,'templates')]

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

if not DEV:
    STATIC_ROOT = "/home/wabarr/webapps/bovidecomorph_static/"
else:
    STATIC_ROOT = "/Users/wab536/Desktop/bovid_static"

AXES_COOLOFF_TIME = 1
AXES_LOCKOUT_TEMPLATE = "login_locked.html"
AXES_LOCKOUT_URL = "/lockout/"

# If True, lock out / log based on an IP address AND a user agent.
# This means requests from different user agents but from the same IP are treated differently
AXES_USE_USER_AGENT = True<commit_msg>debug False<commit_after>"""
Django settings for bovidecoapp project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
import secrets

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = secrets.SECRET_KEY

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEV = False

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['.wabarr.com']

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'bovidecoapp',
    'axes',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'axes.middleware.FailedLoginMiddleware',
)

ROOT_URLCONF = 'bovidecoapp.urls'

WSGI_APPLICATION = 'bovidecoapp.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

if DEV:
    dbPath = os.path.join(BASE_DIR,'bovidecomorph_fortest.db')
else:
    dbPath = "/home/wabarr/bovidecomorph.db"
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': dbPath,
    }
}

#default URL for user logins (used by @requires_permission() decorator)
LOGIN_URL = "/admin/"

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

TEMPLATE_DIRS = [os.path.join(BASE_DIR,'templates')]

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'

if not DEV:
    STATIC_ROOT = "/home/wabarr/webapps/bovidecomorph_static/"
else:
    STATIC_ROOT = "/Users/wab536/Desktop/bovid_static"

AXES_COOLOFF_TIME = 1
AXES_LOCKOUT_TEMPLATE = "login_locked.html"
AXES_LOCKOUT_URL = "/lockout/"

# If True, lock out / log based on an IP address AND a user agent.
# This means requests from different user agents but from the same IP are treated differently
AXES_USE_USER_AGENT = True<|endoftext|>


<commit_before>import datetime
import json
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from mapping.models import Location, Report
from mapping.tasks import process_data
from namelist.models import Category

@csrf_exempt
def receive_data(request):
    if request.method == 'POST' and request.POST.has_key('data'):
        # Grab the player's position and IP, process data in background
        origin_x = request.POST['data']['surroundings']['coords']['x']
        origin_y = request.POST['data']['surroundings']['coords']['y']
        ip = request.META['HTTP_X_REAL_IP']
        process_data.delay(reqest.POST['data'], ip)

        data = []

        # Grab all locations in a 15x15 square, centered on the player's position
        x_range = range(x-1, x+15) + range(x, x-15, -1)
        y_range = range(y-1, y+15) + range(y, y-15, -1)
        locations = Location.objects.filter(x__in=[x_range],
                                            y__in=[y_range])

        for location in locations:

            annotation = {}
            annotation['x'] = location.x
            annotation['y'] = location.y

            # Grab all reports from the last five days
            reports = location.report_set.exclude(reported_date___lte=datetime.datetime.now() - datetime.timedelta(days=5))
            reports = reports.order_by('-reported_date')

            # For barricade info, ignore the reports without levels set
            barricades = reports.exclude(barricade_level=None)
            if barricades:
                    annotation['barricades'] = barricades[0].barricade_level
            else:
                annotations['barricades'] = None

            # Grab report stats if they exist
            annotation['player_count'] = location.player_set.count()
            report = location.report_set.order_by('reported_date')
            if report:
                report = report[0]
                annotation['ruined'] = report.is_ruined
                annotation['illuminated'] = report.is_illuminated
                annotation['zombies'] = report.zombies_present
                annotation['report_age'] = unicode(datetime.datetime.now() - report.reported_date)
            else:
                annotations['ruined'] = None
                annotations['illuminated'] = None
                annotations['zombies'] = None
                annotations['report_date'] = None
            data.append(annotation)

        # Find all reported Christmas trees for smashing
        christmas_trees = Location.objects.filter(has_tree=True).values('x', 'y').distinct()
        trees = []
        for location in christmas_trees:
            trees.append({'x': location.x, 'y': location.y})

        # Get all priority targets and other worthless players
        priority_targets = Player.objects.exclude(category__name__in=[None, u"Goon"])
        priority_targets = priority_targets.exclude(location=None)
        priority_targets = priority_targets.values_list('location__x', 'location__y', 'name', 'profile_id', 'category__color_code')
        targets = []
        for target in priority_targets:
            targets.append(dict(zip('x', 'y', 'name', 'profile_id', 'color_code'), target))

        return HttpResponse(json.dumps({'annotations': data, 'trees':trees, 'targets':targets }), content_type='application/json', status=200)

    return HttpResponse(status=405)
<commit_msg>Need to deserialize the json before trying to use it\!<commit_after>import datetime
import json
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from mapping.models import Location, Report
from mapping.tasks import process_data
from namelist.models import Category

@csrf_exempt
def receive_data(request):
    if request.method == 'POST' and request.POST.has_key('data'):
        data = json.loads(request.POST['data'])
        # Grab the player's position and IP, process data in background
        origin_x = data['surroundings']['coords']['x']
        origin_y = data['surroundings']['coords']['y']
        ip = request.META['HTTP_X_REAL_IP']
        process_data.delay(reqest.POST['data'], ip)

        data = []

        # Grab all locations in a 15x15 square, centered on the player's position
        x_range = range(x-1, x+15) + range(x, x-15, -1)
        y_range = range(y-1, y+15) + range(y, y-15, -1)
        locations = Location.objects.filter(x__in=[x_range],
                                            y__in=[y_range])

        for location in locations:

            annotation = {}
            annotation['x'] = location.x
            annotation['y'] = location.y

            # Grab all reports from the last five days
            reports = location.report_set.exclude(reported_date___lte=datetime.datetime.now() - datetime.timedelta(days=5))
            reports = reports.order_by('-reported_date')

            # For barricade info, ignore the reports without levels set
            barricades = reports.exclude(barricade_level=None)
            if barricades:
                    annotation['barricades'] = barricades[0].barricade_level
            else:
                annotations['barricades'] = None

            # Grab report stats if they exist
            annotation['player_count'] = location.player_set.count()
            report = location.report_set.order_by('reported_date')
            if report:
                report = report[0]
                annotation['ruined'] = report.is_ruined
                annotation['illuminated'] = report.is_illuminated
                annotation['zombies'] = report.zombies_present
                annotation['report_age'] = unicode(datetime.datetime.now() - report.reported_date)
            else:
                annotations['ruined'] = None
                annotations['illuminated'] = None
                annotations['zombies'] = None
                annotations['report_date'] = None
            data.append(annotation)

        # Find all reported Christmas trees for smashing
        christmas_trees = Location.objects.filter(has_tree=True).values('x', 'y').distinct()
        trees = []
        for location in christmas_trees:
            trees.append({'x': location.x, 'y': location.y})

        # Get all priority targets and other worthless players
        priority_targets = Player.objects.exclude(category__name__in=[None, u"Goon"])
        priority_targets = priority_targets.exclude(location=None)
        priority_targets = priority_targets.values_list('location__x', 'location__y', 'name', 'profile_id', 'category__color_code')
        targets = []
        for target in priority_targets:
            targets.append(dict(zip('x', 'y', 'name', 'profile_id', 'color_code'), target))

        return HttpResponse(json.dumps({'annotations': data, 'trees':trees, 'targets':targets }), content_type='application/json', status=200)

    return HttpResponse(status=405)
<|endoftext|>


<commit_before>import datetime
import json
import math
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from mapping.models import Location, Report
from mapping.tasks import process_data, get_player
from namelist.models import Category, Player
from orders.views import user_coord_dist
import redis

CONN = redis.Redis(db=6)

@csrf_exempt
def receive_data(request):
    if request.method == 'POST' and request.POST.has_key('data'):
        data = json.loads(request.POST['data'])
        # Grab the player's position and IP, process data in background
        origin_x = data['surroundings']['position']['coords']['x']
        origin_y = data['surroundings']['position']['coords']['y']
        ip = request.META['HTTP_X_REAL_IP']

        process_data.delay(data, ip)

        payload = {}
        payload['annotation'] = []
        payload['trees'] = [json.loads(x) for x in CONN.smembers('trees')]
        payload['trees'].sort(key=lambda t: user_coord_dist(origin_x, origin_y, t['x'], t['y']))

        # Grab all locations in a 15x15 square, centered on the player's position
        x_range = range(origin_x+1, origin_x+8) + range(origin_x, origin_x-8, -1)
        y_range = range(origin_y+1, origin_y+8) + range(origin_y, origin_y-8, -1)

        for x in x_range:
            for y in y_range:
                annotation = CONN.get('location:{0}:{1}'.format(x, y))
                if annotation:
                    payload['annotation'].append(json.loads(annotation))

        return HttpResponse(json.dumps(payload), content_type='application/json', status=200)

    return HttpResponse(status=405)

<commit_msg>Server side sorting: no good.<commit_after>import datetime
import json
import math
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from mapping.models import Location, Report
from mapping.tasks import process_data, get_player
from namelist.models import Category, Player
import redis

CONN = redis.Redis(db=6)

@csrf_exempt
def receive_data(request):
    if request.method == 'POST' and request.POST.has_key('data'):
        data = json.loads(request.POST['data'])
        # Grab the player's position and IP, process data in background
        origin_x = data['surroundings']['position']['coords']['x']
        origin_y = data['surroundings']['position']['coords']['y']
        ip = request.META['HTTP_X_REAL_IP']

        process_data.delay(data, ip)

        payload = {}
        payload['annotation'] = []
        payload['trees'] = [json.loads(x) for x in CONN.smembers('trees')]

        # Grab all locations in a 15x15 square, centered on the player's position
        x_range = range(origin_x+1, origin_x+8) + range(origin_x, origin_x-8, -1)
        y_range = range(origin_y+1, origin_y+8) + range(origin_y, origin_y-8, -1)

        for x in x_range:
            for y in y_range:
                annotation = CONN.get('location:{0}:{1}'.format(x, y))
                if annotation:
                    payload['annotation'].append(json.loads(annotation))

        return HttpResponse(json.dumps(payload), content_type='application/json', status=200)

    return HttpResponse(status=405)

<|endoftext|>


<commit_before>from unittest import TestCase
from chatterbot.conversation import Response

class ResponseTests(TestCase):

    def setUp(self):
        self.response = Response("A test response.")
<commit_msg>Remove test case for response objects<commit_after><|endoftext|>


<commit_before>import os
import base64
import random
from datetime import datetime
from django.db.models import F, Q
from planetstack.config import Config
from observer.openstacksyncstep import OpenStackSyncStep
from core.models.node import Node
from core.models.deployment import Deployment
from core.models.site import Site

class SyncNodes(OpenStackSyncStep):
    provides=[Node]
    requested_interval=0

    def fetch_pending(self):
        # collect local nodes
        sites = Site.objects.all()
        nodes = Node.objects.all()
        node_hostnames  = [node.name for node in nodes]

        # fetch all nodes from each deployment
        deployments = Deployment.objects.all()
        for deployment in deployments:
            driver = self.driver.admin_driver(deployment=deployment.name)
            compute_nodes = driver.shell.nova.hypervisors.list()
            if compute_node.hypervisor_hostname not in node_hostnames:
                # XX TODO:figure out how to correctly identify a node's site.
                # XX pick a random site to add the node to for now
                site_index = random.randint(0, len(sites))
                node = Node(name=compute_node.hypervisor_hostname,
                            site=sites[site_index], deployment=deployment)
                new_nodes.append(node)

        return new_nodes

    def sync_record(self, node):
        node.save()

<commit_msg>fix bug<commit_after>import os
import base64
import random
from datetime import datetime
from django.db.models import F, Q
from planetstack.config import Config
from observer.openstacksyncstep import OpenStackSyncStep
from core.models.node import Node
from core.models.deployment import Deployment
from core.models.site import Site

class SyncNodes(OpenStackSyncStep):
    provides=[Node]
    requested_interval=0

    def fetch_pending(self):
        # collect local nodes
        sites = Site.objects.all()
        nodes = Node.objects.all()
        node_hostnames  = [node.name for node in nodes]

        # fetch all nodes from each deployment
        deployments = Deployment.objects.all()
        new_nodes = []
        for deployment in deployments:
            driver = self.driver.admin_driver(deployment=deployment.name)
            compute_nodes = driver.shell.nova.hypervisors.list()
            if compute_node.hypervisor_hostname not in node_hostnames:
                # XX TODO:figure out how to correctly identify a node's site.
                # XX pick a random site to add the node to for now
                site_index = random.randint(0, len(sites))
                node = Node(name=compute_node.hypervisor_hostname,
                            site=sites[site_index], deployment=deployment)
                new_nodes.append(node)

        return new_nodes

    def sync_record(self, node):
        node.save()

<|endoftext|>


<commit_before>#!/usr/bin/python
import sqlite3
import datetime
import re
from os.path import expanduser

class Module:
    def __init__(self, mainMenu, params=[]):
        # metadata info about the module, not modified during runtime
        self.info = {
            # name for the module that will appear in module menus
            'Name': 'iMessageDump',

            # list of one or more authors for the module
            'Author': ['Alex Rymdeko-Harvey', '@Killswitch-GUI'],

            # more verbose multi-line description of the module
            'Description': 'This module will enumerate the entire chat and IMessage SQL Database.',

            # True if the module needs to run in the background
            'Background' : False,

            # File extension to save the file as
            'OutputExtension' : "",

            # if the module needs administrative privileges
            'NeedsAdmin' : False,

            # True if the method doesn't touch disk/is reasonably opsec safe
            'OpsecSafe' : True,

            # list of any references/other comments
            'Comments': [
                'Using SQLite3 iMessage has a decent standard to correlate users to messages and isnt encrypted.'
            ]
        }

        # any options needed by the module, settable during runtime
        self.options = {
            # format:
            #   value_name : {description, required, default_value}
            'Agent' : {
                # The 'Agent' option is the only one that MUST be in a module
                'Description'   :   'Agent to run from.',
                'Required'      :   True,
                'Value'         :   ''
            }
        }
        # save off a copy of the mainMenu object to access external functionality
        #   like listeners/agent handlers/etc.
        self.mainMenu = mainMenu

        # During instantiation, any settable option parameters
        #   are passed as an object set to the module and the
        #   options dictionary is automatically set. This is mostly
        #   in case options are passed on the command line
        if params:
            for param in params:
                # parameter format is [Name, Value]
                option, value = param
                if option in self.options:
                    self.options[option]['Value'] = value

    def generate(self):
        script = """
from os.path import expanduser
import sqlite3
import datetime
import re

def func():
    try:
        home = expanduser("~") + '/Library/Messages/chat.db'
        # Open the database handle for the user
        conn = sqlite3.connect(home)
        cur = conn.cursor()
        # Query Date, Text message and place it into a array
        cur.execute("SELECT date,text,service,account,ROWID FROM message;")
        # execute the data enum
        statment = cur.fetchall()
        # handle: Table links the number, country, type to the chat ID
        # SELECT * FROM handle
        # ex: (2, u'+12150000000', u'US', u'iMessage', None)
        cur.execute("SELECT ROWID,id,country,service FROM handle")
        handle = cur.fetchall()
        # chat_message_join: Links the chat ID to the Text ID (sequency number)
        # SELECT * FROM chat_message_join
        cur.execute("SELECT chat_id,message_id FROM chat_message_join")
        messageLink = cur.fetchall()
        #cur.execute("SELECT account_id,service_center,chat_identifier FROM chat")
        #GuidData = cur.fetchall()
        # Itterate over data
        for item in statment:
            try:
                for messageid in messageLink:
                    if str(messageid[1]) == str(item[4]):
                        chatid =  messageid[0]
                        for rowid in handle:
                            if str(rowid[0]) == str(chatid):
                                Number = str(rowid[1])
                                Country = str(rowid[2])
                                Type = str(rowid[3])

                epoch = TimeConv(item[0])
                line =  " \\nROWID: " + str(item[4])
                line += " \\nService: " + item[2]
                line += " \\nAccount: " + item[3]
                line += " \\nDate: " + epoch
                line += " \\nNumber: " + Number
                line += " \\nCountry: " + Country
                line += " \\nType: " + Type
                line += " \\nMessage: " + str(RemoveUnicode(item[1]))
                print line
            except Exception as e:
                print e
        conn.close()
        print " [!] Messages Enumerated: " + str(len(statment))
    except Exception as e:
        print e
    # Close the Database handle

def TimeConv(epoch):
    d = datetime.datetime.strptime("01-01-1904", "%m-%d-%Y")
    time = (d + datetime.timedelta(seconds=epoch)).strftime("%a, %d %b %Y %H:%M:%S GMT")
    return time

def RemoveUnicode(string):
        try:
            string_data = string
            if string_data is None:
                return string_data
            if isinstance(string_data, str):
                string_data = str(string_data.decode('ascii', 'ignore'))
            else:
                string_data = string_data.encode('ascii', 'ignore')
            remove_ctrl_chars_regex = re.compile(r'[^\x20-\x7e]')
            CleanString = remove_ctrl_chars_regex.sub('', string_data)
            return CleanString
        except Exception as e:
            p = '[!] UTF8 Decoding issues Matching: ' + str(e)
            print p
func()
"""
        return script

# handle: Table links the number, country, type to the chat ID
# SELECT * FROM handle
# chat_message_join: Links the chat ID to the Text ID (sequency number)
# SELECT * FROM chat_message_join

# INTEGER: A signed integer up to 8 bytes depending on the magnitude of the value.
# REAL: An 8-byte floating point value.
# TEXT: A text string, typically UTF-8 encoded (depending on the database encoding).
# BLOB: A blob of data (binary large object) for storing binary data.
# NULL: A NULL value, represents missing data or an empty cell.

# SQLITE3 message laylout:

# (u'table', u'message', u'message', 5, u'CREATE TABLE message (ROWID INTEGER PRIMARY KEY AUTOINCREMENT,
# guid TEXT UNIQUE NOT NULL, text TEXT, replace INTEGER DEFAULT 0, service_center TEXT, handle_id INTEGER DEFAULT 0,
# subject TEXT, country TEXT, attributedBody BLOB, version INTEGER DEFAULT 0, type INTEGER DEFAULT 0, service TEXT,
# account TEXT, account_guid TEXT, error INTEGER DEFAULT 0, date INTEGER, date_read INTEGER, date_delivered INTEGER,
# is_delivered INTEGER DEFAULT 0, is_finished INTEGER DEFAULT 0, is_emote INTEGER DEFAULT 0, is_from_me INTEGER DEFAULT 0,
# is_empty INTEGER DEFAULT 0, is_delayed INTEGER DEFAULT 0, is_auto_reply INTEGER DEFAULT 0, is_prepared INTEGER DEFAULT 0,
# is_read INTEGER DEFAULT 0, is_system_message INTEGER DEFAULT 0, is_sent INTEGER DEFAULT 0, has_dd_results INTEGER DEFAULT 0,
# is_service_message INTEGER DEFAULT 0, is_forward INTEGER DEFAULT 0, was_downgraded INTEGER DEFAULT 0, is_archive INTEGER DEFAULT 0,
# cache_has_attachments INTEGER DEFAULT 0, cache_roomnames TEXT, was_data_detected INTEGER DEFAULT 0, was_deduplicated INTEGER DEFAULT 0,
# is_audio_message INTEGER DEFAULT 0, is_played INTEGER DEFAULT 0, date_played INTEGER, item_type INTEGER DEFAULT 0,
# other_handle INTEGER DEFAULT 0, group_title TEXT, group_action_type INTEGER DEFAULT 0, share_status INTEGER DEFAULT 0,
# share_direction INTEGER DEFAULT 0, is_expirable INTEGER DEFAULT 0, expire_state INTEGER DEFAULT 0, message_action_type INTEGER DEFAULT 0,
# message_source INTEGER DEFAULT 0)')
<commit_msg>Update imessage_dump.py<commit_after>#!/usr/bin/python
import sqlite3
import datetime
import re
from os.path import expanduser

class Module:
    def __init__(self, mainMenu, params=[]):
        # metadata info about the module, not modified during runtime
        self.info = {
            # name for the module that will appear in module menus
            'Name': 'iMessageDump',

            # list of one or more authors for the module
            'Author': ['Alex Rymdeko-Harvey', '@Killswitch-GUI'],

            # more verbose multi-line description of the module
            'Description': 'This module will enumerate the entire chat and IMessage SQL Database.',

            # True if the module needs to run in the background
            'Background' : False,

            # File extension to save the file as
            'OutputExtension' : "",

            # if the module needs administrative privileges
            'NeedsAdmin' : False,

            # True if the method doesn't touch disk/is reasonably opsec safe
            'OpsecSafe' : True,

            # list of any references/other comments
            'Comments': [
                'Using SQLite3 iMessage has a decent standard to correlate users to messages and isnt encrypted.'
            ]
        }

        # any options needed by the module, settable during runtime
        self.options = {
            # format:
            #   value_name : {description, required, default_value}
            'Agent' : {
                # The 'Agent' option is the only one that MUST be in a module
                'Description'   :   'Agent to run from.',
                'Required'      :   True,
                'Value'         :   ''
            }
        }
        # save off a copy of the mainMenu object to access external functionality
        #   like listeners/agent handlers/etc.
        self.mainMenu = mainMenu

        # During instantiation, any settable option parameters
        #   are passed as an object set to the module and the
        #   options dictionary is automatically set. This is mostly
        #   in case options are passed on the command line
        if params:
            for param in params:
                # parameter format is [Name, Value]
                option, value = param
                if option in self.options:
                    self.options[option]['Value'] = value

    def generate(self):
        script = """
import sqlite3
import datetime
import re

def func():
    try:
        from os.path import expanduser
        home = expanduser("~") + '/Library/Messages/chat.db'
        # Open the database handle for the user
        conn = sqlite3.connect(home)
        cur = conn.cursor()
        # Query Date, Text message and place it into a array
        cur.execute("SELECT date,text,service,account,ROWID FROM message;")
        # execute the data enum
        statment = cur.fetchall()
        # handle: Table links the number, country, type to the chat ID
        # SELECT * FROM handle
        # ex: (2, u'+12150000000', u'US', u'iMessage', None)
        cur.execute("SELECT ROWID,id,country,service FROM handle")
        handle = cur.fetchall()
        # chat_message_join: Links the chat ID to the Text ID (sequency number)
        # SELECT * FROM chat_message_join
        cur.execute("SELECT chat_id,message_id FROM chat_message_join")
        messageLink = cur.fetchall()
        #cur.execute("SELECT account_id,service_center,chat_identifier FROM chat")
        #GuidData = cur.fetchall()
        # Itterate over data
        for item in statment:
            try:
                for messageid in messageLink:
                    if str(messageid[1]) == str(item[4]):
                        chatid =  messageid[0]
                        for rowid in handle:
                            if str(rowid[0]) == str(chatid):
                                Number = str(rowid[1])
                                Country = str(rowid[2])
                                Type = str(rowid[3])

                epoch = TimeConv(item[0])
                line =  " \\nROWID: " + str(item[4])
                line += " \\nService: " + item[2]
                line += " \\nAccount: " + item[3]
                line += " \\nDate: " + epoch
                line += " \\nNumber: " + Number
                line += " \\nCountry: " + Country
                line += " \\nType: " + Type
                line += " \\nMessage: " + str(RemoveUnicode(item[1]))
                print line
            except Exception as e:
                print e
        conn.close()
        print " [!] Messages Enumerated: " + str(len(statment))
    except Exception as e:
        print e
    # Close the Database handle

def TimeConv(epoch):
    d = datetime.datetime.strptime("01-01-1904", "%m-%d-%Y")
    time = (d + datetime.timedelta(seconds=epoch)).strftime("%a, %d %b %Y %H:%M:%S GMT")
    return time

def RemoveUnicode(string):
        try:
            string_data = string
            if string_data is None:
                return string_data
            if isinstance(string_data, str):
                string_data = str(string_data.decode('ascii', 'ignore'))
            else:
                string_data = string_data.encode('ascii', 'ignore')
            remove_ctrl_chars_regex = re.compile(r'[^\x20-\x7e]')
            CleanString = remove_ctrl_chars_regex.sub('', string_data)
            return CleanString
        except Exception as e:
            p = '[!] UTF8 Decoding issues Matching: ' + str(e)
            print p
func()
"""
        return script

# handle: Table links the number, country, type to the chat ID
# SELECT * FROM handle
# chat_message_join: Links the chat ID to the Text ID (sequency number)
# SELECT * FROM chat_message_join

# INTEGER: A signed integer up to 8 bytes depending on the magnitude of the value.
# REAL: An 8-byte floating point value.
# TEXT: A text string, typically UTF-8 encoded (depending on the database encoding).
# BLOB: A blob of data (binary large object) for storing binary data.
# NULL: A NULL value, represents missing data or an empty cell.

# SQLITE3 message laylout:

# (u'table', u'message', u'message', 5, u'CREATE TABLE message (ROWID INTEGER PRIMARY KEY AUTOINCREMENT,
# guid TEXT UNIQUE NOT NULL, text TEXT, replace INTEGER DEFAULT 0, service_center TEXT, handle_id INTEGER DEFAULT 0,
# subject TEXT, country TEXT, attributedBody BLOB, version INTEGER DEFAULT 0, type INTEGER DEFAULT 0, service TEXT,
# account TEXT, account_guid TEXT, error INTEGER DEFAULT 0, date INTEGER, date_read INTEGER, date_delivered INTEGER,
# is_delivered INTEGER DEFAULT 0, is_finished INTEGER DEFAULT 0, is_emote INTEGER DEFAULT 0, is_from_me INTEGER DEFAULT 0,
# is_empty INTEGER DEFAULT 0, is_delayed INTEGER DEFAULT 0, is_auto_reply INTEGER DEFAULT 0, is_prepared INTEGER DEFAULT 0,
# is_read INTEGER DEFAULT 0, is_system_message INTEGER DEFAULT 0, is_sent INTEGER DEFAULT 0, has_dd_results INTEGER DEFAULT 0,
# is_service_message INTEGER DEFAULT 0, is_forward INTEGER DEFAULT 0, was_downgraded INTEGER DEFAULT 0, is_archive INTEGER DEFAULT 0,
# cache_has_attachments INTEGER DEFAULT 0, cache_roomnames TEXT, was_data_detected INTEGER DEFAULT 0, was_deduplicated INTEGER DEFAULT 0,
# is_audio_message INTEGER DEFAULT 0, is_played INTEGER DEFAULT 0, date_played INTEGER, item_type INTEGER DEFAULT 0,
# other_handle INTEGER DEFAULT 0, group_title TEXT, group_action_type INTEGER DEFAULT 0, share_status INTEGER DEFAULT 0,
# share_direction INTEGER DEFAULT 0, is_expirable INTEGER DEFAULT 0, expire_state INTEGER DEFAULT 0, message_action_type INTEGER DEFAULT 0,
# message_source INTEGER DEFAULT 0)')
<|endoftext|>


<commit_before>import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crashstats.settings')

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

try:
    import newrelic.agent
except ImportError:
    newrelic = False

if newrelic:
    newrelic_ini = os.getenv('NEWRELIC_PYTHON_INI_FILE', None)
    if newrelic_ini:
        newrelic.agent.initialize(newrelic_ini)
        application = newrelic.agent.wsgi_application()(application)
<commit_msg>fixes bug 1226359 - Fail more gracefully if the NEWRELIC_PYTHON_INI_FILE doesn't exist<commit_after>import os
import sys

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crashstats.settings')

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

try:
    import newrelic.agent
except ImportError:
    newrelic = False

if newrelic:
    newrelic_ini = os.getenv('NEWRELIC_PYTHON_INI_FILE', None)
    if newrelic_ini:
        if os.path.isfile(newrelic_ini):
            newrelic.agent.initialize(newrelic_ini)
            application = newrelic.agent.wsgi_application()(application)
        else:
            print >>sys.stderr, (
                "NEWRELIC_PYTHON_INI_FILE set but file does not exist. "
                "Skipping to initialize newrelic agent."
            )
<|endoftext|>


<commit_before># import matplotlib
# matplotlib.use('Agg')

import discretize
from scipy.constants import mu_0
from SimPEG import maps, tests, utils
from SimPEG.electromagnetics import frequency_domain as fdem
from SimPEG.electromagnetics.utils import omega

try:
    from pymatsolver import Pardiso as Solver
except ImportError:
    from SimPEG import Solver as SolverLU

import time
import os
import numpy as np
import unittest

# This could be reduced if we refine the meshes
TOL_FWD = 5e-1  # relative tolerance for prim-sec comparison

TOL_JT = 1e-10
FLR = 1e-20  # "zero", so if residual below this --> pass regardless of order

np.random.seed(2016)

# To test the primary secondary-source, we look at make sure doing primary
# secondary for a simple model gives comprable results to just solving a 3D
# problem

# Also run a sensitivity test, adjoint test

# physical properties
sigmaback = 1e-1
sigmablock = 5e-1

block_x = np.r_[125.0, 225.0]
block_y = np.r_[-50.0, 50.0]
block_z = np.r_[-50.0, 50.0]

# model
model = np.r_[
    np.log(sigmaback),
    np.log(sigmablock),
    np.mean(block_z),
    np.diff(block_z),
    np.mean(block_x),
    np.diff(block_x),
    np.mean(block_y),
    np.diff(block_y),
]

# source
src_loc = np.r_[0.0, 0.0, 0.0]
freq = 10

# receivers
rx_x = np.linspace(-175.0, 175.0, 8)
rx_y = rx_x.copy()
rx_z = np.r_[175.0]
rx_locs = utils.ndgrid(rx_x, rx_y, rx_z)

# mesh
csx, ncx, npadx = 25.0, 16, 10
csz, ncz, npadz = 25.0, 8, 10
pf = 1.5

# primary mesh
hx = [(csx, ncx), (csx, npadx, pf)]
hz = [(csz, npadz, -pf), (csz, ncz), (csz, npadz, pf)]
meshp = discretize.CylMesh([hx, 1.0, hz], x0="0CC")

# secondary mesh
h = [(csz, npadz - 4, -pf), (csz, ncz), (csz, npadz - 4, pf)]
meshs = discretize.TensorMesh(3 * [h], x0="CCC")

# mappings
primaryMapping = (
    maps.ExpMap(meshp) * maps.SurjectFull(meshp) * maps.Projection(nP=8, index=[0])
)

mapping = (
    maps.ExpMap(meshs)
    * maps.ParametricBlockInLayer(meshs)
    * maps.Projection(nP=8, index=np.hstack([np.r_[0], np.arange(0, 8)]))
)

primaryMap2Meshs = (
    maps.ExpMap(meshs) * maps.SurjectFull(meshs) * maps.Projection(nP=8, index=[0])
)

class PrimSecFDEMTest(object):

    # --------------------- Run some tests! --------------------- #
    def DataTest(self):
        print("\nTesting Data")
        dpred_primsec = self.secondarySimulation.dpred(model, f=self.fields_primsec)
        dpred_3D = self.simulation3D.dpred(model, f=self.fields_3D)

        nrx_locs = rx_locs.shape[0]
        dpred_primsec = dpred_primsec.reshape(nrx_locs, len(self.rxlist))
        dpred_3D = dpred_3D.reshape(nrx_locs, len(self.rxlist))

        for i in range(len(self.rxlist)):
            rx = self.rxlist[i]
            normps = np.linalg.norm(dpred_primsec[:, i])
            norm3D = np.linalg.norm(dpred_3D[:, i])
            normdiff = np.linalg.norm(dpred_primsec[:, i] - dpred_3D[:, i])
            passed = normdiff < TOL_FWD * np.mean([normps, norm3D])
            print(
                "  Testing {rxfield}{rxorient} {rxcomp}...   "
                "prim-sec: {normps:10.5e}, 3D: {norm3D:10.5e}, "
                "diff: {diff:10.5e}, passed? {passed}".format(
                    rxfield=rx.projField,
                    rxorient=rx.projComp,
                    rxcomp=rx.component,
                    normps=normps,
                    norm3D=norm3D,
                    diff=normdiff,
                    passed=passed,
                )
            )

            self.assertTrue(passed)
        return True

    def JvecTest(self):
        print("\nTesting Jvec")
        x0 = model

        def fun(x):
            return [
                self.secondarySimulation.dpred(x),
                lambda x: self.secondarySimulation.Jvec(x0, x, f=self.fields_primsec),
            ]

        return tests.checkDerivative(fun, x0, num=2, plotIt=False)

    def AdjointTest(self):
        print("\nTesting adjoint")

        m = model
        f = self.fields_primsec
        v = np.random.rand(self.secondarySurvey.nD)
        w = np.random.rand(self.secondarySimulation.sigmaMap.nP)

        vJw = v.dot(self.secondarySimulation.Jvec(m, w, f))
        wJtv = w.dot(self.secondarySimulation.Jtvec(m, v, f))
        tol = np.max([TOL_JT * (10 ** int(np.log10(np.abs(vJw)))), FLR])
        passed = np.abs(vJw - wJtv) < tol
        print(
            " J: {}, JT: {}, diff: {}, tol: {}, passed? {}".format(
                vJw, wJtv, vJw - wJtv, tol, passed
            )
        )

        return passed

class PrimSecFDEMSrcTest_Cyl2Cart_EB_EB(unittest.TestCase, PrimSecFDEMTest):
    @classmethod
    def setUpClass(self):

        print("\n------- Testing Primary Secondary Source EB -> EB --------\n")
        # receivers
        self.rxlist = []
        for rxtype in ["MagneticFluxDensity", "ElectricField"]:
            rx = getattr(fdem.Rx, "Point{}".format(rxtype))
            for orientation in ["x", "y", "z"]:
                for comp in ["real", "imag"]:
                    self.rxlist.append(
                        rx(rx_locs, component=comp, orientation=orientation)
                    )

        # primary
        self.primarySimulation = fdem.Simulation3DMagneticFluxDensity(
            meshp, sigmaMap=primaryMapping
        )
        self.primarySimulation.solver = Solver
        primarySrc = fdem.Src.MagDipole(self.rxlist, frequency=freq, location=src_loc)
        self.primarySurvey = fdem.Survey([primarySrc])

        self.secondarySrc = fdem.Src.PrimSecMappedSigma(
            self.rxlist,
            freq,
            self.primarySimulation,
            self.primarySurvey,
            primaryMap2Meshs,
        )
        self.secondarySurvey = fdem.Survey([self.secondarySrc])
        # Secondary Problem
        self.secondarySimulation = fdem.Simulation3DMagneticFluxDensity(
            meshs, survey=self.secondarySurvey, sigmaMap=mapping
        )
        self.secondarySimulation.solver = Solver

        # Full 3D problem to compare with
        self.survey3D = fdem.Survey([primarySrc])

        self.simulation3D = fdem.Simulation3DMagneticFluxDensity(
            meshs, survey=self.survey3D, sigmaMap=mapping
        )
        self.simulation3D.solver = Solver

        # solve and store fields
        print("   solving primary - secondary")
        self.fields_primsec = self.secondarySimulation.fields(model)
        print("     ... done")

        self.fields_primsec = self.secondarySimulation.fields(model)
        print("   solving 3D")
        self.fields_3D = self.simulation3D.fields(model)
        print("     ... done")

        return None

    # --------------------- Run some tests! --------------------- #
    def test_data_EB(self):
        self.DataTest()

    def test_Jvec_EB(self):
        self.JvecTest()

    def test_Jadjoint_EB(self):
        self.AdjointTest()

class PrimSecFDEMSrcTest_Cyl2Cart_HJ_EB(unittest.TestCase, PrimSecFDEMTest):
    @classmethod
    def setUpClass(self):

        print("\n------- Testing Primary Secondary Source HJ -> EB --------\n")
        # receivers
        self.rxlist = []
        for rxtype in ["MagneticFluxDensity", "ElectricField"]:
            rx = getattr(fdem.Rx, "Point{}".format(rxtype))
            for orientation in ["x", "y", "z"]:
                for comp in ["real", "imag"]:
                    self.rxlist.append(
                        rx(rx_locs, component=comp, orientation=orientation)
                    )

        # primary
        self.primarySimulation = fdem.Simulation3DCurrentDensity(
            meshp, sigmaMap=primaryMapping
        )
        self.primarySimulation.solver = Solver
        s_e = np.zeros(meshp.nF)
        inds = meshp.nFx + utils.closestPoints(meshp, src_loc, gridLoc="Fz")
        s_e[inds] = 1.0 / csz
        primarySrc = fdem.Src.RawVec_e(
            self.rxlist, frequency=freq, s_e=s_e / meshp.area
        )
        self.primarySurvey = fdem.Survey([primarySrc])

        # Secondary Problem
        self.secondarySrc = fdem.Src.PrimSecMappedSigma(
            self.rxlist,
            freq,
            self.primarySimulation,
            self.primarySurvey,
            primaryMap2Meshs,
        )
        self.secondarySurvey = fdem.Survey([self.secondarySrc])

        self.secondarySimulation = fdem.Simulation3DElectricField(
            meshs, survey=self.secondarySurvey, sigmaMap=mapping,
        )
        self.secondarySimulation.solver = Solver

        # Full 3D problem to compare with
        src3D = fdem.Src.RawVec_e(self.rxlist, frequency=freq, s_e=s_e3D)
        self.survey3D = fdem.Survey([src3D])

        self.simulation3D = fdem.Simulation3DElectricField(
            meshs, survey=self.survey3D, sigmaMap=mapping
        )
        self.simulation3D.solver = Solver

        s_e3D = np.zeros(meshs.nE)
        inds = meshs.nEx + meshs.nEy + utils.closestPoints(meshs, src_loc, gridLoc="Ez")
        s_e3D[inds] = [1.0 / (len(inds))] * len(inds)
        self.simulation3D.model = model

        # solve and store fields
        print("   solving primary - secondary")
        self.fields_primsec = self.secondarySimulation.fields(model)
        print("     ... done")

        self.fields_primsec = self.secondarySimulation.fields(model)
        print("   solving 3D")
        self.fields_3D = self.simulation3D.fields(model)
        print("     ... done")

        return None

    # --------------------- Run some tests! --------------------- #
    def test_data_HJ(self):
        self.DataTest()

    def test_Jvec_HJ(self):
        self.JvecTest()

    def test_Jadjoint_HJ(self):
        self.AdjointTest()

if __name__ == "__main__":
    unittest.main()
<commit_msg>a few more edits<commit_after># import matplotlib
# matplotlib.use('Agg')

import discretize
from scipy.constants import mu_0
from SimPEG import maps, tests, utils
from SimPEG.electromagnetics import frequency_domain as fdem
from SimPEG.electromagnetics.utils import omega

try:
    from pymatsolver import Pardiso as Solver
except ImportError:
    from SimPEG import Solver as SolverLU

import time
import os
import numpy as np
import unittest

# This could be reduced if we refine the meshes
TOL_FWD = 5e-1  # relative tolerance for prim-sec comparison

TOL_JT = 1e-10
FLR = 1e-20  # "zero", so if residual below this --> pass regardless of order

np.random.seed(2016)

# To test the primary secondary-source, we look at make sure doing primary
# secondary for a simple model gives comprable results to just solving a 3D
# problem

# Also run a sensitivity test, adjoint test

# physical properties
sigmaback = 1e-1
sigmablock = 5e-1

block_x = np.r_[125.0, 225.0]
block_y = np.r_[-50.0, 50.0]
block_z = np.r_[-50.0, 50.0]

# model
model = np.r_[
    np.log(sigmaback),
    np.log(sigmablock),
    np.mean(block_z),
    np.diff(block_z),
    np.mean(block_x),
    np.diff(block_x),
    np.mean(block_y),
    np.diff(block_y),
]

# source
src_loc = np.r_[0.0, 0.0, 0.0]
freq = 10

# receivers
rx_x = np.linspace(-175.0, 175.0, 8)
rx_y = rx_x.copy()
rx_z = np.r_[175.0]
rx_locs = utils.ndgrid(rx_x, rx_y, rx_z)

# mesh
csx, ncx, npadx = 25.0, 16, 10
csz, ncz, npadz = 25.0, 8, 10
pf = 1.5

# primary mesh
hx = [(csx, ncx), (csx, npadx, pf)]
hz = [(csz, npadz, -pf), (csz, ncz), (csz, npadz, pf)]
meshp = discretize.CylMesh([hx, 1.0, hz], x0="0CC")

# secondary mesh
h = [(csz, npadz - 4, -pf), (csz, ncz), (csz, npadz - 4, pf)]
meshs = discretize.TensorMesh(3 * [h], x0="CCC")

# mappings
primaryMapping = (
    maps.ExpMap(meshp) * maps.SurjectFull(meshp) * maps.Projection(nP=8, index=[0])
)

mapping = (
    maps.ExpMap(meshs)
    * maps.ParametricBlockInLayer(meshs)
    * maps.Projection(nP=8, index=np.hstack([np.r_[0], np.arange(0, 8)]))
)

primaryMap2Meshs = (
    maps.ExpMap(meshs) * maps.SurjectFull(meshs) * maps.Projection(nP=8, index=[0])
)

class PrimSecFDEMTest(object):

    # --------------------- Run some tests! --------------------- #
    def DataTest(self):
        print("\nTesting Data")
        dpred_primsec = self.secondarySimulation.dpred(model, f=self.fields_primsec)
        dpred_3D = self.simulation3D.dpred(model, f=self.fields_3D)

        nrx_locs = rx_locs.shape[0]
        dpred_primsec = dpred_primsec.reshape(nrx_locs, len(self.rxlist))
        dpred_3D = dpred_3D.reshape(nrx_locs, len(self.rxlist))

        for i in range(len(self.rxlist)):
            rx = self.rxlist[i]
            normps = np.linalg.norm(dpred_primsec[:, i])
            norm3D = np.linalg.norm(dpred_3D[:, i])
            normdiff = np.linalg.norm(dpred_primsec[:, i] - dpred_3D[:, i])
            passed = normdiff < TOL_FWD * np.mean([normps, norm3D])
            print(
                "  Testing {rxfield}{rxorient} {rxcomp}...   "
                "prim-sec: {normps:10.5e}, 3D: {norm3D:10.5e}, "
                "diff: {diff:10.5e}, passed? {passed}".format(
                    rxfield=rx.projField,
                    rxorient=rx.orientation,
                    rxcomp=rx.component,
                    normps=normps,
                    norm3D=norm3D,
                    diff=normdiff,
                    passed=passed,
                )
            )

            self.assertTrue(passed)
        return True

    def JvecTest(self):
        print("\nTesting Jvec")
        x0 = model

        def fun(x):
            return [
                self.secondarySimulation.dpred(x),
                lambda x: self.secondarySimulation.Jvec(x0, x, f=self.fields_primsec),
            ]

        return tests.checkDerivative(fun, x0, num=2, plotIt=False)

    def AdjointTest(self):
        print("\nTesting adjoint")

        m = model
        f = self.fields_primsec
        v = np.random.rand(self.secondarySurvey.nD)
        w = np.random.rand(self.secondarySimulation.sigmaMap.nP)

        vJw = v.dot(self.secondarySimulation.Jvec(m, w, f))
        wJtv = w.dot(self.secondarySimulation.Jtvec(m, v, f))
        tol = np.max([TOL_JT * (10 ** int(np.log10(np.abs(vJw)))), FLR])
        passed = np.abs(vJw - wJtv) < tol
        print(
            " J: {}, JT: {}, diff: {}, tol: {}, passed? {}".format(
                vJw, wJtv, vJw - wJtv, tol, passed
            )
        )

        return passed

class PrimSecFDEMSrcTest_Cyl2Cart_EB_EB(unittest.TestCase, PrimSecFDEMTest):
    @classmethod
    def setUpClass(self):

        print("\n------- Testing Primary Secondary Source EB -> EB --------\n")
        # receivers
        self.rxlist = []
        for rxtype in ["MagneticFluxDensity", "ElectricField"]:
            rx = getattr(fdem.Rx, "Point{}".format(rxtype))
            for orientation in ["x", "y", "z"]:
                for comp in ["real", "imag"]:
                    self.rxlist.append(
                        rx(rx_locs, component=comp, orientation=orientation)
                    )

        # primary
        self.primarySimulation = fdem.Simulation3DMagneticFluxDensity(
            meshp, sigmaMap=primaryMapping
        )
        self.primarySimulation.solver = Solver
        primarySrc = fdem.Src.MagDipole(self.rxlist, frequency=freq, location=src_loc)
        self.primarySurvey = fdem.Survey([primarySrc])

        self.secondarySrc = fdem.Src.PrimSecMappedSigma(
            self.rxlist,
            freq,
            self.primarySimulation,
            self.primarySurvey,
            primaryMap2Meshs,
        )
        self.secondarySurvey = fdem.Survey([self.secondarySrc])
        # Secondary Problem
        self.secondarySimulation = fdem.Simulation3DMagneticFluxDensity(
            meshs, survey=self.secondarySurvey, sigmaMap=mapping
        )
        self.secondarySimulation.solver = Solver

        # Full 3D problem to compare with
        self.survey3D = fdem.Survey([primarySrc])

        self.simulation3D = fdem.Simulation3DMagneticFluxDensity(
            meshs, survey=self.survey3D, sigmaMap=mapping
        )
        self.simulation3D.solver = Solver

        # solve and store fields
        print("   solving primary - secondary")
        self.fields_primsec = self.secondarySimulation.fields(model)
        print("     ... done")

        self.fields_primsec = self.secondarySimulation.fields(model)
        print("   solving 3D")
        self.fields_3D = self.simulation3D.fields(model)
        print("     ... done")

        return None

    # --------------------- Run some tests! --------------------- #
    def test_data_EB(self):
        self.DataTest()

    def test_Jvec_EB(self):
        self.JvecTest()

    def test_Jadjoint_EB(self):
        self.AdjointTest()

class PrimSecFDEMSrcTest_Cyl2Cart_HJ_EB(unittest.TestCase, PrimSecFDEMTest):
    @classmethod
    def setUpClass(self):

        print("\n------- Testing Primary Secondary Source HJ -> EB --------\n")
        # receivers
        self.rxlist = []
        for rxtype in ["MagneticFluxDensity", "ElectricField"]:
            rx = getattr(fdem.Rx, "Point{}".format(rxtype))
            for orientation in ["x", "y", "z"]:
                for comp in ["real", "imag"]:
                    self.rxlist.append(
                        rx(rx_locs, component=comp, orientation=orientation)
                    )

        # primary
        self.primarySimulation = fdem.Simulation3DCurrentDensity(
            meshp, sigmaMap=primaryMapping
        )
        self.primarySimulation.solver = Solver
        s_e = np.zeros(meshp.nF)
        inds = meshp.nFx + utils.closestPoints(meshp, src_loc, gridLoc="Fz")
        s_e[inds] = 1.0 / csz
        primarySrc = fdem.Src.RawVec_e(
            self.rxlist, frequency=freq, s_e=s_e / meshp.area
        )
        self.primarySurvey = fdem.Survey([primarySrc])

        # Secondary Problem
        self.secondarySrc = fdem.Src.PrimSecMappedSigma(
            self.rxlist,
            freq,
            self.primarySimulation,
            self.primarySurvey,
            primaryMap2Meshs,
        )
        self.secondarySurvey = fdem.Survey([self.secondarySrc])

        self.secondarySimulation = fdem.Simulation3DElectricField(
            meshs, survey=self.secondarySurvey, sigmaMap=mapping,
        )
        self.secondarySimulation.solver = Solver

        # Full 3D problem to compare with

        s_e3D = np.zeros(meshs.nE)
        inds = meshs.nEx + meshs.nEy + utils.closestPoints(meshs, src_loc, gridLoc="Ez")
        s_e3D[inds] = [1.0 / (len(inds))] * len(inds)

        src3D = fdem.Src.RawVec_e(self.rxlist, frequency=freq, s_e=s_e3D)
        self.survey3D = fdem.Survey([src3D])

        self.simulation3D = fdem.Simulation3DElectricField(
            meshs, survey=self.survey3D, sigmaMap=mapping
        )
        self.simulation3D.solver = Solver
        self.simulation3D.model = model

        # solve and store fields
        print("   solving primary - secondary")
        self.fields_primsec = self.secondarySimulation.fields(model)
        print("     ... done")

        self.fields_primsec = self.secondarySimulation.fields(model)
        print("   solving 3D")
        self.fields_3D = self.simulation3D.fields(model)
        print("     ... done")

        return None

    # --------------------- Run some tests! --------------------- #
    def test_data_HJ(self):
        self.DataTest()

    def test_Jvec_HJ(self):
        self.JvecTest()

    def test_Jadjoint_HJ(self):
        self.AdjointTest()

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


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

import argparse
import csv
import os
import struct
import sys
import time

import threading
import Queue

import numpy as np

import pygame
import pygame.fastevent
from pygame.locals import *

import m3_common
from m3_logging import get_logger
logger = get_logger(__name__)

rotate_90 = True

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

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', default=None,
		help="Read image data from a file saved from a Saleae capture")
parser.add_argument('-s', '--serial', default=None,
		help="Snoop image data via an ICE board on this serial port")

parser.add_argument('-a', '--address',
		type=lambda x: int(x, 0),
		default=(0x17,),
		nargs='+',
		help="MBus address(es) image data is sent to")

parser.add_argument('-o', '--output-directory',
		default='goc-img-'+time.strftime('%Y-%m-%d--%H:%M:%S'),
		help="Save images into this directory")

parser.add_argument('-p', '--pixels', type=int, default=160,
		help="Number of pixels in a single row of the image")
parser.add_argument('-m', '--multiplier', type=float, default=1.2,
		help="Multiple each raw pixel value by this amount")
parser.add_argument('-l', '--scale', type=int, default=4,
		help="Multiplier to scale the image")

args = parser.parse_args()

if args.file is None and args.serial is None:
	print("Error: Must specify one of -f or -s")
	print("")
	parser.print_help()
	sys.exit(1)

if args.file is not None and args.serial is not None:
	print("Error: Can only specify one of -f or -s")
	print("")
	parser.print_help()
	sys.exit(1)

os.mkdir(args.output_directory)

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

pygame.init()
pygame.fastevent.init()

option_divider_height = 2 * args.scale
option_text_height = 20 * args.scale

window_width = args.pixels * args.scale
window_height = args.pixels  * args.scale + option_divider_height + option_text_height
windowSurfaceObj = pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption('GOC Test')

gocSurfaceObj = windowSurfaceObj.subsurface((0, 0, args.pixels * args.scale, args.pixels * args.scale))

RED   = pygame.Color(255,   0,   0)
GREEN = pygame.Color(  0, 255,   0)
BLUE  = pygame.Color(  0,   0, 255)
WHITE = pygame.Color(255, 255, 255)
BLACK = pygame.Color(  0,   0,   0)

colors = [pygame.Color(x,x,x) for x in xrange(256)]
colors.insert(0, RED)

class Option(object):
    hovered = False

    def __init__(self, text, pos):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()

    def __setattr__(self, name, value):
        if name == 'hovered':
            pygame.display.update(self.rect)
        object.__setattr__(self, name, value)

    def draw(self):
        self.set_rend()
        windowSurfaceObj.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = options_font.render(self.text, True, self.get_color())
        #self.rend = options_font.render(self.text, False, self.get_color())

    def get_color(self):
        if self.hovered:
            return (255, 255, 255)
        else:
            return (100, 100, 100)

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

    def onClick(self):
        try:
            self.on_click()
        except AttributeError:
            pass

options_font = pygame.font.Font(None, option_text_height)
options = {
        'prev' : Option("PREV", (10*args.scale + -10*args.scale, args.pixels*args.scale + option_divider_height)),
        'save' : Option("SAVE", (10*args.scale +  50*args.scale, args.pixels*args.scale + option_divider_height)),
        'next' : Option("NEXT", (10*args.scale + 110*args.scale, args.pixels*args.scale + option_divider_height)),
        }

options_divider = pygame.Rect(0, args.pixels*args.scale, window_width, option_divider_height)
windowSurfaceObj.fill(BLUE, options_divider)
################################################################################

def get_addr17_msg_file():
    data = []
    searching = True
    for line in open(args.file):
        line = line.strip()
        if searching:
            if line in map(lambda a: 'Address {:x}'.format(a), args.address):
                searching = False
            continue
        if line.split()[0] == 'Data':
            data.append(int(line.split()[1], 16))
            continue
        yield data
        data = []
        if line not in map(lambda a: 'Address {:x}'.format(a), args.address):
            searching = True

serial_queue = Queue.Queue()
def get_addr17_msg_serial():
    while True:
        addr, data = serial_queue.get()
        addr = int(addr.encode('hex'), 16)
        if addr in args.address:
            data = list(map(ord, data))
            yield data
        else:
            logger.debug("bad addr {:x}".format(addr))

def Bpp_callback(address, data):
    serial_queue.put((address, data))

class preparsed_snooper(m3_common.mbus_snooper):
    def __init__(self, args, *pyargs, **kwargs):
        self.args = args
        super(preparsed_snooper,self).__init__(*pyargs, **kwargs)

    def parse_args(self):
        self.serial_path = self.args.serial

if args.file:
    get_addr17_msg = get_addr17_msg_file
else:
    snooper = preparsed_snooper(args=args, callback=Bpp_callback)
    get_addr17_msg = get_addr17_msg_serial

def is_end_of_image_msg(m):
    if len(m) == 4:
        data = struct.unpack(">I", struct.pack("BBBB", *m))[0]
        data = data & 0x0000CFFF
        if data == 0:
            return True
    return False

# Decent guess for now
def is_motion_detect_msg(m):
    if len(m) == 4:
        if not is_end_of_image_msg(m):
            return True

def get_image_g(data_generator):
    class UnexpectedAddressException(Exception):
        pass

    while True:
        data = np.zeros((args.pixels,args.pixels), int)
        end_of_image = False

        # Grab an image
        for row in xrange(args.pixels):
            r = data_generator.next()
            while is_motion_detect_msg(r):
                print "Skipping motion detect message"
                r = data_generator.next()
            if is_end_of_image_msg(r):
                print "Unexpected end-of-image. Expecting row", row + 1
                print "Returning partial image"
                end_of_image = True
                break
            if len(r) != args.pixels:
                print "Row %d message incorrect length: %d" % (row, len(r))
                print "Using first %d pixel(s)" % (min(len(r), args.pixels))
            for p in xrange(min(len(r), args.pixels)):
                data[row][p] = r[p] + 1

        while not end_of_image:
            m = data_generator.next()
            if is_end_of_image_msg(m):
                break
            print "Expected end-of-image. Got message of length:", len(m)

        yield data

def correct_endianish_thing_old(data, array):
    for row in xrange(args.pixels):
        for colset in xrange(0, args.pixels, 4):
            for i in xrange(4):
                if rotate_90:
                    val = data[colset+3-i][row]
                else:
                    val = data[row][colset+3-i]
                val = int(val)
                if (val):
                    val -= 1
                    color = pygame.Color(val, val, val)
                    rgb = gocSurfaceObj.map_rgb(color)
                else:
                    rgb = gocSurfaceObj.map_rgb(RED)
                array[row][colset+i] = rgb

def correct_endianish_thing(data, array):
    for rowbase in xrange(0, args.pixels, 4):
        for rowi in xrange(4):
            for col in xrange(0, args.pixels):
                if rotate_90:
                    val = data[col][rowbase + 3-rowi]
                else:
                    val = data[rowbase + 3-rowi][col]
                val = int(val)
                val = int(val * args.multiplier)
                if (val):
                    val -= 1
                    color = pygame.Color(val, val, val)
                    rgb = gocSurfaceObj.map_rgb(color)
                else:
                    rgb = gocSurfaceObj.map_rgb(RED)
                array[rowbase+rowi][col] = rgb

images_q = Queue.Queue()

def get_images():
    if args.file:
        print("Processing static file")
        images_g = get_image_g(get_addr17_msg_file())
        for img in images_g:
            images_q.put(img)
        print("Done reading file")
        event = pygame.event.Event(pygame.USEREVENT)
        pygame.fastevent.post(event)
    elif args.serial:
        images_g = get_image_g(get_addr17_msg_serial())
        for img in images_g:
            print("GOT IMAGE")
            images_q.put(img)
            event = pygame.event.Event(pygame.USEREVENT)
            pygame.fastevent.post(event)
        print("ERR: Should never get here [serial image_g terminated]")

get_images_thread = threading.Thread(target=get_images)
get_images_thread.daemon = True
get_images_thread.start()

images = []
def get_image_idx(idx):
    global images
    while True:
        try:
            img = images_q.get_nowait()
            images.append(img)
        except Queue.Empty:
            break
    return images[idx]

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

pygame.event.set_allowed(None)
pygame.event.set_allowed((QUIT, KEYUP, MOUSEBUTTONUP, MOUSEMOTION))

goc_raw_Surface = pygame.Surface((args.pixels, args.pixels))

def render_raw_goc_data(data):
    print "Request to render raw goc data"
    print "Correcting pixel order bug"
    surface_array = pygame.surfarray.pixels2d(goc_raw_Surface)
    correct_endianish_thing(data, surface_array)
    del surface_array
    print "Scaling %d pixel --> %d pixel%s" % (1, args.scale, ('s','')[args.scale == 1])
    pygame.transform.scale(goc_raw_Surface, (args.pixels*args.scale, args.pixels*args.scale), gocSurfaceObj)
    print "Rendering Image"
    pygame.display.update()
    print

def render_image_idx(idx):
    print "Request to render image", idx
    render_raw_goc_data(get_image_idx(idx))

def save_image(filename):
    pygame.image.save(gocSurfaceObj, filename)
    print 'Image saved to', filename

def save_image_hack():
    imgname = "capture%02d.jpeg" % (current_idx)
    imgname = os.path.join(args.output_directory, imgname)
    save_image(imgname)
    csvname = "capture%02d.csv" % (current_idx)
    csvname = os.path.join(args.output_directory, csvname)
    ofile = csv.writer(open(csvname, 'w'), dialect='excel')
    ofile.writerows(get_image_idx(current_idx))
    print 'CSV of image saved to', csvname
options['save'].on_click = save_image_hack

def advance_image():
    global current_idx
    current_idx += 1
    try:
        render_image_idx(current_idx)
        save_image_hack()
    except IndexError:
        current_idx -= 1
        print "At last image. Display left at image", current_idx
        print
options['next'].on_click = advance_image

def rewind_image():
    global current_idx
    current_idx = max(0, current_idx-1)
    render_image_idx(current_idx)
options['prev'].on_click = rewind_image

def quit():
    pygame.quit()
    sys.exit()

current_idx = -1
advance_image()

while True:
    event = pygame.event.wait()

    if event.type == QUIT:
        quit()

    if event.type == MOUSEBUTTONUP:
        for option in options.values():
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.onClick()

    if event.type == MOUSEMOTION:
        for option in options.values():
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.hovered = True
            else:
                option.hovered = False
            option.draw()

    if event.type == KEYUP:
        if event.key == K_RIGHT:
            advance_image()
        elif event.key == K_LEFT:
            rewind_image()
        elif event.key == K_RETURN:
            save_image_hack()
        elif event.key == K_ESCAPE:
            quit()

    if event.type == USEREVENT:
        advance_image()

<commit_msg>: -> - for windows<commit_after>#!/usr/bin/env python2

import argparse
import csv
import os
import struct
import sys
import time

import threading
import Queue

import numpy as np

import pygame
import pygame.fastevent
from pygame.locals import *

import m3_common
from m3_logging import get_logger
logger = get_logger(__name__)

rotate_90 = True

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

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', default=None,
		help="Read image data from a file saved from a Saleae capture")
parser.add_argument('-s', '--serial', default=None,
		help="Snoop image data via an ICE board on this serial port")

parser.add_argument('-a', '--address',
		type=lambda x: int(x, 0),
		default=(0x17,),
		nargs='+',
		help="MBus address(es) image data is sent to")

parser.add_argument('-o', '--output-directory',
		default='goc-img-'+time.strftime('%Y-%m-%d--%H-%M-%S'),
		help="Save images into this directory")

parser.add_argument('-p', '--pixels', type=int, default=160,
		help="Number of pixels in a single row of the image")
parser.add_argument('-m', '--multiplier', type=float, default=1.2,
		help="Multiple each raw pixel value by this amount")
parser.add_argument('-l', '--scale', type=int, default=4,
		help="Multiplier to scale the image")

args = parser.parse_args()

if args.file is None and args.serial is None:
	print("Error: Must specify one of -f or -s")
	print("")
	parser.print_help()
	sys.exit(1)

if args.file is not None and args.serial is not None:
	print("Error: Can only specify one of -f or -s")
	print("")
	parser.print_help()
	sys.exit(1)

os.mkdir(args.output_directory)

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

pygame.init()
pygame.fastevent.init()

option_divider_height = 2 * args.scale
option_text_height = 20 * args.scale

window_width = args.pixels * args.scale
window_height = args.pixels  * args.scale + option_divider_height + option_text_height
windowSurfaceObj = pygame.display.set_mode((window_width,window_height))
pygame.display.set_caption('GOC Test')

gocSurfaceObj = windowSurfaceObj.subsurface((0, 0, args.pixels * args.scale, args.pixels * args.scale))

RED   = pygame.Color(255,   0,   0)
GREEN = pygame.Color(  0, 255,   0)
BLUE  = pygame.Color(  0,   0, 255)
WHITE = pygame.Color(255, 255, 255)
BLACK = pygame.Color(  0,   0,   0)

colors = [pygame.Color(x,x,x) for x in xrange(256)]
colors.insert(0, RED)

class Option(object):
    hovered = False

    def __init__(self, text, pos):
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()

    def __setattr__(self, name, value):
        if name == 'hovered':
            pygame.display.update(self.rect)
        object.__setattr__(self, name, value)

    def draw(self):
        self.set_rend()
        windowSurfaceObj.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = options_font.render(self.text, True, self.get_color())
        #self.rend = options_font.render(self.text, False, self.get_color())

    def get_color(self):
        if self.hovered:
            return (255, 255, 255)
        else:
            return (100, 100, 100)

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

    def onClick(self):
        try:
            self.on_click()
        except AttributeError:
            pass

options_font = pygame.font.Font(None, option_text_height)
options = {
        'prev' : Option("PREV", (10*args.scale + -10*args.scale, args.pixels*args.scale + option_divider_height)),
        'save' : Option("SAVE", (10*args.scale +  50*args.scale, args.pixels*args.scale + option_divider_height)),
        'next' : Option("NEXT", (10*args.scale + 110*args.scale, args.pixels*args.scale + option_divider_height)),
        }

options_divider = pygame.Rect(0, args.pixels*args.scale, window_width, option_divider_height)
windowSurfaceObj.fill(BLUE, options_divider)
################################################################################

def get_addr17_msg_file():
    data = []
    searching = True
    for line in open(args.file):
        line = line.strip()
        if searching:
            if line in map(lambda a: 'Address {:x}'.format(a), args.address):
                searching = False
            continue
        if line.split()[0] == 'Data':
            data.append(int(line.split()[1], 16))
            continue
        yield data
        data = []
        if line not in map(lambda a: 'Address {:x}'.format(a), args.address):
            searching = True

serial_queue = Queue.Queue()
def get_addr17_msg_serial():
    while True:
        addr, data = serial_queue.get()
        addr = int(addr.encode('hex'), 16)
        if addr in args.address:
            data = list(map(ord, data))
            yield data
        else:
            logger.debug("bad addr {:x}".format(addr))

def Bpp_callback(address, data):
    serial_queue.put((address, data))

class preparsed_snooper(m3_common.mbus_snooper):
    def __init__(self, args, *pyargs, **kwargs):
        self.args = args
        super(preparsed_snooper,self).__init__(*pyargs, **kwargs)

    def parse_args(self):
        self.serial_path = self.args.serial

if args.file:
    get_addr17_msg = get_addr17_msg_file
else:
    snooper = preparsed_snooper(args=args, callback=Bpp_callback)
    get_addr17_msg = get_addr17_msg_serial

def is_end_of_image_msg(m):
    if len(m) == 4:
        data = struct.unpack(">I", struct.pack("BBBB", *m))[0]
        data = data & 0x0000CFFF
        if data == 0:
            return True
    return False

# Decent guess for now
def is_motion_detect_msg(m):
    if len(m) == 4:
        if not is_end_of_image_msg(m):
            return True

def get_image_g(data_generator):
    class UnexpectedAddressException(Exception):
        pass

    while True:
        data = np.zeros((args.pixels,args.pixels), int)
        end_of_image = False

        # Grab an image
        for row in xrange(args.pixels):
            r = data_generator.next()
            while is_motion_detect_msg(r):
                print "Skipping motion detect message"
                r = data_generator.next()
            if is_end_of_image_msg(r):
                print "Unexpected end-of-image. Expecting row", row + 1
                print "Returning partial image"
                end_of_image = True
                break
            if len(r) != args.pixels:
                print "Row %d message incorrect length: %d" % (row, len(r))
                print "Using first %d pixel(s)" % (min(len(r), args.pixels))
            for p in xrange(min(len(r), args.pixels)):
                data[row][p] = r[p] + 1

        while not end_of_image:
            m = data_generator.next()
            if is_end_of_image_msg(m):
                break
            print "Expected end-of-image. Got message of length:", len(m)

        yield data

def correct_endianish_thing_old(data, array):
    for row in xrange(args.pixels):
        for colset in xrange(0, args.pixels, 4):
            for i in xrange(4):
                if rotate_90:
                    val = data[colset+3-i][row]
                else:
                    val = data[row][colset+3-i]
                val = int(val)
                if (val):
                    val -= 1
                    color = pygame.Color(val, val, val)
                    rgb = gocSurfaceObj.map_rgb(color)
                else:
                    rgb = gocSurfaceObj.map_rgb(RED)
                array[row][colset+i] = rgb

def correct_endianish_thing(data, array):
    for rowbase in xrange(0, args.pixels, 4):
        for rowi in xrange(4):
            for col in xrange(0, args.pixels):
                if rotate_90:
                    val = data[col][rowbase + 3-rowi]
                else:
                    val = data[rowbase + 3-rowi][col]
                val = int(val)
                val = int(val * args.multiplier)
                if (val):
                    val -= 1
                    color = pygame.Color(val, val, val)
                    rgb = gocSurfaceObj.map_rgb(color)
                else:
                    rgb = gocSurfaceObj.map_rgb(RED)
                array[rowbase+rowi][col] = rgb

images_q = Queue.Queue()

def get_images():
    if args.file:
        print("Processing static file")
        images_g = get_image_g(get_addr17_msg_file())
        for img in images_g:
            images_q.put(img)
        print("Done reading file")
        event = pygame.event.Event(pygame.USEREVENT)
        pygame.fastevent.post(event)
    elif args.serial:
        images_g = get_image_g(get_addr17_msg_serial())
        for img in images_g:
            print("GOT IMAGE")
            images_q.put(img)
            event = pygame.event.Event(pygame.USEREVENT)
            pygame.fastevent.post(event)
        print("ERR: Should never get here [serial image_g terminated]")

get_images_thread = threading.Thread(target=get_images)
get_images_thread.daemon = True
get_images_thread.start()

images = []
def get_image_idx(idx):
    global images
    while True:
        try:
            img = images_q.get_nowait()
            images.append(img)
        except Queue.Empty:
            break
    return images[idx]

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

pygame.event.set_allowed(None)
pygame.event.set_allowed((QUIT, KEYUP, MOUSEBUTTONUP, MOUSEMOTION))

goc_raw_Surface = pygame.Surface((args.pixels, args.pixels))

def render_raw_goc_data(data):
    print "Request to render raw goc data"
    print "Correcting pixel order bug"
    surface_array = pygame.surfarray.pixels2d(goc_raw_Surface)
    correct_endianish_thing(data, surface_array)
    del surface_array
    print "Scaling %d pixel --> %d pixel%s" % (1, args.scale, ('s','')[args.scale == 1])
    pygame.transform.scale(goc_raw_Surface, (args.pixels*args.scale, args.pixels*args.scale), gocSurfaceObj)
    print "Rendering Image"
    pygame.display.update()
    print

def render_image_idx(idx):
    print "Request to render image", idx
    render_raw_goc_data(get_image_idx(idx))

def save_image(filename):
    pygame.image.save(gocSurfaceObj, filename)
    print 'Image saved to', filename

def save_image_hack():
    imgname = "capture%02d.jpeg" % (current_idx)
    imgname = os.path.join(args.output_directory, imgname)
    save_image(imgname)
    csvname = "capture%02d.csv" % (current_idx)
    csvname = os.path.join(args.output_directory, csvname)
    ofile = csv.writer(open(csvname, 'w'), dialect='excel')
    ofile.writerows(get_image_idx(current_idx))
    print 'CSV of image saved to', csvname
options['save'].on_click = save_image_hack

def advance_image():
    global current_idx
    current_idx += 1
    try:
        render_image_idx(current_idx)
        save_image_hack()
    except IndexError:
        current_idx -= 1
        print "At last image. Display left at image", current_idx
        print
options['next'].on_click = advance_image

def rewind_image():
    global current_idx
    current_idx = max(0, current_idx-1)
    render_image_idx(current_idx)
options['prev'].on_click = rewind_image

def quit():
    pygame.quit()
    sys.exit()

current_idx = -1
advance_image()

while True:
    event = pygame.event.wait()

    if event.type == QUIT:
        quit()

    if event.type == MOUSEBUTTONUP:
        for option in options.values():
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.onClick()

    if event.type == MOUSEMOTION:
        for option in options.values():
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.hovered = True
            else:
                option.hovered = False
            option.draw()

    if event.type == KEYUP:
        if event.key == K_RIGHT:
            advance_image()
        elif event.key == K_LEFT:
            rewind_image()
        elif event.key == K_RETURN:
            save_image_hack()
        elif event.key == K_ESCAPE:
            quit()

    if event.type == USEREVENT:
        advance_image()

<|endoftext|>


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

# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
    This message tester lets a user input a Message event and a json test
    case, and allows the evaluation of the Message event based on the
    test case

    It is supposed to be run in the Mycroft virtualenv, and python-tk
    must be installed (on Ubuntu: apt-get install python-tk)
"""

from Tkinter import Label, Button, Tk, NORMAL, END, DISABLED
import ScrolledText
import skill_tester
import ast

EXAMPLE_EVENT = "{'expect_response': False, \
                'utterance': u'Recording audio for 600 seconds'}"

EXAMPLE_TEST_CASE = '{ \n\
  "utterance": "record", \n\
  "intent_type": "AudioRecordSkillIntent", \n\
  "intent": { \n\
    "AudioRecordSkillKeyword": "record" \n\
  }, \n\
  "expected_response": ".*(recording|audio)" \n\
}'

class MessageTester:
    def __init__(self, root):
        root.title("Message tester")
        Label(root, text="Enter message event below", bg="light green").pack()
        self.event_field = ScrolledText.ScrolledText(root,
                                                     width=180, height=10)
        self.event_field.pack()

        Label(root, text="Enter test case below", bg="light green").pack()
        self.test_case_field = ScrolledText.ScrolledText(root,
                                                         width=180, height=20)
        self.test_case_field.pack()

        Label(root, text="Test result:", bg="light green").pack()

        self.result_field = ScrolledText.ScrolledText(root,
                                                      width=180, height=10)
        self.result_field.pack()
        self.result_field.config(state=DISABLED)
        self.button = Button(root, text="Evaluate", fg="red",
                             command=self._clicked)
        self.button.pack()

        self.event_field.delete('1.0', END)
        self.event_field.insert('insert', EXAMPLE_EVENT)
        self.test_case_field.delete('1.0', END)
        self.test_case_field.insert('insert', EXAMPLE_TEST_CASE)

    def _clicked(self):
        event = self.event_field.get('1.0', END)
        test_case = self.test_case_field.get('1.0', END)

        evaluation = skill_tester.EvaluationRule(ast.literal_eval(test_case))
        evaluation.evaluate(ast.literal_eval(event))
        self.result_field.config(state=NORMAL)
        self.result_field.delete('1.0', END)
        self.result_field.insert('insert', evaluation.rule)
        self.result_field.config(state=DISABLED)

r = Tk()
app = MessageTester(r)
r.mainloop()
<commit_msg>Cleaner and better format for examples in message_tester<commit_after>#!/usr/bin/env python

# Copyright 2017 Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
    This message tester lets a user input a Message event and a json test
    case, and allows the evaluation of the Message event based on the
    test case

    It is supposed to be run in the Mycroft virtualenv, and python-tk
    must be installed (on Ubuntu: apt-get install python-tk)
"""

from Tkinter import Label, Button, Tk, NORMAL, END, DISABLED
import ScrolledText
import skill_tester
import ast

EXAMPLE_EVENT = '''{
  'expect_response': False,
  'utterance': u'Recording audio for 600 seconds'
}'''

EXAMPLE_TEST_CASE = '''{
  "utterance": "record",
  "intent_type": "AudioRecordSkillIntent",
  "intent": {
    "AudioRecordSkillKeyword": "record"
  },
  "expected_response": ".*(recording|audio)"
}'''

class MessageTester:
    def __init__(self, root):
        root.title("Message tester")
        Label(root, text="Enter message event below", bg="light green").pack()
        self.event_field = ScrolledText.ScrolledText(root,
                                                     width=180, height=10)
        self.event_field.pack()

        Label(root, text="Enter test case below", bg="light green").pack()
        self.test_case_field = ScrolledText.ScrolledText(root,
                                                         width=180, height=20)
        self.test_case_field.pack()

        Label(root, text="Test result:", bg="light green").pack()

        self.result_field = ScrolledText.ScrolledText(root,
                                                      width=180, height=10)
        self.result_field.pack()
        self.result_field.config(state=DISABLED)
        self.button = Button(root, text="Evaluate", fg="red",
                             command=self._clicked)
        self.button.pack()

        self.event_field.delete('1.0', END)
        self.event_field.insert('insert', EXAMPLE_EVENT)
        self.test_case_field.delete('1.0', END)
        self.test_case_field.insert('insert', EXAMPLE_TEST_CASE)

    def _clicked(self):
        event = self.event_field.get('1.0', END)
        test_case = self.test_case_field.get('1.0', END)

        evaluation = skill_tester.EvaluationRule(ast.literal_eval(test_case))
        evaluation.evaluate(ast.literal_eval(event))
        self.result_field.config(state=NORMAL)
        self.result_field.delete('1.0', END)
        self.result_field.insert('insert', evaluation.rule)
        self.result_field.config(state=DISABLED)

r = Tk()
app = MessageTester(r)
r.mainloop()
<|endoftext|>