<commit_before>"""
This is the debug renderer.  This is very useful for debugging plugins
and templates.
"""
from Pyblosxom.renderers.base import RendererBase
from Pyblosxom import tools
import types

def E(s):
    if not s:
        return ""

    if not isinstance(s, str):
        s = repr(s)

    return s.replace("&", "&").replace(">", ">").replace("<", "<")

def print_map(f, m):
    keys = m.keys()
    keys.sort()
    for key in keys:
        f("%s -> %s\n" % (E(key), E(m[key])))

class Renderer(RendererBase):
    def render(self, header = 1):
        pyhttp = self._request.getHttp()
        config = self._request.getConfiguration()
        data = self._request.getData()
        printout = self.write

        self.addHeader('Content-type', 'text/html')
        self.showHeaders()
        printout("")
        printout("")
        printout("
")
        printout("Welcome to debug mode!\n")
        printout("You wanted the %(flavour)s flavour if I support flavours\n" % data)

        printout("------------------------------------------------------\n")
        printout("The HTTP Return codes are:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, self._header)

        printout("------------------------------------------------------\n")
        printout("The OS environment contains:\n")
        printout("------------------------------------------------------\n")
        import os
        print_map(printout, os.environ)

        printout("------------------------------------------------------\n")
        printout("Request.getHttp() dict contains:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, pyhttp)

        printout("------------------------------------------------------\n")
        printout("Request.getConfiguration() dict contains:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, config)

        printout("------------------------------------------------------\n")
        printout("Request.getData() dict contains:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, data)

        printout("------------------------------------------------------\n")
        printout("Entries to process:\n")
        printout("------------------------------------------------------\n")
        for content in self._content:
            if not isinstance(content, str):
                printout("%s\n" % E(content.get('filename', 'No such file\n')))

        printout("------------------------------------------------------\n")
        printout("Entries processed:\n")
        printout("------------------------------------------------------\n")
        for content in self._content:
            if not isinstance(content, str):
                printout("------------------------------------------------------\n")
                printout("Items for %s:\n" % E(content.get('filename', 'No such file\n')))
                printout("------------------------------------------------------\n")
                print_map(printout, content)

        printout("------------------------------------------------------\n")
        printout("Cached Titles:\n")
        printout("------------------------------------------------------\n")
        cache = tools.get_cache(self._request)
        for content in self._content:
            if not isinstance(content, str):
                filename = content['filename']

                if cache.has_key(filename):
                    printout("%s\n" % E(cache[filename]['title']))
                cache.close()

        printout("------------------------------------------------------\n")
        printout("Cached Entry Bodies:\n")
        printout("------------------------------------------------------\n")
        for content in self._content:
            if not isinstance(content, str):
                filename = content['filename']
                if cache.has_key(filename):
                    printout("%s\n" % E(cache[filename]['title']))
                    printout("==================================================\n")
                    printout("%s\n" % E(cache[filename]['body']))
                else:
                    printout("Contents of %s is not cached\n" % E(filename))
                cache.close()
                printout("------------------------------------------------------\n")
        printout("")
        printout("")
<commit_msg>Added some additional comments and removed the "import types".<commit_after>"""
This is the debug renderer.  This is very useful for debugging plugins
and templates.
"""
from Pyblosxom.renderers.base import RendererBase
from Pyblosxom import tools

def E(s):
    """
    Takes in a value.  If it's not a string, we repr it and turn it into
    a string.  Then we escape it so it can be printed in HTML safely.

    @param s: any value
    @type  s: varies

    @returns: a safe-to-print-in-html string representation of the value
    @rtype: string
    """
    if not s:
        return ""

    if not isinstance(s, str):
        s = repr(s)

    return s.replace("&", "&").replace(">", ">").replace("<", "<")

def print_map(f, m):
    """
    Takes a map of keys to values and applies the function f to a pretty
    printed version of each key/value pair.

    @param f: function for printing
    @type  f: function

    @param m: a mapping of key/value pairs
    @type  m: map
    """
    keys = m.keys()
    keys.sort()
    for key in keys:
        f("%s -> %s\n" % (E(key), E(m[key])))

class Renderer(RendererBase):
    """
    This is the debug renderer.  This is very useful for debugging plugins
    and templates.
    """
    def render(self, header = 1):
        """
        Renders a PyBlosxom request after we've gone through all the
        motions of converting data and getting entries to render.

        @param header: either prints (1) or does not print (0) the http
                       headers.
        @type  header: boolean
        """
        pyhttp = self._request.getHttp()
        config = self._request.getConfiguration()
        data = self._request.getData()
        printout = self.write

        self.addHeader('Content-type', 'text/html')
        self.showHeaders()
        printout("")
        printout("")
        printout("
")
        printout("Welcome to debug mode!\n")
        printout("You wanted the %(flavour)s flavour if I support flavours\n" % data)

        printout("------------------------------------------------------\n")
        printout("The HTTP Return codes are:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, self._header)

        printout("------------------------------------------------------\n")
        printout("The OS environment contains:\n")
        printout("------------------------------------------------------\n")
        import os
        print_map(printout, os.environ)

        printout("------------------------------------------------------\n")
        printout("Request.getHttp() dict contains:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, pyhttp)

        printout("------------------------------------------------------\n")
        printout("Request.getConfiguration() dict contains:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, config)

        printout("------------------------------------------------------\n")
        printout("Request.getData() dict contains:\n")
        printout("------------------------------------------------------\n")
        print_map(printout, data)

        printout("------------------------------------------------------\n")
        printout("Entries to process:\n")
        printout("------------------------------------------------------\n")
        for content in self._content:
            if not isinstance(content, str):
                printout("%s\n" % E(content.get('filename', 'No such file\n')))

        printout("------------------------------------------------------\n")
        printout("Entries processed:\n")
        printout("------------------------------------------------------\n")
        for content in self._content:
            if not isinstance(content, str):
                printout("------------------------------------------------------\n")
                printout("Items for %s:\n" % E(content.get('filename', 'No such file\n')))
                printout("------------------------------------------------------\n")
                print_map(printout, content)

        printout("------------------------------------------------------\n")
        printout("Cached Titles:\n")
        printout("------------------------------------------------------\n")
        cache = tools.get_cache(self._request)
        for content in self._content:
            if not isinstance(content, str):
                filename = content['filename']

                if cache.has_key(filename):
                    printout("%s\n" % E(cache[filename]['title']))
                cache.close()

        printout("------------------------------------------------------\n")
        printout("Cached Entry Bodies:\n")
        printout("------------------------------------------------------\n")
        for content in self._content:
            if not isinstance(content, str):
                filename = content['filename']
                if cache.has_key(filename):
                    printout("%s\n" % E(cache[filename]['title']))
                    printout("==================================================\n")
                    printout("%s\n" % E(cache[filename]['body']))
                else:
                    printout("Contents of %s is not cached\n" % E(filename))
                cache.close()
                printout("------------------------------------------------------\n")
        printout("")
        printout("")
<|endoftext|>


<commit_before>import time
import serial
import random
from pygame import mixer

mixer.init()

running = True
elapsed_time = 0
next_value = 25

serialPort = serial.Serial(port = '/dev/ttyUSB0', baudrate = 115200)

for x in range(3):
    serialPort.readline()

def play_song():
    # picker = random.randint(2, 4)
    global serialPort
    picker = 2
    if picker is 2:
        file_path = '/home/pi/Music/Still_Alive.mp3'
    if picker is 3:
        file_path = '/home/pi/Music/Mario_Theme.mp3'
    if picker is 4:
        file_path = '/home/pi/Music/Tetris_Theme.mp3'
    mixer.music.load(file_path)
    mixer.music.play()
    serialPort.write(bytes([picker]))

serialPort.flushInput()
serialPort.flushOutput()

for x in range(100):
    serialPort.write(bytes([0]))
    time.sleep(0.2)

serialPort.write(bytes([1]))

elapsed_time = time.time()

while running:
    if serialPort.inWaiting() > 0:
        next_value = float(serialPort.readline())
        print next_value
    if abs(next_value) > 1000:
        elapsed_time = time.time()
        serialPort.write(bytes([1]))
    else:
        if elapsed_time - time.time() >= 25:
            play_song()
            serialPort.write(bytes([0]))
<commit_msg>Changed some stuff<commit_after>import time
import serial
import random
from pygame import mixer

mixer.init()

running = True
elapsed_time = 0
next_value = 25

serialPort = serial.Serial(port = '/dev/ttyUSB0', baudrate = 115200)

for x in range(3):
    serialPort.readline()

def play_song():
    # picker = random.randint(2, 4)
    global serialPort
    picker = 2
    if picker is 2:
        file_path = '/home/pi/Music/Still_Alive.mp3'
    if picker is 3:
        file_path = '/home/pi/Music/Mario_Theme.mp3'
    if picker is 4:
        file_path = '/home/pi/Music/Tetris_Theme.mp3'
    mixer.music.load(file_path)
    mixer.music.play()
    serialPort.write(bytes([picker]))

serialPort.flushInput()
serialPort.flushOutput()

for x in range(100):
    serialPort.write(bytes([0]))
    time.sleep(0.2)

serialPort.write(bytes([1]))

elapsed_time = time.time()

while running:
    if serialPort.inWaiting() > 0:
        next_value = float(serialPort.readline())
        serialPort.flushInput()
        print next_value
    if abs(next_value) > 1000:
        elapsed_time = time.time()
        serialPort.write(bytes([1]))
    else:
        if elapsed_time - time.time() >= 25:
            play_song()
            serialPort.write(bytes([0]))
<|endoftext|>


<commit_before># Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Python_DHT.common as common
import Python_DHT.Raspberry_Pi_2_Driver as driver

def read(sensor, pin):
	# Validate pin is a valid GPIO.
	if pin is None or int(pin) < 0 or int(pin) > 31:
		raise ValueError('Pin must be a valid GPIO number 0 to 31.')
	# Get a reading from C driver code.
	result, humidity, temp = driver.read(sensor, int(pin))
	if result in common.TRANSIENT_ERRORS:
		# Signal no result could be obtained, but the caller can retry.
		return (None, None)
	elif result == common.DHT_ERROR_GPIO:
		raise RuntimeError('Error accessing GPIO. Make sure program is run as root with sudo!')
	elif result != common.DHT_SUCCESS:
		# Some kind of error occured.
		raise RuntimeError('Error calling DHT test driver read: {0}'.format(result))
	return (humidity, temp)
<commit_msg>Removed non sudo Error<commit_after># Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Python_DHT.common as common
import Python_DHT.Raspberry_Pi_2_Driver as driver

def read(sensor, pin):
	# Validate pin is a valid GPIO.
	if pin is None or int(pin) < 0 or int(pin) > 31:
		raise ValueError('Pin must be a valid GPIO number 0 to 31.')
	# Get a reading from C driver code.
	result, humidity, temp = driver.read(sensor, int(pin))
	if result in common.TRANSIENT_ERRORS:
		# Signal no result could be obtained, but the caller can retry.
		return (None, None)
	elif result == common.DHT_ERROR_GPIO:
		raise RuntimeError('Error accessing GPIO.')
	elif result != common.DHT_SUCCESS:
		# Some kind of error occured.
		raise RuntimeError('Error calling DHT test driver read: {0}'.format(result))
	return (humidity, temp)
<|endoftext|>


<commit_before># coding:utf-8
#
# The MIT License (MIT)
#
# Copyright (c) 2016-2018 yutiansut/QUANTAXIS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
QA fetch module

@yutiansut

QAFetch is Under [QAStandard#0.0.2@10x] Protocol

"""
from QUANTAXIS.QAFetch import QAWind as QAWind
from QUANTAXIS.QAFetch import QATushare as QATushare
from QUANTAXIS.QAFetch import QATdx as QATdx
from QUANTAXIS.QAFetch import QAThs as QAThs
from QUANTAXIS.QAFetch import QAQuery_Advance as QAMongo
from QUANTAXIS.QAUtil.QAParameter import FREQUENCE, MARKET_TYPE, DATASOURCE

def QA_fetch(code, start, end, frequence, market, source, output='pandas'):
    """一个统一的fetch

    Arguments:
        code {[type]} -- 证券/股票的代码
        start {[type]} -- 开始日期
        end {[type]} -- 结束日期
        frequence {[type]} -- 频率()
        market {[type]} -- 市场
        source {[type]} -- 来源

    """
    if market is MARKET_TYPE.STOCK_CN:
        if frequence is FREQUENCE.DAY:
            if source is DATASOURCE.MONGO:
                return QAMongo.QA_fetch_stock_day_adv(code, start, end)
            elif source is DATASOURCE.TDX:
                return QATdx.QA_fetch_get_stock_day(code, start, end, '00')
            elif source is DATASOURCE.TUSHARE:
                return QATushare.QA_fetch_get_stock_day(code, start, end, '00')
        elif frequence in [FREQUENCE.ONE_MIN, FREQUENCE.FIVE_MIN, FREQUENCE.FIFTEEN_MIN, FREQUENCE.THIRTY_MIN, FREQUENCE.SIXTY_MIN]:
            if source is DATASOURCE.MONGO:
                return QAMongo.QA_fetch_stock_min_adv(code, start, end, type_=frequence)
            elif source is DATASOURCE.TDX:
                return QATdx.QA_fetch_get_stock_min(code, start, end, level=frequence)
        elif frequence is FREQUENCE.TICK:
            if source is DATASOURCE.TDX:
                return QATdx.QA_fetch_get_stock_transaction(code, start, end)

if __name__ == '__main__':
    print(QA_fetch('000001', '2017-01-01', '2017-01-31', frequence=FREQUENCE.DAY,
                   market=MARKET_TYPE.STOCK_CN, source=DATASOURCE.TDX))
<commit_msg>#fetcher<commit_after># coding:utf-8
#
# The MIT License (MIT)
#
# Copyright (c) 2016-2018 yutiansut/QUANTAXIS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

"""
QA fetch module

@yutiansut

QAFetch is Under [QAStandard#0.0.2@10x] Protocol

"""
from QUANTAXIS.QAFetch import QAWind as QAWind
from QUANTAXIS.QAFetch import QATushare as QATushare
from QUANTAXIS.QAFetch import QATdx as QATdx
from QUANTAXIS.QAFetch import QAThs as QAThs
from QUANTAXIS.QAFetch import QAQuery_Advance as QAMongo
from QUANTAXIS.QAUtil.QAParameter import FREQUENCE, MARKET_TYPE, DATASOURCE, OUTPUT_FORMAT

def QA_fetch(code, start, end, frequence, market, source, output):
    """一个统一的fetch

    Arguments:
        code {[type]} -- 证券/股票的代码
        start {[type]} -- 开始日期
        end {[type]} -- 结束日期
        frequence {[type]} -- 频率()
        market {[type]} -- 市场
        source {[type]} -- 来源

    """
    if market is MARKET_TYPE.STOCK_CN:
        if frequence is FREQUENCE.DAY:
            if source is DATASOURCE.MONGO:
                res = QAMongo.QA_fetch_stock_day_adv(code, start, end)
            elif source is DATASOURCE.TDX:
                res = QATdx.QA_fetch_get_stock_day(code, start, end, '00')
            elif source is DATASOURCE.TUSHARE:
                res = QATushare.QA_fetch_get_stock_day(code, start, end, '00')
        elif frequence in [FREQUENCE.ONE_MIN, FREQUENCE.FIVE_MIN, FREQUENCE.FIFTEEN_MIN, FREQUENCE.THIRTY_MIN, FREQUENCE.SIXTY_MIN]:
            if source is DATASOURCE.MONGO:
                res = QAMongo.QA_fetch_stock_min_adv(
                    code, start, end, type_=frequence)
            elif source is DATASOURCE.TDX:
                res = QATdx.QA_fetch_get_stock_min(
                    code, start, end, level=frequence)
        elif frequence is FREQUENCE.TICK:
            if source is DATASOURCE.TDX:
                res = QATdx.QA_fetch_get_stock_transaction(code, start, end)

    return res

if __name__ == '__main__':
    print(QA_fetch('000001', '2017-01-01', '2017-01-31', frequence=FREQUENCE.DAY,
                   market=MARKET_TYPE.STOCK_CN, source=DATASOURCE.TDX, output=OUTPUT_FORMAT.DATAFRAME))
<|endoftext|>


<commit_before># The MIT License (MIT)
# Copyright (c) 2016 Dell Inc. or its subsidiaries.

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import argparse
from PyU4V.rest_univmax import rest_functions
import time
#This script will gather all performance statistics for all Front End directors and print them.  For use in real world
#output would be writen to a file.

####################################
# Declare Variables                #
# and instantiate session for REST #
# calls                            #
####################################
#end_date = int(round(time.time() * 1000)) #Set end Date to current time EPOCH in Milliseconds
#start_date = (end_date - 3600000)  #Set start date to EPOCH Time 1 hours Earlier
ru = rest_functions()
end_date = int(round(time.time() * 1000))
start_date = (end_date - 3600000)

#No Arguments are Required to run this script running with -h will provide a description.

PARSER = argparse.ArgumentParser(description='This python scrtipt is a basic VMAX REST recipe used Gathering Some Performance Statistics from VMAX and VMAX3 Arrays.')
ARGS = PARSER.parse_args()

dir_list=ru.get_fe_director_list()

def get_last_hour_fe_metrics():
    # First Get a list of all Directors
    # Calculate start and End Dates for Gathering Performance Stats Last 1 Hour
    director_results_combined = dict()
    director_results_list=[]
    director_results=[]
    # print("this is the director list %s" % dir_list)
    director_results_combined['reporting_level'] = "FEDirector"
    for fe_director in dir_list:
        director_metrics = ru.get_fe_director_metrics(director=fe_director, start_date=start_date, end_date=end_date,dataformat='Average')
        director_results=({
            "directorID":fe_director,
            "perfdata":director_metrics[0]['resultList']['result']
        })
        director_results_list.append(director_results)
    director_results_combined['hourlyPerf']=director_results_list

    return director_results_combined

def main():
    feperfdata=get_last_hour_fe_metrics()
    print (feperfdata)

main()

<commit_msg>modified returned payload to format data better.<commit_after># The MIT License (MIT)
# Copyright (c) 2016 Dell Inc. or its subsidiaries.

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import argparse
from PyU4V.rest_univmax import rest_functions
import time
#This script will gather all performance statistics for all Front End directors and print them.  For use in real world
#output would be writen to a file.

####################################
# Declare Variables                #
# and instantiate session for REST #
# calls                            #
####################################
#end_date = int(round(time.time() * 1000)) #Set end Date to current time EPOCH in Milliseconds
#start_date = (end_date - 3600000)  #Set start date to EPOCH Time 1 hours Earlier
ru = rest_functions()
end_date = int(round(time.time() * 1000))
start_date = (end_date - 3600000)

#No Arguments are Required to run this script running with -h will provide a description.

PARSER = argparse.ArgumentParser(description='This python scrtipt is a basic VMAX REST recipe used Gathering Some Performance Statistics from VMAX and VMAX3 Arrays.')
ARGS = PARSER.parse_args()

dir_list=ru.get_fe_director_list()

def get_last_hour_fe_metrics():
    # First Get a list of all Directors
    # Calculate start and End Dates for Gathering Performance Stats Last 1 Hour
    director_results_combined = dict()
    director_results_list=[]
    # print("this is the director list %s" % dir_list)
    director_results_combined['reporting_level'] = "FEDirector"
    for fe_director in dir_list:
        director_metrics = ru.get_fe_director_metrics(director=fe_director, start_date=start_date, end_date=end_date,dataformat='Average')
        director_results=({
            "directorID":fe_director,
            "perfdata":director_metrics[0]['resultList']['result']
        })
        director_results_list.append(director_results)
    director_results_combined['hourlyPerf']=director_results_list

    return director_results_combined

def main():
    feperfdata=get_last_hour_fe_metrics()
    print (feperfdata)

main()

<|endoftext|>


<commit_before># Generated by Django 2.2.6 on 2019-11-15 16:09

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('models', '5533_user_tasks'),
    ]

    operations = [
        migrations.CreateModel(
            name='Notification',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid1, primary_key=True, serialize=False)),
                ('is_read', models.BooleanField(default=False)),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('message', models.TextField(blank=True, null=True)),
                ('recipient_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'db_table': 'notifications',
                'managed': True,
            },
        ),
    ]
<commit_msg>Fixing style errors.<commit_after># Generated by Django 2.2.6 on 2019-11-15 16:09

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import uuid

class Migration(migrations.Migration):

    dependencies = [migrations.swappable_dependency(settings.AUTH_USER_MODEL), ("models", "5533_user_tasks")]

    operations = [
        migrations.CreateModel(
            name="Notification",
            fields=[
                ("id", models.UUIDField(default=uuid.uuid1, primary_key=True, serialize=False)),
                ("is_read", models.BooleanField(default=False)),
                ("created", models.DateTimeField(auto_now_add=True)),
                ("message", models.TextField(blank=True, null=True)),
                ("recipient_id", models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
            ],
            options={"db_table": "notifications", "managed": True},
        )
    ]
<|endoftext|>


<commit_before># Raspberry PI Machine Control / Sensor Demo for Murano platform
import sys
import time
import json
#from pi_stats import PiStats
from sense_hat import SenseHat

from murano import Murano
import requests

if len(sys.argv) < 3:
    print('Usage:')
    print('   python ./device_app <product_id> <device_id>');

PRODUCT_ID = sys.argv[1] # 'do5pjwoazfsf9a4i'
DEVICE_ID = sys.argv[2]  # '001'

def show_state():
    print('Product ID: {0} Device ID: {1}'.format(PRODUCT_ID, DEVICE_ID))

w = [255,255,255] # White
ww = [220,220,220] # slightly grey
x = [65,196,220] #Exosite Blue
a = [92,93,96] #Exosite Grey
d = [34,39,54] #Exosite Dark Blue
xx = [44,157,182] #Exosite Medium Blue

r = [255,0,0]
o = [255,127,0]
y = [255,255,0]
g = [0,255,0]
b = [0,0,255]
i = [75,0,130]
v = [159,0,255]
e = [0,0,0]

image = [
d,d,w,d,d,x,d,d,
d,w,w,ww,x,x,xx,d,
d,w,w,ww,x,x,xx,d,
d,d,w,w,d,xx,d,d,
d,d,d,d,w,w,d,d,
d,d,d,d,w,w,ww,d,
d,d,d,d,w,w,ww,d,
d,d,d,d,d,w,d,d
]

image2 = [
e,e,w,e,e,x,e,e,
e,w,w,ww,x,x,xx,e,
e,w,w,ww,x,x,xx,e,
e,e,w,w,e,xx,e,e,
e,e,e,e,w,w,e,e,
e,e,e,e,w,w,ww,e,
e,e,e,e,w,w,ww,e,
e,e,e,e,e,w,e,e
]

# create object for getting Sense Hat sensor data
sense = SenseHat()
sense.low_light = True
#sense.show_message("Starting...", text_colour=x,back_colour=e)
#sense.set_pixels(image2)
sense.clear()

# create a murano object for the device
murano = Murano(PRODUCT_ID, DEVICE_ID)

# create object for getting local device stats
#stats = PiStats()

# Get the device key (CIK)
try:
    cik = murano.load_cik()
    print('Loaded CIK from file {0}'.format(murano.filename))
except EnvironmentError:
    # no stored CIK, so activate
    cik = murano.activate()
    print('Activated device to obtain CIK')

# initialize the device state of of Pi
class State:
    battery_percent = 100
    cpu_usage = 0
    mem_used = 0
    run_time = 0

# initialize the sensor state
class Sensors:
    temperature = 0
    humidity = 0
    pressure = 0
    x,y,z = 0,0,0

#stats.update_stats()

# write current state of the lock to Murano
#writes = {'battery-percent': State.battery_percent,
#          'lock-state': State.lock_state}
#murano.write(writes)

def print_state():
    print('---------------------------------')
    print('product id:      {0}'.format(PRODUCT_ID))
    print('device id:       {0}'.format(DEVICE_ID))
    print('run time:        {0}'.format(State.run_time))
    print('cpu usage:       {0}'.format(State.cpu_usage))
    print('mem used:        {0}'.format(State.mem_used))
    print('temperature:     {0}'.format(Sensors.temperature))
    print('humidity:        {0}'.format(Sensors.humidity))
    print('pressure:        {0}'.format(Sensors.pressure))
    print('---------------------------------')

print('Waiting for lock commands from Murano')
print('Press Ctrl+C to quit')
timestamp = None

period_sensor_time = 2 #how many seconds to send Sensors
last_sensor_time = 0 # when was last time we sent sensor data

start_time = int(time.time())

while True:
    #stats.update_stats()
    #meminfo = stats.get_memory_info()
    #print "total\tused\tfree\tcached"
    #print "%i\t%i\t%i\t%i"%(meminfo['total'],meminfo['used'],meminfo['free'],meminfo['cached'])
    #print "Memory Usage:\t%i%%"%(meminfo['percent'])
    #print "\n"
    #cpu_info = stats.get_cpu_info()
    #print "CPU Usage:\t%i%%"%(cpu_info['percent'])

    #State.cpu_usage = int(cpu_info['percent'])
    #State.mem_used = int(meminfo['used'])

    State.run_time = int(time.time())-start_time

    # TIME TO SEND PERIODIC SENSOR DATA
    if time.time() - last_sensor_time > 10:
        sense.set_pixel(0,7,d) #set pixel to dark blue

        t = sense.get_temperature()
        p = sense.get_pressure()
        h = sense.get_humidity()

        Sensors.temperature = round(t, 1)
        Sensors.pressure = round(p, 1)
        Sensors.humidity = round(h, 1)

        print_state()

        rawdata  = '{"temperature":' + str(Sensors.temperature) + ', "pressure":' + str(Sensors.pressure)+ ', "humidity":'+ str(Sensors.humidity)+', "runtime":'+ str(State.run_time)+'}'

        writes = {}
        writes['raw_data'] = rawdata

        # send current states up to Murano
        try:
            murano.write(writes)
            sense.set_pixel(0,7,g) #set pixel to medium blue if ok
        except requests.exceptions.RequestException as e:
            sense.set_pixel(0,7,[255,146,30]) #set pixel to orange if having issues
            print str(e)
        last_sensor_time = time.time()

    time.sleep(0.2);
<commit_msg>switching to Grove sensors<commit_after># Raspberry PI Machine Control / Sensor Demo for Murano platform
import sys
import time
import json
import datetime
import grovepi
from grove_rgb_lcd import *

sensor = 7

from murano import Murano
import requests

if len(sys.argv) < 3:
    print('Usage:')
    print('   python ./device_app <product_id> <device_id>');

PRODUCT_ID = sys.argv[1] # 'do5pjwoazfsf9a4i'
DEVICE_ID = sys.argv[2]  # '001'

def show_state():
    print('Product ID: {0} Device ID: {1}'.format(PRODUCT_ID, DEVICE_ID))

# create a murano object for the device
murano = Murano(PRODUCT_ID, DEVICE_ID)

# Get the device key (CIK)
try:
    cik = murano.load_cik()
    print('Loaded CIK from file {0}'.format(murano.filename))
except EnvironmentError:
    # no stored CIK, so activate
    cik = murano.activate()
    print('Activated device to obtain CIK')

# initialize the device state of of Pi
class State:
    battery_percent = 100
    cpu_usage = 0
    mem_used = 0
    run_time = 0

# initialize the sensor state
class Sensors:
    temperature = 0
    humidity = 0
    sound = 0
    light = 0

#stats.update_stats()

# write current state of the lock to Murano
#writes = {'battery-percent': State.battery_percent,
#          'lock-state': State.lock_state}
#murano.write(writes)

def print_state():
    print('---------------------------------')
    print('product id:      {0}'.format(PRODUCT_ID))
    print('device id:       {0}'.format(DEVICE_ID))
    print('run time:        {0}'.format(State.run_time))
    print('cpu usage:       {0}'.format(State.cpu_usage))
    print('mem used:        {0}'.format(State.mem_used))
    print('temperature:     {0}'.format(Sensors.temperature))
    print('humidity:        {0}'.format(Sensors.humidity))
    print('pressure:        {0}'.format(Sensors.pressure))
    print('---------------------------------')

print('Press Ctrl+C to quit')
timestamp = None

period_sensor_time = 2 #how many seconds to send Sensors
last_sensor_time = 0 # when was last time we sent sensor data

start_time = int(time.time())

while True:
    #stats.update_stats()
    #meminfo = stats.get_memory_info()
    #print "total\tused\tfree\tcached"
    #print "%i\t%i\t%i\t%i"%(meminfo['total'],meminfo['used'],meminfo['free'],meminfo['cached'])
    #print "Memory Usage:\t%i%%"%(meminfo['percent'])
    #print "\n"
    #cpu_info = stats.get_cpu_info()
    #print "CPU Usage:\t%i%%"%(cpu_info['percent'])

    #State.cpu_usage = int(cpu_info['percent'])
    #State.mem_used = int(meminfo['used'])

    State.run_time = int(time.time())-start_time

    # TIME TO SEND PERIODIC SENSOR DATA
    if time.time() - last_sensor_time > 10:

        now = datetime.datetime.utcnow()
        [temp,humidity] = grovepi.dht(sensor,1)
        light=int(grovepi.analogRead(0)/10.24)
        sound=int(grovepi.analogRead(1)/10.24)

        print(temp,humidity,light,sound)

        Sensors.temperature = round(temp, 1)
        Sensors.light = round(light, 1)
        Sensors.humidity = round(humidity, 1)
        Sensors.sound = round(sound, 1)

        #setRGB(0,128,64)
        setRGB(0,255,0)
        setText("Temp:" + str(temp) + "C      " + "Humidity :" + str(humidity) + "%")

        rawdata  = '{"temperature":' + str(Sensors.temperature) + ', "sound":' + str(Sensors.sound) + ', "light":' + str(Sensors.light)+ ', "humidity":'+ str(Sensors.humidity)+', "runtime":'+ str(State.run_time)+'}'

        writes = {}
        writes['raw_data'] = rawdata

        # send current states up to Murano
        try:
            murano.write(writes)
        except requests.exceptions.RequestException as e:
            print str(e)
            last_sensor_time = time.time()

    time.sleep(0.2);
<|endoftext|>


<commit_before>import base64
import importlib
import json
import logging
import os
import shutil

# Config applies to loggers created in modules accessed from this module
# Logger must loaded before importing other modules that rely on this logger,
# otherwise they will be getting an empty logger and messages from that module
# will not be logged.
log_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logging.ini')
logging.config.fileConfig(log_file_path)
logger = logging.getLogger(__name__)

import falcon

import engine.util as util
from engine.simple_lxd import simple_lxd as lxd
from engine.runners.python_runner import PythonRunner

cwd = os.path.dirname(os.path.abspath(__file__))
os.chdir(cwd)

class SubmitResource(object):
    def __init__(self):
        self.pid = os.getpid()
        self.container_name = 'lovelace-{:d}'.format(self.pid)

        logger.info("Launching Linux container {:s} (pid={:d})..."
            .format(self.container_name, self.pid))
        lxd.launch("images:ubuntu/xenial/i386", name=self.container_name, profile="lovelace")

    # TODO: Need to figure out how to stop and delete the containers on exit. The current
    # problem is that it crashes upon calling __del__ due to e.g. SIGKILL because Python
    # is trying to shut everything down including logging, so the logging functions
    # actually end up crashing Python and we never get to stop and delete the containers.
    # def __del__(self):
        # logging.shutdown()
        # logger.info("Stopping Linux container {:s}...".format(self.container_name))
        # lxd.stop(self.container_name)

        # logger.info("Deleting Linux container {:s}...".format(self.container_name))
        # lxd.delete(self.container_name)

    def on_post(self, req, resp):
        """Handle POST requests."""
        # payload = parse_payload(req)
        payload = req.media

        code_filename = write_code_to_file(payload['code'], payload['language'])

        # Fetch problem ID and load the correct problem module.
        problem_name = payload['problem'].replace('-', '_')
        problem_module = 'problems.{:s}'.format(problem_name)
        logger.debug("problem_name={:s} problem_module={:s}".format(problem_name, problem_module))

        try:
            problem = importlib.import_module(problem_module)
        except Exception:
            logger.exception("Could not import module {:s}".format(problem_module))
            logger.error("Returning HTTP 400 Bad Request due to possibly invalid JSON.")
            raise falcon.HTTPError(falcon.HTTP_400, 'Invalid JSON.', 'Invalid problem name!')
        else:
            test_case_type_enum = problem.TEST_CASE_TYPE_ENUM
            resources = []
            problem_dir = problem_name
            for resource_file_name in problem.RESOURCES:
                from_path = os.path.join(cwd, '..', 'resources', problem_dir, resource_file_name)
                to_path = os.path.join(cwd, resource_file_name)

                logger.debug("Copying resource from {:s} to {:s}".format(from_path, to_path))

                shutil.copyfile(from_path, to_path)
                resources.append(to_path)

        logger.info("Generating test cases...")
        test_cases = []
        for i, test_type in enumerate(test_case_type_enum):
            for j in range(test_type.multiplicity):
                logger.debug("Generating test case {:d}: {:s} ({:d}/{:d})..."
                    .format(len(test_cases)+1, str(test_type), j+1, test_type.multiplicity))
                test_cases.append(problem.generate_test_case(test_type))

        num_passes = 0  # Number of test cases passed.
        num_cases = len(test_cases)
        test_case_details = []  # List of dicts each containing the details of a particular test case.

        for i, tc in enumerate(test_cases):
            test_case_resource = tc.input.get('dataset_filename')
            if test_case_resource:
                resource_path = os.path.join(cwd, '..', 'resources', test_case_resource)
                resource_filename = os.path.basename(resource_path)
                destination_path = os.path.join(cwd, resource_filename)

                logger.debug("Copying test case resource from {:s} to {:s}"
                    .format(resource_path, destination_path))

                shutil.copyfile(resource_path, destination_path)
                resources.append(destination_path)

            input_tuple = tc.input_tuple()
            logger.debug("Input tuple: {:}".format(input_tuple))

            user_answer, process_info = PythonRunner().run(self.container_name, code_filename, input_tuple)
            logger.debug("User answer: {:}".format(user_answer))
            logger.debug("Process info: {:}".format(process_info))

            passed = problem.verify_user_solution(input_tuple, user_answer)

            logger.info("Test case %d/%d (%s).", i+1, num_cases, tc.test_type.test_name)
            logger.debug("Input tuple:")
            logger.debug("%s", input_tuple)
            logger.debug("User answer:")
            logger.debug("%s", user_answer)

            if passed:
                num_passes += 1
                logger.info("Test case passed!")
            else:
                logger.info("Test case failed.")

            test_case_details.append({
                'testCaseType': tc.test_type.test_name,
                'inputString': str(input_tuple),
                'outputString': str(user_answer),
                'passed': passed,
                'processInfo': process_info
            })

        logger.info("Passed %d/%d test cases.", num_passes, num_cases)

        resp_dict = {
            'success': True if num_passes == num_cases else False,
            'numTestCases': num_cases,
            'numTestCasesPassed': num_passes,
            'testCaseDetails': test_case_details
        }

        resp.status = falcon.HTTP_200
        resp.set_header('Access-Control-Allow-Origin', '*')
        resp.body = json.dumps(resp_dict)

        util.delete_file(code_filename)
        logger.debug("User code file deleted: {:s}".format(code_filename))

        for file_path in resources:
            logging.debug("Deleting resource {:s}".format(file_path))
            os.remove(file_path)

def parse_payload(http_request):
    try:
        raw_payload_data = http_request.stream.read().decode('utf-8')
    except Exception as ex:
        logger.error("Bad request, reason unknown. Returning 400.")
        raise falcon.HTTPError(falcon.HTTP_400, 'Error', ex.message)

    try:
        json_payload = json.loads(raw_payload_data)
    except ValueError:
        logger.error("Received invalid JSON: {:}".format(raw_payload_data))
        logger.error("Returning 400 error.")
        raise falcon.HTTPError(falcon.HTTP_400, 'Invalid JSON', 'Could not decode request body.')

    return json_payload

def write_code_to_file(code, language):
    """
    Write code into a file with the appropriate file extension.

    :param code: a base64 encoded string representing the user's submitted source code
    :param language: the code's programming language
    :return: the name of the file containing the user's code
    """
    if not code:
        raise falcon.HTTPError(falcon.HTTP_400, 'Invalid JSON.', 'No code provided in request.')

    decoded_code = str(base64.b64decode(code), 'utf-8')
    extension = {'python3': '.py'}.get(language)
    code_filename = util.write_str_to_file(decoded_code, extension)

    logger.debug("User code saved in: {:s}".format(code_filename))

    return code_filename

app = falcon.API()
app.add_route('/submit', SubmitResource())
app.add_error_handler(
    Exception,
    lambda ex, req, resp, params: logger.exception(ex)
)
util.configure_lxd()
<commit_msg>Better lxc names and new container.<commit_after>import base64
import importlib
import json
import logging
import os
import shutil
import datetime

# Config applies to loggers created in modules accessed from this module
# Logger must loaded before importing other modules that rely on this logger,
# otherwise they will be getting an empty logger and messages from that module
# will not be logged.
log_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logging.ini')
logging.config.fileConfig(log_file_path)
logger = logging.getLogger(__name__)

import falcon

import engine.util as util
from engine.simple_lxd import simple_lxd as lxd
from engine.runners.python_runner import PythonRunner

cwd = os.path.dirname(os.path.abspath(__file__))
os.chdir(cwd)

class SubmitResource(object):
    def __init__(self):
        self.pid = os.getpid()
        self.container_image = "lovelace-container"
        self.container_name = "lovelace-{:d}-{:s}".format(self.pid, datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))

        logger.info("Launching Linux container {:s} (pid={:d})..."
            .format(self.container_name, self.pid))
        lxd.launch(self.container_image, name=self.container_name, profile="lovelace")

    # TODO: Need to figure out how to stop and delete the containers on exit. The current
    # problem is that it crashes upon calling __del__ due to e.g. SIGKILL because Python
    # is trying to shut everything down including logging, so the logging functions
    # actually end up crashing Python and we never get to stop and delete the containers.
    # def __del__(self):
        # logging.shutdown()
        # logger.info("Stopping Linux container {:s}...".format(self.container_name))
        # lxd.stop(self.container_name)

        # logger.info("Deleting Linux container {:s}...".format(self.container_name))
        # lxd.delete(self.container_name)

    def on_post(self, req, resp):
        """Handle POST requests."""
        # payload = parse_payload(req)
        payload = req.media

        code_filename = write_code_to_file(payload['code'], payload['language'])

        # Fetch problem ID and load the correct problem module.
        problem_name = payload['problem'].replace('-', '_')
        problem_module = 'problems.{:s}'.format(problem_name)
        logger.debug("problem_name={:s} problem_module={:s}".format(problem_name, problem_module))

        try:
            problem = importlib.import_module(problem_module)
        except Exception:
            logger.exception("Could not import module {:s}".format(problem_module))
            logger.error("Returning HTTP 400 Bad Request due to possibly invalid JSON.")
            raise falcon.HTTPError(falcon.HTTP_400, 'Invalid JSON.', 'Invalid problem name!')
        else:
            test_case_type_enum = problem.TEST_CASE_TYPE_ENUM
            resources = []
            problem_dir = problem_name
            for resource_file_name in problem.RESOURCES:
                from_path = os.path.join(cwd, '..', 'resources', problem_dir, resource_file_name)
                to_path = os.path.join(cwd, resource_file_name)

                logger.debug("Copying resource from {:s} to {:s}".format(from_path, to_path))

                shutil.copyfile(from_path, to_path)
                resources.append(to_path)

        logger.info("Generating test cases...")
        test_cases = []
        for i, test_type in enumerate(test_case_type_enum):
            for j in range(test_type.multiplicity):
                logger.debug("Generating test case {:d}: {:s} ({:d}/{:d})..."
                    .format(len(test_cases)+1, str(test_type), j+1, test_type.multiplicity))
                test_cases.append(problem.generate_test_case(test_type))

        num_passes = 0  # Number of test cases passed.
        num_cases = len(test_cases)
        test_case_details = []  # List of dicts each containing the details of a particular test case.

        for i, tc in enumerate(test_cases):
            test_case_resource = tc.input.get('dataset_filename')
            if test_case_resource:
                resource_path = os.path.join(cwd, '..', 'resources', test_case_resource)
                resource_filename = os.path.basename(resource_path)
                destination_path = os.path.join(cwd, resource_filename)

                logger.debug("Copying test case resource from {:s} to {:s}"
                    .format(resource_path, destination_path))

                shutil.copyfile(resource_path, destination_path)
                resources.append(destination_path)

            input_tuple = tc.input_tuple()
            logger.debug("Input tuple: {:}".format(input_tuple))

            user_answer, process_info = PythonRunner().run(self.container_name, code_filename, input_tuple)
            logger.debug("User answer: {:}".format(user_answer))
            logger.debug("Process info: {:}".format(process_info))

            passed = problem.verify_user_solution(input_tuple, user_answer)

            logger.info("Test case %d/%d (%s).", i+1, num_cases, tc.test_type.test_name)
            logger.debug("Input tuple:")
            logger.debug("%s", input_tuple)
            logger.debug("User answer:")
            logger.debug("%s", user_answer)

            if passed:
                num_passes += 1
                logger.info("Test case passed!")
            else:
                logger.info("Test case failed.")

            test_case_details.append({
                'testCaseType': tc.test_type.test_name,
                'inputString': str(input_tuple),
                'outputString': str(user_answer),
                'passed': passed,
                'processInfo': process_info
            })

        logger.info("Passed %d/%d test cases.", num_passes, num_cases)

        resp_dict = {
            'success': True if num_passes == num_cases else False,
            'numTestCases': num_cases,
            'numTestCasesPassed': num_passes,
            'testCaseDetails': test_case_details
        }

        resp.status = falcon.HTTP_200
        resp.set_header('Access-Control-Allow-Origin', '*')
        resp.body = json.dumps(resp_dict)

        util.delete_file(code_filename)
        logger.debug("User code file deleted: {:s}".format(code_filename))

        for file_path in resources:
            logging.debug("Deleting resource {:s}".format(file_path))
            os.remove(file_path)

def parse_payload(http_request):
    try:
        raw_payload_data = http_request.stream.read().decode('utf-8')
    except Exception as ex:
        logger.error("Bad request, reason unknown. Returning 400.")
        raise falcon.HTTPError(falcon.HTTP_400, 'Error', ex.message)

    try:
        json_payload = json.loads(raw_payload_data)
    except ValueError:
        logger.error("Received invalid JSON: {:}".format(raw_payload_data))
        logger.error("Returning 400 error.")
        raise falcon.HTTPError(falcon.HTTP_400, 'Invalid JSON', 'Could not decode request body.')

    return json_payload

def write_code_to_file(code, language):
    """
    Write code into a file with the appropriate file extension.

    :param code: a base64 encoded string representing the user's submitted source code
    :param language: the code's programming language
    :return: the name of the file containing the user's code
    """
    if not code:
        raise falcon.HTTPError(falcon.HTTP_400, 'Invalid JSON.', 'No code provided in request.')

    decoded_code = str(base64.b64decode(code), 'utf-8')
    extension = {'python3': '.py'}.get(language)
    code_filename = util.write_str_to_file(decoded_code, extension)

    logger.debug("User code saved in: {:s}".format(code_filename))

    return code_filename

app = falcon.API()
app.add_route('/submit', SubmitResource())
app.add_error_handler(
    Exception,
    lambda ex, req, resp, params: logger.exception(ex)
)
util.configure_lxd()
<|endoftext|>


<commit_before>from __future__ import division
import numpy as np
import math

# Generate N x D matrix of extended FAST samples (Saltelli 1999)
def sample(N, D, M = 4):

    omega = np.empty([D])
    omega[0] = math.floor((N - 1) / (2 * M))
    m = math.floor(omega[0] / (2 * M))

    if m >= (D-1):
        omega[1:] = np.floor(np.linspace(1, m, D-1))
    else:
        omega[1:] = np.arange(D-1) % m + 1

    # Discretization of the frequency space, s
    s = (2 * math.pi / N) * np.arange(N)

    # Transformation to get points in the X space
    X = np.empty([N*D, D])
    omega2 = np.empty([D])

    for i in range(D):
        omega2[i] = omega[0]
        idx = range(i) + range(i+1,D)
        omega2[idx] = omega[1:]
        l = range(i*N, (i+1)*N)

        for j in range(D):
            g = 0.5 + (1/math.pi) * np.arcsin(np.sin(omega2[j] * s))
            X[l,j] = g

    return X<commit_msg>random phase shift in fast sampling<commit_after>from __future__ import division
import numpy as np
import math

# Generate N x D matrix of extended FAST samples (Saltelli 1999)
def sample(N, D, M = 4):

    omega = np.empty([D])
    omega[0] = math.floor((N - 1) / (2 * M))
    m = math.floor(omega[0] / (2 * M))

    if m >= (D-1):
        omega[1:] = np.floor(np.linspace(1, m, D-1))
    else:
        omega[1:] = np.arange(D-1) % m + 1

    # Discretization of the frequency space, s
    s = (2 * math.pi / N) * np.arange(N)

    # Transformation to get points in the X space
    X = np.empty([N*D, D])
    omega2 = np.empty([D])

    for i in range(D):
        omega2[i] = omega[0]
        idx = range(i) + range(i+1,D)
        omega2[idx] = omega[1:]
        l = range(i*N, (i+1)*N)

        # random phase shift on [0, 2pi) following Saltelli et al. Technometrics 1999
        phi = 2*math.pi*np.random.rand()

        for j in range(D):
            g = 0.5 + (1/math.pi) * np.arcsin(np.sin(omega2[j] * s))
            X[l,j] = g

    return X<|endoftext|>


<commit_before># encoding: utf-8
"""
A mixin for :class:`~IPython.core.application.Application` classes that
launch InteractiveShell instances, load extensions, etc.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import glob
from itertools import chain
import os
import sys

from traitlets.config.application import boolean_flag
from traitlets.config.configurable import Configurable
from traitlets.config.loader import Config
from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS
from IPython.core import pylabtools
from IPython.utils.contexts import preserve_keys
from IPython.utils.path import filefind
from traitlets import (
    Unicode, Instance, List, Bool, CaselessStrEnum, observe,
)
from IPython.terminal import pt_inputhooks

#-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------

gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases))

backend_keys = sorted(pylabtools.backends.keys())
backend_keys.insert(0, 'auto')

shell_flags = {}

addflag = lambda *args: shell_flags.update(boolean_flag(*args))
addflag('autoindent', 'InteractiveShell.autoindent',
        'Turn on autoindenting.', 'Turn off autoindenting.'
)
addflag('automagic', 'InteractiveShell.automagic',
        """Turn on the auto calling of magic commands. Type %%magic at the
        IPython  prompt  for  more information.""",
        'Turn off the auto calling of magic commands.'
)
addflag('pdb', 'InteractiveShell.pdb',
    "Enable auto calling the pdb debugger after every exception.",
    "Disable auto calling the pdb debugger after every exception."
)
addflag('pprint', 'PlainTextFormatter.pprint',
    "Enable auto pretty printing of results.",
    "Disable auto pretty printing of results."
)
addflag('color-info', 'InteractiveShell.color_info',
    """IPython can display information about objects via a set of functions,
    and optionally can use colors for this, syntax highlighting
    source code and various other elements. This is on by default, but can cause
    problems with some pagers. If you see such problems, you can disable the
    colours.""",
    "Disable using colors for info related things."
)
addflag('ignore-cwd', 'InteractiveShellApp.ignore_cwd',
        "Exclude the current working directory from sys.path",
        "Include the current working directory in sys.path",
)
nosep_config = Config()
nosep_config.InteractiveShell.separate_in = ''
nosep_config.InteractiveShell.separate_out = ''
nosep_config.InteractiveShell.separate_out2 = ''

shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
shell_flags['pylab'] = (
    {'InteractiveShellApp' : {'pylab' : 'auto'}},
    """Pre-load matplotlib and numpy for interactive use with
    the default matplotlib backend."""
)
shell_flags['matplotlib'] = (
    {'InteractiveShellApp' : {'matplotlib' : 'auto'}},
    """Configure matplotlib for interactive use with
    the default matplotlib backend."""
)

# it's possible we don't want short aliases for *all* of these:
shell_aliases = dict(
    autocall='InteractiveShell.autocall',
    colors='InteractiveShell.colors',
    logfile='InteractiveShell.logfile',
    logappend='InteractiveShell.logappend',
    c='InteractiveShellApp.code_to_run',
    m='InteractiveShellApp.module_to_run',
    ext='InteractiveShellApp.extra_extension',
    gui='InteractiveShellApp.gui',
    pylab='InteractiveShellApp.pylab',
    matplotlib='InteractiveShellApp.matplotlib',
)
shell_aliases['cache-size'] = 'InteractiveShell.cache_size'

#-----------------------------------------------------------------------------
# Main classes and functions
#-----------------------------------------------------------------------------

class InteractiveShellApp(Configurable):
    """A Mixin for applications that start InteractiveShell instances.

    Provides configurables for loading extensions and executing files
    as part of configuring a Shell environment.

    The following methods should be called by the :meth:`initialize` method
    of the subclass:

      - :meth:`init_path`
      - :meth:`init_shell` (to be implemented by the subclass)
      - :meth:`init_gui_pylab`
      - :meth:`init_extensions`
      - :meth:`init_code`
    """
    extensions = List(Unicode(),
        help="A list of dotted module names of IPython extensions to load."
    ).tag(config=True)
    extra_extension = Unicode('',
        help="dotted module name of an IPython extension to load."
    ).tag(config=True)

    reraise_ipython_extension_failures = Bool(False,
        help="Reraise exceptions encountered loading IPython extensions?",
    ).tag(config=True)

    # Extensions that are always loaded (not configurable)
    default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)

    hide_initial_ns = Bool(True,
        help="""Should variables loaded at startup (by startup files, exec_lines, etc.)
        be hidden from tools like %who?"""
    ).tag(config=True)

    exec_files = List(Unicode(),
        help="""List of files to run at IPython startup."""
    ).tag(config=True)
    exec_PYTHONSTARTUP = Bool(True,
        help="""Run the file referenced by the PYTHONSTARTUP environment
        variable at IPython startup."""
    ).tag(config=True)
    file_to_run = Unicode('',
        help="""A file to be run""").tag(config=True)

    exec_lines = List(Unicode(),
        help="""lines of code to run at IPython startup."""
    ).tag(config=True)
    code_to_run = Unicode('',
        help="Execute the given command string."
    ).tag(config=True)
    module_to_run = Unicode('',
        help="Run the module as a script."
    ).tag(config=True)
    gui = CaselessStrEnum(gui_keys, allow_none=True,
        help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
    ).tag(config=True)
    matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
        help="""Configure matplotlib for interactive use with
        the default matplotlib backend."""
    ).tag(config=True)
    pylab = CaselessStrEnum(backend_keys, allow_none=True,
        help="""Pre-load matplotlib and numpy for interactive use,
        selecting a particular matplotlib backend and loop integration.
        """
    ).tag(config=True)
    pylab_import_all = Bool(True,
        help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
        and an ``import *`` is done from numpy and pylab, when using pylab mode.

        When False, pylab mode should not import any names into the user namespace.
        """
    ).tag(config=True)
    ignore_cwd = Bool(
        False,
        help="""If True, IPython will not add the current working directory to sys.path.
        When False, the current working directory is added to sys.path, allowing imports
        of modules defined in the current directory."""
    ).tag(config=True)
    shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
                     allow_none=True)
    # whether interact-loop should start
    interact = Bool(True)

    user_ns = Instance(dict, args=None, allow_none=True)
    @observe('user_ns')
    def _user_ns_changed(self, change):
        if self.shell is not None:
            self.shell.user_ns = change['new']
            self.shell.init_user_ns()

    def init_path(self):
        """Add current working directory, '', to sys.path

        Unlike Python's default, we insert before the first `site-packages`
        or `dist-packages` directory,
        so that it is after the standard library.

        .. versionchanged:: 7.2
            Try to insert after the standard library, instead of first.
        .. versionchanged:: 8.0
            Allow optionally not including the current directory in sys.path
        """
        if '' in sys.path or self.ignore_cwd:
            return
        for idx, path in enumerate(sys.path):
            parent, last_part = os.path.split(path)
            if last_part in {'site-packages', 'dist-packages'}:
                break
        else:
            # no site-packages or dist-packages found (?!)
            # back to original behavior of inserting at the front
            idx = 0
        sys.path.insert(idx, '')

    def init_shell(self):
        raise NotImplementedError("Override in subclasses")

    def init_gui_pylab(self):
        """Enable GUI event loop integration, taking pylab into account."""
        enable = False
        shell = self.shell
        if self.pylab:
            enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
            key = self.pylab
        elif self.matplotlib:
            enable = shell.enable_matplotlib
            key = self.matplotlib
        elif self.gui:
            enable = shell.enable_gui
            key = self.gui

        if not enable:
            return

        try:
            r = enable(key)
        except ImportError:
            self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?")
            self.shell.showtraceback()
            return
        except Exception:
            self.log.warning("GUI event loop or pylab initialization failed")
            self.shell.showtraceback()
            return

        if isinstance(r, tuple):
            gui, backend = r[:2]
            self.log.info("Enabling GUI event loop integration, "
                      "eventloop=%s, matplotlib=%s", gui, backend)
            if key == "auto":
                print("Using matplotlib backend: %s" % backend)
        else:
            gui = r
            self.log.info("Enabling GUI event loop integration, "
                      "eventloop=%s", gui)

    def init_extensions(self):
        """Load all IPython extensions in IPythonApp.extensions.

        This uses the :meth:`ExtensionManager.load_extensions` to load all
        the extensions listed in ``self.extensions``.
        """
        try:
            self.log.debug("Loading IPython extensions...")
            extensions = self.default_extensions + self.extensions
            if self.extra_extension:
                extensions.append(self.extra_extension)
            for ext in extensions:
                try:
                    self.log.info("Loading IPython extension: %s" % ext)
                    self.shell.extension_manager.load_extension(ext)
                except:
                    if self.reraise_ipython_extension_failures:
                        raise
                    msg = ("Error in loading extension: {ext}\n"
                           "Check your config files in {location}".format(
                               ext=ext,
                               location=self.profile_dir.location
                           ))
                    self.log.warning(msg, exc_info=True)
        except:
            if self.reraise_ipython_extension_failures:
                raise
            self.log.warning("Unknown error in loading extensions:", exc_info=True)

    def init_code(self):
        """run the pre-flight code, specified via exec_lines"""
        self._run_startup_files()
        self._run_exec_lines()
        self._run_exec_files()

        # Hide variables defined here from %who etc.
        if self.hide_initial_ns:
            self.shell.user_ns_hidden.update(self.shell.user_ns)

        # command-line execution (ipython -i script.py, ipython -m module)
        # should *not* be excluded from %whos
        self._run_cmd_line_code()
        self._run_module()

        # flush output, so itwon't be attached to the first cell
        sys.stdout.flush()
        sys.stderr.flush()

    def _run_exec_lines(self):
        """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
        if not self.exec_lines:
            return
        try:
            self.log.debug("Running code from IPythonApp.exec_lines...")
            for line in self.exec_lines:
                try:
                    self.log.info("Running code in user namespace: %s" %
                                  line)
                    self.shell.run_cell(line, store_history=False)
                except:
                    self.log.warning("Error in executing line in user "
                                  "namespace: %s" % line)
                    self.shell.showtraceback()
        except:
            self.log.warning("Unknown error in handling IPythonApp.exec_lines:")
            self.shell.showtraceback()

    def _exec_file(self, fname, shell_futures=False):
        try:
            full_filename = filefind(fname, [u'.', self.ipython_dir])
        except IOError:
            self.log.warning("File not found: %r"%fname)
            return
        # Make sure that the running script gets a proper sys.argv as if it
        # were run from a system shell.
        save_argv = sys.argv
        sys.argv = [full_filename] + self.extra_args[1:]
        try:
            if os.path.isfile(full_filename):
                self.log.info("Running file in user namespace: %s" %
                              full_filename)
                # Ensure that __file__ is always defined to match Python
                # behavior.
                with preserve_keys(self.shell.user_ns, '__file__'):
                    self.shell.user_ns['__file__'] = fname
                    if full_filename.endswith('.ipy') or full_filename.endswith('.ipynb'):
                        self.shell.safe_execfile_ipy(full_filename,
                                                     shell_futures=shell_futures)
                    else:
                        # default to python, even without extension
                        self.shell.safe_execfile(full_filename,
                                                 self.shell.user_ns,
                                                 shell_futures=shell_futures,
                                                 raise_exceptions=True)
        finally:
            sys.argv = save_argv

    def _run_startup_files(self):
        """Run files from profile startup directory"""
        startup_dirs = [self.profile_dir.startup_dir] + [
            os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS)
        ]
        startup_files = []

        if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
                not (self.file_to_run or self.code_to_run or self.module_to_run):
            python_startup = os.environ['PYTHONSTARTUP']
            self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
            try:
                self._exec_file(python_startup)
            except:
                self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
                self.shell.showtraceback()
        for startup_dir in startup_dirs[::-1]:
            startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
            startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
        if not startup_files:
            return

        self.log.debug("Running startup files from %s...", startup_dir)
        try:
            for fname in sorted(startup_files):
                self._exec_file(fname)
        except:
            self.log.warning("Unknown error in handling startup files:")
            self.shell.showtraceback()

    def _run_exec_files(self):
        """Run files from IPythonApp.exec_files"""
        if not self.exec_files:
            return

        self.log.debug("Running files in IPythonApp.exec_files...")
        try:
            for fname in self.exec_files:
                self._exec_file(fname)
        except:
            self.log.warning("Unknown error in handling IPythonApp.exec_files:")
            self.shell.showtraceback()

    def _run_cmd_line_code(self):
        """Run code or file specified at the command-line"""
        if self.code_to_run:
            line = self.code_to_run
            try:
                self.log.info("Running code given at command line (c=): %s" %
                              line)
                self.shell.run_cell(line, store_history=False)
            except:
                self.log.warning("Error in executing line in user namespace: %s" %
                              line)
                self.shell.showtraceback()
                if not self.interact:
                    self.exit(1)

        # Like Python itself, ignore the second if the first of these is present
        elif self.file_to_run:
            fname = self.file_to_run
            if os.path.isdir(fname):
                fname = os.path.join(fname, "__main__.py")
            if not os.path.exists(fname):
                self.log.warning("File '%s' doesn't exist", fname)
                self.exit(2)
            try:
                self._exec_file(fname, shell_futures=True)
            except:
                self.shell.showtraceback(tb_offset=4)
                if not self.interact:
                    self.exit(1)

    def _run_module(self):
        """Run module specified at the command-line."""
        if self.module_to_run:
            # Make sure that the module gets a proper sys.argv as if it were
            # run using `python -m`.
            save_argv = sys.argv
            sys.argv = [sys.executable] + self.extra_args
            try:
                self.shell.safe_run_module(self.module_to_run,
                                           self.shell.user_ns)
            finally:
                sys.argv = save_argv
<commit_msg>Do not exist if self.interact is True<commit_after># encoding: utf-8
"""
A mixin for :class:`~IPython.core.application.Application` classes that
launch InteractiveShell instances, load extensions, etc.
"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import glob
from itertools import chain
import os
import sys

from traitlets.config.application import boolean_flag
from traitlets.config.configurable import Configurable
from traitlets.config.loader import Config
from IPython.core.application import SYSTEM_CONFIG_DIRS, ENV_CONFIG_DIRS
from IPython.core import pylabtools
from IPython.utils.contexts import preserve_keys
from IPython.utils.path import filefind
from traitlets import (
    Unicode, Instance, List, Bool, CaselessStrEnum, observe,
)
from IPython.terminal import pt_inputhooks

#-----------------------------------------------------------------------------
# Aliases and Flags
#-----------------------------------------------------------------------------

gui_keys = tuple(sorted(pt_inputhooks.backends) + sorted(pt_inputhooks.aliases))

backend_keys = sorted(pylabtools.backends.keys())
backend_keys.insert(0, 'auto')

shell_flags = {}

addflag = lambda *args: shell_flags.update(boolean_flag(*args))
addflag('autoindent', 'InteractiveShell.autoindent',
        'Turn on autoindenting.', 'Turn off autoindenting.'
)
addflag('automagic', 'InteractiveShell.automagic',
        """Turn on the auto calling of magic commands. Type %%magic at the
        IPython  prompt  for  more information.""",
        'Turn off the auto calling of magic commands.'
)
addflag('pdb', 'InteractiveShell.pdb',
    "Enable auto calling the pdb debugger after every exception.",
    "Disable auto calling the pdb debugger after every exception."
)
addflag('pprint', 'PlainTextFormatter.pprint',
    "Enable auto pretty printing of results.",
    "Disable auto pretty printing of results."
)
addflag('color-info', 'InteractiveShell.color_info',
    """IPython can display information about objects via a set of functions,
    and optionally can use colors for this, syntax highlighting
    source code and various other elements. This is on by default, but can cause
    problems with some pagers. If you see such problems, you can disable the
    colours.""",
    "Disable using colors for info related things."
)
addflag('ignore-cwd', 'InteractiveShellApp.ignore_cwd',
        "Exclude the current working directory from sys.path",
        "Include the current working directory in sys.path",
)
nosep_config = Config()
nosep_config.InteractiveShell.separate_in = ''
nosep_config.InteractiveShell.separate_out = ''
nosep_config.InteractiveShell.separate_out2 = ''

shell_flags['nosep']=(nosep_config, "Eliminate all spacing between prompts.")
shell_flags['pylab'] = (
    {'InteractiveShellApp' : {'pylab' : 'auto'}},
    """Pre-load matplotlib and numpy for interactive use with
    the default matplotlib backend."""
)
shell_flags['matplotlib'] = (
    {'InteractiveShellApp' : {'matplotlib' : 'auto'}},
    """Configure matplotlib for interactive use with
    the default matplotlib backend."""
)

# it's possible we don't want short aliases for *all* of these:
shell_aliases = dict(
    autocall='InteractiveShell.autocall',
    colors='InteractiveShell.colors',
    logfile='InteractiveShell.logfile',
    logappend='InteractiveShell.logappend',
    c='InteractiveShellApp.code_to_run',
    m='InteractiveShellApp.module_to_run',
    ext='InteractiveShellApp.extra_extension',
    gui='InteractiveShellApp.gui',
    pylab='InteractiveShellApp.pylab',
    matplotlib='InteractiveShellApp.matplotlib',
)
shell_aliases['cache-size'] = 'InteractiveShell.cache_size'

#-----------------------------------------------------------------------------
# Main classes and functions
#-----------------------------------------------------------------------------

class InteractiveShellApp(Configurable):
    """A Mixin for applications that start InteractiveShell instances.

    Provides configurables for loading extensions and executing files
    as part of configuring a Shell environment.

    The following methods should be called by the :meth:`initialize` method
    of the subclass:

      - :meth:`init_path`
      - :meth:`init_shell` (to be implemented by the subclass)
      - :meth:`init_gui_pylab`
      - :meth:`init_extensions`
      - :meth:`init_code`
    """
    extensions = List(Unicode(),
        help="A list of dotted module names of IPython extensions to load."
    ).tag(config=True)
    extra_extension = Unicode('',
        help="dotted module name of an IPython extension to load."
    ).tag(config=True)

    reraise_ipython_extension_failures = Bool(False,
        help="Reraise exceptions encountered loading IPython extensions?",
    ).tag(config=True)

    # Extensions that are always loaded (not configurable)
    default_extensions = List(Unicode(), [u'storemagic']).tag(config=False)

    hide_initial_ns = Bool(True,
        help="""Should variables loaded at startup (by startup files, exec_lines, etc.)
        be hidden from tools like %who?"""
    ).tag(config=True)

    exec_files = List(Unicode(),
        help="""List of files to run at IPython startup."""
    ).tag(config=True)
    exec_PYTHONSTARTUP = Bool(True,
        help="""Run the file referenced by the PYTHONSTARTUP environment
        variable at IPython startup."""
    ).tag(config=True)
    file_to_run = Unicode('',
        help="""A file to be run""").tag(config=True)

    exec_lines = List(Unicode(),
        help="""lines of code to run at IPython startup."""
    ).tag(config=True)
    code_to_run = Unicode('',
        help="Execute the given command string."
    ).tag(config=True)
    module_to_run = Unicode('',
        help="Run the module as a script."
    ).tag(config=True)
    gui = CaselessStrEnum(gui_keys, allow_none=True,
        help="Enable GUI event loop integration with any of {0}.".format(gui_keys)
    ).tag(config=True)
    matplotlib = CaselessStrEnum(backend_keys, allow_none=True,
        help="""Configure matplotlib for interactive use with
        the default matplotlib backend."""
    ).tag(config=True)
    pylab = CaselessStrEnum(backend_keys, allow_none=True,
        help="""Pre-load matplotlib and numpy for interactive use,
        selecting a particular matplotlib backend and loop integration.
        """
    ).tag(config=True)
    pylab_import_all = Bool(True,
        help="""If true, IPython will populate the user namespace with numpy, pylab, etc.
        and an ``import *`` is done from numpy and pylab, when using pylab mode.

        When False, pylab mode should not import any names into the user namespace.
        """
    ).tag(config=True)
    ignore_cwd = Bool(
        False,
        help="""If True, IPython will not add the current working directory to sys.path.
        When False, the current working directory is added to sys.path, allowing imports
        of modules defined in the current directory."""
    ).tag(config=True)
    shell = Instance('IPython.core.interactiveshell.InteractiveShellABC',
                     allow_none=True)
    # whether interact-loop should start
    interact = Bool(True)

    user_ns = Instance(dict, args=None, allow_none=True)
    @observe('user_ns')
    def _user_ns_changed(self, change):
        if self.shell is not None:
            self.shell.user_ns = change['new']
            self.shell.init_user_ns()

    def init_path(self):
        """Add current working directory, '', to sys.path

        Unlike Python's default, we insert before the first `site-packages`
        or `dist-packages` directory,
        so that it is after the standard library.

        .. versionchanged:: 7.2
            Try to insert after the standard library, instead of first.
        .. versionchanged:: 8.0
            Allow optionally not including the current directory in sys.path
        """
        if '' in sys.path or self.ignore_cwd:
            return
        for idx, path in enumerate(sys.path):
            parent, last_part = os.path.split(path)
            if last_part in {'site-packages', 'dist-packages'}:
                break
        else:
            # no site-packages or dist-packages found (?!)
            # back to original behavior of inserting at the front
            idx = 0
        sys.path.insert(idx, '')

    def init_shell(self):
        raise NotImplementedError("Override in subclasses")

    def init_gui_pylab(self):
        """Enable GUI event loop integration, taking pylab into account."""
        enable = False
        shell = self.shell
        if self.pylab:
            enable = lambda key: shell.enable_pylab(key, import_all=self.pylab_import_all)
            key = self.pylab
        elif self.matplotlib:
            enable = shell.enable_matplotlib
            key = self.matplotlib
        elif self.gui:
            enable = shell.enable_gui
            key = self.gui

        if not enable:
            return

        try:
            r = enable(key)
        except ImportError:
            self.log.warning("Eventloop or matplotlib integration failed. Is matplotlib installed?")
            self.shell.showtraceback()
            return
        except Exception:
            self.log.warning("GUI event loop or pylab initialization failed")
            self.shell.showtraceback()
            return

        if isinstance(r, tuple):
            gui, backend = r[:2]
            self.log.info("Enabling GUI event loop integration, "
                      "eventloop=%s, matplotlib=%s", gui, backend)
            if key == "auto":
                print("Using matplotlib backend: %s" % backend)
        else:
            gui = r
            self.log.info("Enabling GUI event loop integration, "
                      "eventloop=%s", gui)

    def init_extensions(self):
        """Load all IPython extensions in IPythonApp.extensions.

        This uses the :meth:`ExtensionManager.load_extensions` to load all
        the extensions listed in ``self.extensions``.
        """
        try:
            self.log.debug("Loading IPython extensions...")
            extensions = self.default_extensions + self.extensions
            if self.extra_extension:
                extensions.append(self.extra_extension)
            for ext in extensions:
                try:
                    self.log.info("Loading IPython extension: %s" % ext)
                    self.shell.extension_manager.load_extension(ext)
                except:
                    if self.reraise_ipython_extension_failures:
                        raise
                    msg = ("Error in loading extension: {ext}\n"
                           "Check your config files in {location}".format(
                               ext=ext,
                               location=self.profile_dir.location
                           ))
                    self.log.warning(msg, exc_info=True)
        except:
            if self.reraise_ipython_extension_failures:
                raise
            self.log.warning("Unknown error in loading extensions:", exc_info=True)

    def init_code(self):
        """run the pre-flight code, specified via exec_lines"""
        self._run_startup_files()
        self._run_exec_lines()
        self._run_exec_files()

        # Hide variables defined here from %who etc.
        if self.hide_initial_ns:
            self.shell.user_ns_hidden.update(self.shell.user_ns)

        # command-line execution (ipython -i script.py, ipython -m module)
        # should *not* be excluded from %whos
        self._run_cmd_line_code()
        self._run_module()

        # flush output, so itwon't be attached to the first cell
        sys.stdout.flush()
        sys.stderr.flush()

    def _run_exec_lines(self):
        """Run lines of code in IPythonApp.exec_lines in the user's namespace."""
        if not self.exec_lines:
            return
        try:
            self.log.debug("Running code from IPythonApp.exec_lines...")
            for line in self.exec_lines:
                try:
                    self.log.info("Running code in user namespace: %s" %
                                  line)
                    self.shell.run_cell(line, store_history=False)
                except:
                    self.log.warning("Error in executing line in user "
                                  "namespace: %s" % line)
                    self.shell.showtraceback()
        except:
            self.log.warning("Unknown error in handling IPythonApp.exec_lines:")
            self.shell.showtraceback()

    def _exec_file(self, fname, shell_futures=False):
        try:
            full_filename = filefind(fname, [u'.', self.ipython_dir])
        except IOError:
            self.log.warning("File not found: %r"%fname)
            return
        # Make sure that the running script gets a proper sys.argv as if it
        # were run from a system shell.
        save_argv = sys.argv
        sys.argv = [full_filename] + self.extra_args[1:]
        try:
            if os.path.isfile(full_filename):
                self.log.info("Running file in user namespace: %s" %
                              full_filename)
                # Ensure that __file__ is always defined to match Python
                # behavior.
                with preserve_keys(self.shell.user_ns, '__file__'):
                    self.shell.user_ns['__file__'] = fname
                    if full_filename.endswith('.ipy') or full_filename.endswith('.ipynb'):
                        self.shell.safe_execfile_ipy(full_filename,
                                                     shell_futures=shell_futures)
                    else:
                        # default to python, even without extension
                        self.shell.safe_execfile(full_filename,
                                                 self.shell.user_ns,
                                                 shell_futures=shell_futures,
                                                 raise_exceptions=True)
        finally:
            sys.argv = save_argv

    def _run_startup_files(self):
        """Run files from profile startup directory"""
        startup_dirs = [self.profile_dir.startup_dir] + [
            os.path.join(p, 'startup') for p in chain(ENV_CONFIG_DIRS, SYSTEM_CONFIG_DIRS)
        ]
        startup_files = []

        if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
                not (self.file_to_run or self.code_to_run or self.module_to_run):
            python_startup = os.environ['PYTHONSTARTUP']
            self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
            try:
                self._exec_file(python_startup)
            except:
                self.log.warning("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
                self.shell.showtraceback()
        for startup_dir in startup_dirs[::-1]:
            startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
            startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
        if not startup_files:
            return

        self.log.debug("Running startup files from %s...", startup_dir)
        try:
            for fname in sorted(startup_files):
                self._exec_file(fname)
        except:
            self.log.warning("Unknown error in handling startup files:")
            self.shell.showtraceback()

    def _run_exec_files(self):
        """Run files from IPythonApp.exec_files"""
        if not self.exec_files:
            return

        self.log.debug("Running files in IPythonApp.exec_files...")
        try:
            for fname in self.exec_files:
                self._exec_file(fname)
        except:
            self.log.warning("Unknown error in handling IPythonApp.exec_files:")
            self.shell.showtraceback()

    def _run_cmd_line_code(self):
        """Run code or file specified at the command-line"""
        if self.code_to_run:
            line = self.code_to_run
            try:
                self.log.info("Running code given at command line (c=): %s" %
                              line)
                self.shell.run_cell(line, store_history=False)
            except:
                self.log.warning("Error in executing line in user namespace: %s" %
                              line)
                self.shell.showtraceback()
                if not self.interact:
                    self.exit(1)

        # Like Python itself, ignore the second if the first of these is present
        elif self.file_to_run:
            fname = self.file_to_run
            if os.path.isdir(fname):
                fname = os.path.join(fname, "__main__.py")
            if not os.path.exists(fname):
                self.log.warning("File '%s' doesn't exist", fname)
                if not self.interact:
                    self.exit(2)
            try:
                self._exec_file(fname, shell_futures=True)
            except:
                self.shell.showtraceback(tb_offset=4)
                if not self.interact:
                    self.exit(1)

    def _run_module(self):
        """Run module specified at the command-line."""
        if self.module_to_run:
            # Make sure that the module gets a proper sys.argv as if it were
            # run using `python -m`.
            save_argv = sys.argv
            sys.argv = [sys.executable] + self.extra_args
            try:
                self.shell.safe_run_module(self.module_to_run,
                                           self.shell.user_ns)
            finally:
                sys.argv = save_argv
<|endoftext|>


<commit_before>import json

from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from cms.models import Placeholder
from cms.api import create_page, add_plugin, publish_page

placeholder = {}

with open('starting_page.json') as data_file:
    content = json.load(data_file)

try:
    # try to get a feature template with fallback
    template = settings.CMS_TEMPLATES[1][0]
    if template != 'feature.html':
        template = settings.CMS_TEMPLATES[0][0]
except IndexError:
    template = settings.CMS_TEMPLATES[0][0]

lang = settings.LANGUAGES[0][0]
page = create_page(_('Home'), template, lang)
placeholder['main'] = page.placeholders.get(slot='content')

try:
    # try to get a feature placeholder
    placeholder_feature = page.placeholders.get(slot='feature')
    p = add_plugin(placeholder_feature, 'TextPlugin', lang,
                   body=content['feature'])
except Placeholder.DoesNotExist:
    # fallback, add it to the
    add_plugin(placeholder['main'], 'TextPlugin', lang, body=content['feature'])
    pass

# Add main content to a MultiColumnPlugin
multi_columns_plugin = add_plugin(placeholder['main'], 'MultiColumnPlugin', lang)
for column_content in content['main']:
    col = add_plugin(placeholder['main'], 'ColumnPlugin', lang,
                     target=multi_columns_plugin, **{'width': '33%'})
    add_plugin(placeholder['main'], 'TextPlugin', lang, body=column_content,
               target=col)

publish_page(page, User.objects.all()[0])
<commit_msg>Support for seperate language publishing<commit_after>import json

from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

from cms.models import Placeholder
from cms.api import create_page, add_plugin, publish_page

placeholder = {}

with open('starting_page.json') as data_file:
    content = json.load(data_file)

try:
    # try to get a feature template with fallback
    template = settings.CMS_TEMPLATES[1][0]
    if template != 'feature.html':
        template = settings.CMS_TEMPLATES[0][0]
except IndexError:
    template = settings.CMS_TEMPLATES[0][0]

lang = settings.LANGUAGES[0][0]
page = create_page(_('Home'), template, lang)
placeholder['main'] = page.placeholders.get(slot='content')

try:
    # try to get a feature placeholder
    placeholder_feature = page.placeholders.get(slot='feature')
    p = add_plugin(placeholder_feature, 'TextPlugin', lang,
                   body=content['feature'])
except Placeholder.DoesNotExist:
    # fallback, add it to the
    add_plugin(placeholder['main'], 'TextPlugin', lang, body=content['feature'])
    pass

# Add main content to a MultiColumnPlugin
multi_columns_plugin = add_plugin(placeholder['main'], 'MultiColumnPlugin', lang)
for column_content in content['main']:
    col = add_plugin(placeholder['main'], 'ColumnPlugin', lang,
                     target=multi_columns_plugin, **{'width': '33%'})
    add_plugin(placeholder['main'], 'TextPlugin', lang, body=column_content,
               target=col)

try:
    publish_page(page, User.objects.all()[0], lang)
except TypeError:
    # supporting old cms versions
    publish_page(page, User.objects.all()[0])
<|endoftext|>