<commit_before>#!/usr/bin/env python2
# Copyright (c) 2014-2016, NVIDIA CORPORATION.  All rights reserved.
from __future__ import absolute_import

import argparse
import ctypes
import platform

class c_cudaDeviceProp(ctypes.Structure):
    """
    Passed to cudart.cudaGetDeviceProperties()
    """
    _fields_ = [
            ('name', ctypes.c_char * 256),
            ('totalGlobalMem', ctypes.c_size_t),
            ('sharedMemPerBlock', ctypes.c_size_t),
            ('regsPerBlock', ctypes.c_int),
            ('warpSize', ctypes.c_int),
            ('memPitch', ctypes.c_size_t),
            ('maxThreadsPerBlock', ctypes.c_int),
            ('maxThreadsDim', ctypes.c_int * 3),
            ('maxGridSize', ctypes.c_int * 3),
            ('clockRate', ctypes.c_int),
            ('totalConstMem', ctypes.c_size_t),
            ('major', ctypes.c_int),
            ('minor', ctypes.c_int),
            ('textureAlignment', ctypes.c_size_t),
            ('texturePitchAlignment', ctypes.c_size_t),
            ('deviceOverlap', ctypes.c_int),
            ('multiProcessorCount', ctypes.c_int),
            ('kernelExecTimeoutEnabled', ctypes.c_int),
            ('integrated', ctypes.c_int),
            ('canMapHostMemory', ctypes.c_int),
            ('computeMode', ctypes.c_int),
            ('maxTexture1D', ctypes.c_int),
            ('maxTexture1DMipmap', ctypes.c_int),
            ('maxTexture1DLinear', ctypes.c_int),
            ('maxTexture2D', ctypes.c_int * 2),
            ('maxTexture2DMipmap', ctypes.c_int * 2),
            ('maxTexture2DLinear', ctypes.c_int * 3),
            ('maxTexture2DGather', ctypes.c_int * 2),
            ('maxTexture3D', ctypes.c_int * 3),
            ('maxTexture3DAlt', ctypes.c_int * 3),
            ('maxTextureCubemap', ctypes.c_int),
            ('maxTexture1DLayered', ctypes.c_int * 2),
            ('maxTexture2DLayered', ctypes.c_int * 3),
            ('maxTextureCubemapLayered', ctypes.c_int * 2),
            ('maxSurface1D', ctypes.c_int),
            ('maxSurface2D', ctypes.c_int * 2),
            ('maxSurface3D', ctypes.c_int * 3),
            ('maxSurface1DLayered', ctypes.c_int * 2),
            ('maxSurface2DLayered', ctypes.c_int * 3),
            ('maxSurfaceCubemap', ctypes.c_int),
            ('maxSurfaceCubemapLayered', ctypes.c_int * 2),
            ('surfaceAlignment', ctypes.c_size_t),
            ('concurrentKernels', ctypes.c_int),
            ('ECCEnabled', ctypes.c_int),
            ('pciBusID', ctypes.c_int),
            ('pciDeviceID', ctypes.c_int),
            ('pciDomainID', ctypes.c_int),
            ('tccDriver', ctypes.c_int),
            ('asyncEngineCount', ctypes.c_int),
            ('unifiedAddressing', ctypes.c_int),
            ('memoryClockRate', ctypes.c_int),
            ('memoryBusWidth', ctypes.c_int),
            ('l2CacheSize', ctypes.c_int),
            ('maxThreadsPerMultiProcessor', ctypes.c_int),
            ('streamPrioritiesSupported', ctypes.c_int),
            ('globalL1CacheSupported', ctypes.c_int),
            ('localL1CacheSupported', ctypes.c_int),
            ('sharedMemPerMultiprocessor', ctypes.c_size_t),
            ('regsPerMultiprocessor', ctypes.c_int),
            ('managedMemSupported', ctypes.c_int),
            ('isMultiGpuBoard', ctypes.c_int),
            ('multiGpuBoardGroupID', ctypes.c_int),
            # Extra space for new fields in future toolkits
            ('__future_buffer', ctypes.c_int * 128),
            # added later with cudart.cudaDeviceGetPCIBusId
            # (needed by NVML)
            ('pciBusID_str', ctypes.c_char * 16),
            ]

class struct_c_nvmlDevice_t(ctypes.Structure):
    """
    Handle to a device in NVML
    """
    pass # opaque handle
c_nvmlDevice_t = ctypes.POINTER(struct_c_nvmlDevice_t)

class c_nvmlMemory_t(ctypes.Structure):
    """
    Passed to nvml.nvmlDeviceGetMemoryInfo()
    """
    _fields_ = [
            ('total', ctypes.c_ulonglong),
            ('free', ctypes.c_ulonglong),
            ('used', ctypes.c_ulonglong),
            # Extra space for new fields in future toolkits
            ('__future_buffer', ctypes.c_ulonglong * 8),
            ]

class c_nvmlUtilization_t(ctypes.Structure):
    """
    Passed to nvml.nvmlDeviceGetUtilizationRates()
    """
    _fields_ = [
            ('gpu', ctypes.c_uint),
            ('memory', ctypes.c_uint),
            # Extra space for new fields in future toolkits
            ('__future_buffer', ctypes.c_uint * 8),
            ]

def get_library(name):
    """
    Returns a ctypes.CDLL or None
    """
    try:
        if platform.system() == 'Windows':
            return ctypes.windll.LoadLibrary(name)
        else:
            return ctypes.cdll.LoadLibrary(name)
    except OSError:
        pass
    return None

def get_cudart():
    """
    Return the ctypes.DLL object for cudart or None
    """
    if platform.system() == 'Windows':
        arch = platform.architecture()[0]
        for ver in range(90,50,-5):
            cudart = get_library('cudart%s_%d.dll' % (arch[:2], ver))
            if cudart is not None:
                return cudart
    else:
        for major in xrange(9,5,-1):
            for minor in (5,0):
                cudart = get_library('libcudart.so.%d.%d' % (major, minor))
                if cudart is not None:
                    return cudart
        return get_library('libcudart.so')
    return None

def get_nvml():
    """
    Return the ctypes.DLL object for cudart or None
    """
    if platform.system() == 'Windows':
        return get_library('nvml.dll')
    else:
        for name in (
                'libnvidia-ml.so.1',
                'libnvidia-ml.so',
                'nvml.so'):
            nvml = get_library(name)
            if nvml is not None:
                return nvml
    return None

devices = None

def get_devices(force_reload=False):
    """
    Returns a list of c_cudaDeviceProp's
    Prints an error and returns None if something goes wrong

    Keyword arguments:
    force_reload -- if False, return the previously loaded list of devices
    """
    global devices
    if not force_reload and devices is not None:
        # Only query CUDA once
        return devices
    devices = []

    cudart = get_cudart()
    if cudart is None:
        return []

    # check CUDA version
    cuda_version = ctypes.c_int()
    rc = cudart.cudaRuntimeGetVersion(ctypes.byref(cuda_version))
    if rc != 0:
        print 'cudaRuntimeGetVersion() failed with error #%s' % rc
        return []
    if cuda_version.value < 6050:
        print 'ERROR: Cuda version must be >= 6.5, not "%s"' % cuda_version.value
        return []

    # get number of devices
    num_devices = ctypes.c_int()
    rc = cudart.cudaGetDeviceCount(ctypes.byref(num_devices))
    if rc != 0:
        print 'cudaGetDeviceCount() failed with error #%s' % rc
        return []

    # query devices
    for x in xrange(num_devices.value):
        properties = c_cudaDeviceProp()
        rc = cudart.cudaGetDeviceProperties(ctypes.byref(properties), x)
        if rc == 0:
            pciBusID_str = ' ' * 16
            # also save the string representation of the PCI bus ID
            rc = cudart.cudaDeviceGetPCIBusId(ctypes.c_char_p(pciBusID_str), 16, x)
            if rc == 0:
                properties.pciBusID_str = pciBusID_str
            devices.append(properties)
        else:
            print 'cudaGetDeviceProperties() failed with error #%s' % rc
        del properties
    return devices

def get_device(device_id):
    """
    Returns a c_cudaDeviceProp
    """
    return get_devices()[int(device_id)]

def get_nvml_info(device_id):
    """
    Gets info from NVML for the given device
    Returns a dict of dicts from different NVML functions
    """
    device = get_device(device_id)
    if device is None:
        return None

    nvml = get_nvml()
    if nvml is None:
        return None

    rc = nvml.nvmlInit()
    if rc != 0:
        raise RuntimeError('nvmlInit() failed with error #%s' % rc)

    try:
        # get device handle
        handle = c_nvmlDevice_t()
        rc = nvml.nvmlDeviceGetHandleByPciBusId(ctypes.c_char_p(device.pciBusID_str), ctypes.byref(handle))
        if rc != 0:
            raise RuntimeError('nvmlDeviceGetHandleByPciBusId() failed with error #%s' % rc)

        # Grab info for this device from NVML
        info = {}

        memory = c_nvmlMemory_t()
        rc = nvml.nvmlDeviceGetMemoryInfo(handle, ctypes.byref(memory))
        if rc == 0:
            info['memory'] = {
                    'total': memory.total,
                    'used': memory.used,
                    'free': memory.free,
                    }

        utilization = c_nvmlUtilization_t()
        rc = nvml.nvmlDeviceGetUtilizationRates(handle, ctypes.byref(utilization))
        if rc == 0:
            info['utilization'] = {
                    'gpu': utilization.gpu,
                    'memory': utilization.memory, # redundant
                    }

        temperature = ctypes.c_int()
        rc = nvml.nvmlDeviceGetTemperature(handle, 0, ctypes.byref(temperature))
        if rc == 0:
            info['temperature'] = temperature.value

        return info
    finally:
        rc = nvml.nvmlShutdown()
        if rc != 0:
            pass

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='DIGITS Device Query')
    parser.add_argument('-v', '--verbose', action='store_true')
    args = parser.parse_args()

    if not len(get_devices()):
        print 'No devices found.'

    for i, device in enumerate(get_devices()):
        print 'Device #%d:' % i
        print '>>> CUDA attributes:'
        for name, t in device._fields_:
            if name in ['__future_buffer']:
                continue
            if not args.verbose and name not in [
                    'name', 'totalGlobalMem', 'clockRate', 'major', 'minor',]:
                continue

            if 'c_int_Array' in t.__name__:
                val = ','.join(str(v) for v in getattr(device, name))
            else:
                val = getattr(device, name)

            print '  %-28s %s' % (name, val)

        info = get_nvml_info(i)
        if info is not None:
            print '>>> NVML attributes:'
            nvml_fmt = '  %-28s %s'
            if 'memory' in info:
                print nvml_fmt % ('Total memory',
                        '%s MB' % (info['memory']['total'] / 2**20,))
                print nvml_fmt % ('Used memory',
                        '%s MB' % (info['memory']['used'] / 2**20,))
                if args.verbose:
                    print nvml_fmt % ('Free memory',
                        '%s MB' % (info['memory']['free'] / 2**20,))
            if 'utilization' in info:
                print nvml_fmt % ('Memory utilization',
                        '%s%%' % info['utilization']['memory'])
                print nvml_fmt % ('GPU utilization',
                        '%s%%' % info['utilization']['gpu'])
            if 'temperature' in info:
                print nvml_fmt % ('Temperature',
                        '%s C' % info['temperature'])
        print

<commit_msg>fix cudart load on osx<commit_after>#!/usr/bin/env python2
# Copyright (c) 2014-2016, NVIDIA CORPORATION.  All rights reserved.
from __future__ import absolute_import

import argparse
import ctypes
import platform

class c_cudaDeviceProp(ctypes.Structure):
    """
    Passed to cudart.cudaGetDeviceProperties()
    """
    _fields_ = [
            ('name', ctypes.c_char * 256),
            ('totalGlobalMem', ctypes.c_size_t),
            ('sharedMemPerBlock', ctypes.c_size_t),
            ('regsPerBlock', ctypes.c_int),
            ('warpSize', ctypes.c_int),
            ('memPitch', ctypes.c_size_t),
            ('maxThreadsPerBlock', ctypes.c_int),
            ('maxThreadsDim', ctypes.c_int * 3),
            ('maxGridSize', ctypes.c_int * 3),
            ('clockRate', ctypes.c_int),
            ('totalConstMem', ctypes.c_size_t),
            ('major', ctypes.c_int),
            ('minor', ctypes.c_int),
            ('textureAlignment', ctypes.c_size_t),
            ('texturePitchAlignment', ctypes.c_size_t),
            ('deviceOverlap', ctypes.c_int),
            ('multiProcessorCount', ctypes.c_int),
            ('kernelExecTimeoutEnabled', ctypes.c_int),
            ('integrated', ctypes.c_int),
            ('canMapHostMemory', ctypes.c_int),
            ('computeMode', ctypes.c_int),
            ('maxTexture1D', ctypes.c_int),
            ('maxTexture1DMipmap', ctypes.c_int),
            ('maxTexture1DLinear', ctypes.c_int),
            ('maxTexture2D', ctypes.c_int * 2),
            ('maxTexture2DMipmap', ctypes.c_int * 2),
            ('maxTexture2DLinear', ctypes.c_int * 3),
            ('maxTexture2DGather', ctypes.c_int * 2),
            ('maxTexture3D', ctypes.c_int * 3),
            ('maxTexture3DAlt', ctypes.c_int * 3),
            ('maxTextureCubemap', ctypes.c_int),
            ('maxTexture1DLayered', ctypes.c_int * 2),
            ('maxTexture2DLayered', ctypes.c_int * 3),
            ('maxTextureCubemapLayered', ctypes.c_int * 2),
            ('maxSurface1D', ctypes.c_int),
            ('maxSurface2D', ctypes.c_int * 2),
            ('maxSurface3D', ctypes.c_int * 3),
            ('maxSurface1DLayered', ctypes.c_int * 2),
            ('maxSurface2DLayered', ctypes.c_int * 3),
            ('maxSurfaceCubemap', ctypes.c_int),
            ('maxSurfaceCubemapLayered', ctypes.c_int * 2),
            ('surfaceAlignment', ctypes.c_size_t),
            ('concurrentKernels', ctypes.c_int),
            ('ECCEnabled', ctypes.c_int),
            ('pciBusID', ctypes.c_int),
            ('pciDeviceID', ctypes.c_int),
            ('pciDomainID', ctypes.c_int),
            ('tccDriver', ctypes.c_int),
            ('asyncEngineCount', ctypes.c_int),
            ('unifiedAddressing', ctypes.c_int),
            ('memoryClockRate', ctypes.c_int),
            ('memoryBusWidth', ctypes.c_int),
            ('l2CacheSize', ctypes.c_int),
            ('maxThreadsPerMultiProcessor', ctypes.c_int),
            ('streamPrioritiesSupported', ctypes.c_int),
            ('globalL1CacheSupported', ctypes.c_int),
            ('localL1CacheSupported', ctypes.c_int),
            ('sharedMemPerMultiprocessor', ctypes.c_size_t),
            ('regsPerMultiprocessor', ctypes.c_int),
            ('managedMemSupported', ctypes.c_int),
            ('isMultiGpuBoard', ctypes.c_int),
            ('multiGpuBoardGroupID', ctypes.c_int),
            # Extra space for new fields in future toolkits
            ('__future_buffer', ctypes.c_int * 128),
            # added later with cudart.cudaDeviceGetPCIBusId
            # (needed by NVML)
            ('pciBusID_str', ctypes.c_char * 16),
            ]

class struct_c_nvmlDevice_t(ctypes.Structure):
    """
    Handle to a device in NVML
    """
    pass # opaque handle
c_nvmlDevice_t = ctypes.POINTER(struct_c_nvmlDevice_t)

class c_nvmlMemory_t(ctypes.Structure):
    """
    Passed to nvml.nvmlDeviceGetMemoryInfo()
    """
    _fields_ = [
            ('total', ctypes.c_ulonglong),
            ('free', ctypes.c_ulonglong),
            ('used', ctypes.c_ulonglong),
            # Extra space for new fields in future toolkits
            ('__future_buffer', ctypes.c_ulonglong * 8),
            ]

class c_nvmlUtilization_t(ctypes.Structure):
    """
    Passed to nvml.nvmlDeviceGetUtilizationRates()
    """
    _fields_ = [
            ('gpu', ctypes.c_uint),
            ('memory', ctypes.c_uint),
            # Extra space for new fields in future toolkits
            ('__future_buffer', ctypes.c_uint * 8),
            ]

def get_library(name):
    """
    Returns a ctypes.CDLL or None
    """
    try:
        if platform.system() == 'Windows':
            return ctypes.windll.LoadLibrary(name)
        else:
            return ctypes.cdll.LoadLibrary(name)
    except OSError:
        pass
    return None

def get_cudart():
    """
    Return the ctypes.DLL object for cudart or None
    """
    if platform.system() == 'Windows':
        arch = platform.architecture()[0]
        for ver in range(90,50,-5):
            cudart = get_library('cudart%s_%d.dll' % (arch[:2], ver))
            if cudart is not None:
                return cudart
    elif platform.system() == 'Darwin':
        for major in xrange(9,5,-1):
            for minor in (5,0):
                cudart = get_library('libcudart.%d.%d.dylib' % (major, minor))
                if cudart is not None:
                    return cudart
        return get_library('libcudart.dylib')
    else:
        for major in xrange(9,5,-1):
            for minor in (5,0):
                cudart = get_library('libcudart.so.%d.%d' % (major, minor))
                if cudart is not None:
                    return cudart
        return get_library('libcudart.so')
    return None

def get_nvml():
    """
    Return the ctypes.DLL object for cudart or None
    """
    if platform.system() == 'Windows':
        return get_library('nvml.dll')
    else:
        for name in (
                'libnvidia-ml.so.1',
                'libnvidia-ml.so',
                'nvml.so'):
            nvml = get_library(name)
            if nvml is not None:
                return nvml
    return None

devices = None

def get_devices(force_reload=False):
    """
    Returns a list of c_cudaDeviceProp's
    Prints an error and returns None if something goes wrong

    Keyword arguments:
    force_reload -- if False, return the previously loaded list of devices
    """
    global devices
    if not force_reload and devices is not None:
        # Only query CUDA once
        return devices
    devices = []

    cudart = get_cudart()
    if cudart is None:
        return []

    # check CUDA version
    cuda_version = ctypes.c_int()
    rc = cudart.cudaRuntimeGetVersion(ctypes.byref(cuda_version))
    if rc != 0:
        print 'cudaRuntimeGetVersion() failed with error #%s' % rc
        return []
    if cuda_version.value < 6050:
        print 'ERROR: Cuda version must be >= 6.5, not "%s"' % cuda_version.value
        return []

    # get number of devices
    num_devices = ctypes.c_int()
    rc = cudart.cudaGetDeviceCount(ctypes.byref(num_devices))
    if rc != 0:
        print 'cudaGetDeviceCount() failed with error #%s' % rc
        return []

    # query devices
    for x in xrange(num_devices.value):
        properties = c_cudaDeviceProp()
        rc = cudart.cudaGetDeviceProperties(ctypes.byref(properties), x)
        if rc == 0:
            pciBusID_str = ' ' * 16
            # also save the string representation of the PCI bus ID
            rc = cudart.cudaDeviceGetPCIBusId(ctypes.c_char_p(pciBusID_str), 16, x)
            if rc == 0:
                properties.pciBusID_str = pciBusID_str
            devices.append(properties)
        else:
            print 'cudaGetDeviceProperties() failed with error #%s' % rc
        del properties
    return devices

def get_device(device_id):
    """
    Returns a c_cudaDeviceProp
    """
    return get_devices()[int(device_id)]

def get_nvml_info(device_id):
    """
    Gets info from NVML for the given device
    Returns a dict of dicts from different NVML functions
    """
    device = get_device(device_id)
    if device is None:
        return None

    nvml = get_nvml()
    if nvml is None:
        return None

    rc = nvml.nvmlInit()
    if rc != 0:
        raise RuntimeError('nvmlInit() failed with error #%s' % rc)

    try:
        # get device handle
        handle = c_nvmlDevice_t()
        rc = nvml.nvmlDeviceGetHandleByPciBusId(ctypes.c_char_p(device.pciBusID_str), ctypes.byref(handle))
        if rc != 0:
            raise RuntimeError('nvmlDeviceGetHandleByPciBusId() failed with error #%s' % rc)

        # Grab info for this device from NVML
        info = {}

        memory = c_nvmlMemory_t()
        rc = nvml.nvmlDeviceGetMemoryInfo(handle, ctypes.byref(memory))
        if rc == 0:
            info['memory'] = {
                    'total': memory.total,
                    'used': memory.used,
                    'free': memory.free,
                    }

        utilization = c_nvmlUtilization_t()
        rc = nvml.nvmlDeviceGetUtilizationRates(handle, ctypes.byref(utilization))
        if rc == 0:
            info['utilization'] = {
                    'gpu': utilization.gpu,
                    'memory': utilization.memory, # redundant
                    }

        temperature = ctypes.c_int()
        rc = nvml.nvmlDeviceGetTemperature(handle, 0, ctypes.byref(temperature))
        if rc == 0:
            info['temperature'] = temperature.value

        return info
    finally:
        rc = nvml.nvmlShutdown()
        if rc != 0:
            pass

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='DIGITS Device Query')
    parser.add_argument('-v', '--verbose', action='store_true')
    args = parser.parse_args()

    if not len(get_devices()):
        print 'No devices found.'

    for i, device in enumerate(get_devices()):
        print 'Device #%d:' % i
        print '>>> CUDA attributes:'
        for name, t in device._fields_:
            if name in ['__future_buffer']:
                continue
            if not args.verbose and name not in [
                    'name', 'totalGlobalMem', 'clockRate', 'major', 'minor',]:
                continue

            if 'c_int_Array' in t.__name__:
                val = ','.join(str(v) for v in getattr(device, name))
            else:
                val = getattr(device, name)

            print '  %-28s %s' % (name, val)

        info = get_nvml_info(i)
        if info is not None:
            print '>>> NVML attributes:'
            nvml_fmt = '  %-28s %s'
            if 'memory' in info:
                print nvml_fmt % ('Total memory',
                        '%s MB' % (info['memory']['total'] / 2**20,))
                print nvml_fmt % ('Used memory',
                        '%s MB' % (info['memory']['used'] / 2**20,))
                if args.verbose:
                    print nvml_fmt % ('Free memory',
                        '%s MB' % (info['memory']['free'] / 2**20,))
            if 'utilization' in info:
                print nvml_fmt % ('Memory utilization',
                        '%s%%' % info['utilization']['memory'])
                print nvml_fmt % ('GPU utilization',
                        '%s%%' % info['utilization']['gpu'])
            if 'temperature' in info:
                print nvml_fmt % ('Temperature',
                        '%s C' % info['temperature'])
        print

<|endoftext|>


<commit_before># Dogecoin Ticker Twitter Bot
# Written by Damen Knight
# Copyright 2014
from __future__ import print_function
import json
import urllib2
import os

cryptsyresponse = urllib2.urlopen('http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132')
cryptsydata = json.load(cryptsyresponse)
vircurexurl = 'https://api.vircurex.com/api/get_highest_bid.json?base=DOGE&alt=BTC'
vircurexvolurl = 'https://api.vircurex.com/api/get_volume.json?base=DOGE&alt=BTC'
vircurexreq = urllib2.Request(vircurexurl, headers={"User-Agent" : "DogeCoin Ticker"})
vircurexvolreq = urllib2.Request(vircurexvolurl, headers={"User-Agent" : "DogeCoin Ticker"})
vircurexresponse = urllib2.urlopen(vircurexreq)
vircurexvolresponse = urllib2.urlopen(vircurexvolreq)
vircurexdata = json.load(vircurexresponse)
vircurexvoldata = json.load(vircurexvolresponse)
coinseresponse = urllib2.urlopen('https://www.coins-e.com/api/v2/market/DOGE_BTC/depth/')
coinsedata = json.load(coinseresponse)
bterurl='http://data.bter.com/api/1/ticker/doge_btc'
bterreq = urllib2.Request(bterurl, headers={"User-Agent" : "DogeCoin Ticker"})
bterresponse = urllib2.urlopen(bterreq)
bterdata = json.load(bterresponse)
coinbaseresponse = urllib2.urlopen('https://coinbase.com/api/v1/prices/buy')
coinbasedata = json.load(coinbaseresponse)
mintpalresponse = urllib2.urlopen('https://api.mintpal.com/v1/market/stats/DOGE/BTC')
mintpaldata = json.load(mintpalresponse)

f2 = open('/path/to/cryptsytrend.txt','r')
f2v = f2.readline()
f2.close()

f3 = open('/path/to/vircurextrend.txt','r')
f3v = f3.readline()
f3.close()

f4 = open('/path/to/coinsetrend.txt','r')
f4v = f4.readline()
f4.close()

f5 = open('/path/to/btertrend.txt','r')
f5v = f5.readline()
f5.close()

f6 = open('/path/to/mintpaltrend.txt','r')
f6v = f6.readline()
f6.close()

if f2v < cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]:
	cryptsytrend = "up"
if f2v == cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]:
	cryptsytrend = "stable"
if f2v > cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]:
	cryptsytrend = "down"

if f3v < vircurexdata["value"]:
	vircurextrend = "up"
if f3v == vircurexdata["value"]:
	vircurextrend = "stable"
if f3v > vircurexdata["value"]:
	vircurextrend = "down"

if f4v < coinsedata["bid"]:
	coinsetrend = "up"
if f4v == coinsedata["bid"]:
	coinsetrend = "stable"
if f4v > coinsedata["bid"]:
	coinsetrend = "down"

if f5v < bterdata["last"]:
	btertrend = "up"
if f5v == bterdata["last"]:
	btertrend = "stable"
if f5v > bterdata["last"]:
	btertrend = "down"

if f6v < mintpaldata[0]["last_price"]:
	mintpaltrend = "up"
if f6v == mintpaldata[0]["last_price"]:
	mintpaltrend = "stable"
if f6v > mintpaldata[0]["last_price"]:
	mintpaltrend = "down"

lowest = cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]
lowexchange = "cryptsy"
if vircurexdata["value"] < lowest:
        lowest = vircurexdata["value"]
        lowexchange = "vircurex"
if coinsedata["bid"] < lowest:
        lowest = coinsedata["bid"]
        lowexchange = "coins-e"
if bterdata["last"] < lowest:
        lowest = bterdata["last"]
        lowexchange = "bter"
if mintpaldata[0]["last_price"] < lowest:
        lowest = mintpaldata[0]["last_price"]
        lowexchange = "mintpal"

f = open('/path/to/coinbasevalue.txt','w')
f.write('Average Dogecoin value' + lowest + '\n')
milliondoge = float(lowest)*1000000
dogeinusd = milliondoge * float(coinbasedata['subtotal']['amount'])
f.write('value of 1m DOGE in USD' + str(dogeinusd) + '\n')
f.close()

f = open('/root/dogecointicker/dogecoinvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' Cryptsy: ' + cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"] + " -- Volume: " + cryptsydata["return"]["markets"]["DOGE"]["volume"] + " Today's trend: " + cryptsytrend + " \n")
f.write('Current value of DOGE in BTC:' +' Vircurex: '+ vircurexdata["value"] + " -- Volume: " + vircurexvoldata["value"] + " Today's trend: " + vircurextrend + " \n")
f.write('Current value of DOGE in BTC:' +' COINS-E: ' + coinsedata["bid"] + " -- Volume: " + coinsedata["total_ask_q"] + " Today's trend: " + coinsetrend + " \n")
f.write('Current value of DOGE in BTC:' +' BTER: ' + bterdata["last"] + " -- Volume: " + str(bterdata["vol_doge"]) + " Today's trend: " + btertrend + " \n")
f.write('Current value of DOGE in BTC:' +' mintpal: ' + mintpaldata[0]["last_price"] + " -- Volume: Unknown" + " Today's trend: " + mintpaltrend + " \n")
f.write('Current approx. value of 1M DOGE in USD: ' + str(dogeinusd) + " values from " + lowexchange + " + coinbase" + " #dogecoin \n")
f.write('Direct message me with the name of one of the exchanges I check to get more current information \n')
f.close()

f = open('/path/to/cryptsyvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' Cryptsy: ' + cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"] + " -- Volume: " + cryptsydata["return"]["markets"]["DOGE"]["volume"] + " Today's trend: " + cryptsytrend)
f.close()

f = open('/path/to/vircurexvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' Vircurex: '+ vircurexdata["value"] + " -- Volume: " + vircurexvoldata["value"] + " Today's trend: " + vircurextrend)
f.close()

f = open('/path/to/coinsevalue.txt','w')
f.write('Current value of DOGE in BTC:' +' COINS-E: ' + coinsedata["bid"] + " -- Volume: " + coinsedata["total_ask_q"] + " Today's trend: " + coinsetrend)
f.close()

f = open('/path/to/btervalue.txt','w')
f.write('Current value of DOGE in BTC:' +' BTER: ' + bterdata["last"] + " -- Volume: " + str(bterdata["vol_doge"]) + " Today's trend: " + btertrend)
f.close()

f = open('/path/to/mintpalvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' mintpal: ' + mintpaldata[0]["last_price"] + " -- Volume: Unknown" + " Today's trend: " + mintpaltrend)
f.close()
<commit_msg>added a dollar sign<commit_after># Dogecoin Ticker Twitter Bot
# Written by Damen Knight
# Copyright 2014
from __future__ import print_function
import json
import urllib2
import os

cryptsyresponse = urllib2.urlopen('http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=132')
cryptsydata = json.load(cryptsyresponse)
vircurexurl = 'https://api.vircurex.com/api/get_highest_bid.json?base=DOGE&alt=BTC'
vircurexvolurl = 'https://api.vircurex.com/api/get_volume.json?base=DOGE&alt=BTC'
vircurexreq = urllib2.Request(vircurexurl, headers={"User-Agent" : "DogeCoin Ticker"})
vircurexvolreq = urllib2.Request(vircurexvolurl, headers={"User-Agent" : "DogeCoin Ticker"})
vircurexresponse = urllib2.urlopen(vircurexreq)
vircurexvolresponse = urllib2.urlopen(vircurexvolreq)
vircurexdata = json.load(vircurexresponse)
vircurexvoldata = json.load(vircurexvolresponse)
coinseresponse = urllib2.urlopen('https://www.coins-e.com/api/v2/market/DOGE_BTC/depth/')
coinsedata = json.load(coinseresponse)
bterurl='http://data.bter.com/api/1/ticker/doge_btc'
bterreq = urllib2.Request(bterurl, headers={"User-Agent" : "DogeCoin Ticker"})
bterresponse = urllib2.urlopen(bterreq)
bterdata = json.load(bterresponse)
coinbaseresponse = urllib2.urlopen('https://coinbase.com/api/v1/prices/buy')
coinbasedata = json.load(coinbaseresponse)
mintpalresponse = urllib2.urlopen('https://api.mintpal.com/v1/market/stats/DOGE/BTC')
mintpaldata = json.load(mintpalresponse)

f2 = open('/path/to/cryptsytrend.txt','r')
f2v = f2.readline()
f2.close()

f3 = open('/path/to/vircurextrend.txt','r')
f3v = f3.readline()
f3.close()

f4 = open('/path/to/coinsetrend.txt','r')
f4v = f4.readline()
f4.close()

f5 = open('/path/to/btertrend.txt','r')
f5v = f5.readline()
f5.close()

f6 = open('/path/to/mintpaltrend.txt','r')
f6v = f6.readline()
f6.close()

if f2v < cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]:
	cryptsytrend = "up"
if f2v == cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]:
	cryptsytrend = "stable"
if f2v > cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]:
	cryptsytrend = "down"

if f3v < vircurexdata["value"]:
	vircurextrend = "up"
if f3v == vircurexdata["value"]:
	vircurextrend = "stable"
if f3v > vircurexdata["value"]:
	vircurextrend = "down"

if f4v < coinsedata["bid"]:
	coinsetrend = "up"
if f4v == coinsedata["bid"]:
	coinsetrend = "stable"
if f4v > coinsedata["bid"]:
	coinsetrend = "down"

if f5v < bterdata["last"]:
	btertrend = "up"
if f5v == bterdata["last"]:
	btertrend = "stable"
if f5v > bterdata["last"]:
	btertrend = "down"

if f6v < mintpaldata[0]["last_price"]:
	mintpaltrend = "up"
if f6v == mintpaldata[0]["last_price"]:
	mintpaltrend = "stable"
if f6v > mintpaldata[0]["last_price"]:
	mintpaltrend = "down"

lowest = cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"]
lowexchange = "cryptsy"
if vircurexdata["value"] < lowest:
        lowest = vircurexdata["value"]
        lowexchange = "vircurex"
if coinsedata["bid"] < lowest:
        lowest = coinsedata["bid"]
        lowexchange = "coins-e"
if bterdata["last"] < lowest:
        lowest = bterdata["last"]
        lowexchange = "bter"
if mintpaldata[0]["last_price"] < lowest:
        lowest = mintpaldata[0]["last_price"]
        lowexchange = "mintpal"

f = open('/path/to/coinbasevalue.txt','w')
f.write('Average Dogecoin value' + lowest + '\n')
milliondoge = float(lowest)*1000000
dogeinusd = milliondoge * float(coinbasedata['subtotal']['amount'])
f.write('value of 1m DOGE in USD' + str(dogeinusd) + '\n')
f.close()

f = open('/root/dogecointicker/dogecoinvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' Cryptsy: ' + cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"] + " -- Volume: " + cryptsydata["return"]["markets"]["DOGE"]["volume"] + " Today's trend: " + cryptsytrend + " \n")
f.write('Current value of DOGE in BTC:' +' Vircurex: '+ vircurexdata["value"] + " -- Volume: " + vircurexvoldata["value"] + " Today's trend: " + vircurextrend + " \n")
f.write('Current value of DOGE in BTC:' +' COINS-E: ' + coinsedata["bid"] + " -- Volume: " + coinsedata["total_ask_q"] + " Today's trend: " + coinsetrend + " \n")
f.write('Current value of DOGE in BTC:' +' BTER: ' + bterdata["last"] + " -- Volume: " + str(bterdata["vol_doge"]) + " Today's trend: " + btertrend + " \n")
f.write('Current value of DOGE in BTC:' +' mintpal: ' + mintpaldata[0]["last_price"] + " -- Volume: Unknown" + " Today's trend: " + mintpaltrend + " \n")
f.write('Current approx. value of 1M DOGE in USD: $' + str(dogeinusd) + " values from " + lowexchange + " + coinbase" + " #dogecoin \n")
f.write('Direct message me with the name of one of the exchanges I check to get more current information \n')
f.close()

f = open('/path/to/cryptsyvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' Cryptsy: ' + cryptsydata["return"]["markets"]["DOGE"]["lasttradeprice"] + " -- Volume: " + cryptsydata["return"]["markets"]["DOGE"]["volume"] + " Today's trend: " + cryptsytrend)
f.close()

f = open('/path/to/vircurexvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' Vircurex: '+ vircurexdata["value"] + " -- Volume: " + vircurexvoldata["value"] + " Today's trend: " + vircurextrend)
f.close()

f = open('/path/to/coinsevalue.txt','w')
f.write('Current value of DOGE in BTC:' +' COINS-E: ' + coinsedata["bid"] + " -- Volume: " + coinsedata["total_ask_q"] + " Today's trend: " + coinsetrend)
f.close()

f = open('/path/to/btervalue.txt','w')
f.write('Current value of DOGE in BTC:' +' BTER: ' + bterdata["last"] + " -- Volume: " + str(bterdata["vol_doge"]) + " Today's trend: " + btertrend)
f.close()

f = open('/path/to/mintpalvalue.txt','w')
f.write('Current value of DOGE in BTC:' +' mintpal: ' + mintpaldata[0]["last_price"] + " -- Volume: Unknown" + " Today's trend: " + mintpaltrend)
f.close()
<|endoftext|>


<commit_before>import csv
from StringIO import StringIO
import zipfile
from couchexport.export import export_from_tables

class WorkBook(object):
    _undefined = None
    @property
    def undefined(self):
        return self._undefined

    def __init__(self):
        self._tables = {}

    def open(self, table, headers):
        self._tables[table] = [[h for h in headers],]

    def write_row(self, table, row):
        headers = self._tables[table][0]
        for key in row:
            if key not in headers:
                raise AttributeError()
        self._tables[table].append([
            row.get(h) for h in headers
        ])

    def format(self, format):
        tables = self._tables.items()
        f = StringIO()
        export_from_tables(tables, f, format)
        return f

class CsvWorkBook(WorkBook):
    def __init__(self):
        self._writers = {}

    def open(self, table, headers):
        f = StringIO()
        writer = csv.DictWriter(f, headers)
        try:
            writer.writeheader()
        except AttributeError:
            f.write(",".join([h.encode('utf-8') for h in headers]))
            f.write("\n")

        self._writers[table] = (f, writer)

    def write_row(self, table, row):
        _, writer = self._writers[table]
        for key in row:
            if isinstance(row[key], unicode):
                row[key] = row[key].encode('utf-8')
        writer.writerow(row)

    def to_zip(self):
        zip_io = StringIO()
        zip = zipfile.ZipFile(zip_io, 'w')
        for (table, (f, writer)) in self._writers.items():
            zip.writestr('{table}.csv'.format(table=table), f.getvalue())
        zip.close()
        return zip_io.getvalue()
<commit_msg>have WorkBook.format return a string rather than a StringIO object<commit_after>import csv
from StringIO import StringIO
import zipfile
from couchexport.export import export_from_tables

class WorkBook(object):
    _undefined = None
    @property
    def undefined(self):
        return self._undefined

    def __init__(self):
        self._tables = {}

    def open(self, table, headers):
        self._tables[table] = [[h for h in headers],]

    def write_row(self, table, row):
        headers = self._tables[table][0]
        for key in row:
            if key not in headers:
                raise AttributeError()
        self._tables[table].append([
            row.get(h) for h in headers
        ])

    def format(self, format):
        tables = self._tables.items()
        f = StringIO()
        export_from_tables(tables, f, format)
        return f.getvalue()

class CsvWorkBook(WorkBook):
    def __init__(self):
        self._writers = {}

    def open(self, table, headers):
        f = StringIO()
        writer = csv.DictWriter(f, headers)
        try:
            writer.writeheader()
        except AttributeError:
            f.write(",".join([h.encode('utf-8') for h in headers]))
            f.write("\n")

        self._writers[table] = (f, writer)

    def write_row(self, table, row):
        _, writer = self._writers[table]
        for key in row:
            if isinstance(row[key], unicode):
                row[key] = row[key].encode('utf-8')
        writer.writerow(row)

    def to_zip(self):
        zip_io = StringIO()
        zip = zipfile.ZipFile(zip_io, 'w')
        for (table, (f, writer)) in self._writers.items():
            zip.writestr('{table}.csv'.format(table=table), f.getvalue())
        zip.close()
        return zip_io.getvalue()
<|endoftext|>


<commit_before>"""
Classes used to communicate between vehicle and server.
"""

import time
import json
import io
import os
import copy

import numpy as np

import requests
import tornado.ioloop
import tornado.web

from PIL import Image

import donkey as dk

class RemoteClient():
    '''
    Class used by vehicle to send driving data and recieve predictions.
    '''

    def __init__(self, remote_url, vehicle_id='mycar'):

        self.record_url = remote_url + '/' + vehicle_id + '/drive/'

    def decide(self, img_arr, angle, throttle, milliseconds):
        '''
        Accepts: image and control attributes and saves
        them to learn how to drive.'''

        #load features
        data = {
                'angle': angle,
                'throttle': throttle,
                'milliseconds': milliseconds
                }

        r = requests.post(self.record_url,
                            files={'img': dk.utils.arr_to_binary(img_arr),
                                   'json': json.dumps(data)}) #hack to put json in file

        data = json.loads(r.text)
        angle = int(float(data['angle']))
        throttle = int(float(data['throttle']))
        print('remote client: %s' %r.text)

        return angle, throttle

class RemoteServer():
    '''
    Class used to create server that accepts driving data, records it,
    runs a predictor and returns the predictions.
    '''

    def __init__(self, session, pilot):

        self.port = int(os.environ.get("PORT", 8887))
        self.session = session
        self.pilot = pilot

        vehicle_data = {'user_angle': 0,
                        'user_throttle': 0,
                        'drive_mode':'user',
                        'milliseconds': 0,
                        'recording': False}

        self.vehicles = {'mycar':vehicle_data}

        this_dir = os.path.dirname(os.path.realpath(__file__))
        self.static_file_path = os.path.join(this_dir, 'templates', 'static')
        print(self.static_file_path)

    def start(self):
        '''
        Start the webserver.
        '''

        #load features
        app = tornado.web.Application([

            #temporary redirect until vehicles is not a singleton
            (r"/", tornado.web.RedirectHandler,
                dict(url="/mycar/")),

            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/", VehicleHandler),

            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/control/",
                ControllerHandler,
                dict(vehicles = self.vehicles)
            ),
            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/mjpeg/?(?P<file>[^/]*)?",
                CameraMJPEGHandler,
                dict(vehicles = self.vehicles)
            ),
            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/drive/",
                DriveHandler,
                dict(pilot=self.pilot, session=self.session, vehicles=self.vehicles)
            ),

            (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": self.static_file_path}),

            ], debug=True)

        app.listen(self.port)
        tornado.ioloop.IOLoop.instance().start()

        return True

class VehicleHandler(tornado.web.RequestHandler):
    def get(self, vehicle_id):
        '''
        Serves web page used to control the vehicle.
        '''
        self.render("templates/monitor.html")

class ControllerHandler(tornado.web.RequestHandler):

    def initialize(self, vehicles):
         self.vehicles = vehicles

    def post(self, vehicle_id):
        '''
        Receive post requests as user changes the angle
        and throttle of the vehicle on a the index webpage
        '''
        data = tornado.escape.json_decode(self.request.body)

        angle = data['angle']
        throttle = data['throttle']

        V = self.vehicles[vehicle_id]

        #Set recording mode
        if data['recording'] == 'true':
            V['recording'] = True
        else:
            V['recording'] = False

        #update vehicle angel based on drive mode
        V['drive_mode'] = data['drive_mode']

        if angle is not "":
            V['user_angle'] = int(angle)
        else:
            V['user_angle'] = 0

        if throttle is not "":
            V['user_throttle'] = int(throttle)
        else:
            V['user_throttle'] = 0

        print(V)

class DriveHandler(tornado.web.RequestHandler):
    def initialize(self, session, pilot, vehicles):
        #the parrent controller
         self.pilot = pilot
         self.session = session
         self.vehicles = vehicles

    def post(self, vehicle_id):
        '''
        Receive post requests from vehicle with camera image.
        Return the angle and throttle the car should be goin.
        '''
        img = self.request.files['img'][0]['body']
        img = Image.open(io.BytesIO(img))
        img_arr = dk.utils.img_to_arr(img)

        #Hack to take json from a file
        #data = json.loads(self.request.files['json'][0]['body'].decode("utf-8") )

        #Get angle/throttle from pilot loaded by the server.
        pilot_angle, pilot_throttle = self.pilot.decide(img_arr)

        V = self.vehicles[vehicle_id]
        V['img'] = img
        V['pilot_angle'] = pilot_angle
        V['pilot_throttle'] = pilot_throttle

        if V['recording'] == True:
            #save image with encoded angle/throttle values
            self.session.put(img,
                             angle=V['user_angle'],
                             throttle=V['user_throttle'],
                             milliseconds=V['milliseconds'])

        #depending on the drive mode, return user or pilot values
        if V['drive_mode'] == 'user':
            angle, throttle  = V['user_angle'], V['user_throttle']
        elif V['drive_mode'] == 'auto_angle':
            angle, throttle  = V['pilot_angle'], V['user_throttle']
        else:
            angle, throttle  = V['pilot_angle'], V['pilot_throttle']

        print('%s: A: %s   T:%s' %(V['drive_mode'], angle, throttle))

        #retun angel/throttle values to vehicle with json response
        self.write(json.dumps({'angle': str(angle), 'throttle': str(throttle)}))

class CameraMJPEGHandler(tornado.web.RequestHandler):
    def initialize(self, vehicles):
         self.vehicles = vehicles

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self, vehicle_id, file):
        '''
        Show a video of the pictures captured by the vehicle.
        '''
        ioloop = tornado.ioloop.IOLoop.current()
        self.set_header("Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross")

        self.served_image_timestamp = time.time()
        my_boundary = "--boundarydonotcross"
        while True:

            interval = .2
            if self.served_image_timestamp + interval < time.time():

                img = self.vehicles[vehicle_id]['img']
                img = dk.utils.img_to_binary(img)

                self.write(my_boundary)
                self.write("Content-type: image/jpeg\r\n")
                self.write("Content-length: %s\r\n\r\n" % len(img))
                self.write(img)
                self.served_image_timestamp = time.time()
                yield tornado.gen.Task(self.flush)
            else:
                yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)

<commit_msg>made port configurable<commit_after>"""
Classes used to communicate between vehicle and server.
"""

import time
import json
import io
import os
import copy

import numpy as np

import requests
import tornado.ioloop
import tornado.web

from PIL import Image

import donkey as dk

class RemoteClient():
    '''
    Class used by vehicle to send driving data and recieve predictions.
    '''

    def __init__(self, remote_url, vehicle_id='mycar'):

        self.record_url = remote_url + '/' + vehicle_id + '/drive/'

    def decide(self, img_arr, angle, throttle, milliseconds):
        '''
        Accepts: image and control attributes and saves
        them to learn how to drive.'''

        #load features
        data = {
                'angle': angle,
                'throttle': throttle,
                'milliseconds': milliseconds
                }

        r = requests.post(self.record_url,
                            files={'img': dk.utils.arr_to_binary(img_arr),
                                   'json': json.dumps(data)}) #hack to put json in file

        data = json.loads(r.text)
        angle = int(float(data['angle']))
        throttle = int(float(data['throttle']))
        print('remote client: %s' %r.text)

        return angle, throttle

class RemoteServer():
    '''
    Class used to create server that accepts driving data, records it,
    runs a predictor and returns the predictions.
    '''

    def __init__(self, session, pilot, port=8887):

        self.port = int(port)
        self.session = session
        self.pilot = pilot

        vehicle_data = {'user_angle': 0,
                        'user_throttle': 0,
                        'drive_mode':'user',
                        'milliseconds': 0,
                        'recording': False}

        self.vehicles = {'mycar':vehicle_data}

        this_dir = os.path.dirname(os.path.realpath(__file__))
        self.static_file_path = os.path.join(this_dir, 'templates', 'static')
        print(self.static_file_path)

    def start(self):
        '''
        Start the webserver.
        '''

        #load features
        app = tornado.web.Application([

            #temporary redirect until vehicles is not a singleton
            (r"/", tornado.web.RedirectHandler,
                dict(url="/mycar/")),

            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/", VehicleHandler),

            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/control/",
                ControllerHandler,
                dict(vehicles = self.vehicles)
            ),
            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/mjpeg/?(?P<file>[^/]*)?",
                CameraMJPEGHandler,
                dict(vehicles = self.vehicles)
            ),
            (r"/?(?P<vehicle_id>[A-Za-z0-9-]+)?/drive/",
                DriveHandler,
                dict(pilot=self.pilot, session=self.session, vehicles=self.vehicles)
            ),

            (r"/static/(.*)", tornado.web.StaticFileHandler, {"path": self.static_file_path}),

            ], debug=True)

        app.listen(self.port)
        tornado.ioloop.IOLoop.instance().start()

        return True

class VehicleHandler(tornado.web.RequestHandler):
    def get(self, vehicle_id):
        '''
        Serves web page used to control the vehicle.
        '''
        self.render("templates/monitor.html")

class ControllerHandler(tornado.web.RequestHandler):

    def initialize(self, vehicles):
         self.vehicles = vehicles

    def post(self, vehicle_id):
        '''
        Receive post requests as user changes the angle
        and throttle of the vehicle on a the index webpage
        '''
        data = tornado.escape.json_decode(self.request.body)

        angle = data['angle']
        throttle = data['throttle']

        V = self.vehicles[vehicle_id]

        #Set recording mode
        if data['recording'] == 'true':
            V['recording'] = True
        else:
            V['recording'] = False

        #update vehicle angel based on drive mode
        V['drive_mode'] = data['drive_mode']

        if angle is not "":
            V['user_angle'] = int(angle)
        else:
            V['user_angle'] = 0

        if throttle is not "":
            V['user_throttle'] = int(throttle)
        else:
            V['user_throttle'] = 0

        print(V)

class DriveHandler(tornado.web.RequestHandler):
    def initialize(self, session, pilot, vehicles):
        #the parrent controller
         self.pilot = pilot
         self.session = session
         self.vehicles = vehicles

    def post(self, vehicle_id):
        '''
        Receive post requests from vehicle with camera image.
        Return the angle and throttle the car should be goin.
        '''
        img = self.request.files['img'][0]['body']
        img = Image.open(io.BytesIO(img))
        img_arr = dk.utils.img_to_arr(img)

        #Hack to take json from a file
        #data = json.loads(self.request.files['json'][0]['body'].decode("utf-8") )

        #Get angle/throttle from pilot loaded by the server.
        pilot_angle, pilot_throttle = self.pilot.decide(img_arr)

        V = self.vehicles[vehicle_id]
        V['img'] = img
        V['pilot_angle'] = pilot_angle
        V['pilot_throttle'] = pilot_throttle

        if V['recording'] == True:
            #save image with encoded angle/throttle values
            self.session.put(img,
                             angle=V['user_angle'],
                             throttle=V['user_throttle'],
                             milliseconds=V['milliseconds'])

        #depending on the drive mode, return user or pilot values
        if V['drive_mode'] == 'user':
            angle, throttle  = V['user_angle'], V['user_throttle']
        elif V['drive_mode'] == 'auto_angle':
            angle, throttle  = V['pilot_angle'], V['user_throttle']
        else:
            angle, throttle  = V['pilot_angle'], V['pilot_throttle']

        print('%s: A: %s   T:%s' %(V['drive_mode'], angle, throttle))

        #retun angel/throttle values to vehicle with json response
        self.write(json.dumps({'angle': str(angle), 'throttle': str(throttle)}))

class CameraMJPEGHandler(tornado.web.RequestHandler):
    def initialize(self, vehicles):
         self.vehicles = vehicles

    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def get(self, vehicle_id, file):
        '''
        Show a video of the pictures captured by the vehicle.
        '''
        ioloop = tornado.ioloop.IOLoop.current()
        self.set_header("Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross")

        self.served_image_timestamp = time.time()
        my_boundary = "--boundarydonotcross"
        while True:

            interval = .2
            if self.served_image_timestamp + interval < time.time():

                img = self.vehicles[vehicle_id]['img']
                img = dk.utils.img_to_binary(img)

                self.write(my_boundary)
                self.write("Content-type: image/jpeg\r\n")
                self.write("Content-length: %s\r\n\r\n" % len(img))
                self.write(img)
                self.served_image_timestamp = time.time()
                yield tornado.gen.Task(self.flush)
            else:
                yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)

<|endoftext|>


<commit_before>import sys
import inspect

import argparse as arg
from dipy.workflows.docstring_parser import NumpyDocString

def get_args_default(func):
    if sys.version_info[0] >= 3:
        sig_object = inspect.signature(func)
        params = sig_object.parameters.values()
        names = [param.name for param in params if param.name is not 'self']
        defaults = [param.default for param in params
                    if param.default is not inspect._empty]
    else:
        specs = inspect.getargspec(func)
        names = specs.args[1:]
        defaults = specs.defaults

    return names, defaults

class IntrospectiveArgumentParser(arg.ArgumentParser):

    def __init__(self, prog=None, usage=None, description=None, epilog=None,
                 parents=[], formatter_class=arg.RawTextHelpFormatter,
                 prefix_chars='-', fromfile_prefix_chars=None,
                 argument_default=None, conflict_handler='resolve',
                 add_help=True):
        """ Augmenting the argument parser to allow automatic creation of
        arguments from workflows

        Parameters
        -----------
        prog : None
            The name of the program (default: sys.argv[0])
        usage : None
            A usage message (default: auto-generated from arguments)
        description : str
            A description of what the program does
        epilog : str
            Text following the argument descriptions
        parents : list
            Parsers whose arguments should be copied into this one
        formatter_class : obj
            HelpFormatter class for printing help messages
        prefix_chars : str
            Characters that prefix optional arguments
        fromfile_prefix_chars : None
            Characters that prefix files containing additional arguments
        argument_default : None
            The default value for all arguments
        conflict_handler : str
            String indicating how to handle conflicts
        add_help : bool
            Add a -h/-help option
        """

        iap = IntrospectiveArgumentParser
        if epilog is None:
            epilog =\
                ("References: \n"
                 "Garyfallidis, E., M. Brett, B. Amirbekian, A. Rokem,"
                 " S. Van Der Walt, M. Descoteaux, and I. Nimmo-Smith. Dipy, a"
                 " library for the analysis of diffusion MRI data. Frontiers"
                 " in Neuroinformatics, 1-18, 2014.")

        super(iap, self).__init__(prog=prog, usage=usage,
                                  description=description,
                                  epilog=epilog, parents=parents,
                                  formatter_class=formatter_class,
                                  prefix_chars=prefix_chars,
                                  fromfile_prefix_chars=fromfile_prefix_chars,
                                  argument_default=argument_default,
                                  conflict_handler=conflict_handler,
                                  add_help=add_help)

        self.doc = None

    def add_workflow(self, workflow):
        """ Take a workflow object and use introspection to extract the parameters,
        types and docstrings of its run method. Then add these parameters
        to the current arparser's own params to parse. If the workflow is of
        type combined_workflow, the optional input parameters of its
        sub workflows will also be added.

        Parameters
        -----------
        workflow : dipy.workflows.workflow.Workflow
            Workflow from which to infer parameters.

        Returns
        -------
        sub_flow_optionals : dictionary of all sub workflow optional parameters
        """

        doc = inspect.getdoc(workflow.run)
        npds = NumpyDocString(doc)
        self.doc = npds['Parameters']
        self.description = ' '.join(npds['Extended Summary'])

        if npds['References']:
            ref_text = [text if text else "\n" for text in npds['References']]
            ref_idx = self.epilog.find('References: \n') + len('References: \n')
            self.epilog = "{0}{1}\n{2}".format(self.epilog[:ref_idx],
                                               ''.join(ref_text),
                                               self.epilog[ref_idx:])

        self.outputs = [param for param in npds['Parameters'] if
                        'out_' in param[0]]

        args, defaults = get_args_default(workflow.run)

        output_args = self.add_argument_group('output arguments(optional)')

        len_args = len(args)
        len_defaults = len(defaults)

        if len_args != len(self.doc):
            raise ValueError(
                    self.prog + ": Number of parameters in the "
                    "doc string and run method does not match. "
                    "Please ensure that the number of parameters "
                    "in the run method is same as the doc string.")

        for i, arg in enumerate(args):
            prefix = ''
            is_optionnal = i >= len_args - len_defaults
            if is_optionnal:
                prefix = '--'

            typestr = self.doc[i][1]
            dtype, isnarg = self._select_dtype(typestr)
            help_msg = ''.join(self.doc[i][2])

            _args = ['{0}{1}'.format(prefix, arg)]
            _kwargs = {'help': help_msg,
                       'type': dtype,
                       'action': 'store'}

            if is_optionnal:
                _kwargs['metavar'] = dtype.__name__
                if dtype is bool:
                    _kwargs['action'] = 'store_true'
                    default_ = dict()
                    default_[arg] = False
                    self.set_defaults(**default_)
                    del _kwargs['type']
                    del _kwargs['metavar']
            elif dtype is bool:
                _kwargs['type'] = int
                _kwargs['choices'] = [0, 1]

            if dtype is tuple:
                _kwargs['type'] = str

            if isnarg:
                _kwargs['nargs'] = '*' if is_optionnal else '+'

            if 'out_' in arg:
                output_args.add_argument(*_args, **_kwargs)
            else:
                self.add_argument(*_args, **_kwargs)

        return self.add_sub_flow_args(workflow.get_sub_runs())

    def add_sub_flow_args(self, sub_flows):
        """ Take an array of workflow objects and use introspection to extract
        the parameters, types and docstrings of their run method. Only the
        optional input parameters are extracted for these as they are treated
        as sub workflows.

        Parameters
        -----------
        sub_flows : array of dipy.workflows.workflow.Workflow
            Workflows to inspect.

        Returns
        -------
        sub_flow_optionals : dictionary of all sub workflow optional parameters
        """

        sub_flow_optionals = dict()
        for name, flow, short_name in sub_flows:
            sub_flow_optionals[name] = {}
            doc = inspect.getdoc(flow)
            npds = NumpyDocString(doc)
            _doc = npds['Parameters']

            args, defaults = get_args_default(flow)

            len_args = len(args)
            len_defaults = len(defaults)

            flow_args = \
                self.add_argument_group('{0} arguments(optional)'.
                                        format(name))

            for i, arg_name in enumerate(args):
                is_not_optionnal = i < len_args - len_defaults
                if 'out_' in arg_name or is_not_optionnal:
                    continue

                arg_name = '{0}.{1}'.format(short_name, arg_name)
                sub_flow_optionals[name][arg_name] = None
                prefix = '--'
                typestr = _doc[i][1]
                dtype, isnarg = self._select_dtype(typestr)
                help_msg = ''.join(_doc[i][2])

                _args = ['{0}{1}'.format(prefix, arg_name)]
                _kwargs = {'help': help_msg,
                           'type': dtype,
                           'action': 'store'}

                _kwargs['metavar'] = dtype.__name__
                if dtype is bool:
                    _kwargs['action'] = 'store_true'
                    default_ = dict()
                    default_[arg_name] = False
                    self.set_defaults(**default_)
                    del _kwargs['type']
                    del _kwargs['metavar']
                elif dtype is bool:
                    _kwargs['type'] = int
                    _kwargs['choices'] = [0, 1]

                if dtype is tuple:
                    _kwargs['type'] = str

                if isnarg:
                    _kwargs['nargs'] = '*'

                flow_args.add_argument(*_args, **_kwargs)

        return sub_flow_optionals

    def _select_dtype(self, text):
        """ Analyses a docstring parameter line and returns the good argparser
        type.

        Parameters
        -----------
        text : string
            Parameter text line to inspect.

        Returns
        -------
        arg_type : The type found by inspecting the text line.

        is_nargs : Whether or not this argument is nargs
        (arparse's multiple values argument)
        """
        text = text.lower()
        nargs_str = 'variable'
        is_nargs = nargs_str in text
        arg_type = None

        if 'str' in text:
            arg_type = str
        if 'int' in text:
            arg_type = int
        if 'float' in text:
            arg_type = float
        if 'bool' in text:
            arg_type = bool
        if 'tuple' in text:
            arg_type = tuple

        return arg_type, is_nargs

    def get_flow_args(self, args=None, namespace=None):
        """ Returns the parsed arguments as a dictionary that will be used
        as a workflow's run method arguments.
        """

        ns_args = self.parse_args(args, namespace)
        dct = vars(ns_args)
        return dict((k, v) for k, v in dct.items() if v is not None)

    def update_argument(self, *args, **kargs):
        self.add_argument(*args, **kargs)

    def show_argument(self, dest):
        for act in self._actions[1:]:
            if act.dest == dest:
                print(act)

    def add_epilogue(self):
        pass

    def add_description(self):
        pass

    def get_outputs(self):
        return self.outputs
<commit_msg>adress ariel comment<commit_after>import sys
import inspect

import argparse
from dipy.workflows.docstring_parser import NumpyDocString

def get_args_default(func):
    if sys.version_info[0] >= 3:
        sig_object = inspect.signature(func)
        params = sig_object.parameters.values()
        names = [param.name for param in params if param.name is not 'self']
        defaults = [param.default for param in params
                    if param.default is not inspect._empty]
    else:
        specs = inspect.getargspec(func)
        names = specs.args[1:]
        defaults = specs.defaults

    return names, defaults

class IntrospectiveArgumentParser(argparse.ArgumentParser):

    def __init__(self, prog=None, usage=None, description=None, epilog=None,
                 parents=[], formatter_class=argparse.RawTextHelpFormatter,
                 prefix_chars='-', fromfile_prefix_chars=None,
                 argument_default=None, conflict_handler='resolve',
                 add_help=True):
        """ Augmenting the argument parser to allow automatic creation of
        arguments from workflows

        Parameters
        -----------
        prog : None
            The name of the program (default: sys.argv[0])
        usage : None
            A usage message (default: auto-generated from arguments)
        description : str
            A description of what the program does
        epilog : str
            Text following the argument descriptions
        parents : list
            Parsers whose arguments should be copied into this one
        formatter_class : obj
            HelpFormatter class for printing help messages
        prefix_chars : str
            Characters that prefix optional arguments
        fromfile_prefix_chars : None
            Characters that prefix files containing additional arguments
        argument_default : None
            The default value for all arguments
        conflict_handler : str
            String indicating how to handle conflicts
        add_help : bool
            Add a -h/-help option
        """

        iap = IntrospectiveArgumentParser
        if epilog is None:
            epilog =\
                ("References: \n"
                 "Garyfallidis, E., M. Brett, B. Amirbekian, A. Rokem,"
                 " S. Van Der Walt, M. Descoteaux, and I. Nimmo-Smith. Dipy, a"
                 " library for the analysis of diffusion MRI data. Frontiers"
                 " in Neuroinformatics, 1-18, 2014.")

        super(iap, self).__init__(prog=prog, usage=usage,
                                  description=description,
                                  epilog=epilog, parents=parents,
                                  formatter_class=formatter_class,
                                  prefix_chars=prefix_chars,
                                  fromfile_prefix_chars=fromfile_prefix_chars,
                                  argument_default=argument_default,
                                  conflict_handler=conflict_handler,
                                  add_help=add_help)

        self.doc = None

    def add_workflow(self, workflow):
        """ Take a workflow object and use introspection to extract the parameters,
        types and docstrings of its run method. Then add these parameters
        to the current arparser's own params to parse. If the workflow is of
        type combined_workflow, the optional input parameters of its
        sub workflows will also be added.

        Parameters
        -----------
        workflow : dipy.workflows.workflow.Workflow
            Workflow from which to infer parameters.

        Returns
        -------
        sub_flow_optionals : dictionary of all sub workflow optional parameters
        """

        doc = inspect.getdoc(workflow.run)
        npds = NumpyDocString(doc)
        self.doc = npds['Parameters']
        self.description = ' '.join(npds['Extended Summary'])

        if npds['References']:
            ref_text = [text if text else "\n" for text in npds['References']]
            ref_idx = self.epilog.find('References: \n') + len('References: \n')
            self.epilog = "{0}{1}\n{2}".format(self.epilog[:ref_idx],
                                               ''.join(ref_text),
                                               self.epilog[ref_idx:])

        self.outputs = [param for param in npds['Parameters'] if
                        'out_' in param[0]]

        args, defaults = get_args_default(workflow.run)

        output_args = self.add_argument_group('output arguments(optional)')

        len_args = len(args)
        len_defaults = len(defaults)

        if len_args != len(self.doc):
            raise ValueError(
                    self.prog + ": Number of parameters in the "
                    "doc string and run method does not match. "
                    "Please ensure that the number of parameters "
                    "in the run method is same as the doc string.")

        for i, arg in enumerate(args):
            prefix = ''
            is_optional = i >= len_args - len_defaults
            if is_optional:
                prefix = '--'

            typestr = self.doc[i][1]
            dtype, isnarg = self._select_dtype(typestr)
            help_msg = ''.join(self.doc[i][2])

            _args = ['{0}{1}'.format(prefix, arg)]
            _kwargs = {'help': help_msg,
                       'type': dtype,
                       'action': 'store'}

            if is_optional:
                _kwargs['metavar'] = dtype.__name__
                if dtype is bool:
                    _kwargs['action'] = 'store_true'
                    default_ = dict()
                    default_[arg] = False
                    self.set_defaults(**default_)
                    del _kwargs['type']
                    del _kwargs['metavar']
            elif dtype is bool:
                _kwargs['type'] = int
                _kwargs['choices'] = [0, 1]

            if dtype is tuple:
                _kwargs['type'] = str

            if isnarg:
                _kwargs['nargs'] = '*' if is_optional else '+'

            if 'out_' in arg:
                output_args.add_argument(*_args, **_kwargs)
            else:
                self.add_argument(*_args, **_kwargs)

        return self.add_sub_flow_args(workflow.get_sub_runs())

    def add_sub_flow_args(self, sub_flows):
        """ Take an array of workflow objects and use introspection to extract
        the parameters, types and docstrings of their run method. Only the
        optional input parameters are extracted for these as they are treated
        as sub workflows.

        Parameters
        -----------
        sub_flows : array of dipy.workflows.workflow.Workflow
            Workflows to inspect.

        Returns
        -------
        sub_flow_optionals : dictionary of all sub workflow optional parameters
        """

        sub_flow_optionals = dict()
        for name, flow, short_name in sub_flows:
            sub_flow_optionals[name] = {}
            doc = inspect.getdoc(flow)
            npds = NumpyDocString(doc)
            _doc = npds['Parameters']

            args, defaults = get_args_default(flow)

            len_args = len(args)
            len_defaults = len(defaults)

            flow_args = \
                self.add_argument_group('{0} arguments(optional)'.
                                        format(name))

            for i, arg_name in enumerate(args):
                is_not_optionnal = i < len_args - len_defaults
                if 'out_' in arg_name or is_not_optionnal:
                    continue

                arg_name = '{0}.{1}'.format(short_name, arg_name)
                sub_flow_optionals[name][arg_name] = None
                prefix = '--'
                typestr = _doc[i][1]
                dtype, isnarg = self._select_dtype(typestr)
                help_msg = ''.join(_doc[i][2])

                _args = ['{0}{1}'.format(prefix, arg_name)]
                _kwargs = {'help': help_msg,
                           'type': dtype,
                           'action': 'store'}

                _kwargs['metavar'] = dtype.__name__
                if dtype is bool:
                    _kwargs['action'] = 'store_true'
                    default_ = dict()
                    default_[arg_name] = False
                    self.set_defaults(**default_)
                    del _kwargs['type']
                    del _kwargs['metavar']
                elif dtype is bool:
                    _kwargs['type'] = int
                    _kwargs['choices'] = [0, 1]

                if dtype is tuple:
                    _kwargs['type'] = str

                if isnarg:
                    _kwargs['nargs'] = '*'

                flow_args.add_argument(*_args, **_kwargs)

        return sub_flow_optionals

    def _select_dtype(self, text):
        """ Analyses a docstring parameter line and returns the good argparser
        type.

        Parameters
        -----------
        text : string
            Parameter text line to inspect.

        Returns
        -------
        arg_type : The type found by inspecting the text line.

        is_nargs : Whether or not this argument is nargs
        (arparse's multiple values argument)
        """
        text = text.lower()
        nargs_str = 'variable'
        is_nargs = nargs_str in text
        arg_type = None

        if 'str' in text:
            arg_type = str
        if 'int' in text:
            arg_type = int
        if 'float' in text:
            arg_type = float
        if 'bool' in text:
            arg_type = bool
        if 'tuple' in text:
            arg_type = tuple

        return arg_type, is_nargs

    def get_flow_args(self, args=None, namespace=None):
        """ Returns the parsed arguments as a dictionary that will be used
        as a workflow's run method arguments.
        """

        ns_args = self.parse_args(args, namespace)
        dct = vars(ns_args)
        return dict((k, v) for k, v in dct.items() if v is not None)

    def update_argument(self, *args, **kargs):
        self.add_argument(*args, **kargs)

    def show_argument(self, dest):
        for act in self._actions[1:]:
            if act.dest == dest:
                print(act)

    def add_epilogue(self):
        pass

    def add_description(self):
        pass

    def get_outputs(self):
        return self.outputs
<|endoftext|>


<commit_before>import os
import subprocess

with open('../README.md', 'r') as f:
    readme = f.readlines()

#remove DOI badge from readme before conversion to RST
for line in readme:
    if 'doi' in line or 'bib' in line:
        readme.remove(line)

with open('../README.md', 'w') as f:
    f.writelines(readme)

#convert README to rst
for file in ['README', 'README_input_format', 'CONTRIBUTING']:
    subprocess.call(['pandoc', '../{}.md'.format(file),
                     '--from', 'markdown', '--to', 'rst',
                     '-s', '-o', '{}.rst'.format(file.lower())])

subprocess.call(['git', 'checkout', '--', '../README.md'])

os.rename('readme_input_format.rst', 'input_format.rst')

with open('readme.rst', 'r') as f:
    readme = f.read()

readme = readme.split('Installation', 1)

intro, installation = readme

installation, references = installation.split('References', 1)

with open('intro.rst', 'w') as f:
    f.write(intro)

with open('installation.rst', 'w') as f:
    f.write('Installation')
    f.write(installation)

with open('references.rst', 'w') as f:
    f.write('References')
    f.write(references)

os.remove('readme.rst')
<commit_msg>don't nuke README.md changes<commit_after>import os
import subprocess

#convert README to rst
for file in ['README', 'README_input_format', 'CONTRIBUTING']:
    subprocess.call(['pandoc', '../{}.md'.format(file),
                     '--from', 'markdown', '--to', 'rst',
                     '-s', '-o', '{}.rst'.format(file.lower())])

with open('readme.rst', 'r') as f:
    readme = f.readlines()

#remove DOI badge from readme before conversion to RST
for line in readme:
    if 'DOI' in line:
        readme.remove(line)

with open('readme.rst', 'w') as f:
    f.writelines(readme)

os.rename('readme_input_format.rst', 'input_format.rst')

with open('readme.rst', 'r') as f:
    readme = f.read()

readme = readme.split('Installation', 1)

intro, installation = readme

installation, references = installation.split('References', 1)

with open('intro.rst', 'w') as f:
    f.write(intro)

with open('installation.rst', 'w') as f:
    f.write('Installation')
    f.write(installation)

with open('references.rst', 'w') as f:
    f.write('References')
    f.write(references)

os.remove('readme.rst')
<|endoftext|>


<commit_before>#!/usr/bin/env python3
"""Sub event helper functions
helpful inspiration: https://gist.github.com/jason-w/4969476
"""
from mongoengine import ValidationError

import logging
import pdb
import pytz

from icalendar import Calendar, Event, vCalAddress, vText, vDatetime
from dateutil.rrule import rrule, MONTHLY, WEEKLY, DAILY, YEARLY
from datetime import datetime, timedelta, timezone, date
from bson import objectid
from mongoengine import *
from icalendar import Calendar

import isodate
import dateutil.parser
import requests

from abe import database as db
from abe.helper_functions.converting_helpers import mongo_to_dict

def duplicate_query_check(sub_event_dict, parent_event):
    """checks whether a dictionary has the same field-value pair as a parent event
    used to check for duplicate information in sub_events and their parent events
    """
    parent_event_dict = mongo_to_dict(parent_event)
    fields_to_pop = []
    for field in sub_event_dict:
        if field in parent_event_dict:
            if sub_event_dict[field] == parent_event_dict[field]:
               fields_to_pop.append(field)
    for field in fields_to_pop:
        sub_event_dict.pop(field)

    return(sub_event_dict)

def create_sub_event(received_data, parent_event):
    """creates an edited sub event in a recurring series for the first time
    """
    sub_event_dict = duplicate_query_check(received_data, parent_event)
    rec_event = db.RecurringEventExc(**sub_event_dict)
    parent_event.update(add_to_set__sub_events=rec_event)
    parent_event.reload()

    return(rec_event)

def update_sub_event(received_data, parent_event, sub_event_id, ics=False):
    """edits a sub_event that has already been created

    sub_event_id        if the update is not coming from an ics feed:
                            - will be an objectid
                        if the update is coming from an ics feed:
                            - will be a rec_id (datetime object)
    """
    convert_timezone = lambda a: a.replace(tzinfo=pytz.UTC) if isinstance(a, datetime) else a
    for sub_event in parent_event.sub_events:
        if ics == False: # if this update is not coming from an ics feed
            # if the sub_event to be updated's id matches the id of the received_data
            if sub_event['_id']== sub_event_id:
                updated_sub_event_dict = create_new_sub_event_defintion(mongo_to_dict(sub_event), received_data, parent_event)
                updated_sub_event = db.RecurringEventExc(**updated_sub_event_dict)
                parent_event.update(pull__sub_events___id=sub_event_id)
                parent_event.update(add_to_set__sub_events=updated_sub_event_dict)
                if updated_sub_event_dict['forever'] == False:
                    parent_event.recurrence_end = find_recurrence_end(parent_event)
                parent_event.save()
                parent_event.reload()
                return(updated_sub_event)
        elif ics == True: # if this update is coming from an ics feed
            sub_event_compare = convert_timezone(sub_event["rec_id"])
            if sub_event_compare == sub_event_id:
                updated_sub_event_dict = create_new_sub_event_defintion(mongo_to_dict(sub_event), received_data, parent_event)
                updated_sub_event = db.RecurringEventExc(**updated_sub_event_dict)
                parent_event.update(pull__sub_events__rec_id=sub_event_id)
                parent_event.update(add_to_set__sub_events=updated_sub_event_dict)
                if updated_sub_event_dict['forever'] == False:
                    parent_event.recurrence_end = find_recurrence_end(parent_event)
                parent_event.save()
                parent_event.reload()
                return(updated_sub_event)

def sub_event_to_full(sub_event_dict, event):
    """expands a sub_event definition to have all fields full_Calendar requires
    uses its parent definition to fill in the blanks
    """
    recurring_def_fields = ["end_recurrence", "recurrence", "sub_events"]
    date_fields = ['start', 'end', 'rec_id']
    for field in event:
        if field not in sub_event_dict and field not in recurring_def_fields:
            if field == 'id':
                sub_event_dict["sid"] = str(event[field])
            elif field == 'ics_id':
                sub_event_dict[field] = str(event[field])
            elif field in date_fields:
                sub_event_dict[field] = dateutil.parser.parse(str(sub_event_dict[field]))
            else:
                sub_event_dict[field] = event[field]
    sub_event_dict['labels'] = event['labels']
    sub_event_dict["id"] = sub_event_dict.pop("_id")
    return(sub_event_dict)

def access_sub_event(parent_event, sub_event_id):
    """gets the sub_event from a parent_event and returns it
    """
    for sub_event in parent_event['sub_events']:
        if sub_event['_id'] == str(sub_event_id):
            return(sub_event)

def create_new_sub_event_defintion(sub_event, updates, parent_event):
    """based on the old sub_event definition and updates submitted
    generates a new sub_event definition
    """
    sub_event.update(updates)
    sub_event = duplicate_query_check(sub_event, parent_event)
    return(sub_event)

def instance_creation(event, end=None):
    """
    Generates list of datetime objects of when recurring events should occur
    Uses rrule from dateutils
    """

    rec_type_list = ['YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY']

    day_list = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']

    recurrence = event.recurrence
    ensure_date_time = lambda a: dateutil.parser.parse(a) if not isinstance(a, datetime) and not isinstance(a, date) else a
    convert_timezone = lambda a: a.replace(tzinfo=None) if isinstance(a, datetime) else a

    rFrequency = rec_type_list.index(recurrence['frequency'])
    rStart = convert_timezone(ensure_date_time(event['start']))
    if recurrence['frequency'] == 'YEARLY':
        # extracts the month and day from the date
        rByMonth = int(rStart.month)
        rByMonthDay = int(rStart.day)
        rByDay = None
    else:
        if 'by_month_day' in recurrence:
            rByMonthDay = [int(x) for x in recurrence['by_month_day']]
        else:
            rByMonthDay = None

        if 'by_month' in recurrence:
            rByMonth = [int(x) for x in recurrence['by_month']]
        else:
            rByMonth = None
        if 'by_day' in recurrence:
            rByDay = []
            for i in recurrence['by_day']:
                rByDay.append(day_list.index(i))
        else:
            rByDay = None

    rInterval = int(recurrence['interval'])
    if recurrence.forever == True:
        rUntil = convert_timezone(ensure_date_time(end)) if end is not None else None
    else:
        rUntil = convert_timezone(ensure_date_time(recurrence['until'])) if 'until' in recurrence else None
    rCount = int(recurrence['count']) if 'count' in recurrence else None

    rule_list = list(rrule(freq=rFrequency, count=rCount, interval=rInterval, until=rUntil, bymonth=rByMonth, \
        bymonthday=rByMonthDay, byweekday=rByDay, dtstart=rStart))
    return(rule_list)

def find_recurrence_end(event):
    """
    Finds the last occurence of an event and returns the day after
    """
    rule_list = instance_creation(event)
    event_end = rule_list[-1] + timedelta(hours=24)
    return(event_end)<commit_msg>Changing rrule for recurrent events with no end date to use query end date by default.<commit_after>#!/usr/bin/env python3
"""Sub event helper functions
helpful inspiration: https://gist.github.com/jason-w/4969476
"""
from mongoengine import ValidationError

import logging
import pdb
import pytz

from icalendar import Calendar, Event, vCalAddress, vText, vDatetime
from dateutil.rrule import rrule, MONTHLY, WEEKLY, DAILY, YEARLY
from datetime import datetime, timedelta, timezone, date
from bson import objectid
from mongoengine import *
from icalendar import Calendar

import isodate
import dateutil.parser
import requests

from abe import database as db
from abe.helper_functions.converting_helpers import mongo_to_dict

def duplicate_query_check(sub_event_dict, parent_event):
    """checks whether a dictionary has the same field-value pair as a parent event
    used to check for duplicate information in sub_events and their parent events
    """
    parent_event_dict = mongo_to_dict(parent_event)
    fields_to_pop = []
    for field in sub_event_dict:
        if field in parent_event_dict:
            if sub_event_dict[field] == parent_event_dict[field]:
               fields_to_pop.append(field)
    for field in fields_to_pop:
        sub_event_dict.pop(field)

    return(sub_event_dict)

def create_sub_event(received_data, parent_event):
    """creates an edited sub event in a recurring series for the first time
    """
    sub_event_dict = duplicate_query_check(received_data, parent_event)
    rec_event = db.RecurringEventExc(**sub_event_dict)
    parent_event.update(add_to_set__sub_events=rec_event)
    parent_event.reload()

    return(rec_event)

def update_sub_event(received_data, parent_event, sub_event_id, ics=False):
    """edits a sub_event that has already been created

    sub_event_id        if the update is not coming from an ics feed:
                            - will be an objectid
                        if the update is coming from an ics feed:
                            - will be a rec_id (datetime object)
    """
    convert_timezone = lambda a: a.replace(tzinfo=pytz.UTC) if isinstance(a, datetime) else a
    for sub_event in parent_event.sub_events:
        if ics == False: # if this update is not coming from an ics feed
            # if the sub_event to be updated's id matches the id of the received_data
            if sub_event['_id']== sub_event_id:
                updated_sub_event_dict = create_new_sub_event_defintion(mongo_to_dict(sub_event), received_data, parent_event)
                updated_sub_event = db.RecurringEventExc(**updated_sub_event_dict)
                parent_event.update(pull__sub_events___id=sub_event_id)
                parent_event.update(add_to_set__sub_events=updated_sub_event_dict)
                if updated_sub_event_dict['forever'] == False:
                    parent_event.recurrence_end = find_recurrence_end(parent_event)
                parent_event.save()
                parent_event.reload()
                return(updated_sub_event)
        elif ics == True: # if this update is coming from an ics feed
            sub_event_compare = convert_timezone(sub_event["rec_id"])
            if sub_event_compare == sub_event_id:
                updated_sub_event_dict = create_new_sub_event_defintion(mongo_to_dict(sub_event), received_data, parent_event)
                updated_sub_event = db.RecurringEventExc(**updated_sub_event_dict)
                parent_event.update(pull__sub_events__rec_id=sub_event_id)
                parent_event.update(add_to_set__sub_events=updated_sub_event_dict)
                if updated_sub_event_dict['forever'] == False:
                    parent_event.recurrence_end = find_recurrence_end(parent_event)
                parent_event.save()
                parent_event.reload()
                return(updated_sub_event)

def sub_event_to_full(sub_event_dict, event):
    """expands a sub_event definition to have all fields full_Calendar requires
    uses its parent definition to fill in the blanks
    """
    recurring_def_fields = ["end_recurrence", "recurrence", "sub_events"]
    date_fields = ['start', 'end', 'rec_id']
    for field in event:
        if field not in sub_event_dict and field not in recurring_def_fields:
            if field == 'id':
                sub_event_dict["sid"] = str(event[field])
            elif field == 'ics_id':
                sub_event_dict[field] = str(event[field])
            elif field in date_fields:
                sub_event_dict[field] = dateutil.parser.parse(str(sub_event_dict[field]))
            else:
                sub_event_dict[field] = event[field]
    sub_event_dict['labels'] = event['labels']
    sub_event_dict["id"] = sub_event_dict.pop("_id")
    return(sub_event_dict)

def access_sub_event(parent_event, sub_event_id):
    """gets the sub_event from a parent_event and returns it
    """
    for sub_event in parent_event['sub_events']:
        if sub_event['_id'] == str(sub_event_id):
            return(sub_event)

def create_new_sub_event_defintion(sub_event, updates, parent_event):
    """based on the old sub_event definition and updates submitted
    generates a new sub_event definition
    """
    sub_event.update(updates)
    sub_event = duplicate_query_check(sub_event, parent_event)
    return(sub_event)

def instance_creation(event, end=None):
    """
    Generates list of datetime objects of when recurring events should occur
    Uses rrule from dateutils
    """

    rec_type_list = ['YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY']

    day_list = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']

    recurrence = event.recurrence
    ensure_date_time = lambda a: dateutil.parser.parse(a) if not isinstance(a, datetime) and not isinstance(a, date) else a
    convert_timezone = lambda a: a.replace(tzinfo=None) if isinstance(a, datetime) else a

    rFrequency = rec_type_list.index(recurrence['frequency'])
    rStart = convert_timezone(ensure_date_time(event['start']))
    if recurrence['frequency'] == 'YEARLY':
        # extracts the month and day from the date
        rByMonth = int(rStart.month)
        rByMonthDay = int(rStart.day)
        rByDay = None
    else:
        if 'by_month_day' in recurrence:
            rByMonthDay = [int(x) for x in recurrence['by_month_day']]
        else:
            rByMonthDay = None

        if 'by_month' in recurrence:
            rByMonth = [int(x) for x in recurrence['by_month']]
        else:
            rByMonth = None
        if 'by_day' in recurrence:
            rByDay = []
            for i in recurrence['by_day']:
                rByDay.append(day_list.index(i))
        else:
            rByDay = None

    rInterval = int(recurrence['interval'])
    if recurrence.forever == True:
        rUntil = convert_timezone(ensure_date_time(end)) if end is not None else None
    else:
        rUntil = convert_timezone(ensure_date_time(recurrence['until'])) if 'until' in recurrence else end
    rCount = int(recurrence['count']) if 'count' in recurrence else None

    rule_list = list(rrule(freq=rFrequency, count=rCount, interval=rInterval, until=rUntil, bymonth=rByMonth, \
        bymonthday=rByMonthDay, byweekday=rByDay, dtstart=rStart))
    return(rule_list)

def find_recurrence_end(event):
    """
    Finds the last occurence of an event and returns the day after
    """
    rule_list = instance_creation(event)
    event_end = rule_list[-1] + timedelta(hours=24)
    return(event_end)
<|endoftext|>