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

"""
Uses the extended ContentHandler from xml_driver to extract the needed fields
from patent grant documents
"""

from cStringIO import StringIO
from datetime import datetime
from unidecode import unidecode
import re
import uuid
import xml.sax
import xml_util
import xml_driver

claim_num_regex = re.compile(r'^\d+\. *') # removes claim number from claim text

class PatentGrant(object):

    def __init__(self, xml_string, is_string=False):
        xh = xml_driver.XMLHandler()
        parser = xml_driver.make_parser()

        parser.setContentHandler(xh)
        parser.setFeature(xml_driver.handler.feature_external_ges, False)
        l = xml.sax.xmlreader.Locator()
        xh.setDocumentLocator(l)
        if is_string:
            parser.parse(StringIO(xml_string))
        else:
            parser.parse(xml_string)
        self.xml = xh.root.us_patent_application

        self.country = self.xml.publication_reference.contents_of('country', upper=False)[0]
        self.patent = xml_util.normalize_document_identifier(self.xml.publication_reference.contents_of('doc_number')[0])
        self.kind = self.xml.publication_reference.contents_of('kind')[0]
        self.date_app = self.xml.publication_reference.contents_of('date')[0] # changed from grant to applied
        self.pat_type = None # not sure exactly what this is
        # don't know what to do about referencing a patent grant
        # or when the patent has been granted
        #
        #
        #
        #
        #
        self.clm_num = len(self.xml.claims.claim)
        self.abstract = self.xml.abstract.contents_of('p', '', as_string=True, upper=False)
        self.invention_title = self.xml.us_bibliographic_data_application.contents_of('invention_title')

        self.pat = {
            "id": self.patent,
            "type": self.pat_type,
            "number": self.patent,
            "country": self.country,
            "date": self._fix_date(self.date_app),
            "abstract": self.abstract,
            "title": self.invention_title,
            "kind": self.kind,
            "num_claims": self.clm_num
        }
        self.app["id"] = str(self.app["date"])[:4] + "/" + self.app["number"]

    def _invention_title(self):
        original = self.xml.contents_of('invention_title', upper=False)[0]
        if isinstance(original, list):
            original = ''.join(original)
        return original

    def _name_helper(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True, upper=False)
        lastname = tag_root.contents_of('last_name', as_string=True, upper=False)
        return xml_util.associate_prefix(firstname, lastname)

    def _name_helper_dict(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True, upper=False)
        lastname = tag_root.contents_of('last_name', as_string=True, upper=False)
        firstname, lastname = xml_util.associate_prefix(firstname, lastname)
        return {'name_first': firstname, 'name_last': lastname}

    def _fix_date(self, datestring):
        """
        Converts a number representing YY/MM to a Date
        """
        if not datestring:
            return None
        elif datestring[:4] < "1900":
            return None
        # default to first of month in absence of day
        if datestring[-4:-2] == '00':
            datestring = datestring[:-4] + '01' + datestring[-2:]
        if datestring[-2:] == '00':
            datestring = datestring[:6] + '01'
        try:
            datestring = datetime.strptime(datestring, '%Y%m%d')
            return datestring
        except Exception as inst:
            print inst, datestring
            return None

    @property
    def assignee_list(self):
        """
        Returns list of dictionaries:
        assignee:
          name_last
          name_first
          residence
          nationality
          organization
          sequence
        location:
          id
          city
          state
          country
        """
        assignees = self.xml.assignees.assignee
        if not assignees:
            return []
        res = []
        for i, assignee in enumerate(assignees):
            # add assignee data
            asg = {}
            asg.update(self._name_helper_dict(assignee))  # add firstname, lastname
            asg['organization'] = assignee.contents_of('orgname', as_string=True, upper=False)
            asg['role'] = assignee.contents_of('role', as_string=True)
            asg['nationality'] = assignee.nationality.contents_of('country')[0]
            asg['residence'] = assignee.nationality.contents_of('country')[0]
            # add location data for assignee
            loc = {}
            for tag in ['city', 'state', 'country']:
                loc[tag] = assignee.contents_of(tag, as_string=True, upper=False)
            #this is created because of MySQL foreign key case sensitivities
            loc['id'] = unidecode(u"|".join([loc['city'], loc['state'], loc['country']]).lower())
            if any(asg.values()) or any(loc.values()):
                asg['sequence'] = i
                asg['uuid'] = str(uuid.uuid1())
                res.append([asg, loc])
        return res
<commit_msg>rename some fields application rather than patent, add type to app<commit_after>#!/usr/bin/env python

"""
Uses the extended ContentHandler from xml_driver to extract the needed fields
from patent grant documents
"""

from cStringIO import StringIO
from datetime import datetime
from unidecode import unidecode
import re
import uuid
import xml.sax
import xml_util
import xml_driver

claim_num_regex = re.compile(r'^\d+\. *') # removes claim number from claim text

class PatentGrant(object):

    def __init__(self, xml_string, is_string=False):
        xh = xml_driver.XMLHandler()
        parser = xml_driver.make_parser()

        parser.setContentHandler(xh)
        parser.setFeature(xml_driver.handler.feature_external_ges, False)
        l = xml.sax.xmlreader.Locator()
        xh.setDocumentLocator(l)
        if is_string:
            parser.parse(StringIO(xml_string))
        else:
            parser.parse(xml_string)
        self.xml = xh.root.us_patent_application

        self.country = self.xml.publication_reference.contents_of('country', upper=False)[0]
        self.application = xml_util.normalize_document_identifier(self.xml.publication_reference.contents_of('doc_number')[0])
        self.kind = self.xml.publication_reference.contents_of('kind')[0]
        self.date_app = self.xml.publication_reference.contents_of('date')[0]
        if self.xml.application_reference:
            self.pat_type = self.xml.application_reference[0].get_attribute('appl-type', upper=False)
        else:
            self.pat_type = None
        self.clm_num = len(self.xml.claims.claim)
        self.abstract = self.xml.abstract.contents_of('p', '', as_string=True, upper=False)
        self.invention_title = self.xml.us_bibliographic_data_application.contents_of('invention_title')

        self.pat = {
            "id": self.application,
            "type": self.pat_type,
            "number": self.application,
            "country": self.country,
            "date": self._fix_date(self.date_app),
            "abstract": self.abstract,
            "title": self.invention_title,
            "kind": self.kind,
            "num_claims": self.clm_num
        }
        self.app["id"] = str(self.app["date"])[:4] + "/" + self.app["number"]

    def _invention_title(self):
        original = self.xml.contents_of('invention_title', upper=False)[0]
        if isinstance(original, list):
            original = ''.join(original)
        return original

    def _name_helper(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True, upper=False)
        lastname = tag_root.contents_of('last_name', as_string=True, upper=False)
        return xml_util.associate_prefix(firstname, lastname)

    def _name_helper_dict(self, tag_root):
        """
        Returns dictionary of firstname, lastname with prefix associated
        with lastname
        """
        firstname = tag_root.contents_of('first_name', as_string=True, upper=False)
        lastname = tag_root.contents_of('last_name', as_string=True, upper=False)
        firstname, lastname = xml_util.associate_prefix(firstname, lastname)
        return {'name_first': firstname, 'name_last': lastname}

    def _fix_date(self, datestring):
        """
        Converts a number representing YY/MM to a Date
        """
        if not datestring:
            return None
        elif datestring[:4] < "1900":
            return None
        # default to first of month in absence of day
        if datestring[-4:-2] == '00':
            datestring = datestring[:-4] + '01' + datestring[-2:]
        if datestring[-2:] == '00':
            datestring = datestring[:6] + '01'
        try:
            datestring = datetime.strptime(datestring, '%Y%m%d')
            return datestring
        except Exception as inst:
            print inst, datestring
            return None

    @property
    def assignee_list(self):
        """
        Returns list of dictionaries:
        assignee:
          name_last
          name_first
          residence
          nationality
          organization
          sequence
        location:
          id
          city
          state
          country
        """
        assignees = self.xml.assignees.assignee
        if not assignees:
            return []
        res = []
        for i, assignee in enumerate(assignees):
            # add assignee data
            asg = {}
            asg.update(self._name_helper_dict(assignee))  # add firstname, lastname
            asg['organization'] = assignee.contents_of('orgname', as_string=True, upper=False)
            asg['role'] = assignee.contents_of('role', as_string=True)
            asg['nationality'] = assignee.nationality.contents_of('country')[0]
            asg['residence'] = assignee.nationality.contents_of('country')[0]
            # add location data for assignee
            loc = {}
            for tag in ['city', 'state', 'country']:
                loc[tag] = assignee.contents_of(tag, as_string=True, upper=False)
            #this is created because of MySQL foreign key case sensitivities
            loc['id'] = unidecode(u"|".join([loc['city'], loc['state'], loc['country']]).lower())
            if any(asg.values()) or any(loc.values()):
                asg['sequence'] = i
                asg['uuid'] = str(uuid.uuid1())
                res.append([asg, loc])
        return res
<|endoftext|>


<commit_before>VERSION = '0.10.10'
<commit_msg>bump version to 0.10.11<commit_after>VERSION = '0.10.11'
<|endoftext|>


<commit_before>"""This file contains the benchmarks that are run the benchmark.py script."""
import pytest
import qutip
import scipy
import numpy as np

# Available datatypes (using qutip_dense and qutip_csr to avoid confusion
# with density parameters)
@pytest.fixture(params=['numpy', 'scipy_csr', 'qutip_dense', 'qutip_csr'])
def dtype(request): return request.param

@pytest.fixture(params=np.logspace(1, 9, 9, base=2, dtype=int).tolist())
def size(request): return request.param

@pytest.fixture(params=["dense", "sparse"])
def density(request): return request.param

@pytest.fixture()
def matrix(size, density, dtype):
    """Return a random matrix of size `sizexsize'. Density is either 'dense'
    or 'sparse' and returns a fully dense or a tridiagonal matrix respectively.
    The matrices are Hermitian."""
    np.random.seed(1)

    if density == "sparse":
        ofdiag = np.random.rand(size-1) + 1j*np.random.rand(size-1)
        diag = np.random.rand(size) + 1j*np.random.rand(size)

        res = (np.diag(ofdiag, k=-1) +
               np.diag(diag, k=0) +
               np.diag(ofdiag.conj(), k=1))

    elif density == "dense":
        H = np.random.random((size, size)) + 1j*np.random.random((size, size))
        res = H + H.T.conj()

    if dtype == 'numpy':
        return res
    elif dtype == 'scipy_csr':
        return scipy.sparse.csr_matrix(res)
    else:
        res = qutip.Qobj(res)
        # the to() method only accepts dense or csr as inputs
        return res.to(dtype[6:])

@pytest.fixture(params=["op", 'ket'])
def matrix_2(matrix, size, density, dtype, request):
    if request.param == "op":
        return matrix
    if density == "sparse":
        res = qutip.rand_ket(size, density=0.3)
    else:
        res = qutip.rand_ket(size, density=1)

    if dtype == 'numpy':
        return res.full()
    if dtype == 'scipy_csr':
        return scipy.sparse.csr_matrix(res.full())
    if dtype == "qutip_csr":
        return res.to("csr")
    return res.to("dense")

def add(A, B):
    return A+B

def matmul(A, B):
    return A@B

def test_add(benchmark, matrix, request):
    # Group benchmark by operation, density and size.
    group = request.node.callspec.id
    group = "Add-" + group
    benchmark.group = group

    A = matrix
    B = matrix

    # Benchmark operations
    result = benchmark(add, A, B)

    return result

def test_matmul(benchmark, matrix, matrix_2, request):
    # Group benchmark by operation, density and size.
    group = request.node.callspec.id
    group = "Matmul-" + group
    benchmark.group = group

    A = matrix
    B = matrix_2

    # Benchmark operations
    result = benchmark(matmul, A, B)

    return result
<commit_msg>fix qutip ket dtype<commit_after>"""This file contains the benchmarks that are run the benchmark.py script."""
import pytest
import qutip
import scipy
import numpy as np

# Available datatypes (using qutip_dense and qutip_csr to avoid confusion
# with density parameters)
@pytest.fixture(params=['numpy', 'scipy_csr', 'qutip_dense', 'qutip_csr'])
def dtype(request): return request.param

@pytest.fixture(params=np.logspace(1, 9, 9, base=2, dtype=int).tolist())
def size(request): return request.param

@pytest.fixture(params=["dense", "sparse"])
def density(request): return request.param

@pytest.fixture()
def matrix(size, density, dtype):
    """Return a random matrix of size `sizexsize'. Density is either 'dense'
    or 'sparse' and returns a fully dense or a tridiagonal matrix respectively.
    The matrices are Hermitian."""
    np.random.seed(1)

    if density == "sparse":
        ofdiag = np.random.rand(size-1) + 1j*np.random.rand(size-1)
        diag = np.random.rand(size) + 1j*np.random.rand(size)

        res = (np.diag(ofdiag, k=-1) +
               np.diag(diag, k=0) +
               np.diag(ofdiag.conj(), k=1))

    elif density == "dense":
        H = np.random.random((size, size)) + 1j*np.random.random((size, size))
        res = H + H.T.conj()

    if dtype == 'numpy':
        return res
    elif dtype == 'scipy_csr':
        return scipy.sparse.csr_matrix(res)
    else:
        res = qutip.Qobj(res)
        # the to() method only accepts dense or csr as inputs
        return res.to(dtype[6:])

@pytest.fixture(params=["op", 'ket'])
def matrix_2(matrix, size, density, dtype, request):
    if request.param == "op":
        return matrix
    if density == "sparse":
        res = qutip.rand_ket(size, density=0.3)
    else:
        res = qutip.rand_ket(size, density=1)

    if dtype == 'numpy':
        return res.full()
    if dtype == 'scipy_csr':
        return scipy.sparse.csr_matrix(res.full())
    return res.to(dtype[6:])

def add(A, B):
    return A+B

def matmul(A, B):
    return A@B

def test_add(benchmark, matrix, request):
    # Group benchmark by operation, density and size.
    group = request.node.callspec.id
    group = "Add-" + group
    benchmark.group = group

    A = matrix
    B = matrix

    # Benchmark operations
    result = benchmark(add, A, B)

    return result

def test_matmul(benchmark, matrix, matrix_2, request):
    # Group benchmark by operation, density and size.
    group = request.node.callspec.id
    group = "Matmul-" + group
    benchmark.group = group

    A = matrix
    B = matrix_2

    # Benchmark operations
    result = benchmark(matmul, A, B)

    return result
<|endoftext|>


<commit_before>"""
SAX style events again.
"""

import ast

from dataclasses import dataclass
from functools import singledispatch
from typing import Iterator, List

from breakfast.position import Position
from breakfast.source import Source
from tests import make_source

class Event:
    pass

@dataclass
class Occurrence(Event):
    name: str
    position: Position
    node: ast.AST

@dataclass
class EnterScope(Event):
    node: ast.AST

@dataclass
class LeaveScope(Event):
    node: ast.AST

def node_position(
    node: ast.AST, source: Source, row_offset=0, column_offset=0
) -> Position:
    return Position(
        source=source,
        row=(node.lineno - 1) + row_offset,
        column=node.col_offset + column_offset,
    )

@singledispatch
def visit(node: ast.AST, source: Source) -> Iterator[Event]:
    """Visit a node.

    Copied and reworked from NodeVisitor in:

    https://github.com/python/cpython/blob/master/Lib/ast.py
    """
    yield from generic_visit(node, source)

@visit.register
def visit_name(node: ast.Name, source: Source) -> Iterator[Event]:
    yield Occurrence(name=node.id, position=node_position(node, source), node=node)

@visit.register
def visit_function(node: ast.FunctionDef, source: Source):
    row_offset, column_offset = len(node.decorator_list), len("def ")
    position = node_position(
        node, source, row_offset=row_offset, column_offset=column_offset
    )
    yield Occurrence(node.name, position, node)

    for arg in node.args.args:

        position = node_position(arg, source)
        yield Occurrence(arg.arg, position, arg)

    yield from generic_visit(node, source)

@visit.register
def visit_attribute(node: ast.Attribute, source: Source) -> Iterator[Event]:
    yield from visit(node.value, source)
    position = node_position(node, source)
    yield Occurrence(node.attr, position, node)

@visit.register
def visit_import(node: ast.Import, source: Source) -> Iterator[Event]:
    start = node_position(node, source)
    for alias in node.names:
        name = alias.name
        position = source.find_after(name, start)
        yield Occurrence(name, position, alias)

@visit.register
def visit_call(node: ast.Call, source: Source):
    call_position = node_position(node, source)
    yield from visit(node.func, source)

    for arg in node.args:
        yield from visit(arg, source)
    for keyword in node.keywords:
        if not keyword.arg:
            continue

        position = source.find_after(keyword.arg, call_position)
        yield Occurrence(keyword.arg, position, node)

def generic_visit(node, source: Source) -> Iterator[Event]:
    """Called if no explicit visitor function exists for a node.

    Copied and reworked from NodeVisitor in:

    https://github.com/python/cpython/blob/master/Lib/ast.py
    """
    for _, value in ast.iter_fields(node):
        if isinstance(value, list):
            for item in value:
                if isinstance(item, ast.AST):
                    yield from visit(item, source)
        elif isinstance(value, ast.AST):
            yield from visit(value, source)

def get_occurrences(source: Source) -> List[Occurrence]:
    initial_node = source.get_ast()
    return [
        event
        for event in visit(initial_node, source=source)
        if isinstance(event, Occurrence)
    ]

def test_finds_occurrences_in_and_outside_of_function():
    source = make_source(
        """
        def fun():
            old = 12
            old2 = 13
            result = old + old2
            del old
            return result

        old = 20
        """
    )

    assert [o.name for o in get_occurrences(source)] == [
        "fun",
        "old",
        "old2",
        "result",
        "old",
        "old2",
        "old",
        "result",
        "old",
    ]

def test_finds_imports():
    source = make_source(
        """
        import os
        """
    )

    assert [o.name for o in get_occurrences(source)] == ["os"]

def test_finds_attributes():
    source = make_source(
        """
        import os

        path = os.path.dirname(__file__)
        """
    )

    assert [o.name for o in get_occurrences(source)] == [
        "os",
        "path",
        "os",
        "path",
        "dirname",
        "__file__",
    ]

def test_finds_parameters():
    source = make_source(
        """
        def fun(arg, arg2):
            return arg + arg2
        fun(arg=1, arg2=2)
        """
    )

    assert [o.name for o in get_occurrences(source)] == [
        "fun",
        "arg",
        "arg2",
        "arg",
        "arg2",
        "fun",
        "arg",
        "arg2",
    ]
<commit_msg>classes<commit_after>"""
SAX style events again.
"""

import ast

from dataclasses import dataclass
from functools import singledispatch
from typing import Iterator, List

from breakfast.position import Position
from breakfast.source import Source
from tests import make_source

class Event:
    pass

@dataclass
class Occurrence(Event):
    name: str
    position: Position
    node: ast.AST

@dataclass
class EnterScope(Event):
    node: ast.AST

@dataclass
class LeaveScope(Event):
    node: ast.AST

def node_position(
    node: ast.AST, source: Source, row_offset=0, column_offset=0
) -> Position:
    return Position(
        source=source,
        row=(node.lineno - 1) + row_offset,
        column=node.col_offset + column_offset,
    )

@singledispatch
def visit(node: ast.AST, source: Source) -> Iterator[Event]:
    """Visit a node.

    Copied and reworked from NodeVisitor in:

    https://github.com/python/cpython/blob/master/Lib/ast.py
    """
    yield from generic_visit(node, source)

@visit.register
def visit_name(node: ast.Name, source: Source) -> Iterator[Event]:
    yield Occurrence(name=node.id, position=node_position(node, source), node=node)

@visit.register
def visit_class(node: ast.ClassDef, source: Source):
    row_offset, column_offset = len(node.decorator_list), len("class ")
    position = node_position(
        node, source, row_offset=row_offset, column_offset=column_offset
    )
    yield Occurrence(node.name, position, node)
    yield from generic_visit(node, source)

@visit.register
def visit_function(node: ast.FunctionDef, source: Source):
    row_offset, column_offset = len(node.decorator_list), len("def ")
    position = node_position(
        node, source, row_offset=row_offset, column_offset=column_offset
    )
    yield Occurrence(node.name, position, node)

    for arg in node.args.args:

        position = node_position(arg, source)
        yield Occurrence(arg.arg, position, arg)

    yield from generic_visit(node, source)

@visit.register
def visit_attribute(node: ast.Attribute, source: Source) -> Iterator[Event]:
    yield from visit(node.value, source)
    position = node_position(node, source)
    yield Occurrence(node.attr, position, node)

@visit.register
def visit_import(node: ast.Import, source: Source) -> Iterator[Event]:
    start = node_position(node, source)
    for alias in node.names:
        name = alias.name
        position = source.find_after(name, start)
        yield Occurrence(name, position, alias)

@visit.register
def visit_call(node: ast.Call, source: Source):
    call_position = node_position(node, source)
    yield from visit(node.func, source)

    for arg in node.args:
        yield from visit(arg, source)
    for keyword in node.keywords:
        if not keyword.arg:
            continue

        position = source.find_after(keyword.arg, call_position)
        yield Occurrence(keyword.arg, position, node)

def generic_visit(node, source: Source) -> Iterator[Event]:
    """Called if no explicit visitor function exists for a node.

    Copied and reworked from NodeVisitor in:

    https://github.com/python/cpython/blob/master/Lib/ast.py
    """
    for _, value in ast.iter_fields(node):
        if isinstance(value, list):
            for item in value:
                if isinstance(item, ast.AST):
                    yield from visit(item, source)
        elif isinstance(value, ast.AST):
            yield from visit(value, source)

def get_occurrences(source: Source) -> List[Occurrence]:
    initial_node = source.get_ast()
    return [
        event
        for event in visit(initial_node, source=source)
        if isinstance(event, Occurrence)
    ]

def test_finds_occurrences_in_and_outside_of_function():
    source = make_source(
        """
        def fun():
            old = 12
            old2 = 13
            result = old + old2
            del old
            return result

        old = 20
        """
    )

    assert [o.name for o in get_occurrences(source)] == [
        "fun",
        "old",
        "old2",
        "result",
        "old",
        "old2",
        "old",
        "result",
        "old",
    ]

def test_finds_imports():
    source = make_source(
        """
        import os
        """
    )

    assert [o.name for o in get_occurrences(source)] == ["os"]

def test_finds_attributes():
    source = make_source(
        """
        import os

        path = os.path.dirname(__file__)
        """
    )

    assert [o.name for o in get_occurrences(source)] == [
        "os",
        "path",
        "os",
        "path",
        "dirname",
        "__file__",
    ]

def test_finds_parameters():
    source = make_source(
        """
        def fun(arg, arg2):
            return arg + arg2
        fun(arg=1, arg2=2)
        """
    )

    assert [o.name for o in get_occurrences(source)] == [
        "fun",
        "arg",
        "arg2",
        "arg",
        "arg2",
        "fun",
        "arg",
        "arg2",
    ]

def test_finds_class_name():
    source = make_source(
        """
        class A:

            def old(self):
                pass

        unbound = A.old
        """
    )

    assert [o.name for o in get_occurrences(source)] == [
        "A",
        "old",
        "self",
        "unbound",
        "A",
        "old",
    ]
<|endoftext|>


<commit_before>import sys
import os
import tempfile
import shutil
from StringIO import StringIO

from distutils.core import Extension, Distribution
from distutils.command.build_ext import build_ext
from distutils import sysconfig

import unittest
from test import test_support

class BuildExtTestCase(unittest.TestCase):
    def setUp(self):
        # Create a simple test environment
        # Note that we're making changes to sys.path
        self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
        self.sys_path = sys.path[:]
        sys.path.append(self.tmp_dir)

        xx_c = os.path.join(sysconfig.project_base, 'Modules', 'xxmodule.c')
        shutil.copy(xx_c, self.tmp_dir)

    def test_build_ext(self):
        xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
        xx_ext = Extension('xx', [xx_c])
        dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        if os.name == "nt":
            # On Windows, we must build a debug version iff running
            # a debug build of Python
            cmd.debug = sys.executable.endswith("_d.exe")
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        old_stdout = sys.stdout
        if not test_support.verbose:
            # silence compiler output
            sys.stdout = StringIO()
        try:
            cmd.ensure_finalized()
            cmd.run()
        finally:
            sys.stdout = old_stdout

        import xx

        for attr in ('error', 'foo', 'new', 'roj'):
            self.assert_(hasattr(xx, attr))

        self.assertEquals(xx.foo(2, 5), 7)
        self.assertEquals(xx.foo(13,15), 28)
        self.assertEquals(xx.new().demo(), None)
        doc = 'This is a template module just for instruction.'
        self.assertEquals(xx.__doc__, doc)
        self.assert_(isinstance(xx.Null(), xx.Null))
        self.assert_(isinstance(xx.Str(), xx.Str))

    def tearDown(self):
        # Get everything back to normal
        test_support.unload('xx')
        sys.path = self.sys_path
        # XXX on Windows the test leaves a directory with xx.pyd in TEMP
        shutil.rmtree(self.tmp_dir, False if os.name != "nt" else True)

def test_suite():
    if not sysconfig.python_build:
        if test_support.verbose:
            print 'test_build_ext: The test must be run in a python build dir'
        return unittest.TestSuite()
    else: return unittest.makeSuite(BuildExtTestCase)

if __name__ == '__main__':
    test_support.run_unittest(test_suite())
<commit_msg>Issue #3925: Ignores shutil.rmtree error on cygwin too. Reviewed by Benjamin Peterson.<commit_after>import sys
import os
import tempfile
import shutil
from StringIO import StringIO

from distutils.core import Extension, Distribution
from distutils.command.build_ext import build_ext
from distutils import sysconfig

import unittest
from test import test_support

class BuildExtTestCase(unittest.TestCase):
    def setUp(self):
        # Create a simple test environment
        # Note that we're making changes to sys.path
        self.tmp_dir = tempfile.mkdtemp(prefix="pythontest_")
        self.sys_path = sys.path[:]
        sys.path.append(self.tmp_dir)

        xx_c = os.path.join(sysconfig.project_base, 'Modules', 'xxmodule.c')
        shutil.copy(xx_c, self.tmp_dir)

    def test_build_ext(self):
        xx_c = os.path.join(self.tmp_dir, 'xxmodule.c')
        xx_ext = Extension('xx', [xx_c])
        dist = Distribution({'name': 'xx', 'ext_modules': [xx_ext]})
        dist.package_dir = self.tmp_dir
        cmd = build_ext(dist)
        if os.name == "nt":
            # On Windows, we must build a debug version iff running
            # a debug build of Python
            cmd.debug = sys.executable.endswith("_d.exe")
        cmd.build_lib = self.tmp_dir
        cmd.build_temp = self.tmp_dir

        old_stdout = sys.stdout
        if not test_support.verbose:
            # silence compiler output
            sys.stdout = StringIO()
        try:
            cmd.ensure_finalized()
            cmd.run()
        finally:
            sys.stdout = old_stdout

        import xx

        for attr in ('error', 'foo', 'new', 'roj'):
            self.assert_(hasattr(xx, attr))

        self.assertEquals(xx.foo(2, 5), 7)
        self.assertEquals(xx.foo(13,15), 28)
        self.assertEquals(xx.new().demo(), None)
        doc = 'This is a template module just for instruction.'
        self.assertEquals(xx.__doc__, doc)
        self.assert_(isinstance(xx.Null(), xx.Null))
        self.assert_(isinstance(xx.Str(), xx.Str))

    def tearDown(self):
        # Get everything back to normal
        test_support.unload('xx')
        sys.path = self.sys_path
        # XXX on Windows the test leaves a directory with xx module in TEMP
        shutil.rmtree(self.tmp_dir, os.name == 'nt' or sys.platform == 'cygwin')

def test_suite():
    if not sysconfig.python_build:
        if test_support.verbose:
            print 'test_build_ext: The test must be run in a python build dir'
        return unittest.TestSuite()
    else: return unittest.makeSuite(BuildExtTestCase)

if __name__ == '__main__':
    test_support.run_unittest(test_suite())
<|endoftext|>


<commit_before>'''
.. module:: resize

This module defines the :py:class:`ShapeAdaptor` and :py:class:`ResizePolicy` classes which can
be used to wrap a :py:class:`datasources.DataSource` object so that the items it yields contain
a resized version of the original image.

.. moduleauthor Rasmus Diederichsen
'''
from datasources import DataSource, InputData
from network import Network
import numpy as np

class _ResizePolicyBase(object):
    '''Base class defining common properties of all resizing policies.

    Attributes
    ----------
    _new_shape  :   tuple
                    The shape to convert images to. Will be stripped of singular dimensions
    '''
    _new_shape: tuple = None

    def setShape(self, new_shape):
        '''Set the shape to match. If the last dimension is 1, it is removed.

        Parameters
        ----------
        new_shape   :   tuple or list
                        Shape to match.

        Raises
        ------
        ValueError
            If leading dimension is None (aka batch)
        '''
        if new_shape[0] is None:
            raise ValueError('Cannot work with None dimensions')
        if new_shape[-1] == 1:
            # remove channel dim
            new_shape = new_shape[:-1]
        self._new_shape = new_shape

    def resize(self, image):
        '''Resize an image according to this policy.

        Parameters
        ----------
        image   :   np.ndarray
                    Image to resize
        '''
        raise NotImplementedError('Abstract base class ResizePolicy cannot be used directly.')

class ResizePolicyBilinear(_ResizePolicyBase):
    '''Resize policy which bilinearly interpolates images to the target size.'''

    def resize(self, img):
        from skimage.transforms import resize
        if self._new_shape is None:
            return img
        else:
            return resize(img, self._new_shape, preserve_range=True)

class ResizePolicyPad(_ResizePolicyBase):
    '''Resize policy which will pad the input image to the target size. This will not work if
    the target size is smaller than the source size in any dimension.'''

    def __init__(self, mode, **kwargs):
        '''
        Parameters
        ----------
        mode    :   str
                    Any of the values excepted by :py:func:`np.pad`
        kwargs  :   dict
                    Additional arguments to pass to :py:func:`np.pad`, such as the value to pad with
        '''
        self._pad_mode = mode
        self._pad_kwargs = kwargs

    def resize(self, img):
        if self._new_shape is None or self._new_shape == img.shape:
            return img
        h, w = img.shape[:2]
        new_h, new_w = self._new_shape[:2]

        if new_h < h or new_w < w:
            raise ValueError('Cannot pad to a smaller size. Use `ResizePolicy.Crop` instead.')

        # necessary padding to reach desired size
        pad_h = new_h - h
        pad_w = new_w - w
        ###############################################################################
        #  If padding is not even, we put one more padding pixel to the bottom/right  #
        ###############################################################################
        top = pad_h // 2
        bottom = pad_h - top
        left = pad_w // 2
        right = pad_w - left
        print(self._new_shape)
        print(img.shape)
        __import__('ipdb').set_trace()

        return np.pad(img, (top, bottom, left, right), self._pad_mode, **self._pad_kwargs)

class ResizePolicy(object):

    @staticmethod
    def Bilinear():
        '''Create a bilinear interpolation policy.'''
        return ResizePolicyBilinear()

    @staticmethod
    def Pad():
        '''Create a pad-with-zero policy.'''
        return ResizePolicyPad('constant', constant_values=0)

    @staticmethod
    def Crop():
        raise NotImplementedError

class ShapeAdaptor(DataSource):
    '''Adaptive wrapper around a :py:class:`DataSource`'''

    def __init__(self, network: Network, source: DataSource, resize_policy):
        '''
        Parameters
        ----------
        network :   network.Network
                    Network to adapt to
        source  :   DataSource
                    Source to adapt from
        resize  :   ResizePolicy
                    Policy to use for resizing images
        '''
        super().__init__(f'ShapeAdaptor< {source._description} >')
        self._source = source
        self._resize = resize_policy
        self.setNetwork(network)

    def setNetwork(self, network: Network):
        '''Change the network to adapt to.

        Parameters
        ----------
        network :   network.Network
        '''
        self._resize.setShape(network.get_input_shape(include_batch=False))

    def __getitem__(self, item: int):
        img, name = self._source[item]
        return InputData(self._resize.resize(img), name)

    def __len__(self):
        return len(self._source)
<commit_msg>Fix sphinx errror<commit_after>'''
.. module:: resize

This module defines the :py:class:`ShapeAdaptor` and :py:class:`ResizePolicy` classes which can
be used to wrap a :py:class:`datasources.DataSource` object so that the items it yields contain
a resized version of the original image.

.. moduleauthor:: Rasmus Diederichsen
'''
from datasources import DataSource, InputData
from network import Network
import numpy as np

class _ResizePolicyBase(object):
    '''Base class defining common properties of all resizing policies.

    Attributes
    ----------
    _new_shape  :   tuple
                    The shape to convert images to. Will be stripped of singular dimensions
    '''
    _new_shape: tuple = None

    def setShape(self, new_shape):
        '''Set the shape to match. If the last dimension is 1, it is removed.

        Parameters
        ----------
        new_shape   :   tuple or list
                        Shape to match.

        Raises
        ------
        ValueError
            If leading dimension is None (aka batch)
        '''
        if new_shape[0] is None:
            raise ValueError('Cannot work with None dimensions')
        if new_shape[-1] == 1:
            # remove channel dim
            new_shape = new_shape[:-1]
        self._new_shape = new_shape

    def resize(self, image):
        '''Resize an image according to this policy.

        Parameters
        ----------
        image   :   np.ndarray
                    Image to resize
        '''
        raise NotImplementedError('Abstract base class ResizePolicy cannot be used directly.')

class ResizePolicyBilinear(_ResizePolicyBase):
    '''Resize policy which bilinearly interpolates images to the target size.'''

    def resize(self, img):
        from skimage.transforms import resize
        if self._new_shape is None:
            return img
        else:
            return resize(img, self._new_shape, preserve_range=True)

class ResizePolicyPad(_ResizePolicyBase):
    '''Resize policy which will pad the input image to the target size. This will not work if
    the target size is smaller than the source size in any dimension.'''

    def __init__(self, mode, **kwargs):
        '''
        Parameters
        ----------
        mode    :   str
                    Any of the values excepted by :py:func:`np.pad`
        kwargs  :   dict
                    Additional arguments to pass to :py:func:`np.pad`, such as the value to pad with
        '''
        self._pad_mode = mode
        self._pad_kwargs = kwargs

    def resize(self, img):
        if self._new_shape is None or self._new_shape == img.shape:
            return img
        h, w = img.shape[:2]
        new_h, new_w = self._new_shape[:2]

        if new_h < h or new_w < w:
            raise ValueError('Cannot pad to a smaller size. Use `ResizePolicy.Crop` instead.')

        # necessary padding to reach desired size
        pad_h = new_h - h
        pad_w = new_w - w
        ###############################################################################
        #  If padding is not even, we put one more padding pixel to the bottom/right  #
        ###############################################################################
        top = pad_h // 2
        bottom = pad_h - top
        left = pad_w // 2
        right = pad_w - left
        print(self._new_shape)
        print(img.shape)
        __import__('ipdb').set_trace()

        return np.pad(img, (top, bottom, left, right), self._pad_mode, **self._pad_kwargs)

class ResizePolicy(object):

    @staticmethod
    def Bilinear():
        '''Create a bilinear interpolation policy.'''
        return ResizePolicyBilinear()

    @staticmethod
    def Pad():
        '''Create a pad-with-zero policy.'''
        return ResizePolicyPad('constant', constant_values=0)

    @staticmethod
    def Crop():
        raise NotImplementedError

class ShapeAdaptor(DataSource):
    '''Adaptive wrapper around a :py:class:`DataSource`'''

    def __init__(self, network: Network, source: DataSource, resize_policy):
        '''
        Parameters
        ----------
        network :   network.Network
                    Network to adapt to
        source  :   DataSource
                    Source to adapt from
        resize  :   ResizePolicy
                    Policy to use for resizing images
        '''
        super().__init__(f'ShapeAdaptor< {source._description} >')
        self._source = source
        self._resize = resize_policy
        self.setNetwork(network)

    def setNetwork(self, network: Network):
        '''Change the network to adapt to.

        Parameters
        ----------
        network :   network.Network
        '''
        self._resize.setShape(network.get_input_shape(include_batch=False))

    def __getitem__(self, item: int):
        img, name = self._source[item]
        return InputData(self._resize.resize(img), name)

    def __len__(self):
        return len(self._source)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# ----------------------------------------------------------------------------
# Copyright 2015-2017 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------
"""
CNN layers into a simple MLP with softmax output on LUNA16 data.
Just tests that aeon dataloader works and can be fed to simple model in neon.

"""

from neon import logger as neon_logger
from neon.initializers import Gaussian
from neon.optimizers import Adam
from neon.layers import Conv, Dropout, Activation, Pooling, GeneralizedCost, Affine
from neon.transforms import Rectlin, Softmax, CrossEntropyMulti, Misclassification, PrecisionRecall
from neon.models import Model
from aeon import DataLoader
from neon.callbacks.callbacks import Callbacks
from neon.util.argparser import NeonArgparser, extract_valid_args
from neon.backends import gen_backend
from neon.data.dataloader_transformers import BGRMeanSubtract, TypeCast, OneHot
import numpy as np

# parse the command line arguments
parser = NeonArgparser(__doc__)
parser.add_argument("--learning_rate", default=0.05,
                    help="initial learning rate")
parser.add_argument("--weight_decay", default=0.001, help="weight decay")
parser.add_argument('--deconv', action='store_true',
                    help='save visualization data from deconvolution')
args = parser.parse_args()

# hyperparameters
num_epochs = args.epochs

# setup backend
be = gen_backend(**extract_valid_args(args, gen_backend))
be.enable_winograd = 4  # default to winograd 4 for fast autotune

# Set up the training set to load via aeon
# Augmentating the data via flipping, rotating, changing contrast/brightness
image_config = dict(height=64, width=64, flip_enable=True,
                    contrast=(0.5,1.0), brightness=(0.5,1.0),
                    fixed_aspect_ratio = True, angle=(0,45))
label_config = dict(binary=False)
config = dict(type="image,label",
              image=image_config,
              label=label_config,
              manifest_filename='manifest_subset0.txt', subset_fraction=0.1,
              minibatch_size=128)
train_set = DataLoader(config, be)
train_set = TypeCast(train_set, index=0, dtype=np.float32)  # cast image to float
train_set = OneHot(train_set, index=1, nclasses=2)

# Set up the test set to load via aeon
image_config = dict(height=64, width=64)
label_config = dict(binary=False)
config = dict(type="image,label",
              image=image_config,
              label=label_config,
              manifest_filename='manifest_subset1.txt',
              minibatch_size=128)
valid_set = DataLoader(config, be)
valid_set = TypeCast(valid_set, index=0, dtype=np.float32)  # cast image to float
valid_set = OneHot(valid_set, index=1, nclasses=2)

init_uni = Gaussian(scale=0.05)
opt_gdm = Adam(learning_rate=args.learning_rate, beta_1=0.9, beta_2=0.999)

relu = Rectlin()
conv = dict(init=init_uni, batch_norm=True, activation=relu)
convp1 = dict(init=init_uni, batch_norm=True, activation=relu, padding=1)
convp1s2 = dict(init=init_uni, batch_norm=True,
                activation=relu, padding=1, strides=2)

layers = [Dropout(keep=.8),
          Conv((3, 3, 96), **convp1),
          Conv((3, 3, 96), **convp1),
          Conv((3, 3, 96), **convp1s2),
          Dropout(keep=.5),
          Conv((3, 3, 192), **convp1),
          Conv((3, 3, 192), **convp1),
          Conv((3, 3, 192), **convp1s2),
          Dropout(keep=.5),
          Conv((3, 3, 192), **convp1),
          Conv((1, 1, 192), **conv),
          Conv((1, 1, 16), **conv),
          Affine(nout=512, init=init_uni, activation=relu),
          Affine(nout=2, init=init_uni, activation=Softmax())]

cost = GeneralizedCost(costfunc=CrossEntropyMulti())

lunaModel = Model(layers=layers)

if args.model_file:
    import os
    assert os.path.exists(args.model_file), '%s not found' % args.model_file
    mlp.load_params(args.model_file)

# configure callbacks
#callbacks = Callbacks(lunaModel, eval_set=valid_set, **args.callback_args)
callbacks = Callbacks(lunaModel, eval_set=valid_set, metric=PrecisionRecall(num_classes=2), **args.callback_args)

if args.deconv:
    callbacks.add_deconv_callback(train_set, valid_set)

lunaModel.fit(train_set, optimizer=opt_gdm, num_epochs=num_epochs,
        cost=cost, callbacks=callbacks)

neon_logger.display('Finished training. Calculating error on the validation set...')
neon_logger.display('Misclassification error = %.1f%%' %
                    (lunaModel.eval(valid_set, metric=Misclassification()) * 100))

neon_logger.display('Precision/recall = {}'.format(lunaModel.eval(valid_set, metric=PrecisionRecall(num_classes=2))))
<commit_msg>Working CNN model with one hot encoding and precision-recall report<commit_after>#!/usr/bin/env python
# ----------------------------------------------------------------------------
# Copyright 2015-2017 Nervana Systems Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------
"""
CNN layers into a simple MLP with softmax output on LUNA16 data.
Just tests that aeon dataloader works and can be fed to simple model in neon.

"""

from neon import logger as neon_logger
from neon.initializers import Gaussian
from neon.optimizers import Adam
from neon.layers import Conv, Dropout, Activation, Pooling, GeneralizedCost, Affine
from neon.transforms import Rectlin, Softmax, CrossEntropyMulti, Misclassification, PrecisionRecall
from neon.models import Model
from aeon import DataLoader
from neon.callbacks.callbacks import Callbacks
from neon.util.argparser import NeonArgparser, extract_valid_args
from neon.backends import gen_backend
from neon.data.dataloader_transformers import BGRMeanSubtract, TypeCast, OneHot
import numpy as np

# parse the command line arguments
parser = NeonArgparser(__doc__)
parser.add_argument("--learning_rate", default=0.05,
                    help="initial learning rate")
parser.add_argument("--weight_decay", default=0.001, help="weight decay")
parser.add_argument('--deconv', action='store_true',
                    help='save visualization data from deconvolution')
args = parser.parse_args()

# hyperparameters
num_epochs = args.epochs

# setup backend
be = gen_backend(**extract_valid_args(args, gen_backend))
be.enable_winograd = 4  # default to winograd 4 for fast autotune

# Set up the training set to load via aeon
# Augmentating the data via flipping, rotating, changing contrast/brightness
image_config = dict(height=64, width=64, flip_enable=True,
                    contrast=(0.5,1.0), brightness=(0.5,1.0),
                    fixed_aspect_ratio = True, angle=(0,45))
label_config = dict(binary=False)
config = dict(type="image,label",
              image=image_config,
              label=label_config,
              manifest_filename='manifest_subset0.txt',
              minibatch_size=128)
train_set = DataLoader(config, be)
train_set = TypeCast(train_set, index=0, dtype=np.float32)  # cast image to float
train_set = OneHot(train_set, index=1, nclasses=2)

# Set up the test set to load via aeon
image_config = dict(height=64, width=64)
label_config = dict(binary=False)
config = dict(type="image,label",
              image=image_config,
              label=label_config,
              manifest_filename='manifest_subset1.txt',
              minibatch_size=128)
valid_set = DataLoader(config, be)
valid_set = TypeCast(valid_set, index=0, dtype=np.float32)  # cast image to float
valid_set = OneHot(valid_set, index=1, nclasses=2)

init_uni = Gaussian(scale=0.05)
opt_gdm = Adam(learning_rate=args.learning_rate, beta_1=0.9, beta_2=0.999)

relu = Rectlin()
conv = dict(init=init_uni, batch_norm=True, activation=relu)
convp1 = dict(init=init_uni, batch_norm=True, activation=relu, padding=1)
convp1s2 = dict(init=init_uni, batch_norm=True,
                activation=relu, padding=1, strides=2)

layers = [Dropout(keep=.8),
          Conv((3, 3, 96), **convp1),
          Conv((3, 3, 96), **convp1),
          Conv((3, 3, 96), **convp1s2),
          Dropout(keep=.5),
          Conv((3, 3, 192), **convp1),
          Conv((3, 3, 192), **convp1),
          Conv((3, 3, 192), **convp1s2),
          Dropout(keep=.5),
          Conv((3, 3, 192), **convp1),
          Conv((1, 1, 192), **conv),
          Conv((1, 1, 16), **conv),
          Affine(nout=512, init=init_uni, activation=relu),
          Affine(nout=2, init=init_uni, activation=Softmax())]

cost = GeneralizedCost(costfunc=CrossEntropyMulti())

lunaModel = Model(layers=layers)

if args.model_file:
    import os
    assert os.path.exists(args.model_file), '%s not found' % args.model_file
    mlp.load_params(args.model_file)

# configure callbacks
#callbacks = Callbacks(lunaModel, eval_set=valid_set, **args.callback_args)
callbacks = Callbacks(lunaModel, eval_set=valid_set, metric=PrecisionRecall(num_classes=2), **args.callback_args)

if args.deconv:
    callbacks.add_deconv_callback(train_set, valid_set)

lunaModel.fit(train_set, optimizer=opt_gdm, num_epochs=num_epochs,
        cost=cost, callbacks=callbacks)

neon_logger.display('Finished training. Calculating error on the validation set...')
neon_logger.display('Misclassification error = %.1f%%' %
                    (lunaModel.eval(valid_set, metric=Misclassification()) * 100))

neon_logger.display('Precision/recall = {}'.format(lunaModel.eval(valid_set, metric=PrecisionRecall(num_classes=2))))
<|endoftext|>


<commit_before>import os
from os.path import dirname, abspath

from celery.schedules import crontab

def media_directory(path):
    """Return absolute path to subdirectory of MEDIA_ROOT.
    The directory will be created if it doesn't exist.
    """
    abs_path = os.path.join(MEDIA_ROOT, path)
    if not os.path.isdir(abs_path):
        os.makedirs(abs_path)
    return abs_path

DEBUG = False
THUMBNAIL_DEBUG = False

BASE_DIR = dirname(dirname(dirname(abspath(__file__))))

ADMINS = (
    ('Mathieu Comandon', 'strycore@gmail.com'),
    ('Pascal', 'dev@xod.me'),
)
MANAGERS = ADMINS
INTERNAL_IPS = ('127.0.0.1',)
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'lutris.db'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

TIME_ZONE = 'Europe/Paris'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Remove when updated to Django 1.11
SILENCED_SYSTEM_CHECKS = [
    'urls.W002',
]

# Static files
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "public"),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

# Media files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

FILES_ROOT = media_directory('files')
FILES_URL = 'http://localhost:8000/media/files/'

TOSEC_PATH = media_directory('tosec')
TOSEC_CURRENT = "TOSEC - DAT Pack - Complete (2292) (TOSEC-v2014-10-31)"
TOSEC_DAT_PACK = os.path.join(TOSEC_PATH, TOSEC_CURRENT)

# TheGamesDB directories
TGD_ROOT = media_directory('thegamesdb')
TGD_CLEAR_LOGO_PATH = media_directory('thegamesdb/clearlogo')
TGD_BANNER_PATH = media_directory('thegamesdb/banners')
TGD_SCREENSHOT_PATH = media_directory('thegamesdb/screenshot')
TGD_FANART_PATH = media_directory('thegamesdb/fanart')
TGD_LUTRIS_BANNER_PATH = media_directory('thegamesdb/lutris-banners')

SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme')

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

ROOT_URLCONF = 'lutrisweb.urls'
WSGI_APPLICATION = 'lutrisweb.wsgi.application'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, "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.template.context_processors.request",
                "django.contrib.messages.context_processors.messages",
            ],
            'debug': False
        }
    }
]

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'grappelli',
    'django.contrib.admin',
    'django.contrib.admindocs',

    'sorl.thumbnail',
    'tastypie',
    'rest_framework',
    'rest_framework.authtoken',
    'django_jcrop',
    'crispy_forms',
    'django_select2',
    'markupfield',
    'django_openid_auth',
    'django_extensions',

    'common',
    'platforms',
    'games',
    'bundles',
    'runners',
    'accounts',
    'tosec',
)

BANNER_SIZE = "184x69"
ICON_SIZE = "32x32"
ICON_LARGE_SIZE = "256x256"
THUMBNAIL_ENGINE = 'sorl.thumbnail.engines.convert_engine.Engine'
THUMBNAIL_COLORSPACE = "sRGB"

AUTH_USER_MODEL = 'accounts.User'
AUTH_PROFILE_MODULE = "accounts.Profile"
ACCOUNT_ACTIVATION_DAYS = 3
LOGIN_REDIRECT_URL = "/"
LOGIN_URL = "/user/login/"
AUTHENTICATION_BACKENDS = (
    'django_openid_auth.auth.OpenIDBackend',
    'django.contrib.auth.backends.ModelBackend',
)
OPENID_SSO_SERVER_URL = 'http://steamcommunity.com/openid'

DISCOURSE_SSO_SECRET = os.environ.get('DISCOURSE_SSO_SECRET')
DISCOURSE_URL = 'https://forums.lutris.net'

# Modify temporarily the session serializer because the json serializer in
# Django 1.6 can't serialize openid.yadis.manager.YadisServiceManager objects
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

CLIENT_VERSION = "0.3.8"

# Crispy forms
CRISPY_TEMPLATE_PACK = 'bootstrap3'
CRISPY_FAIL_SILENTLY = not DEBUG

# Admin
GRAPPELLI_ADMIN_TITLE = "Lutris Administration"

# Select2
SELECT2_JS = '/static/js/select2.min.js'
SELECT2_CSS = '/static/css/select2.min.css'

# Email
DEFAULT_FROM_EMAIL = "admin@lutris.net"
SERVER_EMAIL = "admin@lutris.net"
EMAIL_SUBJECT_PREFIX = "[Lutris] "

# Celery
CELERY_SEND_TASK_ERROR_EMAILS = True

CELERYBEAT_SCHEDULE = {
    'send-daily-mod-mail': {
        'task': 'accounts.tasks.daily_mod_mail',
        'schedule': crontab(hour=12, minute=0),
    }
}
BROKER_URL = 'amqp://guest:guest@localhost//'
# API Keys
STEAM_API_KEY = "********************************"

# Rest Framework
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 250,
}

# Logging
SEND_BROKEN_LINK_EMAILS = False
LOGGING_HANDLERS = ['file', 'mail_admins', 'console']
DEFAULT_LOGGING_CONFIG = {
    'handlers': LOGGING_HANDLERS,
    'level': 'DEBUG',
    'propagate': True,
}
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        }
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'mail_admins': {
            'level': 'ERROR',
            'include_html': True,
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'formatter': 'verbose',
            'filename': 'lutrisweb.log'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django': {
            'handlers': LOGGING_HANDLERS,
            'propagate': True,
            'level': 'INFO',
        },
        'factory': {
            'handlers': ['null'],
            'propagate': False,
            'level': 'INFO',
        },
        'django.request': {
            'handlers': LOGGING_HANDLERS,
            'level': 'WARNING',
            'propagate': True,
        },
        'lutrisweb': DEFAULT_LOGGING_CONFIG,
        'accounts': DEFAULT_LOGGING_CONFIG,
        'common': DEFAULT_LOGGING_CONFIG,
        'games': DEFAULT_LOGGING_CONFIG,
        'thegamesdb': DEFAULT_LOGGING_CONFIG
    }
}
<commit_msg>Update to 0.4.0<commit_after>import os
from os.path import dirname, abspath

from celery.schedules import crontab

def media_directory(path):
    """Return absolute path to subdirectory of MEDIA_ROOT.
    The directory will be created if it doesn't exist.
    """
    abs_path = os.path.join(MEDIA_ROOT, path)
    if not os.path.isdir(abs_path):
        os.makedirs(abs_path)
    return abs_path

DEBUG = False
THUMBNAIL_DEBUG = False

BASE_DIR = dirname(dirname(dirname(abspath(__file__))))

ADMINS = (
    ('Mathieu Comandon', 'strycore@gmail.com'),
    ('Pascal', 'dev@xod.me'),
)
MANAGERS = ADMINS
INTERNAL_IPS = ('127.0.0.1',)
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'lutris.db'),
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}

TIME_ZONE = 'Europe/Paris'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Remove when updated to Django 1.11
SILENCED_SYSTEM_CHECKS = [
    'urls.W002',
]

# Static files
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "public"),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

# Media files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

FILES_ROOT = media_directory('files')
FILES_URL = 'http://localhost:8000/media/files/'

TOSEC_PATH = media_directory('tosec')
TOSEC_CURRENT = "TOSEC - DAT Pack - Complete (2292) (TOSEC-v2014-10-31)"
TOSEC_DAT_PACK = os.path.join(TOSEC_PATH, TOSEC_CURRENT)

# TheGamesDB directories
TGD_ROOT = media_directory('thegamesdb')
TGD_CLEAR_LOGO_PATH = media_directory('thegamesdb/clearlogo')
TGD_BANNER_PATH = media_directory('thegamesdb/banners')
TGD_SCREENSHOT_PATH = media_directory('thegamesdb/screenshot')
TGD_FANART_PATH = media_directory('thegamesdb/fanart')
TGD_LUTRIS_BANNER_PATH = media_directory('thegamesdb/lutris-banners')

SECRET_KEY = os.environ.get('SECRET_KEY', 'changeme')

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

ROOT_URLCONF = 'lutrisweb.urls'
WSGI_APPLICATION = 'lutrisweb.wsgi.application'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, "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.template.context_processors.request",
                "django.contrib.messages.context_processors.messages",
            ],
            'debug': False
        }
    }
]

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'grappelli',
    'django.contrib.admin',
    'django.contrib.admindocs',

    'sorl.thumbnail',
    'tastypie',
    'rest_framework',
    'rest_framework.authtoken',
    'django_jcrop',
    'crispy_forms',
    'django_select2',
    'markupfield',
    'django_openid_auth',
    'django_extensions',

    'common',
    'platforms',
    'games',
    'bundles',
    'runners',
    'accounts',
    'tosec',
)

BANNER_SIZE = "184x69"
ICON_SIZE = "32x32"
ICON_LARGE_SIZE = "256x256"
THUMBNAIL_ENGINE = 'sorl.thumbnail.engines.convert_engine.Engine'
THUMBNAIL_COLORSPACE = "sRGB"

AUTH_USER_MODEL = 'accounts.User'
AUTH_PROFILE_MODULE = "accounts.Profile"
ACCOUNT_ACTIVATION_DAYS = 3
LOGIN_REDIRECT_URL = "/"
LOGIN_URL = "/user/login/"
AUTHENTICATION_BACKENDS = (
    'django_openid_auth.auth.OpenIDBackend',
    'django.contrib.auth.backends.ModelBackend',
)
OPENID_SSO_SERVER_URL = 'http://steamcommunity.com/openid'

DISCOURSE_SSO_SECRET = os.environ.get('DISCOURSE_SSO_SECRET')
DISCOURSE_URL = 'https://forums.lutris.net'

# Modify temporarily the session serializer because the json serializer in
# Django 1.6 can't serialize openid.yadis.manager.YadisServiceManager objects
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

CLIENT_VERSION = "0.4.0"

# Crispy forms
CRISPY_TEMPLATE_PACK = 'bootstrap3'
CRISPY_FAIL_SILENTLY = not DEBUG

# Admin
GRAPPELLI_ADMIN_TITLE = "Lutris Administration"

# Select2
SELECT2_JS = '/static/js/select2.min.js'
SELECT2_CSS = '/static/css/select2.min.css'

# Email
DEFAULT_FROM_EMAIL = "admin@lutris.net"
SERVER_EMAIL = "admin@lutris.net"
EMAIL_SUBJECT_PREFIX = "[Lutris] "

# Celery
CELERY_SEND_TASK_ERROR_EMAILS = True

CELERYBEAT_SCHEDULE = {
    'send-daily-mod-mail': {
        'task': 'accounts.tasks.daily_mod_mail',
        'schedule': crontab(hour=12, minute=0),
    }
}
BROKER_URL = 'amqp://guest:guest@localhost//'
# API Keys
STEAM_API_KEY = "********************************"

# Rest Framework
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 250,
}

# Logging
SEND_BROKEN_LINK_EMAILS = False
LOGGING_HANDLERS = ['file', 'mail_admins', 'console']
DEFAULT_LOGGING_CONFIG = {
    'handlers': LOGGING_HANDLERS,
    'level': 'DEBUG',
    'propagate': True,
}
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        }
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'mail_admins': {
            'level': 'ERROR',
            'include_html': True,
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'formatter': 'verbose',
            'filename': 'lutrisweb.log'
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'django': {
            'handlers': LOGGING_HANDLERS,
            'propagate': True,
            'level': 'INFO',
        },
        'factory': {
            'handlers': ['null'],
            'propagate': False,
            'level': 'INFO',
        },
        'django.request': {
            'handlers': LOGGING_HANDLERS,
            'level': 'WARNING',
            'propagate': True,
        },
        'lutrisweb': DEFAULT_LOGGING_CONFIG,
        'accounts': DEFAULT_LOGGING_CONFIG,
        'common': DEFAULT_LOGGING_CONFIG,
        'games': DEFAULT_LOGGING_CONFIG,
        'thegamesdb': DEFAULT_LOGGING_CONFIG
    }
}
<|endoftext|>


<commit_before>import sys

import pytest

from commitizen import cli, cmd, git
from commitizen.exceptions import (
    BumpTagFailedError,
    CurrentVersionNotFoundError,
    DryRunExit,
    ExpectedExit,
    NoCommitsFoundError,
    NoPatternMapError,
    NotAGitProjectError,
    NoVersionSpecifiedError,
)
from tests.utils import create_file_and_commit

@pytest.mark.parametrize(
    "commit_msg",
    (
        "fix: username exception",
        "fix(user): username exception",
        "refactor: remove ini configuration support",
        "refactor(config): remove ini configuration support",
        "perf: update to use multiproess",
        "perf(worker): update to use multiproess",
    ),
)
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_patch_increment(commit_msg, mocker):
    create_file_and_commit(commit_msg)
    # testargs = ["cz", "bump"]
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()
    tag_exists = git.tag_exist("0.1.1")
    assert tag_exists is True

@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_minor_increment(commit_msg, mocker):
    create_file_and_commit(commit_msg)
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()
    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize(
    "commit_msg",
    (
        "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported",
        "feat(user): new user interface\n\nBREAKING CHANGE: age is no longer supported",
        "BREAKING CHANGE: age is no longer supported",
        "BREAKING-CHANGE: age is no longer supported",
        "age is not longer supported!",
    ),
)
def test_bump_major_increment(commit_msg, mocker):
    create_file_and_commit(commit_msg)

    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("1.0.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_command_prelease(mocker):
    # PRERELEASE
    create_file_and_commit("feat: location")

    testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.0a0")
    assert tag_exists is True

    # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
    testargs = ["cz", "bump"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_on_git_with_hooks_no_verify_disabled(mocker):
    """Bump commit without --no-verify"""
    cmd.run("mkdir .git/hooks")
    with open(".git/hooks/pre-commit", "w") as f:
        f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"')
    cmd.run("chmod +x .git/hooks/pre-commit")

    # MINOR
    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    cli.main()

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_tag_exists_raises_exception(mocker):
    cmd.run("mkdir .git/hooks")
    with open(".git/hooks/post-commit", "w") as f:
        f.write("#!/usr/bin/env bash\n" "exit 9")
    cmd.run("chmod +x .git/hooks/post-commit")

    # MINOR
    create_file_and_commit("feat: new file")
    git.tag("0.2.0")

    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(BumpTagFailedError) as excinfo:
        cli.main()
    assert "0.2.0" in str(excinfo.value)  # This should be a fatal error

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_on_git_with_hooks_no_verify_enabled(mocker):
    cmd.run("mkdir .git/hooks")
    with open(".git/hooks/pre-commit", "w") as f:
        f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"')
    cmd.run("chmod +x .git/hooks/pre-commit")

    # MINOR
    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes", "--no-verify"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

def test_bump_when_bumpping_is_not_support(mocker, tmp_commitizen_project):
    create_file_and_commit(
        "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
    )

    testargs = ["cz", "-n", "cz_jira", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(NoPatternMapError) as excinfo:
        cli.main()

    assert "'cz_jira' rule does not support bump" in str(excinfo.value)

@pytest.mark.usefixtures("tmp_git_project")
def test_bump_when_version_is_not_specify(mocker):
    mocker.patch.object(sys, "argv", ["cz", "bump"])

    with pytest.raises(NoVersionSpecifiedError) as excinfo:
        cli.main()

    assert NoVersionSpecifiedError.message in str(excinfo.value)

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_when_no_new_commit(mocker):
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(NoCommitsFoundError) as excinfo:
        cli.main()

    expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found."
    assert expected_error_message in str(excinfo.value)

def test_bump_when_version_inconsistent_in_version_files(
    tmp_commitizen_project, mocker
):
    tmp_version_file = tmp_commitizen_project.join("__version__.py")
    tmp_version_file.write("100.999.10000")
    tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
    tmp_commitizen_cfg_file.write(
        f"{tmp_commitizen_cfg_file.read()}\n"
        f'version_files = ["{str(tmp_version_file)}"]'
    )

    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes", "--check-consistency"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(CurrentVersionNotFoundError) as excinfo:
        cli.main()

    partial_expected_error_message = "Current version 0.1.0 is not found in"
    assert partial_expected_error_message in str(excinfo.value)

def test_bump_files_only(mocker, tmp_commitizen_project):
    tmp_version_file = tmp_commitizen_project.join("__version__.py")
    tmp_version_file.write("0.1.0")
    tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
    tmp_commitizen_cfg_file.write(
        f"{tmp_commitizen_cfg_file.read()}\n"
        f'version_files = ["{str(tmp_version_file)}"]'
    )

    create_file_and_commit("feat: new user interface")
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()
    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

    create_file_and_commit("feat: another new feature")
    testargs = ["cz", "bump", "--yes", "--files-only"]
    mocker.patch.object(sys, "argv", testargs)
    with pytest.raises(ExpectedExit):
        cli.main()

    tag_exists = git.tag_exist("0.3.0")
    assert tag_exists is False

    with open(tmp_version_file, "r") as f:
        assert "0.3.0" in f.read()

def test_bump_dry_run(mocker, capsys, tmp_commitizen_project):
    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes", "--dry-run"]
    mocker.patch.object(sys, "argv", testargs)
    with pytest.raises(DryRunExit):
        cli.main()

    out, _ = capsys.readouterr()
    assert "0.2.0" in out

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is False

def test_bump_in_non_git_project(tmpdir, config, mocker):
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with tmpdir.as_cwd():
        with pytest.raises(NotAGitProjectError):
            with pytest.raises(ExpectedExit):
                cli.main()
<commit_msg>test(bump): Remove bump case with msg ends with `!`<commit_after>import sys

import pytest

from commitizen import cli, cmd, git
from commitizen.exceptions import (
    BumpTagFailedError,
    CurrentVersionNotFoundError,
    DryRunExit,
    ExpectedExit,
    NoCommitsFoundError,
    NoPatternMapError,
    NotAGitProjectError,
    NoVersionSpecifiedError,
)
from tests.utils import create_file_and_commit

@pytest.mark.parametrize(
    "commit_msg",
    (
        "fix: username exception",
        "fix(user): username exception",
        "refactor: remove ini configuration support",
        "refactor(config): remove ini configuration support",
        "perf: update to use multiproess",
        "perf(worker): update to use multiproess",
    ),
)
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_patch_increment(commit_msg, mocker):
    create_file_and_commit(commit_msg)
    # testargs = ["cz", "bump"]
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()
    tag_exists = git.tag_exist("0.1.1")
    assert tag_exists is True

@pytest.mark.parametrize("commit_msg", ("feat: new file", "feat(user): new file"))
@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_minor_increment(commit_msg, mocker):
    create_file_and_commit(commit_msg)
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()
    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize(
    "commit_msg",
    (
        "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported",
        "feat(user): new user interface\n\nBREAKING CHANGE: age is no longer supported",
        "BREAKING CHANGE: age is no longer supported",
        "BREAKING-CHANGE: age is no longer supported",
    ),
)
def test_bump_major_increment(commit_msg, mocker):
    create_file_and_commit(commit_msg)

    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("1.0.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_command_prelease(mocker):
    # PRERELEASE
    create_file_and_commit("feat: location")

    testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.0a0")
    assert tag_exists is True

    # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE
    testargs = ["cz", "bump"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_on_git_with_hooks_no_verify_disabled(mocker):
    """Bump commit without --no-verify"""
    cmd.run("mkdir .git/hooks")
    with open(".git/hooks/pre-commit", "w") as f:
        f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"')
    cmd.run("chmod +x .git/hooks/pre-commit")

    # MINOR
    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    cli.main()

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_tag_exists_raises_exception(mocker):
    cmd.run("mkdir .git/hooks")
    with open(".git/hooks/post-commit", "w") as f:
        f.write("#!/usr/bin/env bash\n" "exit 9")
    cmd.run("chmod +x .git/hooks/post-commit")

    # MINOR
    create_file_and_commit("feat: new file")
    git.tag("0.2.0")

    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(BumpTagFailedError) as excinfo:
        cli.main()
    assert "0.2.0" in str(excinfo.value)  # This should be a fatal error

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_on_git_with_hooks_no_verify_enabled(mocker):
    cmd.run("mkdir .git/hooks")
    with open(".git/hooks/pre-commit", "w") as f:
        f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"')
    cmd.run("chmod +x .git/hooks/pre-commit")

    # MINOR
    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes", "--no-verify"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

def test_bump_when_bumpping_is_not_support(mocker, tmp_commitizen_project):
    create_file_and_commit(
        "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported"
    )

    testargs = ["cz", "-n", "cz_jira", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(NoPatternMapError) as excinfo:
        cli.main()

    assert "'cz_jira' rule does not support bump" in str(excinfo.value)

@pytest.mark.usefixtures("tmp_git_project")
def test_bump_when_version_is_not_specify(mocker):
    mocker.patch.object(sys, "argv", ["cz", "bump"])

    with pytest.raises(NoVersionSpecifiedError) as excinfo:
        cli.main()

    assert NoVersionSpecifiedError.message in str(excinfo.value)

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_bump_when_no_new_commit(mocker):
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(NoCommitsFoundError) as excinfo:
        cli.main()

    expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found."
    assert expected_error_message in str(excinfo.value)

def test_bump_when_version_inconsistent_in_version_files(
    tmp_commitizen_project, mocker
):
    tmp_version_file = tmp_commitizen_project.join("__version__.py")
    tmp_version_file.write("100.999.10000")
    tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
    tmp_commitizen_cfg_file.write(
        f"{tmp_commitizen_cfg_file.read()}\n"
        f'version_files = ["{str(tmp_version_file)}"]'
    )

    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes", "--check-consistency"]
    mocker.patch.object(sys, "argv", testargs)

    with pytest.raises(CurrentVersionNotFoundError) as excinfo:
        cli.main()

    partial_expected_error_message = "Current version 0.1.0 is not found in"
    assert partial_expected_error_message in str(excinfo.value)

def test_bump_files_only(mocker, tmp_commitizen_project):
    tmp_version_file = tmp_commitizen_project.join("__version__.py")
    tmp_version_file.write("0.1.0")
    tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
    tmp_commitizen_cfg_file.write(
        f"{tmp_commitizen_cfg_file.read()}\n"
        f'version_files = ["{str(tmp_version_file)}"]'
    )

    create_file_and_commit("feat: new user interface")
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)
    cli.main()
    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is True

    create_file_and_commit("feat: another new feature")
    testargs = ["cz", "bump", "--yes", "--files-only"]
    mocker.patch.object(sys, "argv", testargs)
    with pytest.raises(ExpectedExit):
        cli.main()

    tag_exists = git.tag_exist("0.3.0")
    assert tag_exists is False

    with open(tmp_version_file, "r") as f:
        assert "0.3.0" in f.read()

def test_bump_dry_run(mocker, capsys, tmp_commitizen_project):
    create_file_and_commit("feat: new file")

    testargs = ["cz", "bump", "--yes", "--dry-run"]
    mocker.patch.object(sys, "argv", testargs)
    with pytest.raises(DryRunExit):
        cli.main()

    out, _ = capsys.readouterr()
    assert "0.2.0" in out

    tag_exists = git.tag_exist("0.2.0")
    assert tag_exists is False

def test_bump_in_non_git_project(tmpdir, config, mocker):
    testargs = ["cz", "bump", "--yes"]
    mocker.patch.object(sys, "argv", testargs)

    with tmpdir.as_cwd():
        with pytest.raises(NotAGitProjectError):
            with pytest.raises(ExpectedExit):
                cli.main()
<|endoftext|>


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

"""
config.py contains most CONSTANTS in the project
"""
# @author chengdujin
# @contact chengdujin@gmail.com
# @created Jan 17, 2013

import sys
reload(sys)
sys.setdefaultencoding('UTF-8')

# SERVICES
from pymongo.connection import Connection
from pymongo.database import Database
from pymongo.collection import Collection
from pymongo.errors import CollectionInvalid
con = Connection('127.0.0.1:27017')
db = Database(con, 'news')

import redis
#rclient = redis.StrictRedis(host='10.240.35.40', port=6379)
rclient = redis.StrictRedis(host='127.0.0.1')

from HTMLParser import HTMLParser
hparser = HTMLParser()

# CONSTANTS
#PUBLIC = 'http://mobile-global.baidu.com/news/%s'  # hk01-hao123-mob01/mob02
#PUBLIC = 'http://180.76.2.34/%s'                   # hk01-hao123-mob00
PUBLIC = 'http://54.251.107.116/%s'                # AWS singapore
#PUBLIC = 'http://54.232.81.44/%s'                  # AWS sao paolo
#PUBLIC = 'http://54.248.227.71/%s'                 # AWS tokyo
#LOCAL = '/home/work/STATIC/%s'                     # official server prefix
#LOCAL = '/home/ubuntu/STATIC/%s'                   # AWS server prefix
LOCAL = '/home/jinyuan/Downloads/%s'               # local server prefix

TRANSCODED_LOCAL_DIR = LOCAL % 'news/ts/'
TRANSCODED_PUBLIC_DIR = PUBLIC % 'ts/'

IMAGES_LOCAL_DIR = LOCAL % 'news/img/'
IMAGES_PUBLIC_DIR = PUBLIC % 'img/'

MEDIA_LOCAL_DIR = LOCAL % 'news/mid/'
MEDIA_PUBLIC_DIR = PUBLIC % 'mid/'

MEDIA_TEMP_LOCAL_DIR = LOCAL % 'news/tmp/'

NEWS_TEMPLATE = LOCAL % 'news/templates/index.html'
NEWS_TEMPLATE_ARABIC = LOCAL % 'news/templates/index_arabic.html'
UCK_TRANSCODING = 'http://gate.baidu.com/tc?m=8&from=bdpc_browser&src='
UCK_TRANSCODING_NEW = 'http://m.baidu.com/openapp?/webapp?debug=1&from=bd_international&onlyspdebug=1&structpage&siteType=7&nextpage=1&siteappid=1071361&src='
TRANSCODED_ENCODING = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n'

TRANSCODING_BTN_EN = 'Original page'
TRANSCODING_BTN_PT = 'Página original'
TRANSCODING_BTN_JA = '元のページ'
TRANSCODING_BTN_IND = 'Laman Asli'
TRANSCODING_BTN_TH = 'หน้าเดิม'
TRANSCODING_BTN_AR = 'ﺎﻠﻤﺻﺩﺭ'
TRANSCODING_BTN_ZH_CN = '查看原始网页'
TRANSCODING_BTN_ZH_HK = '查看原始鏈接'

HOTNEWS_TITLE_EN = 'Hot News'
HOTNEWS_TITLE_PT = 'Notícias Quentes'
HOTNEWS_TITLE_JA = '人気ニュース'
HOTNEWS_TITLE_IND = 'Berita Terbaru'
HOTNEWS_TITLE_TH = 'ข่าวฮิต'
HOTNEWS_TITLE_AR = 'أخبار عاجلة'
HOTNEWS_TITLE_ZH_CN = '查看原始网页'
HOTNEWS_TITLE_ZH_HK = '查看原始鏈接'

DATABASE_REMOVAL_DAYS = 90
MEMORY_RESTORATION_DAYS = 20
MEMORY_EXPIRATION_DAYS = 20

STRATEGY_WITHOUT_WEIGHTS = 1
STRATEGY_WITH_WEIGHTS = 2

FEED_REGISTRAR = 'feeds'

PARAGRAPH_CRITERIA = 40
SUMMARY_LENGTH_LIMIT = 500
UCK_TIMEOUT = 15  # 15 seconds timeout
GOOGLE_TTS_TIMEOUT = 15

LANGUAGES = ['en', 'th', 'ind', 'ja', 'pt', 'en-rIN', 'ar', 'zh-CN', 'zh-HK']
MIN_IMAGE_SIZE = 150, 150
THUMBNAIL_STYLE = 1.4
THUMBNAIL_LANDSCAPE_SIZE_HIGH = 600, 226
THUMBNAIL_LANDSCAPE_SIZE_NORMAL = 450, 169
THUMBNAIL_LANDSCAPE_SIZE_LOW = 230, 85
THUMBNAIL_PORTRAIT_SIZE_HIGH = 310, 400
THUMBNAIL_PORTRAIT_SIZE_NORMAL = 175, 210
THUMBNAIL_PORTRAIT_SIZE_LOW = 90, 110
CATEGORY_IMAGE_SIZE = 310, 250
HOT_IMAGE_SIZE = 600, 250
<commit_msg>removed strategy constants in config.py<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
config.py contains most CONSTANTS in the project
"""
# @author chengdujin
# @contact chengdujin@gmail.com
# @created Jan 17, 2013

import sys
reload(sys)
sys.setdefaultencoding('UTF-8')

# SERVICES
from pymongo.connection import Connection
from pymongo.database import Database
from pymongo.collection import Collection
from pymongo.errors import CollectionInvalid
con = Connection('127.0.0.1:27017')
db = Database(con, 'news')

import redis
#rclient = redis.StrictRedis(host='10.240.35.40', port=6379)
rclient = redis.StrictRedis(host='127.0.0.1')

from HTMLParser import HTMLParser
hparser = HTMLParser()

# CONSTANTS
#PUBLIC = 'http://mobile-global.baidu.com/news/%s'  # hk01-hao123-mob01/mob02
#PUBLIC = 'http://180.76.2.34/%s'                   # hk01-hao123-mob00
PUBLIC = 'http://54.251.107.116/%s'                # AWS singapore
#PUBLIC = 'http://54.232.81.44/%s'                  # AWS sao paolo
#PUBLIC = 'http://54.248.227.71/%s'                 # AWS tokyo
#LOCAL = '/home/work/STATIC/%s'                     # official server prefix
#LOCAL = '/home/ubuntu/STATIC/%s'                   # AWS server prefix
LOCAL = '/home/jinyuan/Downloads/%s'               # local server prefix

TRANSCODED_LOCAL_DIR = LOCAL % 'news/ts/'
TRANSCODED_PUBLIC_DIR = PUBLIC % 'ts/'

IMAGES_LOCAL_DIR = LOCAL % 'news/img/'
IMAGES_PUBLIC_DIR = PUBLIC % 'img/'

MEDIA_LOCAL_DIR = LOCAL % 'news/mid/'
MEDIA_PUBLIC_DIR = PUBLIC % 'mid/'

MEDIA_TEMP_LOCAL_DIR = LOCAL % 'news/tmp/'

NEWS_TEMPLATE = LOCAL % 'news/templates/index.html'
NEWS_TEMPLATE_ARABIC = LOCAL % 'news/templates/index_arabic.html'
UCK_TRANSCODING = 'http://gate.baidu.com/tc?m=8&from=bdpc_browser&src='
UCK_TRANSCODING_NEW = 'http://m.baidu.com/openapp?/webapp?debug=1&from=bd_international&onlyspdebug=1&structpage&siteType=7&nextpage=1&siteappid=1071361&src='
TRANSCODED_ENCODING = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n'

TRANSCODING_BTN_EN = 'Original page'
TRANSCODING_BTN_PT = 'Página original'
TRANSCODING_BTN_JA = '元のページ'
TRANSCODING_BTN_IND = 'Laman Asli'
TRANSCODING_BTN_TH = 'หน้าเดิม'
TRANSCODING_BTN_AR = 'ﺎﻠﻤﺻﺩﺭ'
TRANSCODING_BTN_ZH_CN = '查看原始网页'
TRANSCODING_BTN_ZH_HK = '查看原始鏈接'

HOTNEWS_TITLE_EN = 'Hot News'
HOTNEWS_TITLE_PT = 'Notícias Quentes'
HOTNEWS_TITLE_JA = '人気ニュース'
HOTNEWS_TITLE_IND = 'Berita Terbaru'
HOTNEWS_TITLE_TH = 'ข่าวฮิต'
HOTNEWS_TITLE_AR = 'أخبار عاجلة'
HOTNEWS_TITLE_ZH_CN = '查看原始网页'
HOTNEWS_TITLE_ZH_HK = '查看原始鏈接'

DATABASE_REMOVAL_DAYS = 90
MEMORY_RESTORATION_DAYS = 20
MEMORY_EXPIRATION_DAYS = 20

FEED_REGISTRAR = 'feeds'

PARAGRAPH_CRITERIA = 40
SUMMARY_LENGTH_LIMIT = 500
UCK_TIMEOUT = 15  # 15 seconds timeout
GOOGLE_TTS_TIMEOUT = 15

LANGUAGES = ['en', 'th', 'ind', 'ja', 'pt', 'en-rIN', 'ar', 'zh-CN', 'zh-HK']
MIN_IMAGE_SIZE = 150, 150
THUMBNAIL_STYLE = 1.4
THUMBNAIL_LANDSCAPE_SIZE_HIGH = 600, 226
THUMBNAIL_LANDSCAPE_SIZE_NORMAL = 450, 169
THUMBNAIL_LANDSCAPE_SIZE_LOW = 230, 85
THUMBNAIL_PORTRAIT_SIZE_HIGH = 310, 400
THUMBNAIL_PORTRAIT_SIZE_NORMAL = 175, 210
THUMBNAIL_PORTRAIT_SIZE_LOW = 90, 110
CATEGORY_IMAGE_SIZE = 310, 250
HOT_IMAGE_SIZE = 600, 250
<|endoftext|>