<commit_before>from kiss.views.templates import TemplateResponse
from kiss.views.core import RedirectResponse
from kiss.controllers.core import Controller
import requests
import json
from werkzeug.urls import url_decode
from putils.types import Dict, Regex
from putils.patterns import Singleton
from urlparse import urljoin
from werkzeug.urls import url_encode

class AuthBackend(object):
	"""
	Base oauth backend
	"""
	def get_code_url(self, request, options):
		params = {
			"client_id": options["client_id"],
			"redirect_uri": options["redirect_uri"],
			"scope": options["scope"],
			"response_type": "code",
			"approval_prompt": "force",
			"access_type": "offline"
		}
		return "%s?%s" % (options["authorization_uri"], url_encode(params))

	def get_access_token(self, request, options):
		params = {
			"client_id": options["client_id"],
			"client_secret": options["client_secret"],
			"grant_type": "authorization_code",
			"code": request.args["code"],
			"redirect_uri": options["redirect_uri"]
		}
		response = requests.post(options["get_token_uri"], params).text
		return self.prepare_access_token_response(response)

	def prepare_access_token_response(self, response):
		return json.loads(response)

	def get_user_info(self, request, options, access_token_result):
		self.access_token = access_token_result["access_token"]
		params = self.prepare_user_info_request_params(access_token_result)
		user_info_response = json.loads(requests.get("%s?%s" % (options["target_uri"], url_encode(params)), auth=self.auth).text)
		user_info_response = self.process_user_info_response(request, user_info_response)
		user_info_response["provider"] = request.params["backend"]
		user_info_response["access_token"] = params["access_token"]
		return user_info_response

	def prepare_user_info_request_params(self, access_token_result):
		return {"access_token": access_token_result["access_token"]}

	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		return result

	def auth(self, request):
		request.headers["Authorization"] = "Bearer %s" % self.access_token
		return request

class GoogleAuthBackend(AuthBackend):
	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		result["email"] = user_info_response["email"]
		result["name"] = user_info_response["name"]
		return result

class VkAuthBackend(AuthBackend):
	def prepare_user_info_request_params(self, access_token_result):
		return {"access_token": access_token_result["access_token"], "uids": access_token_result["user_id"], "fields": "uid, first_name, last_name, nickname, screen_name, sex, bdate, city, country, photo, photo_medium, photo_big"}

	def process_user_info_response(self, request, user_info_response):
		result = {}
		user_info_response = user_info_response["response"][0]
		result["id"] = user_info_response["uid"]
		result["name"] = "%s %s" % (user_info_response["first_name"], user_info_response["last_name"])
		return result

class FacebookAuthBackend(AuthBackend):
	def prepare_access_token_response(self, response):
		return url_decode(response)

	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		result["email"] = user_info_response["email"]
		result["name"] = user_info_response["name"]
		return result

class YandexAuthBackend(AuthBackend):
	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		result["email"] = user_info_response["default_email"]
		result["name"] = user_info_response["real_name"]
		return result

class AuthManager(Singleton):
	"""
	Auth manager which calls appropriate backend
	"""
	def __init__(self, opts):
		self.options = {
			"common": {
				"base_uri": "http://localhost:8080/auth/",
				"success_uri": "success/",
				"error_uri": "error/"
			},
			"google": {
				"authorization_uri": "https://accounts.google.com/o/oauth2/auth",
				"scope": "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
				"get_token_uri": "https://accounts.google.com/o/oauth2/token",
				"redirect_uri": "google/callback",
				"target_uri": "https://www.googleapis.com/oauth2/v1/userinfo",
				"backend": GoogleAuthBackend()
			},
			"vk": {
				"authorization_uri": "http://api.vk.com/oauth/authorize",
				"scope": "",
				"get_token_uri": "https://api.vk.com/oauth/token",
				"redirect_uri": "vk/callback",
				"target_uri": "https://api.vk.com/method/users.get",
				"backend": VkAuthBackend()
			},
			"facebook": {
				"authorization_uri": "https://www.facebook.com/dialog/oauth",
				"scope": "email",
				"get_token_uri": "https://graph.facebook.com/oauth/access_token",
				"redirect_uri": "facebook/callback",
				"target_uri": "https://graph.facebook.com/me",
				"backend": FacebookAuthBackend()
			},
			"yandex": {
				"authorization_uri": "https://oauth.yandex.ru/authorize",
				"scope": "",
				"get_token_uri": "https://oauth.yandex.ru/token",
				"redirect_uri": "yandex/callback",
				"target_uri": "https://login.yandex.ru/info",
				"backend": YandexAuthBackend()
			}
		}
		self.options = Dict.merge(self.options, opts)
		base_uri = self.options["common"]["base_uri"]
		self.options["common"]["success_uri"] = urljoin(base_uri, self.options["common"]["success_uri"])
		self.options["common"]["error_uri"] = urljoin(base_uri, self.options["common"]["error_uri"])
		for backend, params in self.options.items():
			if "redirect_uri" in params:
				params["redirect_uri"] = urljoin(base_uri, params["redirect_uri"])

	def get_provider_url(self, request):
		current_options = self.options[request.params["backend"]]
		return current_options["backend"].get_code_url(request, current_options)

	def get_result_url(self, request):
		current_options = self.options[request.params["backend"]]
		access_token_result = current_options["backend"].get_access_token(request, current_options)
		if "access_token" not in access_token_result or not access_token_result["access_token"]:
			return self.options["common"]["error_uri"]
		user_info_response = current_options["backend"].get_user_info(request, current_options, access_token_result)
		return "%s?%s" % (self.options["common"]["success_uri"], url_encode(user_info_response))

class AuthController(object):
	"""
	Controller for social auth, use it in your url mappings
	"""
	def __new__(cls, opts):
		AuthManager(opts)
		return {
			Regex.string_url_regex("backend"): {
				"": StartAuthController,
				"callback": EndAuthController,
			}
		}

class StartAuthController(Controller):
	"""
	Controller which starts oauth flow.
	"""
	def get(self, request):
		return RedirectResponse(AuthManager().get_provider_url(request))

class EndAuthController(Controller):
	"""
	Controller which finishes oauth flow.
	"""
	def get(self, request):
		return RedirectResponse(AuthManager().get_result_url(request))

<commit_msg>small fix in auth<commit_after>from kiss.views.templates import TemplateResponse
from kiss.views.core import RedirectResponse
from kiss.controllers.core import Controller
import requests
import json
from werkzeug.urls import url_decode
from putils.types import Dict, Regex
from putils.patterns import Singleton
from urlparse import urljoin
from werkzeug.urls import url_encode

class AuthBackend(object):
	"""
	Base oauth backend
	"""
	def get_code_url(self, request, options):
		params = {
			"client_id": options["client_id"],
			"redirect_uri": options["redirect_uri"],
			"scope": options["scope"],
			"response_type": "code",
			"approval_prompt": "force",
			"access_type": "offline"
		}
		return "%s?%s" % (options["authorization_uri"], url_encode(params))

	def get_access_token(self, request, options):
		params = {
			"client_id": options["client_id"],
			"client_secret": options["client_secret"],
			"grant_type": "authorization_code",
			"code": request.args["code"],
			"redirect_uri": options["redirect_uri"]
		}
		response = requests.post(options["get_token_uri"], params).text
		return self.prepare_access_token_response(response)

	def prepare_access_token_response(self, response):
		return json.loads(response)

	def get_user_info(self, request, options, access_token_result):
		self.access_token = access_token_result["access_token"]
		params = self.prepare_user_info_request_params(access_token_result)
		user_info_response = json.loads(requests.get("%s?%s" % (options["target_uri"], url_encode(params)), auth=self.auth).text)
		user_info_response = self.process_user_info_response(request, user_info_response)
		user_info_response["provider"] = request.params["backend"]
		user_info_response["access_token"] = params["access_token"]
		return user_info_response

	def prepare_user_info_request_params(self, access_token_result):
		return {"access_token": access_token_result["access_token"]}

	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		return result

	def auth(self, request):
		request.headers["Authorization"] = "Bearer %s" % self.access_token
		return request

class GoogleAuthBackend(AuthBackend):
	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		result["email"] = user_info_response["email"]
		result["name"] = user_info_response["name"]
		return result

class VkAuthBackend(AuthBackend):
	def prepare_user_info_request_params(self, access_token_result):
		return {"access_token": access_token_result["access_token"], "uids": access_token_result["user_id"], "fields": "uid, first_name, last_name, nickname, screen_name, sex, bdate, city, country, photo, photo_medium, photo_big"}

	def process_user_info_response(self, request, user_info_response):
		result = {}
		user_info_response = user_info_response["response"][0]
		result["id"] = user_info_response["uid"]
		result["name"] = "%s %s" % (user_info_response["first_name"], user_info_response["last_name"])
		return result

class FacebookAuthBackend(AuthBackend):
	def prepare_access_token_response(self, response):
		return url_decode(response)

	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		result["email"] = user_info_response["email"]
		result["name"] = user_info_response["name"]
		return result

class YandexAuthBackend(AuthBackend):
	def process_user_info_response(self, request, user_info_response):
		result = {}
		result["id"] = user_info_response["id"]
		result["email"] = user_info_response["default_email"]
		result["name"] = user_info_response["real_name"]
		return result

class AuthManager(Singleton):
	"""
	Auth manager which calls appropriate backend
	"""
	def __init__(self, opts):
		self.options = {
			"common": {
				"base_uri": "http://localhost:8080/auth/",
				"success_uri": "success/",
				"error_uri": "error/"
			},
			"google": {
				"authorization_uri": "https://accounts.google.com/o/oauth2/auth",
				"scope": "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email",
				"get_token_uri": "https://accounts.google.com/o/oauth2/token",
				"redirect_uri": "google/callback",
				"target_uri": "https://www.googleapis.com/oauth2/v1/userinfo",
				"backend": GoogleAuthBackend()
			},
			"vk": {
				"authorization_uri": "http://api.vk.com/oauth/authorize",
				"scope": "",
				"get_token_uri": "https://api.vk.com/oauth/token",
				"redirect_uri": "vk/callback",
				"target_uri": "https://api.vk.com/method/users.get",
				"backend": VkAuthBackend()
			},
			"facebook": {
				"authorization_uri": "https://www.facebook.com/dialog/oauth",
				"scope": "email",
				"get_token_uri": "https://graph.facebook.com/oauth/access_token",
				"redirect_uri": "facebook/callback",
				"target_uri": "https://graph.facebook.com/me",
				"backend": FacebookAuthBackend()
			},
			"yandex": {
				"authorization_uri": "https://oauth.yandex.ru/authorize",
				"scope": "",
				"get_token_uri": "https://oauth.yandex.ru/token",
				"redirect_uri": "yandex/callback",
				"target_uri": "https://login.yandex.ru/info",
				"backend": YandexAuthBackend()
			}
		}
		self.options = Dict.merge(self.options, opts)
		base_uri = self.options["common"]["base_uri"]
		self.options["common"]["success_uri"] = urljoin(base_uri, self.options["common"]["success_uri"])
		self.options["common"]["error_uri"] = urljoin(base_uri, self.options["common"]["error_uri"])
		for backend, params in self.options.items():
			if "redirect_uri" in params:
				params["redirect_uri"] = urljoin(base_uri, params["redirect_uri"])

	def get_provider_url(self, request):
		current_options = self.options[request.params["backend"]]
		return current_options["backend"].get_code_url(request, current_options)

	def get_result_url(self, request):
		current_options = self.options[request.params["backend"]]
		access_token_result = current_options["backend"].get_access_token(request, current_options)
		if "access_token" not in access_token_result or not access_token_result["access_token"]:
			return self.options["common"]["error_uri"]
		user_info_response = current_options["backend"].get_user_info(request, current_options, access_token_result)
		return "%s?%s" % (self.options["common"]["success_uri"], url_encode(user_info_response))

class AuthController(object):
	"""
	Controller for social auth, use it in your url mappings
	"""
	def __new__(cls, opts):
		AuthManager(opts)
		return {
			Regex.string_url_regex("backend"): {
				"": StartAuthController,
				"callback": EndAuthController,
			}
		}

class StartAuthController(Controller):
	"""
	Controller which starts oauth flow.
	"""
	def get(self, request):
		if "success_uri" in request.args:
			AuthManager().options["common"]["success_uri"] = request.args["success_uri"]
		if "error_uri" in request.args:
			AuthManager().options["common"]["error_uri"] = request.args["error_uri"]
		return RedirectResponse(AuthManager().get_provider_url(request))

class EndAuthController(Controller):
	"""
	Controller which finishes oauth flow.
	"""
	def get(self, request):
		return RedirectResponse(AuthManager().get_result_url(request))

<|endoftext|>


<commit_before>"""
	initializate CstrikeRCON as a module
	to be imported by third parties
"""

import CstrikeRCON
<commit_msg>rm newline<commit_after>"""
	initializate CstrikeRCON as a module
	to be imported by third parties
"""
import CstrikeRCON
<|endoftext|>


<commit_before>import random
import json

def poor_big_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 40000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(0,12):
        poor_status = random.uniform(0, 0.5)
        saving_rate = random.uniform(0,3)
        spending_rate = random.uniform(7,10)
        monthly_expenditure = monthly_income/saving_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

def saver_big_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 200000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(0,12):
        poor_status = random.uniform(0, 1)
        saving_rate = random.uniform(7,10)
        spending_rate = random.uniform(7,10)
        monthly_expenditure = monthly_income/saving_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

def saver_small_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 200000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(0,12):
        poor_status = random.uniform(0, 1)
        saving_rate = random.uniform(7,10)
        spending_rate = random.uniform(0,3)
        monthly_expenditure = monthly_income/spending_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

def average_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 200000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(0,12):
        poor_status = random.uniform(0, 1)
        saving_rate = random.uniform(4,7)
        spending_rate = random.uniform(4,7)
        monthly_expenditure = monthly_income/spending_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

if __name__ == "__main__":
    data = []
    targets = []
    for i in range(0, 1000000000):
        rand = random.randint(0, 4)
        targets.append(rand)
        if(rand == 0): data.append(poor_big_spender())
        elif (rand == 1): data.append(saver_big_spender())
        elif (rand == 2): data.append(saver_small_spender())
        else: data.append(average_spender())

    file = open('data.txt', 'w')
    file.write(json.dumps(data))
    file.close()

    file = open('targets.txt', 'w')
    file.write(json.dumps(targets))
    file.close()
<commit_msg>Update dataset generator<commit_after>import random
import json

def poor_big_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 40000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(1,12):
        poor_status = random.uniform(0, 0.5)
        saving_rate = random.uniform(0,3)
        spending_rate = random.uniform(7,10)
        monthly_expenditure = monthly_income/saving_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

def saver_big_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 200000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(1,12):
        poor_status = random.uniform(0, 1)
        saving_rate = random.uniform(7,10)
        spending_rate = random.uniform(7,10)
        monthly_expenditure = monthly_income/saving_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

def saver_small_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 200000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(1,12):
        poor_status = random.uniform(0, 1)
        saving_rate = random.uniform(7,10)
        spending_rate = random.uniform(0,3)
        monthly_expenditure = monthly_income/spending_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

def average_spender():
    data = [0,0,0,0,0,0]
    age = random.randint(16, 80)
    total_income = random.randint(6000, 200000)+random.randint(-1, 1)*random.randint(1000, 3000)
    monthly_income = total_income/12.0
    rcs = -0.6
    for i in range(1,12):
        poor_status = random.uniform(0, 1)
        saving_rate = random.uniform(4,7)
        spending_rate = random.uniform(4,7)
        monthly_expenditure = monthly_income/spending_rate
        account_total = total_income*(1.0-poor_status)
        rcs = (monthly_income-monthly_expenditure)/monthly_income
        data = [x + y for x, y in zip(data, [monthly_income, monthly_expenditure, total_income, rcs, age, account_total])]
    data = [x / 12 for x in data]
    return data

if __name__ == "__main__":
    data = []
    targets = []
    for i in range(0, 1000000):
        rand = random.randint(0, 3)
        targets.append(rand)
        if(rand == 0): data.append(poor_big_spender())
        elif (rand == 1): data.append(saver_big_spender())
        elif (rand == 2): data.append(saver_small_spender())
        else: data.append(average_spender())

    file = open('data.txt', 'w')
    file.write(json.dumps(data))
    file.close()

    file = open('targets.txt', 'w')
    file.write(json.dumps(targets))
    file.close()
<|endoftext|>


<commit_before>"""Test suite for our JSON utilities.
"""
#-----------------------------------------------------------------------------
#  Copyright (C) 2010-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING.txt, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# stdlib
import datetime
import json
from base64 import decodestring

# third party
import nose.tools as nt

# our own
from IPython.utils import jsonutil, tz
from ..jsonutil import json_clean, encode_images
from ..py3compat import unicode_to_str, str_to_bytes, iteritems

#-----------------------------------------------------------------------------
# Test functions
#-----------------------------------------------------------------------------
class Int(int):
    def __str__(self):
        return 'Int(%i)' % self

def test():
    # list of input/expected output.  Use None for the expected output if it
    # can be the same as the input.
    pairs = [(1, None), # start with scalars
             (1.0, None),
             ('a', None),
             (True, None),
             (False, None),
             (None, None),
             # complex numbers for now just go to strings, as otherwise they
             # are unserializable
             (1j, '1j'),
             # Containers
             ([1, 2], None),
             ((1, 2), [1, 2]),
             (set([1, 2]), [1, 2]),
             (dict(x=1), None),
             ({'x': 1, 'y':[1,2,3], '1':'int'}, None),
             # More exotic objects
             ((x for x in range(3)), [0, 1, 2]),
             (iter([1, 2]), [1, 2]),
             (Int(5), 5),
             ]

    for val, jval in pairs:
        if jval is None:
            jval = val
        out = json_clean(val)
        # validate our cleanup
        nt.assert_equal(out, jval)
        # and ensure that what we return, indeed encodes cleanly
        json.loads(json.dumps(out))

def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'

    fmt = {
        'image/png'  : pngdata,
        'image/jpeg' : jpegdata,
        'application/pdf' : pdfdata
    }
    encoded = encode_images(fmt)
    for key, value in iteritems(fmt):
        # encoded has unicode, want bytes
        decoded = decodestring(encoded[key].encode('ascii'))
        nt.assert_equal(decoded, value)
    encoded2 = encode_images(encoded)
    nt.assert_equal(encoded, encoded2)

    b64_str = {}
    for key, encoded in iteritems(encoded):
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = encode_images(b64_str)
    nt.assert_equal(encoded3, b64_str)
    for key, value in iteritems(fmt):
        # encoded3 has str, want bytes
        decoded = decodestring(str_to_bytes(encoded3[key]))
        nt.assert_equal(decoded, value)

def test_lambda():
    jc = json_clean(lambda : 1)
    nt.assert_is_instance(jc, str)
    nt.assert_in('<lambda>', jc)
    json.dumps(jc)

def test_extract_dates():
    timestamps = [
        '2013-07-03T16:34:52.249482',
        '2013-07-03T16:34:52.249482Z',
        '2013-07-03T16:34:52.249482Z-0800',
        '2013-07-03T16:34:52.249482Z+0800',
        '2013-07-03T16:34:52.249482Z+08:00',
        '2013-07-03T16:34:52.249482Z-08:00',
        '2013-07-03T16:34:52.249482-0800',
        '2013-07-03T16:34:52.249482+0800',
        '2013-07-03T16:34:52.249482+08:00',
        '2013-07-03T16:34:52.249482-08:00',
    ]
    extracted = jsonutil.extract_dates(timestamps)
    ref = extracted[0]
    for dt in extracted:
        nt.assert_true(isinstance(dt, datetime.datetime))
        nt.assert_equal(dt, ref)

def test_parse_ms_precision():
    base = '2013-07-03T16:34:52'
    digits = '1234567890'

    parsed = jsonutil.parse_date(base)
    nt.assert_is_instance(parsed, datetime.datetime)
    for i in range(len(digits)):
        ts = base + '.' + digits[:i]
        parsed = jsonutil.parse_date(ts)
        if i >= 1 and i <= 6:
            nt.assert_is_instance(parsed, datetime.datetime)
        else:
            nt.assert_is_instance(parsed, str)

def test_date_default():
    data = dict(today=datetime.datetime.now(), utcnow=tz.utcnow())
    jsondata = json.dumps(data, default=jsonutil.date_default)
    nt.assert_in("+00", jsondata)
    nt.assert_equal(jsondata.count("+00"), 1)
    extracted = jsonutil.extract_dates(json.loads(jsondata))
    for dt in extracted.values():
        nt.assert_is_instance(dt, datetime.datetime)

def test_exception():
    bad_dicts = [{1:'number', '1':'string'},
                 {True:'bool', 'True':'string'},
                 ]
    for d in bad_dicts:
        nt.assert_raises(ValueError, json_clean, d)

<commit_msg>test rekey modify-in-place<commit_after>"""Test suite for our JSON utilities.
"""
#-----------------------------------------------------------------------------
#  Copyright (C) 2010-2011  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING.txt, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# stdlib
import datetime
import json
from base64 import decodestring

# third party
import nose.tools as nt

# our own
from IPython.utils import jsonutil, tz
from ..jsonutil import json_clean, encode_images
from ..py3compat import unicode_to_str, str_to_bytes, iteritems

#-----------------------------------------------------------------------------
# Test functions
#-----------------------------------------------------------------------------
class Int(int):
    def __str__(self):
        return 'Int(%i)' % self

def test():
    # list of input/expected output.  Use None for the expected output if it
    # can be the same as the input.
    pairs = [(1, None), # start with scalars
             (1.0, None),
             ('a', None),
             (True, None),
             (False, None),
             (None, None),
             # complex numbers for now just go to strings, as otherwise they
             # are unserializable
             (1j, '1j'),
             # Containers
             ([1, 2], None),
             ((1, 2), [1, 2]),
             (set([1, 2]), [1, 2]),
             (dict(x=1), None),
             ({'x': 1, 'y':[1,2,3], '1':'int'}, None),
             # More exotic objects
             ((x for x in range(3)), [0, 1, 2]),
             (iter([1, 2]), [1, 2]),
             (Int(5), 5),
             ]

    for val, jval in pairs:
        if jval is None:
            jval = val
        out = json_clean(val)
        # validate our cleanup
        nt.assert_equal(out, jval)
        # and ensure that what we return, indeed encodes cleanly
        json.loads(json.dumps(out))

def test_rekey():
    # This could fail due to modifying the dict keys in-place on Python 3
    d = { i:i for i in map(str, range(128)) }
    d = jsonutil.rekey(d)
    for key in d:
        nt.assert_is_instance(key, int)

def test_encode_images():
    # invalid data, but the header and footer are from real files
    pngdata = b'\x89PNG\r\n\x1a\nblahblahnotactuallyvalidIEND\xaeB`\x82'
    jpegdata = b'\xff\xd8\xff\xe0\x00\x10JFIFblahblahjpeg(\xa0\x0f\xff\xd9'
    pdfdata = b'%PDF-1.\ntrailer<</Root<</Pages<</Kids[<</MediaBox[0 0 3 3]>>]>>>>>>'

    fmt = {
        'image/png'  : pngdata,
        'image/jpeg' : jpegdata,
        'application/pdf' : pdfdata
    }
    encoded = encode_images(fmt)
    for key, value in iteritems(fmt):
        # encoded has unicode, want bytes
        decoded = decodestring(encoded[key].encode('ascii'))
        nt.assert_equal(decoded, value)
    encoded2 = encode_images(encoded)
    nt.assert_equal(encoded, encoded2)

    b64_str = {}
    for key, encoded in iteritems(encoded):
        b64_str[key] = unicode_to_str(encoded)
    encoded3 = encode_images(b64_str)
    nt.assert_equal(encoded3, b64_str)
    for key, value in iteritems(fmt):
        # encoded3 has str, want bytes
        decoded = decodestring(str_to_bytes(encoded3[key]))
        nt.assert_equal(decoded, value)

def test_lambda():
    jc = json_clean(lambda : 1)
    nt.assert_is_instance(jc, str)
    nt.assert_in('<lambda>', jc)
    json.dumps(jc)

def test_extract_dates():
    timestamps = [
        '2013-07-03T16:34:52.249482',
        '2013-07-03T16:34:52.249482Z',
        '2013-07-03T16:34:52.249482Z-0800',
        '2013-07-03T16:34:52.249482Z+0800',
        '2013-07-03T16:34:52.249482Z+08:00',
        '2013-07-03T16:34:52.249482Z-08:00',
        '2013-07-03T16:34:52.249482-0800',
        '2013-07-03T16:34:52.249482+0800',
        '2013-07-03T16:34:52.249482+08:00',
        '2013-07-03T16:34:52.249482-08:00',
    ]
    extracted = jsonutil.extract_dates(timestamps)
    ref = extracted[0]
    for dt in extracted:
        nt.assert_true(isinstance(dt, datetime.datetime))
        nt.assert_equal(dt, ref)

def test_parse_ms_precision():
    base = '2013-07-03T16:34:52'
    digits = '1234567890'

    parsed = jsonutil.parse_date(base)
    nt.assert_is_instance(parsed, datetime.datetime)
    for i in range(len(digits)):
        ts = base + '.' + digits[:i]
        parsed = jsonutil.parse_date(ts)
        if i >= 1 and i <= 6:
            nt.assert_is_instance(parsed, datetime.datetime)
        else:
            nt.assert_is_instance(parsed, str)

def test_date_default():
    data = dict(today=datetime.datetime.now(), utcnow=tz.utcnow())
    jsondata = json.dumps(data, default=jsonutil.date_default)
    nt.assert_in("+00", jsondata)
    nt.assert_equal(jsondata.count("+00"), 1)
    extracted = jsonutil.extract_dates(json.loads(jsondata))
    for dt in extracted.values():
        nt.assert_is_instance(dt, datetime.datetime)

def test_exception():
    bad_dicts = [{1:'number', '1':'string'},
                 {True:'bool', 'True':'string'},
                 ]
    for d in bad_dicts:
        nt.assert_raises(ValueError, json_clean, d)

<|endoftext|>


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

'''
Submit sample to VMRay.

Requires "vmray_rest_api"

The expansion module vmray_submit and import module vmray_import are a two step
process to import data from VMRay.
You can automate this by setting the PyMISP example script 'vmray_automation'
as a cron job

'''

import json
import base64
from distutils.util import strtobool

import io
import zipfile

from ._vmray.vmray_rest_api import VMRayRESTAPI

misperrors = {'error': 'Error'}
mispattributes = {'input': ['attachment', 'malware-sample'], 'output': ['text', 'sha1', 'sha256', 'md5', 'link']}
moduleinfo = {'version': '0.3', 'author': 'Koen Van Impe',
              'description': 'Submit a sample to VMRay',
              'module-type': ['expansion']}
moduleconfig = ['apikey', 'url', 'shareable', 'do_not_reanalyze', 'do_not_include_vmrayjobids']

include_vmrayjobids = False

def handler(q=False):
    global include_vmrayjobids

    if q is False:
        return False
    request = json.loads(q)

    try:
        data = request.get("data")
        if 'malware-sample' in request:
            # malicious samples are encrypted with zip (password infected) and then base64 encoded
            sample_filename = request.get("malware-sample").split("|", 1)[0]
            data = base64.b64decode(data)
            fl = io.BytesIO(data)
            zf = zipfile.ZipFile(fl)
            sample_hashname = zf.namelist()[0]
            data = zf.read(sample_hashname, b"infected")
            zf.close()
        elif 'attachment' in request:
            # All attachments get base64 encoded
            sample_filename = request.get("attachment")
            data = base64.b64decode(data)

        else:
            misperrors['error'] = "No malware sample or attachment supplied"
            return misperrors
    except Exception:
        misperrors['error'] = "Unable to process submited sample data"
        return misperrors

    if (request["config"].get("apikey") is None) or (request["config"].get("url") is None):
        misperrors["error"] = "Missing API key or server URL (hint: try cloud.vmray.com)"
        return misperrors

    api = VMRayRESTAPI(request["config"].get("url"), request["config"].get("apikey"), False)

    shareable = request["config"].get("shareable")
    do_not_reanalyze = request["config"].get("do_not_reanalyze")
    do_not_include_vmrayjobids = request["config"].get("do_not_include_vmrayjobids")

    try:
        shareable = bool(strtobool(shareable))                                # Do we want the sample to be shared?
        reanalyze = not bool(strtobool(do_not_reanalyze))                     # Always reanalyze the sample?
        include_vmrayjobids = not bool(strtobool(do_not_include_vmrayjobids)) # Include the references to VMRay job IDs
    except ValueError:
        misperrors["error"] = "Error while processing settings. Please double-check your values."
        return misperrors

    if data and sample_filename:
        args = {}
        args["shareable"] = shareable
        args["sample_file"] = {'data': io.BytesIO(data), 'filename': sample_filename}
        args["reanalyze"] = reanalyze

        try:
            vmraydata = vmraySubmit(api, args)
            if vmraydata["errors"] and "Submission not stored" not in vmraydata["errors"][0]["error_msg"]:
                misperrors['error'] = "VMRay: %s" % vmraydata["errors"][0]["error_msg"]
                return misperrors
            else:
                return vmrayProcess(vmraydata)
        except Exception:
            misperrors['error'] = "Problem when calling API."
            return misperrors
    else:
        misperrors['error'] = "No sample data or filename."
        return misperrors

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo

def vmrayProcess(vmraydata):
    ''' Process the JSON file returned by vmray'''
    if vmraydata:
        try:
            sample = vmraydata["samples"][0]
            jobs = vmraydata["jobs"]

            # Result received?
            if sample:
                r = {'results': []}
                r['results'].append({'types': 'md5', 'values': sample['sample_md5hash']})
                r['results'].append({'types': 'sha1', 'values': sample['sample_sha1hash']})
                r['results'].append({'types': 'sha256', 'values': sample['sample_sha256hash']})
                r['results'].append({'types': 'text', 'values': 'VMRay Sample ID: %s' % sample['sample_id'], 'tags': 'workflow:state="incomplete"'})
                r['results'].append({'types': 'link', 'values': sample['sample_webif_url']})

                # Include data from different jobs
                if include_vmrayjobids and len(jobs) > 0:
                    for job in jobs:
                        job_id = job["job_id"]
                        job_vm_name = job["job_vm_name"]
                        job_configuration_name = job["job_configuration_name"]
                        r["results"].append({"types": "text", "values": "VMRay Job ID %s (%s - %s)" % (job_id, job_vm_name, job_configuration_name)})
                return r
            else:
                misperrors['error'] = "No valid results returned."
                return misperrors
        except Exception:
            misperrors['error'] = "No valid submission data returned."
            return misperrors
    else:
        misperrors['error'] = "Unable to parse results."
        return misperrors

def vmraySubmit(api, args):
    ''' Submit the sample to VMRay'''
    vmraydata = api.call("POST", "/rest/sample/submit", args)
    return vmraydata
<commit_msg>fix: [pep] Comply to PEP E261<commit_after>#!/usr/bin/env python3

'''
Submit sample to VMRay.

Requires "vmray_rest_api"

The expansion module vmray_submit and import module vmray_import are a two step
process to import data from VMRay.
You can automate this by setting the PyMISP example script 'vmray_automation'
as a cron job

'''

import json
import base64
from distutils.util import strtobool

import io
import zipfile

from ._vmray.vmray_rest_api import VMRayRESTAPI

misperrors = {'error': 'Error'}
mispattributes = {'input': ['attachment', 'malware-sample'], 'output': ['text', 'sha1', 'sha256', 'md5', 'link']}
moduleinfo = {'version': '0.3', 'author': 'Koen Van Impe',
              'description': 'Submit a sample to VMRay',
              'module-type': ['expansion']}
moduleconfig = ['apikey', 'url', 'shareable', 'do_not_reanalyze', 'do_not_include_vmrayjobids']

include_vmrayjobids = False

def handler(q=False):
    global include_vmrayjobids

    if q is False:
        return False
    request = json.loads(q)

    try:
        data = request.get("data")
        if 'malware-sample' in request:
            # malicious samples are encrypted with zip (password infected) and then base64 encoded
            sample_filename = request.get("malware-sample").split("|", 1)[0]
            data = base64.b64decode(data)
            fl = io.BytesIO(data)
            zf = zipfile.ZipFile(fl)
            sample_hashname = zf.namelist()[0]
            data = zf.read(sample_hashname, b"infected")
            zf.close()
        elif 'attachment' in request:
            # All attachments get base64 encoded
            sample_filename = request.get("attachment")
            data = base64.b64decode(data)

        else:
            misperrors['error'] = "No malware sample or attachment supplied"
            return misperrors
    except Exception:
        misperrors['error'] = "Unable to process submited sample data"
        return misperrors

    if (request["config"].get("apikey") is None) or (request["config"].get("url") is None):
        misperrors["error"] = "Missing API key or server URL (hint: try cloud.vmray.com)"
        return misperrors

    api = VMRayRESTAPI(request["config"].get("url"), request["config"].get("apikey"), False)

    shareable = request["config"].get("shareable")
    do_not_reanalyze = request["config"].get("do_not_reanalyze")
    do_not_include_vmrayjobids = request["config"].get("do_not_include_vmrayjobids")

    try:
        shareable = bool(strtobool(shareable))                                 # Do we want the sample to be shared?
        reanalyze = not bool(strtobool(do_not_reanalyze))                      # Always reanalyze the sample?
        include_vmrayjobids = not bool(strtobool(do_not_include_vmrayjobids))  # Include the references to VMRay job IDs
    except ValueError:
        misperrors["error"] = "Error while processing settings. Please double-check your values."
        return misperrors

    if data and sample_filename:
        args = {}
        args["shareable"] = shareable
        args["sample_file"] = {'data': io.BytesIO(data), 'filename': sample_filename}
        args["reanalyze"] = reanalyze

        try:
            vmraydata = vmraySubmit(api, args)
            if vmraydata["errors"] and "Submission not stored" not in vmraydata["errors"][0]["error_msg"]:
                misperrors['error'] = "VMRay: %s" % vmraydata["errors"][0]["error_msg"]
                return misperrors
            else:
                return vmrayProcess(vmraydata)
        except Exception:
            misperrors['error'] = "Problem when calling API."
            return misperrors
    else:
        misperrors['error'] = "No sample data or filename."
        return misperrors

def introspection():
    return mispattributes

def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo

def vmrayProcess(vmraydata):
    ''' Process the JSON file returned by vmray'''
    if vmraydata:
        try:
            sample = vmraydata["samples"][0]
            jobs = vmraydata["jobs"]

            # Result received?
            if sample:
                r = {'results': []}
                r['results'].append({'types': 'md5', 'values': sample['sample_md5hash']})
                r['results'].append({'types': 'sha1', 'values': sample['sample_sha1hash']})
                r['results'].append({'types': 'sha256', 'values': sample['sample_sha256hash']})
                r['results'].append({'types': 'text', 'values': 'VMRay Sample ID: %s' % sample['sample_id'], 'tags': 'workflow:state="incomplete"'})
                r['results'].append({'types': 'link', 'values': sample['sample_webif_url']})

                # Include data from different jobs
                if include_vmrayjobids and len(jobs) > 0:
                    for job in jobs:
                        job_id = job["job_id"]
                        job_vm_name = job["job_vm_name"]
                        job_configuration_name = job["job_configuration_name"]
                        r["results"].append({"types": "text", "values": "VMRay Job ID %s (%s - %s)" % (job_id, job_vm_name, job_configuration_name)})
                return r
            else:
                misperrors['error'] = "No valid results returned."
                return misperrors
        except Exception:
            misperrors['error'] = "No valid submission data returned."
            return misperrors
    else:
        misperrors['error'] = "Unable to parse results."
        return misperrors

def vmraySubmit(api, args):
    ''' Submit the sample to VMRay'''
    vmraydata = api.call("POST", "/rest/sample/submit", args)
    return vmraydata
<|endoftext|>


<commit_before>"""
=========================
Getting started with DIPY
=========================

In diffusion MRI (dMRI) usually we use three types of files, a Nifti file with the
diffusion weighted data, and two text files one with b-values and
one with the b-vectors.

In DIPY_ we provide tools to load and process these files and we also provide
access to publically available datasets for those who haven't acquired yet
their own datasets.

With the following commands we can download a dMRI dataset
"""

from dipy.data import fetch_sherbrooke_3shell
fetch_sherbrooke_3shell()

"""
By default these datasets will go in the ``.dipy`` folder inside your home directory.
Here is how you can access them.
"""

from os.path import expanduser, join
home = expanduser('~')

"""
``dname`` holds the directory name where the 3 files are in.
"""

dname = join(home, '.dipy', 'sherbrooke_3shell')

"""
Here, we show the complete filenames of the 3 files
"""

fdwi = join(dname, 'HARDI193.nii.gz')

print(fdwi)

fbval = join(dname, 'HARDI193.bval')

print(fbval)

fbvec = join(dname, 'HARDI193.bvec')

print(fbvec)

"""
``/home/username/.dipy/sherbrooke_3shell/HARDI193.nii.gz``

``/home/username/.dipy/sherbrooke_3shell/HARDI193.bval``

``/home/username/.dipy/sherbrooke_3shell/HARDI193.bvec``

Now, that we have their filenames we can start checking what these look like.

Let's start first by loading the dMRI datasets. For this purpose, we
use a python library called nibabel_ which enables us to read and write
neuroimaging-specific file formats.
"""

import nibabel as nib
img = nib.load(fdwi)
data = img.get_data()

"""
``data`` is a 4D array where the first 3 dimensions are the i, j, k voxel
coordinates and the last dimension is the number of non-weighted (S0s) and
diffusion-weighted volumes.

We can very easily check the size of ``data`` in the following way:
"""

print(data.shape)

"""
``(128, 128, 60, 193)``

We can also check the dimensions of each voxel in the following way:
"""

print(img.header.get_zooms()[:3])

"""
``(2.0, 2.0, 2.0)``

We can quickly visualize the results using matplotlib_. For example,
let's show here the middle axial slices of volume 0 and volume 10.
"""

import matplotlib.pyplot as plt

axial_middle = data.shape[2] // 2
plt.figure('Showing the datasets')
plt.subplot(1, 2, 1).set_axis_off()
plt.imshow(data[:, :, axial_middle, 0].T, cmap='gray', origin='lower')
plt.subplot(1, 2, 2).set_axis_off()
plt.imshow(data[:, :, axial_middle, 10].T, cmap='gray', origin='lower')
plt.show()
plt.savefig('data.png', bbox_inches='tight')

"""
.. figure:: data.png
   :align: center

   Showing the middle axial slice without (left) and with (right) diffusion weighting.

The next step is to load the b-values and b-vectors from the disk using
the function ``read_bvals_bvecs``.
"""

from dipy.io import read_bvals_bvecs
bvals, bvecs = read_bvals_bvecs(fbval, fbvec)

"""
In DIPY, we use an object called ``GradientTable`` which holds all the
acquisition specific parameters, e.g. b-values, b-vectors, timings and others.
To create this object you can use the function ``gradient_table``.
"""

from dipy.core.gradients import gradient_table
gtab = gradient_table(bvals, bvecs)

"""
Finally, you can use ``gtab`` (the GradientTable object) to show some information about the
acquisition parameters
"""

print(gtab.info)

"""
B-values shape (193,)
         min 0.000000
         max 3500.000000
B-vectors shape (193, 3)
         min -0.964050
         max 0.999992

You can also see the b-values using:
"""

print(gtab.bvals)

"""

::

    [    0.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.]

Or, for example the 10 first b-vectors using:
"""

print(gtab.bvecs[:10, :])

"""

::

    array([[ 0.        ,  0.        ,  0.        ],
           [ 0.999979  , -0.00504001, -0.00402795],
           [ 0.        ,  0.999992  , -0.00398794],
           [-0.0257055 ,  0.653861  , -0.756178  ],
           [ 0.589518  , -0.769236  , -0.246462  ],
           [-0.235785  , -0.529095  , -0.815147  ],
           [-0.893578  , -0.263559  , -0.363394  ],
           [ 0.79784   ,  0.133726  , -0.587851  ],
           [ 0.232937  ,  0.931884  , -0.278087  ],
           [ 0.93672   ,  0.144139  , -0.31903   ]])

``gtab``  can be used to tell what part of the data is the S0 volumes
(volumes which correspond to b-values of 0).
"""

S0s = data[:, :, :, gtab.b0s_mask]

"""
Here, we had only 1 S0 as we can verify by looking at the dimensions of S0s
"""

print(S0s.shape)

"""
``(128, 128, 60, 1)``

Just, for fun let's save this in a new Nifti file.
"""

nib.save(nib.Nifti1Image(S0s, img.affine), 'HARDI193_S0.nii.gz')

"""
Now, that we learned how to load dMRI datasets we can start the analysis.
See example :ref:`example_reconst_dti` to learn how to create FA maps.

.. include:: ../links_names.inc

"""
<commit_msg>minor typo fix in quickstart<commit_after>"""
=========================
Getting started with DIPY
=========================

In diffusion MRI (dMRI) usually we use three types of files, a Nifti file with the
diffusion weighted data, and two text files one with b-values and
one with the b-vectors.

In DIPY_ we provide tools to load and process these files and we also provide
access to publicly available datasets for those who haven't acquired yet
their own datasets.

With the following commands we can download a dMRI dataset
"""

from dipy.data import fetch_sherbrooke_3shell
fetch_sherbrooke_3shell()

"""
By default these datasets will go in the ``.dipy`` folder inside your home directory.
Here is how you can access them.
"""

from os.path import expanduser, join
home = expanduser('~')

"""
``dname`` holds the directory name where the 3 files are in.
"""

dname = join(home, '.dipy', 'sherbrooke_3shell')

"""
Here, we show the complete filenames of the 3 files
"""

fdwi = join(dname, 'HARDI193.nii.gz')

print(fdwi)

fbval = join(dname, 'HARDI193.bval')

print(fbval)

fbvec = join(dname, 'HARDI193.bvec')

print(fbvec)

"""
``/home/username/.dipy/sherbrooke_3shell/HARDI193.nii.gz``

``/home/username/.dipy/sherbrooke_3shell/HARDI193.bval``

``/home/username/.dipy/sherbrooke_3shell/HARDI193.bvec``

Now, that we have their filenames we can start checking what these look like.

Let's start first by loading the dMRI datasets. For this purpose, we
use a python library called nibabel_ which enables us to read and write
neuroimaging-specific file formats.
"""

import nibabel as nib
img = nib.load(fdwi)
data = img.get_data()

"""
``data`` is a 4D array where the first 3 dimensions are the i, j, k voxel
coordinates and the last dimension is the number of non-weighted (S0s) and
diffusion-weighted volumes.

We can very easily check the size of ``data`` in the following way:
"""

print(data.shape)

"""
``(128, 128, 60, 193)``

We can also check the dimensions of each voxel in the following way:
"""

print(img.header.get_zooms()[:3])

"""
``(2.0, 2.0, 2.0)``

We can quickly visualize the results using matplotlib_. For example,
let's show here the middle axial slices of volume 0 and volume 10.
"""

import matplotlib.pyplot as plt

axial_middle = data.shape[2] // 2
plt.figure('Showing the datasets')
plt.subplot(1, 2, 1).set_axis_off()
plt.imshow(data[:, :, axial_middle, 0].T, cmap='gray', origin='lower')
plt.subplot(1, 2, 2).set_axis_off()
plt.imshow(data[:, :, axial_middle, 10].T, cmap='gray', origin='lower')
plt.show()
plt.savefig('data.png', bbox_inches='tight')

"""
.. figure:: data.png
   :align: center

   Showing the middle axial slice without (left) and with (right) diffusion weighting.

The next step is to load the b-values and b-vectors from the disk using
the function ``read_bvals_bvecs``.
"""

from dipy.io import read_bvals_bvecs
bvals, bvecs = read_bvals_bvecs(fbval, fbvec)

"""
In DIPY, we use an object called ``GradientTable`` which holds all the
acquisition specific parameters, e.g. b-values, b-vectors, timings and others.
To create this object you can use the function ``gradient_table``.
"""

from dipy.core.gradients import gradient_table
gtab = gradient_table(bvals, bvecs)

"""
Finally, you can use ``gtab`` (the GradientTable object) to show some information about the
acquisition parameters
"""

print(gtab.info)

"""
B-values shape (193,)
         min 0.000000
         max 3500.000000
B-vectors shape (193, 3)
         min -0.964050
         max 0.999992

You can also see the b-values using:
"""

print(gtab.bvals)

"""

::

    [    0.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.  1000.
      1000.  1000.  1000.  1000.  1000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.
      2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  2000.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.  3500.
      3500.  3500.  3500.]

Or, for example the 10 first b-vectors using:
"""

print(gtab.bvecs[:10, :])

"""

::

    array([[ 0.        ,  0.        ,  0.        ],
           [ 0.999979  , -0.00504001, -0.00402795],
           [ 0.        ,  0.999992  , -0.00398794],
           [-0.0257055 ,  0.653861  , -0.756178  ],
           [ 0.589518  , -0.769236  , -0.246462  ],
           [-0.235785  , -0.529095  , -0.815147  ],
           [-0.893578  , -0.263559  , -0.363394  ],
           [ 0.79784   ,  0.133726  , -0.587851  ],
           [ 0.232937  ,  0.931884  , -0.278087  ],
           [ 0.93672   ,  0.144139  , -0.31903   ]])

``gtab``  can be used to tell what part of the data is the S0 volumes
(volumes which correspond to b-values of 0).
"""

S0s = data[:, :, :, gtab.b0s_mask]

"""
Here, we had only 1 S0 as we can verify by looking at the dimensions of S0s
"""

print(S0s.shape)

"""
``(128, 128, 60, 1)``

Just, for fun let's save this in a new Nifti file.
"""

nib.save(nib.Nifti1Image(S0s, img.affine), 'HARDI193_S0.nii.gz')

"""
Now, that we learned how to load dMRI datasets we can start the analysis.
See example :ref:`example_reconst_dti` to learn how to create FA maps.

.. include:: ../links_names.inc

"""
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015,2016 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import collections
from datetime import datetime, timedelta
import time as _time
import json
import threading
import requests
import sseclient
import urllib3

import backtrader as bt
from backtrader.metabase import MetaParams
from backtrader.utils.py3 import queue, with_metaclass
from backtrader.utils import AutoDict

class MetaSingleton(MetaParams):
    '''Metaclass to make a metaclassed class a singleton'''
    def __init__(cls, name, bases, dct):
        super(MetaSingleton, cls).__init__(name, bases, dct)
        cls._singleton = None

    def __call__(cls, *args, **kwargs):
        if cls._singleton is None:
            cls._singleton = (
                super(MetaSingleton, cls).__call__(*args, **kwargs))

        return cls._singleton

class SharpPointStore(with_metaclass(MetaSingleton, object)):
    '''Singleton class wrapping to control the connections to SharpPoint.

    Params:

      - ``token`` (default:``None``): API access token

      - ``account`` (default: ``None``): account id

      - ``practice`` (default: ``False``): use the test environment

      - ``account_tmout`` (default: ``10.0``): refresh period for account
        value/cash refresh
    '''

    BrokerCls = None  # broker class will autoregister
    DataCls = None  # data class will auto register

    params = (
        ('gateway', 'http://localhost:5000/'),
        ('token', ''),
        ('account', ''),
        ('login', None),
        ('practice', False),
        ('debug', True),
        ('account_tmout', 30.0),
    )

    _DTEPOCH = datetime(1970, 1, 1)
    _ENVPRACTICE = 'practice'
    _ENVLIVE = 'live'

    @classmethod
    def getdata(cls, *args, **kwargs):
        '''Returns ``DataCls`` with args, kwargs'''
        return cls.DataCls(*args, **kwargs)

    @classmethod
    def getbroker(cls, *args, **kwargs):
        '''Returns broker with *args, **kwargs from registered ``BrokerCls``'''
        return cls.BrokerCls(*args, **kwargs)

    def __init__(self):
        super(SharpPointStore, self).__init__()

        self.notifs = collections.deque()  # store notifications for cerebro

        self._env = None  # reference to cerebro for general notifications
        self.broker = None  # broker instance
        self.datas = list()  # datas that have registered over start

        self._orders = collections.OrderedDict()  # map order.ref to oid
        self._ordersrev = collections.OrderedDict()  # map oid to order.ref
        self._transpend = collections.defaultdict(collections.deque)

        self._oenv = self._ENVPRACTICE if self.p.practice else self._ENVLIVE

        self._cash = 0.0
        self._value = 0.0
        self._evt_acct = threading.Event()

    def start(self, data=None, broker=None):
        # Datas require some processing to kickstart data reception
        if data is None and broker is None:
            self.cash = None
            return

        if data is not None:
            self._env = data._env
            # For datas simulate a queue with None to kickstart co
            self.datas.append(data)

            if self.broker is not None:
                self.broker.data_started(data)

        elif broker is not None:
            self.broker = broker
            self.broker_threads()
            self.streaming_events()

    def stop(self):
        # signal end of thread
        if self.broker is not None:
            self.q_ordercreate.put(None)
            self.q_orderclose.put(None)
            self.q_account.put(None)

    def put_notification(self, msg, *args, **kwargs):
        self.notifs.append((msg, args, kwargs))

    def get_notifications(self):
        '''Return the pending "store" notifications'''
        self.notifs.append(None)  # put a mark / threads could still append
        return [x for x in iter(self.notifs.popleft, None)]

    def get_positions(self):
        pass

    def get_granularity(self, timeframe, compression):
        pass

    def get_instrument(self, dataname):
        pass

    def streaming_events(self, tmout=None):
        q = queue.Queue()
        kwargs = {'q': q, 'tmout': tmout}

        t = threading.Thread(target=self._t_streaming_listener, kwargs=kwargs)
        t.daemon = True
        t.start()

    def _t_streaming_listener(self, q, tmout=None):
        http = urllib3.PoolManager()
        response = http.request("GET",
                                self.p.gateway + "log/subscribe",
                                preload_content=False)
        client = sseclient.SSEClient(response)
        for event in client.events():
            print(str(event))

    def get_cash(self):
        return self._cash

    def get_value(self):
        return self._value

    def broker_threads(self):
        self.q_account = queue.Queue()
        self.q_account.put(True)  # force an immediate update
        t = threading.Thread(target=self._t_account)
        t.daemon = True
        t.start()

        self.q_ordercreate = queue.Queue()
        t = threading.Thread(target=self._t_order_create)
        t.daemon = True
        t.start()

        self.q_orderclose = queue.Queue()
        t = threading.Thread(target=self._t_order_cancel)
        t.daemon = True
        t.start()

    _ORDEREXECS = {
        bt.Order.Market: 'market',
        bt.Order.Limit: 'limit',
        bt.Order.Stop: 'stop',
        bt.Order.StopLimit: 'stop',
    }

    def _t_account(self):
        if self.p.debug:
            print("t_account")
        while True:
            try:
                msg = self.q_account.get(timeout=self.p.account_tmout)
                if msg is None:
                    break  # end of thread
            except queue.Empty:  # tmout -> time to refresh
                pass
            try:
                login_info = requests.get(self.p.gateway + "login-info").json()
                if self.p.debug:
                    print("login", self.p.login)
                    print(login_info)
                if int(login_info['status']) != -1:
                    continue
                if self.p.debug is not None:
                    r = requests.post(self.p.gateway + "login",
                                      json=self.p.login)
            except Exception as e:
                self.put_notification(e)
                continue

    def order_create(self, order, **kwargs):
        okwargs = {"DecInPrice": 0,
                   "Ref": "test",
                   "ClOrderId": "test2",
                   "OpenClose": 0,
                   "CondType": 0,
                   "OrderType": 0,
                   "ValidType": 0,
                   "StopType": 0,
                   "OrderOptions": 0}
        if order.isbuy():
            okwargs['BuySell'] = "B"
        elif order.issell():
            okwargs['BuySell'] = "S"
        okwargs['Price'] = order.created.price
        okwargs['Qty'] = abs(order.created.size)
        if hasattr(order.data, "product"):
            okwargs['ProdCode'] = order.data.product()
        else:
            okwargs['ProdCode'] = order.data._dataname
        okwargs['Ref2'] = str(order.ref)
        if self.p.debug:
            print(okwargs)
        self.q_ordercreate.put((order.ref, okwargs,))
        return order

    def _t_order_create(self):
        while True:
            msg = self.q_ordercreate.get()
            if msg is None:
                break
            oref, okwargs = msg
            if self.p.debug:
                print(msg)
            try:
                r = requests.post(self.p.gateway + "order/add",
                                  json=okwargs)
            except Exception as e:
                self.put_notification(e)
                self.broker._reject(order.ref)
                return

    def order_cancel(self, order):
        self.q_orderclose.put(order.ref)
        return order

    def _t_order_cancel(self):
        while True:
            order = self.q_orderclose.get()
            if oref is None:
                break

            oid = self._orders.get(oref, None)
            if oid is None:
                continue  # the order is no longer there
            try:
                o = self.oapi.close_order(self.p.account, oid)
            except Exception as e:
                continue  # not cancelled - FIXME: notify

            self.broker._cancel(oref)

if __name__ == '__main__':
    s = SharpPointStore()
<commit_msg>fix up spstore<commit_after>#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015,2016 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import collections
from datetime import datetime, timedelta
import time as _time
import json
import threading
import requests
import sseclient

import backtrader as bt
from backtrader.metabase import MetaParams
from backtrader.utils.py3 import queue, with_metaclass
from backtrader.utils import AutoDict

class MetaSingleton(MetaParams):
    '''Metaclass to make a metaclassed class a singleton'''
    def __init__(cls, name, bases, dct):
        super(MetaSingleton, cls).__init__(name, bases, dct)
        cls._singleton = None

    def __call__(cls, *args, **kwargs):
        if cls._singleton is None:
            cls._singleton = (
                super(MetaSingleton, cls).__call__(*args, **kwargs))

        return cls._singleton

class SharpPointStore(with_metaclass(MetaSingleton, object)):
    '''Singleton class wrapping to control the connections to SharpPoint.

    Params:

      - ``token`` (default:``None``): API access token

      - ``account`` (default: ``None``): account id

      - ``practice`` (default: ``False``): use the test environment

      - ``account_tmout`` (default: ``10.0``): refresh period for account
        value/cash refresh
    '''

    BrokerCls = None  # broker class will autoregister
    DataCls = None  # data class will auto register

    params = (
        ('gateway', 'http://localhost:5000/'),
        ('token', ''),
        ('account', ''),
        ('login', None),
        ('practice', False),
        ('debug', True),
        ('account_tmout', 30.0),
    )

    _DTEPOCH = datetime(1970, 1, 1)
    _ENVPRACTICE = 'practice'
    _ENVLIVE = 'live'

    @classmethod
    def getdata(cls, *args, **kwargs):
        '''Returns ``DataCls`` with args, kwargs'''
        return cls.DataCls(*args, **kwargs)

    @classmethod
    def getbroker(cls, *args, **kwargs):
        '''Returns broker with *args, **kwargs from registered ``BrokerCls``'''
        return cls.BrokerCls(*args, **kwargs)

    def __init__(self):
        super(SharpPointStore, self).__init__()

        self.notifs = collections.deque()  # store notifications for cerebro

        self._env = None  # reference to cerebro for general notifications
        self.broker = None  # broker instance
        self.datas = list()  # datas that have registered over start

        self._orders = collections.OrderedDict()  # map order.ref to oid
        self._ordersrev = collections.OrderedDict()  # map oid to order.ref
        self._transpend = collections.defaultdict(collections.deque)

        self._oenv = self._ENVPRACTICE if self.p.practice else self._ENVLIVE

        self._cash = 0.0
        self._value = 0.0
        self._evt_acct = threading.Event()

    def start(self, data=None, broker=None):
        # Datas require some processing to kickstart data reception
        if data is None and broker is None:
            self.cash = None
            return

        if data is not None:
            self._env = data._env
            # For datas simulate a queue with None to kickstart co
            self.datas.append(data)

            if self.broker is not None:
                self.broker.data_started(data)

        elif broker is not None:
            self.broker = broker
            self.broker_threads()
            self.streaming_events()

    def stop(self):
        # signal end of thread
        if self.broker is not None:
            self.q_ordercreate.put(None)
            self.q_orderclose.put(None)
            self.q_account.put(None)

    def put_notification(self, msg, *args, **kwargs):
        self.notifs.append((msg, args, kwargs))

    def get_notifications(self):
        '''Return the pending "store" notifications'''
        self.notifs.append(None)  # put a mark / threads could still append
        return [x for x in iter(self.notifs.popleft, None)]

    def get_positions(self):
        pass

    def get_granularity(self, timeframe, compression):
        pass

    def get_instrument(self, dataname):
        pass

    def streaming_events(self, tmout=None):
        q = queue.Queue()
        kwargs = {'q': q, 'tmout': tmout}

        t = threading.Thread(target=self._t_streaming_listener, kwargs=kwargs)
        t.daemon = True
        t.start()

    def _t_streaming_listener(self, q, tmout=None):
        response = requests.get(self.p.gateway + "log/subscribe",
                                stream=True)
        client = sseclient.SSEClient(response)
        for event in client.events():
            if self.p.debug:
                print(str(event))
            data = json.loads(event.data)
            if event.event == "OrderBeforeSendReport":
                if self.p.debug:
                    print(data)
                self.broker._submit(int(data['data']['Ref2']))
            elif event.event == "OrderRequestFailed":
                if self.p.debug:
                    print(data)
                self.broker._reject(int(data['data']['Ref2']))
            elif event.event == "OrderReport":
                if self.p.debug:
                    print(data)
            elif event.event == "TradeReport":
                if self.p.debug:
                    print(data)

    def get_cash(self):
        return self._cash

    def get_value(self):
        return self._value

    def broker_threads(self):
        self.q_account = queue.Queue()
        self.q_account.put(True)  # force an immediate update
        t = threading.Thread(target=self._t_account)
        t.daemon = True
        t.start()

        self.q_ordercreate = queue.Queue()
        t = threading.Thread(target=self._t_order_create)
        t.daemon = True
        t.start()

        self.q_orderclose = queue.Queue()
        t = threading.Thread(target=self._t_order_cancel)
        t.daemon = True
        t.start()

    _ORDEREXECS = {
        bt.Order.Market: 'market',
        bt.Order.Limit: 'limit',
        bt.Order.Stop: 'stop',
        bt.Order.StopLimit: 'stop',
    }

    def _t_account(self):
        if self.p.debug:
            print("t_account")
        while True:
            try:
                msg = self.q_account.get(timeout=self.p.account_tmout)
                if msg is None:
                    break  # end of thread
            except queue.Empty:  # tmout -> time to refresh
                pass
            try:
                login_info = requests.get(self.p.gateway + "login-info").json()
                if self.p.debug:
                    print("login-info", login_info)
                if int(login_info['status']) != -1:
                    continue
                if self.p.login is not None:
                    if self.p.debug:
                        print("login", self.p.login)
                    r = requests.post(self.p.gateway + "login",
                                      json=self.p.login)
            except Exception as e:
                self.put_notification(e)
                continue

    def order_create(self, order, **kwargs):
        okwargs = {"DecInPrice": 0,
                   "Ref": "test",
                   "ClOrderId": "test2",
                   "OpenClose": 0,
                   "CondType": 0,
                   "OrderType": 0,
                   "ValidType": 0,
                   "StopType": 0,
                   "OrderOptions": 0}
        if order.isbuy():
            okwargs['BuySell'] = "B"
        elif order.issell():
            okwargs['BuySell'] = "S"
        okwargs['Price'] = order.created.price
        okwargs['Qty'] = abs(order.created.size)
        if hasattr(order.data, "product"):
            okwargs['ProdCode'] = order.data.product()
        else:
            okwargs['ProdCode'] = order.data._dataname
        okwargs['Ref2'] = str(order.ref)
        if self.p.debug:
            print(okwargs)
        self.q_ordercreate.put((order.ref, okwargs,))
        return order

    def _t_order_create(self):
        while True:
            msg = self.q_ordercreate.get()
            if msg is None:
                break
            oref, okwargs = msg
            if self.p.debug:
                print(msg)
            try:
                r = requests.post(self.p.gateway + "order/add",
                                  json=okwargs)
            except Exception as e:
                self.put_notification(e)
                self.broker._reject(order.ref)
                return

    def order_cancel(self, order):
        self.q_orderclose.put(order.ref)
        return order

    def _t_order_cancel(self):
        while True:
            order = self.q_orderclose.get()
            if oref is None:
                break

            oid = self._orders.get(oref, None)
            if oid is None:
                continue  # the order is no longer there
            try:
                o = self.oapi.close_order(self.p.account, oid)
            except Exception as e:
                continue  # not cancelled - FIXME: notify

            self.broker._cancel(oref)

if __name__ == '__main__':
    s = SharpPointStore()
<|endoftext|>


<commit_before>import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
SECRET_KEY = 'secret_key'

DEBUG = True
TESTING = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test.sqlite3',
    }
}

# Django < 2.0
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

# Django >= 2.0
MIDDLEWARE = MIDDLEWARE_CLASSES

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.staticfiles',
    'django.contrib.sessions',
    'notifications.tests',
    'notifications',
)

ROOT_URLCONF = 'notifications.tests.urls'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static-files")

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

LOGIN_REDIRECT_URL = 'test/'
LOGIN_URL = '/admin/login/'
APPEND_SLASH = True

DJANGO_NOTIFICATIONS_CONFIG = {
    'USE_JSONFIELD': True,
}
<commit_msg>Improve code quality of tests/settings.py<commit_after>''' Django notification settings for tests '''
# -*- coding: utf-8 -*-
import os

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
SECRET_KEY = 'secret_key' #  noqa

DEBUG = True
TESTING = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test.sqlite3',
    }
}

# Django < 2.0
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

# Django >= 2.0
MIDDLEWARE = MIDDLEWARE_CLASSES

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.staticfiles',
    'django.contrib.sessions',
    'notifications.tests',
    'notifications',
)

ROOT_URLCONF = 'notifications.tests.urls'
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, "static-files")

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

LOGIN_REDIRECT_URL = 'test/'
LOGIN_URL = '/admin/login/'
APPEND_SLASH = True

DJANGO_NOTIFICATIONS_CONFIG = {
    'USE_JSONFIELD': True,
}
<|endoftext|>