<commit_before>import numpy
import collections
import topology
import minibatch
import cPickle

__all__ = ['infer', 'Inference']

class Inference(object):
    """
    Inference combines neural network output and parameters together
    to do inference.

    ..  code-block:: python

        inferer = Inference(output_layer=prediction, parameters=parameters)
        for data_batch in batches:
            print inferer.infer(data_batch)

    :param output_layer: The neural network that should be inferenced.
    :type output_layer: paddle.v2.config_base.Layer or the sequence
                        of paddle.v2.config_base.Layer
    :param parameters: The parameters dictionary.
    :type parameters: paddle.v2.parameters.Parameters
    """

    def __init__(self, parameters, output_layer=None, fileobj=None):
        import py_paddle.swig_paddle as api

        if output_layer is not None:
            topo = topology.Topology(output_layer)
            gm = api.GradientMachine.createFromConfigProto(
                topo.proto(), api.CREATE_MODE_TESTING, [api.PARAMETER_VALUE])
            self.__data_types__ = topo.data_type()
        elif fileobj is not None:
            tmp = cPickle.load(fileobj)
            gm = api.GradientMachine.createByConfigProtoStr(
                tmp['protobin'], api.CREATE_MODE_TESTING,
                [api.PARAMETER_VALUE])
            self.__data_types__ = tmp['data_type']
        else:
            raise ValueError("Either output_layer or fileobj must be set")

        for param in gm.getParameters():
            val = param.getBuf(api.PARAMETER_VALUE)
            name = param.getName()
            assert isinstance(val, api.Vector)
            val.copyFromNumpyArray(parameters.get(name).flatten())
            # the setValueUpdated function is called in randomize, zeroMem,
            # load function in paddle/parameter/Parameter.cpp. But in the
            # inference mode, the setValueUpdated is never called, it will
            # cause the parameter will not be dispatched
            # in MultiGradientMachine for multi-GPU. So setValueUpdated is
            # called here, but it's better to call this function in one place.
            param.setValueUpdated()
        self.__gradient_machine__ = gm

    def iter_infer(self, input, feeding=None):
        from data_feeder import DataFeeder
        feeder = DataFeeder(self.__data_types__, feeding)
        batch_size = len(input)

        def __reader_impl__():
            for each_sample in input:
                yield each_sample

        reader = minibatch.batch(__reader_impl__, batch_size=batch_size)

        self.__gradient_machine__.start()
        for data_batch in reader():
            yield self.__gradient_machine__.forwardTest(feeder(data_batch))
        self.__gradient_machine__.finish()

    def iter_infer_field(self, field, **kwargs):
        if not isinstance(field, list) and not isinstance(field, tuple):
            field = [field]

        for result in self.iter_infer(**kwargs):
            for each_result in result:
                item = [each_result[each_field] for each_field in field]
                yield item

    def infer(self, input, field='value', flatten_result=True, **kwargs):
        """
        Infer a data by model.
        :param input: input data batch. Should be python iterable object.
        :param field: output field.
        """
        retv = None
        kwargs['input'] = input
        for result in self.iter_infer_field(field=field, **kwargs):
            if retv is None:
                retv = [[] for i in xrange(len(result))]
            for i, item in enumerate(result):
                retv[i].append(item)

        if flatten_result:
            retv = [numpy.concatenate(out) for out in retv]

        if len(retv) == 1:
            return retv[0]
        else:
            return retv

def infer(output_layer, parameters, input, feeding=None, field='value'):
    """
    Infer a neural network by given neural network output and parameters.  The
    user should pass either a batch of input data or reader method.

    Example usage for sinlge output_layer:

    ..  code-block:: python

        result = paddle.infer(output_layer=prediction,
                              parameters=parameters,
                              input=SomeData)
        print result

    Example usage for multiple outout_layers and fields:

    ..  code-block:: python

        result = paddle.infer(output_layer=[prediction1, prediction2],
                              parameters=parameters,
                              input=SomeData,
                              field=[id, value]])
        print result

    :param output_layer: output of the neural network that would be inferred
    :type output_layer: paddle.v2.config_base.Layer or a list of
                        paddle.v2.config_base.Layer
    :param parameters: parameters of the neural network.
    :type parameters: paddle.v2.parameters.Parameters
    :param input: input data batch. Should be a python iterable object, and each
                  element is the data batch.
    :type input: collections.Iterable
    :param feeding: Reader dictionary. Default could generate from input
                        value.
    :param field: The prediction field. It should in [`value`, `id`, `prob`].
                  `value` and `prob` mean return the prediction probabilities,
                  `id` means return the prediction labels. Default is `value`.
                  Note that `prob` only used when output_layer is beam_search
                  or max_id.
    :type field: str
    :return: The prediction result. If there are multiple outout_layers and fields,
             the return order is outout_layer1.field1, outout_layer2.field1, ...,
             outout_layer1.field2, outout_layer2.field2 ...
    :rtype: numpy.ndarray
    """

    inferer = Inference(output_layer=output_layer, parameters=parameters)
    return inferer.infer(field=field, input=input, feeding=feeding)
<commit_msg>Fix infer when input is empty in v2/inference.py<commit_after>import numpy
import collections
import topology
import minibatch
import cPickle

__all__ = ['infer', 'Inference']

class Inference(object):
    """
    Inference combines neural network output and parameters together
    to do inference.

    ..  code-block:: python

        inferer = Inference(output_layer=prediction, parameters=parameters)
        for data_batch in batches:
            print inferer.infer(data_batch)

    :param output_layer: The neural network that should be inferenced.
    :type output_layer: paddle.v2.config_base.Layer or the sequence
                        of paddle.v2.config_base.Layer
    :param parameters: The parameters dictionary.
    :type parameters: paddle.v2.parameters.Parameters
    """

    def __init__(self, parameters, output_layer=None, fileobj=None):
        import py_paddle.swig_paddle as api

        if output_layer is not None:
            topo = topology.Topology(output_layer)
            gm = api.GradientMachine.createFromConfigProto(
                topo.proto(), api.CREATE_MODE_TESTING, [api.PARAMETER_VALUE])
            self.__data_types__ = topo.data_type()
        elif fileobj is not None:
            tmp = cPickle.load(fileobj)
            gm = api.GradientMachine.createByConfigProtoStr(
                tmp['protobin'], api.CREATE_MODE_TESTING,
                [api.PARAMETER_VALUE])
            self.__data_types__ = tmp['data_type']
        else:
            raise ValueError("Either output_layer or fileobj must be set")

        for param in gm.getParameters():
            val = param.getBuf(api.PARAMETER_VALUE)
            name = param.getName()
            assert isinstance(val, api.Vector)
            val.copyFromNumpyArray(parameters.get(name).flatten())
            # the setValueUpdated function is called in randomize, zeroMem,
            # load function in paddle/parameter/Parameter.cpp. But in the
            # inference mode, the setValueUpdated is never called, it will
            # cause the parameter will not be dispatched
            # in MultiGradientMachine for multi-GPU. So setValueUpdated is
            # called here, but it's better to call this function in one place.
            param.setValueUpdated()
        self.__gradient_machine__ = gm

    def iter_infer(self, input, feeding=None):
        from data_feeder import DataFeeder
        feeder = DataFeeder(self.__data_types__, feeding)
        batch_size = len(input)

        def __reader_impl__():
            for each_sample in input:
                yield each_sample

        reader = minibatch.batch(__reader_impl__, batch_size=batch_size)

        self.__gradient_machine__.start()
        for data_batch in reader():
            yield self.__gradient_machine__.forwardTest(feeder(data_batch))
        self.__gradient_machine__.finish()

    def iter_infer_field(self, field, **kwargs):
        if not isinstance(field, list) and not isinstance(field, tuple):
            field = [field]

        for result in self.iter_infer(**kwargs):
            for each_result in result:
                item = [each_result[each_field] for each_field in field]
                yield item

    def infer(self, input, field='value', flatten_result=True, **kwargs):
        """
        Infer a data by model.
        :param input: input data batch. Should be python iterable object.
        :param field: output field.
        """
        retv = None
        kwargs['input'] = input
        for result in self.iter_infer_field(field=field, **kwargs):
            if retv is None:
                retv = [[] for i in xrange(len(result))]
            for i, item in enumerate(result):
                retv[i].append(item)

        if retv == None:
            return []

        if flatten_result:
            retv = [numpy.concatenate(out) for out in retv]

        if len(retv) == 1:
            return retv[0]
        else:
            return retv

def infer(output_layer, parameters, input, feeding=None, field='value'):
    """
    Infer a neural network by given neural network output and parameters.  The
    user should pass either a batch of input data or reader method.

    Example usage for sinlge output_layer:

    ..  code-block:: python

        result = paddle.infer(output_layer=prediction,
                              parameters=parameters,
                              input=SomeData)
        print result

    Example usage for multiple outout_layers and fields:

    ..  code-block:: python

        result = paddle.infer(output_layer=[prediction1, prediction2],
                              parameters=parameters,
                              input=SomeData,
                              field=[id, value]])
        print result

    :param output_layer: output of the neural network that would be inferred
    :type output_layer: paddle.v2.config_base.Layer or a list of
                        paddle.v2.config_base.Layer
    :param parameters: parameters of the neural network.
    :type parameters: paddle.v2.parameters.Parameters
    :param input: input data batch. Should be a python iterable object, and each
                  element is the data batch.
    :type input: collections.Iterable
    :param feeding: Reader dictionary. Default could generate from input
                        value.
    :param field: The prediction field. It should in [`value`, `id`, `prob`].
                  `value` and `prob` mean return the prediction probabilities,
                  `id` means return the prediction labels. Default is `value`.
                  Note that `prob` only used when output_layer is beam_search
                  or max_id.
    :type field: str
    :return: The prediction result. If there are multiple outout_layers and fields,
             the return order is outout_layer1.field1, outout_layer2.field1, ...,
             outout_layer1.field2, outout_layer2.field2 ...
    :rtype: numpy.ndarray
    """

    inferer = Inference(output_layer=output_layer, parameters=parameters)
    return inferer.infer(field=field, input=input, feeding=feeding)
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import re

import cmath as math
range = xrange

def factors(n):
	"Yield every pair of factors of n (x,y where n/x == y and n/y == x), except for (1,n) and (n,1)."
	limit = math.sqrt(n).real
	if n == 1:
		yield (1, 1)
		return
	for i in range(1, int(limit + 1)):
		if n % i == 0:
			pair = (i, n/i)
			yield pair

			opposite_pair = (pair[1], pair[0])
			#If n is square, one of the pairs will be (sqrt, sqrt). We want to yield that only once. All other pairs, we want to yield both ways round.
			if pair != opposite_pair:
				yield opposite_pair

def except_one(pairs):
	"Given a sequence of pairs (x, y), yield every pair where neither x nor y is 1."
	for pair in pairs:
		if 1 not in pair:
			yield pair

MD5_exp = re.compile(r'^MD5 \(.*\) = ([0-9a-fA-f]+)')
RSA_exp = re.compile(r'^RSA key fingerprint is ([:0-9a-fA-f]+)\.')
def extract_hash_from_line(input_line):
	if input_line[:1] == 'M':
		match = MD5_exp.match(input_line)
		if match:
			return match.group(1)
		else:
			return ''
	elif input_line[:1] == 'R':
		match = RSA_exp.match(input_line)
		if match:
			return match.group(1)
		else:
			return ''
	else:
		try:
			hash, not_the_hash = input_line.split(None, 1)
		except ValueError:
			# Insufficient fields. This line doesn't contain any whitespace. Use the entire line.
			hash = input_line

		try:
			int(hash, 16)
		except ValueError:
			# Not a hex number.
			return None
		else:
			return hash

def parse_hex(hex):
	hex = hex.lstrip(':')
	while hex:
		byte_hex, hex = hex[:2], hex[2:].lstrip(':')
		yield int(byte_hex, 16)

def hash_to_pic(hash):
	bytes = parse_hex(hash)
	def fgcolor(idx):
		idx = ((idx >> 4) & 0xf)
		# 90 is bright foreground; 30 is dull foreground.
		base = 90 if idx > 0x7 else 30
		return '\x1b[{0}m'.format(base + idx)
	def bgcolor(idx):
		idx = (idx & 0xf)
		# 100 is bright background; 40 is dull background.
		base = 100 if idx > 0x7 else 40
		return '\x1b[{0}m'.format(base + idx)
	reset = '\x1b[0m'
	characters = [
		'▚',
		'▞',
		'▀',
		'▌',
	]
	pairs = list((w, h) for (w, h) in except_one(factors(len(hash) / 2)) if w >= h)
	if not pairs:
		# Prefer (w, 1) over (1, h) if we have that choice.
		pairs = list((w, h) for (w, h) in factors(len(hash) / 2) if w >= h)

	output_chunks = []
	last_byte = 0
	character_idx = None
	for b in bytes:
		character_idx = b % len(characters)
		output_chunks.append(fgcolor(b) + bgcolor(b) + characters[character_idx])
		last_byte = b

	pixels_per_row, num_rows = pairs[last_byte % len(pairs)]
	while output_chunks:
		yield ''.join(output_chunks[:pixels_per_row]) + reset
		del output_chunks[:pixels_per_row]

run_tests = False
if run_tests:
	# A square number. Should contain a diagonal pair (in this case, (16,16)).
	factors_of_256 = set(factors(256))
	assert factors_of_256 == set([(256, 1), (16, 16), (8, 32), (2, 128), (64, 4), (1, 256), (32, 8), (128, 2), (4, 64)])

	# A rectangular number: not square, but still composite. No diagonal pair here.
	factors_of_12 = set(factors(12))
	assert factors_of_12 == set([(2, 6), (12, 1), (1, 12), (6, 2), (4, 3), (3, 4)])

	assert (1, 256) in factors_of_256
	assert (256, 1) in factors_of_256
	assert (1, 256) not in except_one(factors_of_256)
	assert (256, 1) not in except_one(factors_of_256)

	# A prime number. Should have exactly one pair of factors.
	factors_of_5 = set(factors(5))
	assert factors_of_5 == set([(1, 5), (5, 1)])

	assert list(parse_hex('ab15e')) == [0xab, 0x15, 0x0e]
	assert list(parse_hex(':::ab:15:e')) == [0xab, 0x15, 0x0e]

	assert extract_hash_from_line('RSA key fingerprint is b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38.\n') == 'b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38'
	assert extract_hash_from_line('RSA key fingerprint is b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38.') == 'b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38'
	assert extract_hash_from_line('MD5 (hashvis.py) = e21c7b846f76826d52a0ade79ef9cb49\n') == 'e21c7b846f76826d52a0ade79ef9cb49'
	assert extract_hash_from_line('MD5 (hashvis.py) = e21c7b846f76826d52a0ade79ef9cb49') == 'e21c7b846f76826d52a0ade79ef9cb49'
	assert extract_hash_from_line('8b948e9c85fdf68f872017d7064e839c  hashvis.py\n') == '8b948e9c85fdf68f872017d7064e839c'
	assert extract_hash_from_line('8b948e9c85fdf68f872017d7064e839c  hashvis.py') == '8b948e9c85fdf68f872017d7064e839c'
	assert extract_hash_from_line('2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924  hashvis.py\n') == '2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924'
	assert extract_hash_from_line('2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924  hashvis.py') == '2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924'
	assert extract_hash_from_line('#!/usr/bin/python\n') is None

	sys.exit(0)

import fileinput

for input_line in fileinput.input():
	print input_line.rstrip('\n')

	hash = extract_hash_from_line(input_line)
	if hash:
		for output_line in hash_to_pic(hash):
			print output_line
<commit_msg>Add an alternate format used in newer OpenSSH's.<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import re

import cmath as math
range = xrange

def factors(n):
	"Yield every pair of factors of n (x,y where n/x == y and n/y == x), except for (1,n) and (n,1)."
	limit = math.sqrt(n).real
	if n == 1:
		yield (1, 1)
		return
	for i in range(1, int(limit + 1)):
		if n % i == 0:
			pair = (i, n/i)
			yield pair

			opposite_pair = (pair[1], pair[0])
			#If n is square, one of the pairs will be (sqrt, sqrt). We want to yield that only once. All other pairs, we want to yield both ways round.
			if pair != opposite_pair:
				yield opposite_pair

def except_one(pairs):
	"Given a sequence of pairs (x, y), yield every pair where neither x nor y is 1."
	for pair in pairs:
		if 1 not in pair:
			yield pair

MD5_exp = re.compile(r'^MD5 \(.*\) = ([0-9a-fA-f]+)')
RSA_exp = re.compile(r'^RSA key fingerprint is (?:MD5:)?([:0-9a-fA-f]+)\.')
def extract_hash_from_line(input_line):
	if input_line[:1] == 'M':
		match = MD5_exp.match(input_line)
		if match:
			return match.group(1)
		else:
			return ''
	elif input_line[:1] == 'R':
		match = RSA_exp.match(input_line)
		if match:
			return match.group(1)
		else:
			return ''
	else:
		try:
			hash, not_the_hash = input_line.split(None, 1)
		except ValueError:
			# Insufficient fields. This line doesn't contain any whitespace. Use the entire line.
			hash = input_line

		try:
			int(hash, 16)
		except ValueError:
			# Not a hex number.
			return None
		else:
			return hash

def parse_hex(hex):
	hex = hex.lstrip(':')
	while hex:
		byte_hex, hex = hex[:2], hex[2:].lstrip(':')
		yield int(byte_hex, 16)

def hash_to_pic(hash):
	bytes = parse_hex(hash)
	def fgcolor(idx):
		idx = ((idx >> 4) & 0xf)
		# 90 is bright foreground; 30 is dull foreground.
		base = 90 if idx > 0x7 else 30
		return '\x1b[{0}m'.format(base + idx)
	def bgcolor(idx):
		idx = (idx & 0xf)
		# 100 is bright background; 40 is dull background.
		base = 100 if idx > 0x7 else 40
		return '\x1b[{0}m'.format(base + idx)
	reset = '\x1b[0m'
	characters = [
		'▚',
		'▞',
		'▀',
		'▌',
	]
	pairs = list((w, h) for (w, h) in except_one(factors(len(hash) / 2)) if w >= h)
	if not pairs:
		# Prefer (w, 1) over (1, h) if we have that choice.
		pairs = list((w, h) for (w, h) in factors(len(hash) / 2) if w >= h)

	output_chunks = []
	last_byte = 0
	character_idx = None
	for b in bytes:
		character_idx = b % len(characters)
		output_chunks.append(fgcolor(b) + bgcolor(b) + characters[character_idx])
		last_byte = b

	pixels_per_row, num_rows = pairs[last_byte % len(pairs)]
	while output_chunks:
		yield ''.join(output_chunks[:pixels_per_row]) + reset
		del output_chunks[:pixels_per_row]

run_tests = False
if run_tests:
	# A square number. Should contain a diagonal pair (in this case, (16,16)).
	factors_of_256 = set(factors(256))
	assert factors_of_256 == set([(256, 1), (16, 16), (8, 32), (2, 128), (64, 4), (1, 256), (32, 8), (128, 2), (4, 64)])

	# A rectangular number: not square, but still composite. No diagonal pair here.
	factors_of_12 = set(factors(12))
	assert factors_of_12 == set([(2, 6), (12, 1), (1, 12), (6, 2), (4, 3), (3, 4)])

	assert (1, 256) in factors_of_256
	assert (256, 1) in factors_of_256
	assert (1, 256) not in except_one(factors_of_256)
	assert (256, 1) not in except_one(factors_of_256)

	# A prime number. Should have exactly one pair of factors.
	factors_of_5 = set(factors(5))
	assert factors_of_5 == set([(1, 5), (5, 1)])

	assert list(parse_hex('ab15e')) == [0xab, 0x15, 0x0e]
	assert list(parse_hex(':::ab:15:e')) == [0xab, 0x15, 0x0e]

	assert extract_hash_from_line('RSA key fingerprint is b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38.\n') == 'b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38'
	assert extract_hash_from_line('RSA key fingerprint is b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38.') == 'b8:79:03:7d:00:44:98:6e:67:a0:59:1a:01:21:36:38'
	#Alternate output example from https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Authentication_Keys :
	assert extract_hash_from_line('RSA key fingerprint is MD5:10:4a:ec:d2:f1:38:f7:ea:0a:a0:0f:17:57:ea:a6:16.') == '10:4a:ec:d2:f1:38:f7:ea:0a:a0:0f:17:57:ea:a6:16'

	assert extract_hash_from_line('MD5 (hashvis.py) = e21c7b846f76826d52a0ade79ef9cb49\n') == 'e21c7b846f76826d52a0ade79ef9cb49'
	assert extract_hash_from_line('MD5 (hashvis.py) = e21c7b846f76826d52a0ade79ef9cb49') == 'e21c7b846f76826d52a0ade79ef9cb49'
	assert extract_hash_from_line('8b948e9c85fdf68f872017d7064e839c  hashvis.py\n') == '8b948e9c85fdf68f872017d7064e839c'
	assert extract_hash_from_line('8b948e9c85fdf68f872017d7064e839c  hashvis.py') == '8b948e9c85fdf68f872017d7064e839c'
	assert extract_hash_from_line('2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924  hashvis.py\n') == '2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924'
	assert extract_hash_from_line('2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924  hashvis.py') == '2c9997ce32cb35823b2772912e221b350717fcb2d782c667b8f808be44ae77ba1a7b94b4111e386c64a2e87d15c64a2fc2177cd826b9a0fba6b348b4352ed924'
	assert extract_hash_from_line('#!/usr/bin/python\n') is None

	sys.exit(0)

import fileinput

for input_line in fileinput.input():
	print input_line.rstrip('\n')

	hash = extract_hash_from_line(input_line)
	if hash:
		for output_line in hash_to_pic(hash):
			print output_line
<|endoftext|>


<commit_before>"""Custom Query class for MS SQL Serever."""
import re

# query_class returns the base class to use for Django queries.
# The custom 'SqlServerQuery' class derives from django.db.models.sql.query.Query
# which is passed in as "QueryClass" by Django itself.
#
# SqlServerQuery overrides:
# ...insert queries to add "SET IDENTITY_INSERT" if needed.
# ...select queries to emulate LIMIT/OFFSET for sliced queries.

_re_order_limit_offset = re.compile(
    r'(?:ORDER BY\s+(.+?))?\s*(?:LIMIT\s+(\d+))?\s*(?:OFFSET\s+(\d+))?$')

def _get_order_limit_offset(sql):
    return _re_order_limit_offset.search(sql).groups()

def _remove_order_limit_offset(sql):
    return _re_order_limit_offset.sub('',sql)

def query_class(QueryClass):
    """Return a custom Query subclass for SQL Server."""
    class SqlServerQuery(QueryClass):
        def __init__(self, *args, **kwargs):
            super(SqlServerQuery, self).__init__(*args, **kwargs)

            # If we are an insert query, wrap "as_sql"
            if self.__class__.__name__ == "InsertQuery":
                self._parent_as_sql = self.as_sql
                self.as_sql = self._insert_as_sql

        def resolve_columns(self, row, fields=()):
            # If the results are sliced, the resultset will have an initial
            # "row number" column. Remove this column before the ORM sees it.
            if self._using_row_number:
                return row[1:]
            return row

        def as_sql(self, with_limits=True, with_col_aliases=False):
            self._using_row_number = False

            # Get out of the way if we're not a select query or there's no limiting involved.
            check_limits = with_limits and (self.low_mark or self.high_mark is not None)
            if self.__class__.__name__ != 'SqlServerQuery' or not check_limits:
                return super(SqlServerQuery, self).as_sql(with_limits, with_col_aliases)

            raw_sql, fields = super(SqlServerQuery, self).as_sql(False, with_col_aliases)

            # Check for high mark only and replace with "TOP"
            if self.high_mark and not self.low_mark:
                sql = re.sub(r'(?i)^SELECT', 'SELECT TOP %s' % self.high_mark, raw_sql, 1)
                return sql, fields

            # Else we have limits; rewrite the query using ROW_NUMBER()
            self._using_row_number = True

            order, limit_ignore, offset_ignore = _get_order_limit_offset(raw_sql)

            # Lop off ORDER... and the initial "SELECT"
            inner_select = _remove_order_limit_offset(raw_sql).split(None, 1)[1]

            # Using ROW_NUMBER requires an ordering
            if order is None:
                meta = self.get_meta()
                qn = self.connection.ops.quote_name
                order = '%s.%s ASC' % (qn(meta.db_table), qn(meta.pk.attname))

            where_row_num = "%s < _row_num" % (self.low_mark)
            if self.high_mark:
                where_row_num += " and _row_num <= %s" % (self.high_mark)

            outer_select, inner_select = self._alias_columns(inner_select)

            sql = "SELECT _row_num, %s FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY %s) as _row_num, %s) as QQQ where %s"\
                 % (outer_select, order, inner_select, where_row_num)

            return sql, fields

        def _alias_columns(self, sql):
            """Return tuple of SELECT and FROM clauses, aliasing duplicate column names."""
            qn = self.connection.ops.quote_name

            # Pattern to find the quoted column name at the end of a field specification
            _pat_col = r"\[([^[]+)\]$"
            #]) Funky comment to get e's syntax highlighting back on track.

            outer = list()
            inner = list()

            names_seen = list()
            original_names = [x.strip() for x in sql[:sql.find(' FROM [')].split(',')]
            for col in original_names:
                col_name = re.search(_pat_col, col).group(1)

                # If column name was already seen, alias it.
                if col_name in names_seen:
                    unique_col_name = qn('%s___%s' % (col_name, names_seen.count(col_name)))

                    outer.append(unique_col_name)
                    inner.append("%s as %s" % (col, unique_col_name))
                else:
                    outer.append(qn(col_name))
                    inner.append(col)

                names_seen.append(col_name)

            # Add FROM clause back to inner select
            return ', '.join(outer), ', '.join(inner) + sql[sql.find(' FROM ['):]

        def _insert_as_sql(self, *args, **kwargs):
            sql, params = self._parent_as_sql(*args,**kwargs)
            meta = self.get_meta()

            if (meta.pk.attname in self.columns) and (meta.pk.__class__.__name__ == "AutoField"):
                quoted_table = self.connection.ops.quote_name(meta.db_table)
                sql = "SET IDENTITY_INSERT %s ON;%s;SET IDENTITY_INSERT %s OFF" %\
                    (quoted_table, sql, quoted_table)

            return sql, params

    return SqlServerQuery
<commit_msg>Tweaks to aliasing code, hopefully made it slightly more readable.<commit_after>"""Custom Query class for MS SQL Serever."""
import re

# query_class returns the base class to use for Django queries.
# The custom 'SqlServerQuery' class derives from django.db.models.sql.query.Query
# which is passed in as "QueryClass" by Django itself.
#
# SqlServerQuery overrides:
# ...insert queries to add "SET IDENTITY_INSERT" if needed.
# ...select queries to emulate LIMIT/OFFSET for sliced queries.

_re_order_limit_offset = re.compile(
    r'(?:ORDER BY\s+(.+?))?\s*(?:LIMIT\s+(\d+))?\s*(?:OFFSET\s+(\d+))?$')

def _break(s, find):
    """Break a string s into the part before the substring to find,
    and the part including and after the substring."""
    i = s.find(find)
    return s[:i], s[i:]

def _get_order_limit_offset(sql):
    return _re_order_limit_offset.search(sql).groups()

def _remove_order_limit_offset(sql):
    return _re_order_limit_offset.sub('',sql).split(None, 1)[1]

def query_class(QueryClass):
    """Return a custom Query subclass for SQL Server."""
    class SqlServerQuery(QueryClass):
        def __init__(self, *args, **kwargs):
            super(SqlServerQuery, self).__init__(*args, **kwargs)

            # If we are an insert query, wrap "as_sql"
            if self.__class__.__name__ == "InsertQuery":
                self._parent_as_sql = self.as_sql
                self.as_sql = self._insert_as_sql

        def resolve_columns(self, row, fields=()):
            # If the results are sliced, the resultset will have an initial
            # "row number" column. Remove this column before the ORM sees it.
            if self._using_row_number:
                return row[1:]
            return row

        def as_sql(self, with_limits=True, with_col_aliases=False):
            self._using_row_number = False

            # Get out of the way if we're not a select query or there's no limiting involved.
            check_limits = with_limits and (self.low_mark or self.high_mark is not None)
            if self.__class__.__name__ != 'SqlServerQuery' or not check_limits:
                return super(SqlServerQuery, self).as_sql(with_limits, with_col_aliases)

            raw_sql, fields = super(SqlServerQuery, self).as_sql(False, with_col_aliases)

            # Check for high mark only and replace with "TOP"
            if self.high_mark and not self.low_mark:
                sql = re.sub(r'(?i)^SELECT', 'SELECT TOP %s' % self.high_mark, raw_sql, 1)
                return sql, fields

            # Else we have limits; rewrite the query using ROW_NUMBER()
            self._using_row_number = True

            order, limit_ignore, offset_ignore = _get_order_limit_offset(raw_sql)

            # Using ROW_NUMBER requires an ordering
            if order is None:
                meta = self.get_meta()
                qn = self.connection.ops.quote_name
                order = '%s.%s ASC' % (qn(meta.db_table), qn(meta.pk.attname))

            where_row_num = "%s < _row_num" % (self.low_mark)
            if self.high_mark:
                where_row_num += " and _row_num <= %s" % (self.high_mark)

            # Lop off ORDER... and the initial "SELECT"
            inner_select = _remove_order_limit_offset(raw_sql)
            outer_fields, inner_select = self._alias_columns(inner_select)

            sql = "SELECT _row_num, %s FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY %s) as _row_num, %s) as QQQ where %s"\
                 % (outer_fields, order, inner_select, where_row_num)

            return sql, fields

        def _alias_columns(self, sql):
            """Return tuple of SELECT and FROM clauses, aliasing duplicate column names."""
            qn = self.connection.ops.quote_name

            # Pattern to find the quoted column name at the end of a field specification
            _pat_col = r"\[([^[]+)\]$"
            #]) Funky comment to get e's syntax highlighting back on track.

            outer = list()
            inner = list()

            names_seen = list()

            select_list, from_clause = _break(sql, ' FROM [')
            for col in [x.strip() for x in select_list.split(',')]:
                col_name = re.search(_pat_col, col).group(1)

                # If column name was already seen, alias it.
                if col_name in names_seen:
                    alias = qn('%s___%s' % (col_name, names_seen.count(col_name)))
                    outer.append(alias)
                    inner.append("%s as %s" % (col, alias))
                else:
                    outer.append(qn(col_name))
                    inner.append(col)

                names_seen.append(col_name)

            # Add FROM clause back to inner select
            return ', '.join(outer), ', '.join(inner) + from_clause

        def _insert_as_sql(self, *args, **kwargs):
            sql, params = self._parent_as_sql(*args,**kwargs)
            meta = self.get_meta()

            if (meta.pk.attname in self.columns) and (meta.pk.__class__.__name__ == "AutoField"):
                quoted_table = self.connection.ops.quote_name(meta.db_table)
                sql = "SET IDENTITY_INSERT %s ON;%s;SET IDENTITY_INSERT %s OFF" %\
                    (quoted_table, sql, quoted_table)

            return sql, params

    return SqlServerQuery
<|endoftext|>


<commit_before>#!/usr/bin/python
"""June Application Wrapper

June Application Wrapper provides a good way to organize your tornado app
like blueprints or Django.

:copyright: (c) 2012 by lepture
:license: BSD
"""

import os

#: always use utc time
os.environ['TZ'] = 'UTC'

#: always place python3 egg cache in /tmp
os.environ["PYTHON_EGG_CACHE"] = "/tmp/egg"

from tornado.options import define, options

_first_run = True
if _first_run:
    #: server configuration
    define('debug', default=True, type=bool, help='show debug information')
    define('port', default=5000, type=int, help='run server on this port')
    define('settings', default='', type=str, help='setting file path')

    #: application configuration
    define('template_path', type=str,
           help='absolute path of template directory')
    define('static_path', type=str, help='absolute path of static directory')
    define('static_url_prefix', default='/static/', type=str)
    define('enable_app_static', default=True, type=bool)
    define('xsrf_cookies', default=False, type=bool)
    define('cookie_secret', type=str)

    define('locale_path', type=str, help='absolute path of locale directory')

    define('login_url', default='/account/login', type=str,
           help='when use is not authenticated, redirect to login_url')

    _first_run = False

import logging
from tornado import web, escape
from tornado.util import import_object
from tornado.template import Loader, Template
from junetornado.util import parse_config_file

def register_app(app_list):
    """Detect if an app is available"""
    for app in app_list:
        name = app[0]
        app = app[1]
        if '.' in app:
            module = import_object(app)
        else:
            module = __import__(app)

        if hasattr(module, '__version__'):
            version = module.__version__
        elif hasattr(module, 'version'):
            version = module.version
        elif hasattr(module, 'VERSION'):
            version = module.VERSION
        else:
            version = 'unknown'
        logging.info("Load App: %s %s", name, version)

def register_app_handlers(handlers, app_list):
    """Register the app to handlers from app_list.

    Define your app_list at your project config.py file::

        app_list = [
            ('appname', 'yourproject.yourapp'),
            (...),
        ]

    And in yourproject/yourapp/handlers.py, define urls as usual. This
    function will create the new pattern of urls for you. An example::

        #: `~yourproject.yourapp.handlers.urls`
        urls = [
            ('/create', CreateHandler),  # a tuple
            url('/delete', DeleteHandler, name='delete'),  # URLSpec
            (...),
        ]

    Tuple and URLSpec are accepted like default tornado does. And the
    final urls will be something like::

        /appname/create
        /appname/delete
        /appname/...
    """
    for app in app_list:
        print('register: %s' % app[0])
        try:
            app_handlers = import_object('%s.handlers.urls' % app[1])
        except AttributeError:
            app_handlers = []
        for spec in app_handlers:
            if isinstance(spec, tuple):
                assert len(spec) in (2, 3)
                pattern = spec[0]
                handler = spec[1]

                if isinstance(handler, str):
                    handler = import_object(handler)

                if len(spec) == 3:
                    kwargs = spec[2]
                else:
                    kwargs = {}

                pattern = '/%s%s' % (app[0], pattern)
                spec = web.URLSpec(pattern, handler, kwargs)
            elif isinstance(spec, web.URLSpec):
                pattern = '/%s%s' % (app[0], spec.regex.pattern)
                spec = web.URLSpec(pattern, app[1], spec.kwargs, spec.name)

            handlers.append(spec)

def register_app_ui_modules(ui_modules, app_list):
    """Register the app to ui_modules from app_list.

    Define your app_list at your project config.py file::

        app_list = [
            ('appname', 'yourproject.yourapp'),
            (...),
        ]

    And in yourproject/yourapp/handlers.py, define ui_modules as usual::

        #: `~yourproject.yourapp.handlers.ui_modules`
        ui_modules = {
            'MyModule': MyUIModule,
            ...
        }

    """
    for app in app_list:
        try:
            app_modules = import_object('%s.handlers.ui_modules' % app[1])
        except AttributeError:
            app_modules = {}
        ui_modules.update(app_modules)

class JuneTemplateLoader(Loader):
    def __init__(self, root_directory, app_list, **kwargs):
        super(JuneTemplateLoader, self).__init__(root_directory, **kwargs)
        self.app_list = app_list

    def _create_template(self, name):
        path = self._detect_template_path(name)
        if not path:
            raise OSError("Can't find file: %s" % name)
        f = open(path, 'r')
        template = Template(f.read(), name=name, loader=self)
        f.close()
        return template

    def _detect_template_path(self, name):
        """
        First load template from project templates directory.

        If template not in project templates directory, load from app templates
        directories.

        Directory example of an app::

            app/
                templates/
                    appname/   <---- better with an appname
                        layout.html
                        screen.html

        """
        path = os.path.join(self.root, name)
        if os.path.exists(path):
            return path
        for app in self.app_list:
            app = app[1]
            if '.' in app:
                app_path = import_object(app).__path__[0]
            else:
                app_path = __import__(app).__path__[0]
            path = os.path.join(app_path, 'templates', name)
            if os.path.exists(path):
                return path

        return None

class JuneApplication(web.Application):
    """June Application
    """
    def __init__(self, handlers=[], default_host="", transforms=None,
                 wsgi=False, **settings):
        try:
            import config
        except ImportError:
            config = None
        db = getattr(config, 'db', None)
        cache = getattr(config, 'cache', None)
        app_list = getattr(config, 'app_list', [])

        try:
            import urls
        except ImportError:
            urls = None

        handlers.extend(getattr(urls, 'handlers', []))
        register_app_handlers(handlers, app_list)

        ui_modules = getattr(urls, 'ui_modules', {})
        register_app_ui_modules(ui_modules, app_list)

        settings.update(dict(
            debug=options.debug,
            login_url=options.login_url,

            cookie_secret=options.cookie_secret,
            xsrf_cookies=options.xsrf_cookies,

            template_path=options.template_path,
            static_path=options.static_path,
            static_url_prefix=options.static_url_prefix,

            ui_modules=ui_modules,
            app_list=app_list,
        ))

        if hasattr(options, 'autoescape'):
            settings['autoescape'] = options.autoescape

        super(JuneApplication, self).__init__(handlers, default_host,
                                              transforms, wsgi, **settings)

        if db:
            JuneApplication.db = db.session
        else:
            JuneApplication.db = None
        JuneApplication.cache = cache

        if options.locale_path:
            #TODO: load app locale
            self.load_locale()

    def load_locale(self):
        import tornado.locale
        tornado.locale.load_translations(options.locale_path)
        tornado.locale.set_default_locale(options.default_locale)

    def load_app_static(self, handlers, app_list):
        #TODO
        pass

class JuneHandler(web.RequestHandler):
    app_template = False

    @property
    def db(self):
        if hasattr(self.application, 'db'):
            return self.application.db
        return None

    @property
    def cache(self):
        if hasattr(self.application, 'cache'):
            return self.application.cache
        return None

    def create_template_loader(self, template_path):
        settings = self.application.settings
        if 'app_list' not in settings:
            return super(JuneHandler,
                         self).create_template_loader(template_path)
        kwargs = {}
        if 'autoescape' in settings:
            kwargs['autoescape'] = settings['autoescape']
        return JuneTemplateLoader(template_path, settings['app_list'],
                                  **kwargs)

class JsonHandler(web.RequestHandler):
    xsrf_protect = False

    def check_xsrf_cookie(self):
        if not self.xsrf_protect:
            return
        return super(JsonHandler, self).check_xsrf_cookie()

    def is_ajax(self):
        return "XMLHttpRequest" == self.request.headers.get("X-Requested-With")

    def write(self, chunk):
        self.set_header("Content-Type", "application/json; charset=UTF-8")
        if isinstance(chunk, (dict, list)):
            chunk = escape.json_encode(chunk)
            callback = self.get_argument('callback', None)
            if callback:
                chunk = "%s(%s)" % (callback, escape.to_unicode(chunk))
                self.set_header("Content-Type",
                                "application/javascript; charset=UTF-8")
        super(JsonHandler, self).write(chunk)

class UIModule(web.UIModule):
    @property
    def db(self):
        return self.handler.db

    @property
    def cache(self):
        return self.handler.cache

def run_server(app):
    import tornado.options
    from tornado import httpserver, ioloop
    if options.settings:
        parse_config_file(options.settings)

    tornado.options.parse_command_line()
    server = httpserver.HTTPServer(app, xheaders=True)
    server.listen(int(options.port))

    try:
        import config
    except ImportError:
        config = None
    app_list = getattr(config, 'app_list', [])
    register_app(app_list)
    logging.info('Start server at 0.0.0.0:%s' % options.port)
    ioloop.IOLoop.instance().start()
<commit_msg>fix on load locale, fix on register app<commit_after>#!/usr/bin/python
"""June Application Wrapper

June Application Wrapper provides a good way to organize your tornado app
like blueprints or Django.

:copyright: (c) 2012 by lepture
:license: BSD
"""

import os

#: always use utc time
os.environ['TZ'] = 'UTC'

#: always place python3 egg cache in /tmp
os.environ["PYTHON_EGG_CACHE"] = "/tmp/egg"

from tornado.options import define, options

_first_run = True
if _first_run:
    #: server configuration
    define('debug', default=True, type=bool, help='show debug information')
    define('port', default=5000, type=int, help='run server on this port')
    define('settings', default='', type=str, help='setting file path')

    #: application configuration
    define('template_path', type=str,
           help='absolute path of template directory')
    define('static_path', type=str, help='absolute path of static directory')
    define('static_url_prefix', default='/static/', type=str)
    define('enable_app_static', default=True, type=bool)
    define('xsrf_cookies', default=False, type=bool)
    define('cookie_secret', type=str)

    define('locale_path', type=str, help='absolute path of locale directory')
    define('default_locale', default='en_US', type=str)

    define('login_url', default='/account/login', type=str,
           help='when use is not authenticated, redirect to login_url')

    _first_run = False

import logging
from tornado import web, escape
from tornado.util import import_object
from tornado.template import Loader, Template
from junetornado.util import parse_config_file

def register_app(app_list):
    """Detect if an app is available"""
    for app in app_list:
        name = app[0]
        app = app[1]
        if '.' in app:
            module = import_object(app)
        else:
            module = __import__(app)

        if hasattr(module, '__version__'):
            version = module.__version__
        elif hasattr(module, 'version'):
            version = module.version
        elif hasattr(module, 'VERSION'):
            version = module.VERSION
        else:
            version = 'unknown'
        logging.info("Load App: %s %s", name, version)

def register_app_handlers(handlers, app_list):
    """Register the app to handlers from app_list.

    Define your app_list at your project config.py file::

        app_list = [
            ('appname', 'yourproject.yourapp'),
            (...),
        ]

    And in yourproject/yourapp/handlers.py, define urls as usual. This
    function will create the new pattern of urls for you. An example::

        #: `~yourproject.yourapp.handlers.urls`
        urls = [
            ('/create', CreateHandler),  # a tuple
            url('/delete', DeleteHandler, name='delete'),  # URLSpec
            (...),
        ]

    Tuple and URLSpec are accepted like default tornado does. And the
    final urls will be something like::

        /appname/create
        /appname/delete
        /appname/...
    """
    if not app_list:
        return
    for app in app_list:
        try:
            app_handlers = import_object('%s.handlers.urls' % app[1])
        except AttributeError:
            app_handlers = []
        except ImportError:
            app_handlers = []
        for spec in app_handlers:
            if isinstance(spec, tuple):
                assert len(spec) in (2, 3)
                pattern = spec[0]
                handler = spec[1]

                if isinstance(handler, str):
                    handler = import_object(handler)

                if len(spec) == 3:
                    kwargs = spec[2]
                else:
                    kwargs = {}

                pattern = '/%s%s' % (app[0], pattern)
                spec = web.URLSpec(pattern, handler, kwargs)
            elif isinstance(spec, web.URLSpec):
                pattern = '/%s%s' % (app[0], spec.regex.pattern)
                spec = web.URLSpec(pattern, app[1], spec.kwargs, spec.name)

            handlers.append(spec)

def register_app_ui_modules(ui_modules, app_list):
    """Register the app to ui_modules from app_list.

    Define your app_list at your project config.py file::

        app_list = [
            ('appname', 'yourproject.yourapp'),
            (...),
        ]

    And in yourproject/yourapp/handlers.py, define ui_modules as usual::

        #: `~yourproject.yourapp.handlers.ui_modules`
        ui_modules = {
            'MyModule': MyUIModule,
            ...
        }

    """
    if not app_list:
        return
    for app in app_list:
        try:
            app_modules = import_object('%s.handlers.ui_modules' % app[1])
        except AttributeError:
            app_modules = {}
        except ImportError:
            app_modules = {}
        ui_modules.update(app_modules)

class JuneTemplateLoader(Loader):
    def __init__(self, root_directory, app_list, **kwargs):
        super(JuneTemplateLoader, self).__init__(root_directory, **kwargs)
        self.app_list = app_list

    def _create_template(self, name):
        path = self._detect_template_path(name)
        if not path:
            raise OSError("Can't find file: %s" % name)
        f = open(path, 'r')
        template = Template(f.read(), name=name, loader=self)
        f.close()
        return template

    def _detect_template_path(self, name):
        """
        First load template from project templates directory.

        If template not in project templates directory, load from app templates
        directories.

        Directory example of an app::

            app/
                templates/
                    appname/   <---- better with an appname
                        layout.html
                        screen.html

        """
        path = os.path.join(self.root, name)
        if os.path.exists(path):
            return path
        for app in self.app_list:
            app = app[1]
            if '.' in app:
                app_path = import_object(app).__path__[0]
            else:
                app_path = __import__(app).__path__[0]
            path = os.path.join(app_path, 'templates', name)
            if os.path.exists(path):
                return path

        return None

class JuneApplication(web.Application):
    """June Application
    """
    def __init__(self, handlers=[], default_host="", transforms=None,
                 wsgi=False, **settings):
        try:
            import config
        except ImportError:
            config = None
        db = getattr(config, 'db', None)
        cache = getattr(config, 'cache', None)
        app_list = getattr(config, 'app_list', [])

        try:
            import urls
        except ImportError:
            urls = None

        try:
            handlers.extend(urls.handlers)
        except AttributeError:
            pass
        register_app_handlers(handlers, app_list)

        try:
            ui_modules = urls.ui_modules
        except AttributeError:
            ui_modules = {}
        register_app_ui_modules(ui_modules, app_list)

        settings.update(dict(
            debug=options.debug,
            login_url=options.login_url,

            cookie_secret=options.cookie_secret,
            xsrf_cookies=options.xsrf_cookies,

            template_path=options.template_path,
            static_path=options.static_path,
            static_url_prefix=options.static_url_prefix,

            ui_modules=ui_modules,
            app_list=app_list,
        ))

        if hasattr(options, 'autoescape'):
            settings['autoescape'] = options.autoescape

        super(JuneApplication, self).__init__(handlers, default_host,
                                              transforms, wsgi, **settings)

        if db:
            JuneApplication.db = db.session
        else:
            JuneApplication.db = None
        JuneApplication.cache = cache

    def load_app_static(self, handlers, app_list):
        #TODO
        pass

class JuneHandler(web.RequestHandler):
    app_template = False

    @property
    def db(self):
        if hasattr(self.application, 'db'):
            return self.application.db
        return None

    @property
    def cache(self):
        if hasattr(self.application, 'cache'):
            return self.application.cache
        return None

    def create_template_loader(self, template_path):
        settings = self.application.settings
        if 'app_list' not in settings:
            return super(JuneHandler,
                         self).create_template_loader(template_path)
        kwargs = {}
        if 'autoescape' in settings:
            kwargs['autoescape'] = settings['autoescape']
        return JuneTemplateLoader(template_path, settings['app_list'],
                                  **kwargs)

class JsonHandler(web.RequestHandler):
    xsrf_protect = False

    def check_xsrf_cookie(self):
        if not self.xsrf_protect:
            return
        return super(JsonHandler, self).check_xsrf_cookie()

    def is_ajax(self):
        return "XMLHttpRequest" == self.request.headers.get("X-Requested-With")

    def write(self, chunk):
        self.set_header("Content-Type", "application/json; charset=UTF-8")
        if isinstance(chunk, (dict, list)):
            chunk = escape.json_encode(chunk)
            callback = self.get_argument('callback', None)
            if callback:
                chunk = "%s(%s)" % (callback, escape.to_unicode(chunk))
                self.set_header("Content-Type",
                                "application/javascript; charset=UTF-8")
        super(JsonHandler, self).write(chunk)

class UIModule(web.UIModule):
    @property
    def db(self):
        return self.handler.db

    @property
    def cache(self):
        return self.handler.cache

def run_server(app_func):
    import tornado.options
    import tornado.locale
    from tornado import httpserver, ioloop

    tornado.options.parse_command_line()
    if options.settings:
        parse_config_file(options.settings)

    server = httpserver.HTTPServer(app_func(), xheaders=True)
    server.listen(int(options.port))

    if options.locale_path:
        tornado.locale.load_translations(options.locale_path)
        tornado.locale.set_default_locale(options.default_locale)

    try:
        import config
    except ImportError:
        config = None
    app_list = getattr(config, 'app_list', [])
    register_app(app_list)
    logging.info('Start server at 0.0.0.0:%s' % options.port)
    ioloop.IOLoop.instance().start()
<|endoftext|>


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

'''
Generate commit hashes with a prefix you like.
'''

import os
import re
import sys
import zlib
import json
import argparse
from hashlib import sha1
from subprocess import Popen, PIPE

# Helpers to parse commit objects.
NULL = '\0'
TIMESTAMP = r'\>.*\d{10}\s'

# Debug flag.
DEBUG_FLAG = False

def parse(commit):
    '''
    Parse the commit object into a JSON.
    '''
    payload, author, committer, messages = {}, None, None, []
    cat_file = ['git', 'cat-file', 'commit', commit]
    execute = Popen(cat_file, stdout=PIPE, stdin=PIPE)
    stdout, _ = execute.communicate()

    for line in stdout.split('\n'):
        if re.search(r'author', line):
            author = line
        if re.search(r'committer', line):
            committer = line

        if not re.search(r'^(author|committer|tree|parent|commit)', line):
            messages.append(line)

    if execute.returncode == 0:
        payload.update({'length': len(stdout)})
        payload.update({
            'raw': stdout,
            'length': len(stdout),
            'commit': commit,
            'author': get_timestamp(author),
            'message': '\n'.join(messages),
            'committer': get_timestamp(committer)
        })

    if DEBUG_FLAG:
        print 'Parsed commit object:'
        print json.dumps(payload, indent=4, sort_keys=True)
    return payload

def get_timestamp(string):
    '''
    Get the timestamp from the commit object.
    '''
    if DEBUG_FLAG:
        print 'Received: {0}.'.format(string)

    timestamp = re.search(TIMESTAMP, string).group()
    timestamp = re.sub('>', '', timestamp).strip()
    return timestamp

def get_hash(commit):
    '''
    Get the SHA1 of the commit (if refspecs are used).
    '''
    commit_hash = None
    rev_parse = ['git', 'rev-parse', commit]
    execute = Popen(rev_parse, stdout=PIPE, stdin=PIPE)
    stdout, _ = execute.communicate()

    if execute.returncode == 0:
        commit_hash = stdout.strip()

    if DEBUG_FLAG:
        print 'SHA1 of {0}: {1}.'.format(commit, commit_hash)
    return commit_hash

def reconstruct(commit):
    '''
    Print the raw content of the commit object from the repository.
    '''
    commit_hash = get_hash(commit)
    path = '.git/objects/' + commit_hash[:2] + '/' + commit_hash[2:]
    with open(path) as _file:
        print zlib.decompress(_file.read())

def generate_hash(payload, prefix):
    '''
    Generate SHA1 hash of the commit object.
    '''
    solution, random, flag = get_hash(payload['commit']), '', False

    try:
        int(prefix, 16)
    except ValueError:
        if DEBUG_FLAG:
            print '{0} is not a valid hexadecimal prefix.'.format(prefix)
        return solution, random, False

    while not solution.startswith(prefix):
        flag = True

        random = 'foo: {0}'.format(sha1(str(os.urandom(64))).hexdigest())
        length = str(payload['length'] + len(random) + 2)
        to_be_hashed = ''.join(['commit ', length, NULL, payload['raw'],
                                '\n', random, '\n'])
        solution = sha1(to_be_hashed).hexdigest()

    if DEBUG_FLAG:
        if not flag:
            print ('Existing prefix and the specified prefix '
                   'are the same; nothing to do here.')

        else:
            print 'Calculated SHA1: {0}.'.format(solution)
            print 'Random string to be appended: {0}.'.format(random)
    return solution, random, flag

def post(expected, revision):
    '''
    Post-validation check.
    '''
    current = get_hash(revision)
    return True if expected == current else False

def make_commit(commit, prefix):
    '''
    Make a commit with the prefix.
    '''
    payload = parse(commit)
    expected, string, flag = generate_hash(payload, prefix)

    if flag:
        os.environ['GIT_AUTHOR_DATE'] = payload['author']
        os.environ['GIT_COMMITTER_DATE'] = payload['committer']

        messages = payload['message'].split('\n') + [string]
        messages = map(lambda x: x if x != '' else '-m', messages)

        commit_it = ['git', 'commit', '--amend'] + messages

        if DEBUG_FLAG:
            print 'Commit command:\n{0}'.format(commit_it)

        execute = Popen(commit_it, stdout=PIPE, stderr=PIPE)

        stdout, _ = execute.communicate()
        print stdout

        del os.environ['GIT_AUTHOR_DATE']
        del os.environ['GIT_COMMITTER_DATE']

    return True if post(expected, commit) else False

def main():
    '''
    This is the main method.
    '''
    global DEBUG_FLAG
    description = 'Create vanity commits with a prefix you want.'
    parser = argparse.ArgumentParser(description=description)

    parser.add_argument('-p', '--prefix', help='a valid hexadecimal prefix',
                        required=True)
    parser.add_argument('-d', '--debug', help='enable debugging',
                        action='store_true', default=False)

    args = vars(parser.parse_args())

    if args['debug']:
        DEBUG_FLAG = True

    validated = make_commit('HEAD', args['prefix'])

    if DEBUG_FLAG:
        if validated:
            print 'Done.'
        else:
            print 'Fail.'

    return_code = 0 if validated else 1
    sys.exit(return_code)

if __name__ == '__main__':
    main()
<commit_msg>Did some refactoring.<commit_after>#! /usr/bin/env python2.7

'''
Generate commit hashes with a prefix you like.
'''

import os
import re
import sys
import zlib
import json
import argparse
from hashlib import sha1
from subprocess import Popen, PIPE

# Debug flag.
DEBUG_FLAG = False

def parse(commit):
    '''
    Parse the commit object into a JSON.
    '''
    payload, author, committer, messages = {}, None, None, []
    cat_file = ['git', 'cat-file', 'commit', commit]
    execute = Popen(cat_file, stdout=PIPE, stdin=PIPE)
    stdout, _ = execute.communicate()

    for line in stdout.split('\n'):
        if re.search(r'author', line):
            author = line
        if re.search(r'committer', line):
            committer = line

        if not re.search(r'^(author|committer|tree|parent|commit)', line):
            messages.append(line)

    if execute.returncode == 0:
        payload.update({'length': len(stdout)})
        payload.update({
            'raw': stdout,
            'length': len(stdout),
            'commit': commit,
            'author': get_timestamp(author),
            'message': '\n'.join(messages),
            'committer': get_timestamp(committer)
        })

    if DEBUG_FLAG:
        print 'Parsed commit object:'
        print json.dumps(payload, indent=4, sort_keys=True)
    return payload

def get_timestamp(string):
    '''
    Get the timestamp from the commit object.
    '''
    if DEBUG_FLAG:
        print 'Received: {0}.'.format(string)

    timestamp = re.search(r'\>.*\d{10}\s', string).group()
    timestamp = re.sub('>', '', timestamp).strip()
    return timestamp

def get_hash(commit):
    '''
    Get the SHA1 of the commit (if refspecs are used).
    '''
    commit_hash = None
    rev_parse = ['git', 'rev-parse', commit]
    execute = Popen(rev_parse, stdout=PIPE, stdin=PIPE)
    stdout, _ = execute.communicate()

    if execute.returncode == 0:
        commit_hash = stdout.strip()

    if DEBUG_FLAG:
        print 'SHA1 of {0}: {1}.'.format(commit, commit_hash)
    return commit_hash

def reconstruct(commit):
    '''
    Print the raw content of the commit object from the repository.
    '''
    commit_hash = get_hash(commit)
    path = '.git/objects/' + commit_hash[:2] + '/' + commit_hash[2:]
    with open(path) as _file:
        print zlib.decompress(_file.read())

def generate_hash(payload, prefix):
    '''
    Generate SHA1 hash of the commit object.
    '''
    solution, random, flag = get_hash(payload['commit']), '', False

    try:
        int(prefix, 16)
    except ValueError:
        if DEBUG_FLAG:
            print '{0} is not a valid hexadecimal prefix.'.format(prefix)
        return solution, random, False

    while not solution.startswith(prefix):
        flag = True

        random = 'foo: {0}'.format(sha1(str(os.urandom(64))).hexdigest())
        length = str(payload['length'] + len(random) + 2)
        to_be_hashed = ''.join(['commit ', length, '\0', payload['raw'],
                                '\n', random, '\n'])
        solution = sha1(to_be_hashed).hexdigest()

    if DEBUG_FLAG:
        if not flag:
            print ('Existing prefix and the specified prefix '
                   'are the same; nothing to do here.')

        else:
            print 'Calculated SHA1: {0}.'.format(solution)
            print 'Random string to be appended: {0}.'.format(random)
    return solution, random, flag

def post(expected, revision):
    '''
    Post-validation check.
    '''
    current = get_hash(revision)
    return True if expected == current else False

def make_commit(commit, prefix):
    '''
    Make a commit with the prefix.
    '''
    payload = parse(commit)
    expected, string, flag = generate_hash(payload, prefix)

    if flag:
        os.environ['GIT_AUTHOR_DATE'] = payload['author']
        os.environ['GIT_COMMITTER_DATE'] = payload['committer']

        messages = payload['message'].split('\n') + [string]
        messages = map(lambda x: x if x != '' else '-m', messages)

        commit_it = ['git', 'commit', '--amend'] + messages

        if DEBUG_FLAG:
            print 'Commit command:\n{0}'.format(commit_it)

        execute = Popen(commit_it, stdout=PIPE, stderr=PIPE)

        stdout, _ = execute.communicate()
        print stdout

        del os.environ['GIT_AUTHOR_DATE']
        del os.environ['GIT_COMMITTER_DATE']

    return True if post(expected, commit) else False

def main():
    '''
    This is the main method.
    '''
    global DEBUG_FLAG
    description = 'Create vanity commits with a prefix you want.'
    parser = argparse.ArgumentParser(description=description)

    parser.add_argument('-p', '--prefix', help='a valid hexadecimal prefix',
                        required=True)
    parser.add_argument('-d', '--debug', help='enable debugging',
                        action='store_true', default=False)

    args = vars(parser.parse_args())

    if args['debug']:
        DEBUG_FLAG = True

    validated = make_commit('HEAD', args['prefix'])

    if DEBUG_FLAG:
        if validated:
            print 'Done.'
        else:
            print 'Fail.'

    return_code = 0 if validated else 1
    sys.exit(return_code)

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


<commit_before>import os
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
from django.core.urlresolvers import reverse_lazy

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(BASE_DIR, '..', '..')

SECRET_KEY = 'na2p&yexkp-g83$2m^&b!r+a%nv2ci1!d9vh^a_7h!hv*7&h79'

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

SITE_ID = 1

INSTALLED_APPS = (
    # Django apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'user_sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Asyncronous worker support
    'celery',

    # i18n/l10n
    'django_babel',
    'statici18n',

    # For our REST Api
    'rest_framework',

    # Form helpers
    'floppyforms',

    # user (social-) account management
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.google',

    # Keybar apps
    'keybar',
)

MIDDLEWARE_CLASSES = (
    'user_sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'keybar.middlewares.server_header.ServerHeaderMiddleware'
)

ROOT_URLCONF = 'keybar.urls'

WSGI_APPLICATION = 'keybar.wsgi.application'

AUTH_USER_MODEL = 'keybar.User'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'keybar_dev',
    }
}

TEMPLATE_CONTEXT_PROCESSORS = TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',
    'django.contrib.messages.context_processors.messages',
    'allauth.account.context_processors.account',
    'allauth.socialaccount.context_processors.socialaccount',

    # Overwrite the allauth context processor because... it actively
    # verifies that the allauth processor exists.
    'keybar.context_processors.social.socialaccount'
)

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'bower_components'),
    os.path.join(PROJECT_DIR, 'src', 'keybar', 'static'),
)

TEMPLATE_DIRS = (
    os.path.join(PROJECT_DIR, 'src', 'keybar', 'templates'),
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend'
)

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(PROJECT_DIR, 'web', 'static')

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'web', 'media')

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Celery / Queue configuration
from kombu import Queue

BROKER_URL = 'redis://localhost:6379'

# Just so that this won't be forgotten, see
# http://docs.celeryproject.org/en/latest/getting-started/brokers/redis.html#caveats
# for details.
BROKER_TRANSPORT_OPTIONS = {
    'fanout_prefix': True,
    'fanout_patterns': True
}

# Force always eager to be False (it's by default but here for documentation)
CELERY_ALWAYS_EAGER = False
CELERY_EAGER_PROPAGATES_EXCEPTIONS = False

# Only accept JSON. This will be the default in Celery 3.2
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'

CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

# Track started tasks. This adds a new STARTED state once a task
# is started by the celery worker.
CELERY_TRACK_STARTED = True

CELERY_IMPORTS = (
    'keybar.tasks.mail',
)

CELERY_QUEUES = (
    Queue('default', routing_key='default'),
    Queue('celery', routing_key='celery'),
)

# Make our `LOGGING` configuration the only truth and don't let celery
# overwrite it.
CELERYD_HIJACK_ROOT_LOGGER = False

# Don't log celery log-redirection as warning (default).
# We manage our logging through `django.conf.settings.LOGGING` and
# want that to be our first-citizen config.
CELERY_REDIRECT_STDOUTS_LEVEL = 'INFO'

# Django security related settings.
SECURE_SSL_REDIRECT = True

# Commit to HTTPS only for 12 months (HSTS)
SECURE_HSTS_SECONDS = 31536000

# prevent framing
SECURE_FRAME_DENY = True

# Don't let the browser guess content-types
SECURE_CONTENT_TYPE_NOSNIFF = True

# Enable browser XSS Filter
SECURE_BROWSER_XSS_FILTER = True

# Force cookies to be https only (or at least tell the browsers to do so...)
SESSION_COOKIE_SECURE = True

SESSION_COOKIE_HTTPONLY = True

SESSION_ENGINE = 'user_sessions.backends.db'

GEOIP_PATH = os.path.join(BASE_DIR, 'resources', 'geoip')

SESSION_SERIALIZER = 'keybar.utils.helpers.UUIDCapableJSONSerializer'

# Django REST Framework related settings.
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'keybar.api.auth.KeybarApiSignatureAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'keybar.api.parsers.JSONParser',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
}

# (social-) auth related settings
LOGIN_URL = reverse_lazy('account_login')
LOGIN_REDIRECT_URL = reverse_lazy('keybar-index')

ACCOUNT_AUTHENTICATION_METHOD = 'email'

ACCOUNT_EMAIL_REQUIRED = True

ACCOUNT_EMAIL_VERIFICATION = 'optional'

ACCOUNT_EMAIL_SUBJECT_PREFIX = 'Keybar - '

ACCOUNT_SIGNUP_FORM_CLASS = 'keybar.web.forms.RegisterForm'

ACCOUNT_SIGNUP_PASSWORD_VERIFICATION = False

ACCOUNT_UNIQUE_EMAIL = True

ACCOUNT_USER_DISPLAY = 'keybar.utils.get_user_name'

ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email'

ACCOUNT_USERNAME_REQUIRED = False

ACCOUNT_PASSWORD_INPUT_RENDER_VALUE = False

ACCOUNT_SESSION_REMEMBER = False

ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'https'

# Directly logout once the user visits /account/logout/
ACCOUNT_LOGOUT_ON_GET = True

SOCIALACCOUNT_QUERY_EMAIL = ACCOUNT_EMAIL_REQUIRED

SOCIALACCOUNT_AUTO_SIGNUP = True

# Keybar related settings
KEYBAR_SERVER_CERTIFICATE = None
KEYBAR_SERVER_KEY = None

KEYBAR_CLIENT_CERTIFICATE = None
KEYBAR_CLIENT_KEY = None

KEYBAR_VERIFY_CLIENT_CERTIFICATE = True

try:
    import certifi
except ImportError:
    certifi = None

def _default_ca_certs():
    if certifi is None:
        raise Exception('The \'certifi\' package is required to use https')
    return certifi.where()

KEYBAR_CA_BUNDLE = _default_ca_certs()

KEYBAR_DOMAIN = None
KEYBAR_HOST = None

# NOTE: CHANGING THOSE VALUES REQUIRES RE-ENCRYPTION OF EVERYTHING
# AND HAS SOME DEEP IMPACT. !! JUST DONT !!
# In 2013 100,000 was the recommended value, so we settle with one million
# for now.
KEYBAR_KDF_ITERATIONS = 100000
<commit_msg>Port to new TEMPLATES setting<commit_after>import os
from django.core.urlresolvers import reverse_lazy

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(BASE_DIR, '..', '..')

SECRET_KEY = 'na2p&yexkp-g83$2m^&b!r+a%nv2ci1!d9vh^a_7h!hv*7&h79'

DEBUG = True

INSTALLED_APPS = (
    # Django apps
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'user_sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Asyncronous worker support
    'celery',

    # i18n/l10n
    'django_babel',
    'statici18n',

    # For our REST Api
    'rest_framework',

    # Form helpers
    'floppyforms',

    # user (social-) account management
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.facebook',
    'allauth.socialaccount.providers.google',

    # Keybar apps
    'keybar',
)

MIDDLEWARE_CLASSES = (
    'user_sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'keybar.middlewares.server_header.ServerHeaderMiddleware'
)

ROOT_URLCONF = 'keybar.urls'

WSGI_APPLICATION = 'keybar.wsgi.application'

AUTH_USER_MODEL = 'keybar.User'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'keybar_dev',
    }
}

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_DIR, 'src', 'keybar', 'templates'),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.core.context_processors.request',
                'django.contrib.messages.context_processors.messages',
                'allauth.account.context_processors.account',
                'allauth.socialaccount.context_processors.socialaccount',

                # Overwrite the allauth context processor because... it actively
                # verifies that the allauth processor exists.
                'keybar.context_processors.social.socialaccount'
            ],
        },
    },
]

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'bower_components'),
    os.path.join(PROJECT_DIR, 'src', 'keybar', 'static'),
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend'
)

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(PROJECT_DIR, 'web', 'static')

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'web', 'media')

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

# Celery / Queue configuration
from kombu import Queue

BROKER_URL = 'redis://localhost:6379'

# Just so that this won't be forgotten, see
# http://docs.celeryproject.org/en/latest/getting-started/brokers/redis.html#caveats
# for details.
BROKER_TRANSPORT_OPTIONS = {
    'fanout_prefix': True,
    'fanout_patterns': True
}

# Force always eager to be False (it's by default but here for documentation)
CELERY_ALWAYS_EAGER = False
CELERY_EAGER_PROPAGATES_EXCEPTIONS = False

# Only accept JSON. This will be the default in Celery 3.2
CELERY_ACCEPT_CONTENT = ['json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'

CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'

# Track started tasks. This adds a new STARTED state once a task
# is started by the celery worker.
CELERY_TRACK_STARTED = True

CELERY_IMPORTS = (
    'keybar.tasks.mail',
)

CELERY_QUEUES = (
    Queue('default', routing_key='default'),
    Queue('celery', routing_key='celery'),
)

# Make our `LOGGING` configuration the only truth and don't let celery
# overwrite it.
CELERYD_HIJACK_ROOT_LOGGER = False

# Don't log celery log-redirection as warning (default).
# We manage our logging through `django.conf.settings.LOGGING` and
# want that to be our first-citizen config.
CELERY_REDIRECT_STDOUTS_LEVEL = 'INFO'

# Django security related settings.
SECURE_SSL_REDIRECT = True

# Commit to HTTPS only for 12 months (HSTS)
SECURE_HSTS_SECONDS = 31536000

# prevent framing
SECURE_FRAME_DENY = True

# Don't let the browser guess content-types
SECURE_CONTENT_TYPE_NOSNIFF = True

# Enable browser XSS Filter
SECURE_BROWSER_XSS_FILTER = True

# Force cookies to be https only (or at least tell the browsers to do so...)
SESSION_COOKIE_SECURE = True

SESSION_COOKIE_HTTPONLY = True

SESSION_ENGINE = 'user_sessions.backends.db'

GEOIP_PATH = os.path.join(BASE_DIR, 'resources', 'geoip')

SESSION_SERIALIZER = 'keybar.utils.helpers.UUIDCapableJSONSerializer'

# Django REST Framework related settings.
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'keybar.api.auth.KeybarApiSignatureAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_PARSER_CLASSES': (
        'keybar.api.parsers.JSONParser',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
}

# (social-) auth related settings
LOGIN_URL = reverse_lazy('account_login')
LOGIN_REDIRECT_URL = reverse_lazy('keybar-index')

ACCOUNT_AUTHENTICATION_METHOD = 'email'

ACCOUNT_EMAIL_REQUIRED = True

ACCOUNT_EMAIL_VERIFICATION = 'optional'

ACCOUNT_EMAIL_SUBJECT_PREFIX = 'Keybar - '

ACCOUNT_SIGNUP_FORM_CLASS = 'keybar.web.forms.RegisterForm'

ACCOUNT_SIGNUP_PASSWORD_VERIFICATION = False

ACCOUNT_UNIQUE_EMAIL = True

ACCOUNT_USER_DISPLAY = 'keybar.utils.get_user_name'

ACCOUNT_USER_MODEL_USERNAME_FIELD = 'email'

ACCOUNT_USERNAME_REQUIRED = False

ACCOUNT_PASSWORD_INPUT_RENDER_VALUE = False

ACCOUNT_SESSION_REMEMBER = False

ACCOUNT_DEFAULT_HTTP_PROTOCOL = 'https'

# Directly logout once the user visits /account/logout/
ACCOUNT_LOGOUT_ON_GET = True

SOCIALACCOUNT_QUERY_EMAIL = ACCOUNT_EMAIL_REQUIRED

SOCIALACCOUNT_AUTO_SIGNUP = True

# Keybar related settings
KEYBAR_SERVER_CERTIFICATE = None
KEYBAR_SERVER_KEY = None

KEYBAR_CLIENT_CERTIFICATE = None
KEYBAR_CLIENT_KEY = None

KEYBAR_VERIFY_CLIENT_CERTIFICATE = True

try:
    import certifi
except ImportError:
    certifi = None

def _default_ca_certs():
    if certifi is None:
        raise Exception('The \'certifi\' package is required to use https')
    return certifi.where()

KEYBAR_CA_BUNDLE = _default_ca_certs()

KEYBAR_DOMAIN = None
KEYBAR_HOST = None

# NOTE: CHANGING THOSE VALUES REQUIRES RE-ENCRYPTION OF EVERYTHING
# AND HAS SOME DEEP IMPACT. !! JUST DONT !!
# In 2013 100,000 was the recommended value, so we settle with one million
# for now.
KEYBAR_KDF_ITERATIONS = 100000
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
__version__ = '2.0.1'

class ContainerAware(object):
    """An interface for classes that should have a container.

    Primarily used by the IocContainer, any class that subclasses it will
    have the container it was called from automatically injected into it.
    """
    _container = None

    @property
    def container(self):
        """
        Returns:
            The instance of the injected container.
        """
        return self._container

    @container.setter
    def container(self, container):
        self._container = container
<commit_msg>Bumped version number<commit_after># -*- coding: utf-8 -*-
__version__ = '2.0.2'

class ContainerAware(object):
    """An interface for classes that should have a container.

    Primarily used by the IocContainer, any class that subclasses it will
    have the container it was called from automatically injected into it.
    """
    _container = None

    @property
    def container(self):
        """
        Returns:
            The instance of the injected container.
        """
        return self._container

    @container.setter
    def container(self, container):
        self._container = container
<|endoftext|>


<commit_before># coding=utf-8
"""
"""
from __future__ import absolute_import

from wtforms.fields import StringField
from flask import current_app

from abilian.core.models.tag import Tag
from abilian.web.forms import Field, ModelForm
from abilian.web.forms.widgets import Select2, ListWidget
from abilian.web.forms.validators import required
from abilian.web.forms.filters import strip

class TagsField(Field):
  """
  Handle tags selection on for a given :attr:`tags namespace<.Tag.ns>`.

  Usage::

      __tags__ = TagsField(ns='tags namespace')

  """
  multiple = True
  widget = Select2(js_init='tags-select', multiple=True)
  view_widget = ListWidget()

  def __init__(self, ns, *args, **kwargs):
    kwargs.setdefault('view_widget', self.view_widget)
    super(TagsField, self).__init__(*args, **kwargs)
    self.ns = ns.strip()
    assert self.ns

  def iter_choices(self):
    choices = []
    extension = current_app.extensions['tags']
    ns_tags = extension.get(ns=self.ns)

    for tag in ns_tags:
      choices.append((tag.label, tag.label, tag in self.data))

    # in case of error during form validation, don't forget new added tags
    for tag in self.data - set(ns_tags):
      choices.append((tag.label, tag.label, True))

    return sorted(choices)

  def default(self):
    return set()

  def process_formdata(self, valuelist):
    extension = current_app.extensions['tags']
    valuelist = set(valuelist[0].split(u';'))
    data = set()

    for label in valuelist:
      tag = extension.get(ns=self.ns, label=label)
      if tag is None:
        tag = Tag(ns=self.ns, label=label)
      data.add(tag)

    self.data = data

  def populate_obj(self, obj, name):
    extension = current_app.extensions['tags']
    all_tags = extension.entity_tags(obj)
    to_remove = all_tags - self.data

    for tag in to_remove:
      all_tags.remove(tag)

    for tag in self.data:
      all_tags.add(tag)

class TagForm(ModelForm):
  """
  Form for a single tag
  """
  ns = StringField(u'Namespace',
                   validators=[required()],
                   filters=(strip,),
  )

  label = StringField(u'Label', filters=(strip,), validators=[required(),])

  class Meta:
    model = Tag
    include_primary_keys = True
    assign_required = False # for 'id': allow None, for new records
<commit_msg>fix TagsField: add/remove only self.ns tags<commit_after># coding=utf-8
"""
"""
from __future__ import absolute_import

from wtforms.fields import StringField
from flask import current_app

from abilian.core.models.tag import Tag
from abilian.web.forms import Field, ModelForm
from abilian.web.forms.widgets import Select2, ListWidget
from abilian.web.forms.validators import required
from abilian.web.forms.filters import strip

class TagsField(Field):
  """
  Handle tags selection on for a given :attr:`tags namespace<.Tag.ns>`.

  Usage::

      __tags__ = TagsField(ns='tags namespace')

  """
  multiple = True
  widget = Select2(js_init='tags-select', multiple=True)
  view_widget = ListWidget()

  def __init__(self, ns, *args, **kwargs):
    kwargs.setdefault('view_widget', self.view_widget)
    super(TagsField, self).__init__(*args, **kwargs)
    self.ns = ns.strip()
    assert self.ns

  def iter_choices(self):
    choices = []
    extension = current_app.extensions['tags']
    ns_tags = extension.get(ns=self.ns)

    for tag in ns_tags:
      choices.append((tag.label, tag.label, tag in self.data))

    # in case of error during form validation, don't forget new added tags
    for tag in self.data - set(ns_tags):
      choices.append((tag.label, tag.label, True))

    return sorted(choices)

  def default(self):
    return set()

  def process_data(self, data):
    data = {t for t in data if t.ns == self.ns}
    return super(TagsField, self).process_data(data)

  def process_formdata(self, valuelist):
    extension = current_app.extensions['tags']
    valuelist = set(valuelist[0].split(u';'))
    data = set()

    for label in valuelist:
      tag = extension.get(ns=self.ns, label=label)
      if tag is None:
        tag = Tag(ns=self.ns, label=label)
      data.add(tag)

    self.data = data

  def populate_obj(self, obj, name):
    extension = current_app.extensions['tags']
    # all_tags is an InstrumentedSet. add/remove will result in DB operations.
    all_tags = extension.entity_tags(obj)
    all_ns_tags = {t for t in extension.entity_tags(obj)
                if t.ns == self.ns}
    to_remove = all_ns_tags - self.data

    for tag in to_remove:
      all_tags.remove(tag)

    for tag in self.data:
      all_tags.add(tag)

class TagForm(ModelForm):
  """
  Form for a single tag
  """
  ns = StringField(u'Namespace',
                   validators=[required()],
                   filters=(strip,),
  )

  label = StringField(u'Label', filters=(strip,), validators=[required(),])

  class Meta:
    model = Tag
    include_primary_keys = True
    assign_required = False # for 'id': allow None, for new records
<|endoftext|>