<commit_before>#!/usr/bin/env python
from os import path
import sys
import sqlite3
import mvmv
import random
import argparse
import re

def get_db_loc():
    dbs = ['a', 'b', 'c']
    return random.choice(dbs)

def get_parser():
    usage_str = "%(prog)s [OPTIONS] [-r] [-w] [-d] DIRECTORY [DIRECTORY ...]"
    parser = argparse.ArgumentParser(usage=usage_str)

    parser.add_argument("-f", "--file", dest="filename", metavar="FILE",
                        type=str, nargs='*', default=[],
                        help="Rename this FILE")

    parser.add_argument("-d", "--dir", dest="dirname", metavar="DIR",
                        type=str, nargs='*', default=[],
                        help="Rename all files in this DIRECTORY")

    # FIXME(pbhandari): doesn't work properly
    parser.add_argument("-e", "--excludes", dest="excludes", metavar="REGEX",
                        type=str, nargs='*', default=[],
                        help="Rename all files in this DIRECTORY")

    parser.add_argument("-r", "-R", "--recursive", action="store_true",
                        dest="recursive", default=False,
                        help="Recursively scan the directories for files.",)

    parser.add_argument("-m", "--max-depth", dest="depth", metavar="DEPTH",
                        default=None, type=int, nargs='?',
                        help="Recursively scan the directories for files.",)

    parser.add_argument("-g", "--gui", action="store_true", dest="start_gui",
                        default=False,
                        help="Start the program as a GUI.")
    parser.add_argument("-w", "--watch", action="store_true", dest="watch",
                        default=False,
                        help="Watch the given directories for new files")

    parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
                        default=False,
                        help="Be more verbose.")
    parser.add_argument("-q", "--quiet", dest="quiet", action="store_true",
                        default=False,
                        help="Only output errors.")

    parser.add_argument("-y", "--always-yes", dest="always_yes",
                        action="store_true", default=False,
                        help="Assume yes for every prompt")

    parser.add_argument("-u", "--updatedb", dest="remotedb", default=None,
                        metavar="PATH", type=str, nargs='?',
                        help="Update the movies list from the given DBPATH.")

    # TODO(pbhandari): default db path should be sane.
    parser.add_argument("-p", "--dbpath", dest="dbpath", nargs='?',
                         metavar="PATH", type=str, default="movies.db",
                        help="Alternate path for the database of movies.")

    parser.add_argument('args', nargs=argparse.REMAINDER)
    return parser

if __name__ == '__main__':
    args = get_parser().parse_args()

    conn = sqlite3.connect(args.dbpath)
    cursor = conn.cursor()
    args.excludes = [re.compile(a) for a in args.excludes]

    # TODO(pbhandari): Code is ugly and stupid
    renames = []
    for query in args.args + args.filename + args.dirname:
        movies = []
        if path.isdir(query):
            movies = mvmv.get_movies_list(path.abspath(query), args.excludes)
        elif mvmv.is_valid_file(query, args.excludes):
            movies = [(path.dirname(path.abspath(query)), path.basename(query))]

        renames += [(m[0], m[1], mvmv.search(m[1], cursor)) for m in movies]

    print(renames)

    conn.close()
<commit_msg>Add support for daemonizing the process via a command-line switch<commit_after>#!/usr/bin/env python
from os import path
import sys
import sqlite3
import random
import argparse
import re

import mvmv
import mvmvd

def get_db_loc():
    dbs = ['a', 'b', 'c']
    return random.choice(dbs)

def get_parser():
    usage_str = "%(prog)s [OPTIONS] [-r] [-w] [-d] DIRECTORY [DIRECTORY ...]"
    parser = argparse.ArgumentParser(usage=usage_str)

    parser.add_argument("-f", "--file", dest="files", metavar="FILE",
                        type=str, nargs='*', default=[],
                        help="Rename this FILE")

    parser.add_argument("-d", "--dir", dest="dirs", metavar="DIR",
                        type=str, nargs='*', default=[],
                        help="Rename all files in this DIRECTORY")

    # FIXME(pbhandari): doesn't work properly
    parser.add_argument("-e", "--excludes", dest="excludes", metavar="REGEX",
                        type=str, nargs='*', default=[],
                        help="Rename all files in this DIRECTORY")

    parser.add_argument("-r", "-R", "--recursive", action="store_true",
                        dest="recursive", default=False,
                        help="Recursively scan the directories for files.",)

    parser.add_argument("-m", "--max-depth", dest="depth", metavar="DEPTH",
                        default=None, type=int, nargs='?',
                        help="Recursively scan the directories for files.",)

    parser.add_argument("-g", "--gui", action="store_true", dest="start_gui",
                        default=False,
                        help="Start the program as a GUI.")
    parser.add_argument("-w", "--watch", action="store_true", dest="watch",
                        default=False,
                        help="Watch the given directories for new files")

    parser.add_argument("-v", "--verbose", dest="verbose", action="store_true",
                        default=False,
                        help="Be more verbose.")
    parser.add_argument("-q", "--quiet", dest="quiet", action="store_true",
                        default=False,
                        help="Only output errors.")

    parser.add_argument("-y", "--always-yes", dest="always_yes",
                        action="store_true", default=False,
                        help="Assume yes for every prompt")

    parser.add_argument("-u", "--updatedb", dest="remotedb", default=None,
                        metavar="PATH", type=str, nargs='?',
                        help="Update the movies list from the given DBPATH.")

    # TODO(pbhandari): default db path should be sane.
    parser.add_argument("-p", "--dbpath", dest="dbpath", nargs='?',
                        metavar="PATH", type=str, default="movies.db",
                        help="Alternate path for the database of movies.")

    parser.add_argument("--pidfile", dest="pidfile", nargs=1,
                        metavar="FILE", type=str, default="./mvmv.pid",
                        help="The file where the pid is stored for the daemon")

    parser.add_argument('args', nargs=argparse.REMAINDER)
    return parser

if __name__ == '__main__':
    args = get_parser().parse_args()

    conn = sqlite3.connect(args.dbpath)
    cursor = conn.cursor()
    args.excludes = [re.compile(a) for a in args.excludes]

    # TODO(wmak): Mark the given directories as watched
    # TODO(wmak): Change this line into a call to the mvmvd executable
    if args.watch:
        mvmvd.mvmvd(args.pidfile).start()

    # TODO(pbhandari): Code is ugly and stupid
    renames = []
    for query in args.args + args.files + args.dirs:
        movies = []
        if path.isdir(query):
            movies = mvmv.get_movies_list(path.abspath(query), args.excludes)
        elif mvmv.is_valid_file(query, args.excludes):
            movies = [(path.dirname(path.abspath(query)), path.basename(query))]

        renames += [(m[0], m[1], mvmv.search(m[1], cursor)) for m in movies]

    print(renames)

    conn.close()
<|endoftext|>


<commit_before>#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
import unittest

from nupic.data.pattern_machine import ConsecutivePatternMachine
from nupic.data.sequence_machine import SequenceMachine
from nupic.research.temporal_memory import InspectTemporalMemory

class InspectTemporalMemoryTest(unittest.TestCase):

  def setUp(self):
    self.patternMachine = ConsecutivePatternMachine(100, 5)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = InspectTemporalMemory(columnDimensions=[100],
                                    cellsPerColumn=4,
                                    initialPermanence=0.6,
                                    connectedPermanence=0.5,
                                    minThreshold=1,
                                    maxNewSynapseCount=6,
                                    permanenceIncrement=0.1,
                                    permanenceDecrement=0.05,
                                    activationThreshold=1)

  def testFeedSequence(self):
    sequence = self._generateSequence()

    # Replace last pattern (before the None) with an unpredicted one
    sequence[-2] = self.patternMachine.get(4)

    self._feedSequence(sequence, sequenceLabel="Test")

    self.assertEqual(len(self.tm.patterns), len(sequence))
    self.assertEqual(len(self.tm.sequenceLabels), len(sequence))
    self.assertEqual(len(self.tm.predictedActiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedActiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList), len(sequence))

    self.assertEqual(self.tm.patterns[-2], self.patternMachine.get(4))
    self.assertEqual(self.tm.sequenceLabels[-2], "Test")
    self.assertEqual(len(self.tm.predictedActiveCellsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveCellsList[-2]), 5)
    self.assertEqual(len(self.tm.predictedActiveColumnsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveColumnsList[-2]), 5)
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList[-2]), 5)

    self.assertTrue("Test" in self.tm.predictedActiveCellsForSequenceDict)
    predictedActiveCells = reduce(lambda x, y: x | y,
                                  self.tm.predictedActiveCellsList)
    self.assertEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                     predictedActiveCells)

    sequence.reverse()
    sequence.append(sequence.pop(0))  # Move None (reset) to the end
    self._feedSequence(sequence, sequenceLabel="Test2")

    self.assertTrue("Test" in self.tm.predictedActiveCellsForSequenceDict)
    self.assertEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                     predictedActiveCells)
    self.assertTrue("Test2" in self.tm.predictedActiveCellsForSequenceDict)
    self.assertNotEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                        self.tm.predictedActiveCellsForSequenceDict["Test2"])

  def testFeedSequenceNoSequenceLabel(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence)
    self.assertEqual(len(self.tm.predictedActiveCellsForSequenceDict), 0)

  def testComputeStatistics(self):
    sequence = self._generateSequence()

    self._feedSequence(sequence)  # train
    self.tm.clearHistory()
    self._feedSequence(sequence)  # test
    stats = self.tm.getStatistics()

    self.assertEqual(len(stats), 6)
    self.assertEqual(stats.predictedInactiveCells.sum, 0)
    self.assertEqual(stats.predictedInactiveColumns.sum, 0)
    self.assertEqual(stats.unpredictedActiveColumns.sum, 0)
    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.sum, None)

  def testComputeStatisticsSequenceStatistics(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence, "Test1")

    sequence.reverse()
    sequence.append(sequence.pop(0))  # Move None (reset) to the end
    self._feedSequence(sequence, "Test2")

    stats = self.tm.getStatistics()

    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.average, 1)
    self.assertEqual(stats.sequencesPredictedActiveCellsShared.average, 1)

    self._feedSequence(sequence, "Test3")

    stats = self.tm.getStatistics()

    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.average, 1)
    self.assertTrue(stats.sequencesPredictedActiveCellsShared.average > 1)

  def testMapCellsToColumns(self):
    columnsForCells = self.tm.mapCellsToColumns(set([0, 1, 2, 5, 399]))
    self.assertEqual(columnsForCells[0], set([0, 1, 2]))
    self.assertEqual(columnsForCells[1], set([5]))
    self.assertEqual(columnsForCells[99], set([399]))

  # ==============================
  # Helper functions
  # ==============================

  def _generateSequence(self):
    numbers = range(0, 10)
    sequence = self.sequenceMachine.generateFromNumbers(numbers)
    sequence.append(None)
    sequence *= 3

    return sequence

  def _feedSequence(self, sequence, sequenceLabel=None):
    for pattern in sequence:
      if pattern is None:
        self.tm.reset()
      else:
        self.tm.compute(pattern, sequenceLabel=sequenceLabel)

if __name__ == "__main__":
  unittest.main()
<commit_msg>Update test<commit_after>#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2014, Numenta, Inc.  Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
import unittest

from nupic.data.pattern_machine import ConsecutivePatternMachine
from nupic.data.sequence_machine import SequenceMachine
from nupic.research.temporal_memory import InspectTemporalMemory

class InspectTemporalMemoryTest(unittest.TestCase):

  def setUp(self):
    self.patternMachine = ConsecutivePatternMachine(100, 5)
    self.sequenceMachine = SequenceMachine(self.patternMachine)

    self.tm = InspectTemporalMemory(columnDimensions=[100],
                                    cellsPerColumn=4,
                                    initialPermanence=0.6,
                                    connectedPermanence=0.5,
                                    minThreshold=1,
                                    maxNewSynapseCount=6,
                                    permanenceIncrement=0.1,
                                    permanenceDecrement=0.05,
                                    activationThreshold=1)

  def testFeedSequence(self):
    sequence = self._generateSequence()

    # Replace last pattern (before the None) with an unpredicted one
    sequence[-2] = self.patternMachine.get(4)

    self._feedSequence(sequence, sequenceLabel="Test")

    self.assertEqual(len(self.tm.patterns), len(sequence))
    self.assertEqual(len(self.tm.sequenceLabels), len(sequence))
    self.assertEqual(len(self.tm.predictedActiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveCellsList), len(sequence))
    self.assertEqual(len(self.tm.predictedActiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.predictedInactiveColumnsList), len(sequence))
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList), len(sequence))

    self.assertEqual(self.tm.patterns[-2], self.patternMachine.get(4))
    self.assertEqual(self.tm.sequenceLabels[-2], "Test")
    self.assertEqual(len(self.tm.predictedActiveCellsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveCellsList[-2]), 5)
    self.assertEqual(len(self.tm.predictedActiveColumnsList[-2]), 0)
    self.assertEqual(len(self.tm.predictedInactiveColumnsList[-2]), 5)
    self.assertEqual(len(self.tm.unpredictedActiveColumnsList[-2]), 5)

    self.assertTrue("Test" in self.tm.predictedActiveCellsForSequenceDict)
    predictedActiveCells = reduce(lambda x, y: x | y,
                                  self.tm.predictedActiveCellsList)
    self.assertEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                     predictedActiveCells)

    sequence.reverse()
    sequence.append(sequence.pop(0))  # Move None (reset) to the end
    self._feedSequence(sequence, sequenceLabel="Test2")

    self.assertTrue("Test" in self.tm.predictedActiveCellsForSequenceDict)
    self.assertEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                     predictedActiveCells)
    self.assertTrue("Test2" in self.tm.predictedActiveCellsForSequenceDict)
    self.assertNotEqual(self.tm.predictedActiveCellsForSequenceDict["Test"],
                        self.tm.predictedActiveCellsForSequenceDict["Test2"])

  def testFeedSequenceNoSequenceLabel(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence)
    self.assertEqual(len(self.tm.predictedActiveCellsForSequenceDict), 0)

  def testComputeStatistics(self):
    sequence = self._generateSequence()

    self._feedSequence(sequence)  # train
    self.tm.clearHistory()
    self._feedSequence(sequence)  # test
    stats = self.tm.getStatistics()

    self.assertEqual(len(stats), 7)
    self.assertEqual(stats.predictedInactiveCells.sum, 0)
    self.assertEqual(stats.predictedInactiveColumns.sum, 0)
    self.assertEqual(stats.unpredictedActiveColumns.sum, 0)
    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.sum, None)
    self.assertEqual(stats.sequencesPredictedActiveCellsShared.sum, None)

  def testComputeStatisticsSequenceStatistics(self):
    sequence = self._generateSequence()
    self._feedSequence(sequence, "Test1")

    sequence.reverse()
    sequence.append(sequence.pop(0))  # Move None (reset) to the end
    self._feedSequence(sequence, "Test2")

    stats = self.tm.getStatistics()

    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.average, 1)
    self.assertEqual(stats.sequencesPredictedActiveCellsShared.average, 1)

    self._feedSequence(sequence, "Test3")

    stats = self.tm.getStatistics()

    self.assertEqual(stats.sequencesPredictedActiveCellsPerColumn.average, 1)
    self.assertTrue(stats.sequencesPredictedActiveCellsShared.average > 1)

  def testMapCellsToColumns(self):
    columnsForCells = self.tm.mapCellsToColumns(set([0, 1, 2, 5, 399]))
    self.assertEqual(columnsForCells[0], set([0, 1, 2]))
    self.assertEqual(columnsForCells[1], set([5]))
    self.assertEqual(columnsForCells[99], set([399]))

  # ==============================
  # Helper functions
  # ==============================

  def _generateSequence(self):
    numbers = range(0, 10)
    sequence = self.sequenceMachine.generateFromNumbers(numbers)
    sequence.append(None)
    sequence *= 3

    return sequence

  def _feedSequence(self, sequence, sequenceLabel=None):
    for pattern in sequence:
      if pattern is None:
        self.tm.reset()
      else:
        self.tm.compute(pattern, sequenceLabel=sequenceLabel)

if __name__ == "__main__":
  unittest.main()
<|endoftext|>


<commit_before>import glob
import json

#format
for fn in glob.iglob("schemas/*/*.json"):
    with open(fn,encoding="utf8") as f:
        obj=json.load(f)
    with open(fn,"w",encoding="utf8") as f:
        json.dump(obj,f, indent=1, sort_keys=True)

#format
for fn in glob.iglob("docs/**/*.rst"):
    with open(fn,encoding="utf8") as f:
        lines=[l.rstrip().replace("\t","    ") for l in f]
    with open(fn,"w",encoding="utf8") as f:
        for l in lines:
            f.write(l)
            f.write("\n")
        if lines[-1]:
            f.write("\n")

<commit_msg>zeilenenden für unix normalisiert<commit_after>import glob
import json

#format
for fn in glob.iglob("schemas/*/*.json"):
    with open(fn,encoding="utf8") as f:
        obj=json.load(f)
    with open(fn,"w",encoding="utf8") as f:
        json.dump(obj,f, indent=1, sort_keys=True)

#format
for fn in glob.iglob("docs/**/*.rst"):
    with open(fn,encoding="utf8") as f:
        lines=[l.rstrip().replace("\t","    ") for l in f]
    with open(fn,"w",encoding="utf8",newline="") as f:
        for l in lines:
            f.write(l)
            f.write("\n")
        if lines[-1]:
            f.write("\n")

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2007 Edgewall Software
# Copyright (C) 2003-2007 Christian Boos <cboos@neuf.fr>
# Copyright (C) 2003-2007 Alec Thomas <alec@swapoff.org>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Christian Boos <cboos@neuf.fr>
#         Alec Thomas <alec@swapoff.org>

from StringIO import StringIO

from trac.util.html import html
from trac.core import *
from trac.util import reversed

class ResourceError(TracError):
    """Base resource exception."""

class InvalidResourceSelector(ResourceError):
    """Thrown when an invalid resource selector is provided."""

class IContextProvider(Interface):
    """Map between Trac resources and their corresponding contexts."""

    def get_context_classes():
        """Generator yielding a list of `Context` subclasses."""

class Context(object):
    """Base class for Wiki rendering contexts.

    A context specifies ''which'' resource is accessed and ''how''
    this resource is accessed.

    The context is a lightweight resource descriptor. It identifies
    a resource using a `realm` information (e.g. `"wiki"`, `"ticket"`,
    etc.) and a `id` uniquely identifying the resource within its realm.
    If there's a data model associated to the resource, it can be
    retrieved using the `resource` property.
    As a resource is usually versioned, there's also a `version` property
    with the usual convention that `None` refers to the latest version
    of the resource.

    The context also knows about the "access context" of a Wiki content,
    i.e. how the resource is actually accessed. Most of the time, this
    is by the way of a web request (`req`). The parameters of that web
    request will be reused in order to generate correct URLs for hyperlinks
    related to the context (like `resource_href()`).
    If the request is not present in the context, the canonical base URLs
    as configured in the environment will be used.

    This also means that the user accessing the resource is known, and that
    specific access control rules can be enforced.

    Another aspect related to the access context consists of the scope or
    context trail by which the information belonging to a context is
    presented. It is quite usual that contexts are embedded in other
    contexts. This can be known by querying the `parent` context, which
    is automatically set when creating a subcontext from another context.
    This does '''not''' automatically imply that there's any relationship
    between the corresponding resources, though it can be that some
    resources are only fully identified when taking their parent context
    into account (like attachments, see `permid()`).

    For example, when rendering a ticket description from within a
    Custom Query rendered by the TicketQuery macro inside a wiki page,
    the context ''path'' will be:

    `Context(env, req)('wiki', 'CurrentStatus')('query')('ticket', '12')`

    Finally, the context could also know about the expected output MIME type
    which should be used to present the information to the user (TODO)
    """

    realm = None

    def __init__(self, env, req, realm=None, id=None, parent=None,
                 version=None, abs_urls=False, db=None, resource=None):
        if not env:
            raise TracError("Environment not specified for Context")
        self.env = env
        self.req = req
        self.realm = realm
        self.id = id
        self.parent = parent
        self.version = version
        self.abs_urls = abs_urls
        self._db = db
        self._perm = None
        self.resource = resource

    def __repr__(self):
        path = []
        current = self
        while current:
            name = current.realm or '[root]'
            if current.id:
                name += ':' + unicode(current.id) # id can be numerical
            if current.version:
                name += '@' + unicode(current.version)
            path.append(name)
            current = current.parent
        detail = ''
        if self.abs_urls:
            detail += ' [abs]'
        if self._resource:
            detail += ' %r' % self._resource
        return '<Context %s%s%s>' % \
               (', '.join(reversed(path)),
                self.req and ' %r' % self.req or '', detail)

    def __call__(self, realm=None, id=None, version=False, abs_urls=None,
                 resource=None):
        """Create a new Context, usually a child of this Context.

        >>> from trac.test import EnvironmentStub
        >>> c = Context(EnvironmentStub(), None)

        >>> c1 = c('wiki', 'CurrentStatus')
        >>> c1
        <Context [root], wiki:CurrentStatus>

        If both `realm` and `id` are `None`, then the new context will
        actually be a copy of the current context, instead of a child context.

        >>> c2 = c1()
        >>> c2
        <Context [root], wiki:CurrentStatus>

        >>> (c1.parent == c2.parent, c1.parent == c)
        (True, True)

        >>> c(abs_urls=True)('query')('ticket', '12')
        <Context [root], query, ticket:12 [abs]>

        In the case of a copy, if `version` is not specified or `False`,
        the current version will be kept. Setting `version` explicitely
        to `None` will request the latest version.

        >>> c3 = c1(version=3)
        >>> c3
        <Context [root], wiki:CurrentStatus@3>

        >>> c4 = c3()
        >>> c3
        <Context [root], wiki:CurrentStatus@3>

        Only changing the `id` results in another resource in the same realm.

        >>> c5 = c4(id='AnotherOne')
        >>> c5
        <Context [root], wiki:CurrentStatus@3, wiki:AnotherOne>

        """
        abs_urls = [abs_urls, self.abs_urls][abs_urls is None]
        if realm or id:
            # create a child context
            if version is False: # not set, use latest
                version = None
            return ResourceSystem(self.env).create_context(
                self.req, realm or self.realm, id, self,
                version=version, abs_urls=abs_urls, resource=resource)
        else:
            # copy current context
            copy = object.__new__(self.__class__)
            # strict copy
            copy.env = self.env
            copy.req = self.req
            copy.realm = self.realm
            copy.id = self.id
            copy.parent = self.parent
            copy._db = self._db
            copy._perm = self._perm
            # copy + update
            if version is False: # not set, keep existing
                version = self.version
            copy.version = version
            copy.abs_urls = abs_urls
            copy.resource = resource or self._resource
            return copy

    def from_resource(cls, req, resource, *args, **kwargs):
        """Create a new Context from an existing Resource.

        Can take any other argument `__call__` can take, except `realm` and `id`
        which are deduced from the `resource` itself.
        """
        kwargs['resource'] = resource
        res = ResourceSystem(resource.env)
        return res.create_context(req, resource.realm, resource.id,
                                  *args, **kwargs)
    from_resource = classmethod(from_resource)

    def get_resource(self):
        """Return the actual resource this context refers to.

        This should be overridden by subclasses in order to return
        the data model object corresponding to resource specified by
        the context.
        """
        return None

    def set_resource(self, resource):
        """Attach the given resource to this context.

        This could be overridden by subclasses, to ensure that the resource
        descriptor properties of the Context are always correctly reflecting
        the state of the `resource` itself (for the `version` property, for
        example).
        """
        pass

    def _get_db(self):
        if not self._db:
            self._db = self.env.get_db_cnx()
        return self._db
    db = property(fget=_get_db)

    def _get_href(self):
        """Return an Href instance, adapted to the context."""
        base = self.req or self.env
        if self.abs_urls:
            return base.abs_href
        else:
            return base.href
    href = property(fget=_get_href)

    def _get_resource(self):
        if not self._resource:
            self._resource = self.get_resource()
            if not self._resource:
                raise InvalidResourceSelector("Can't retrieve resource %s:%s" %
                                              (self.realm, self.id))
        return self._resource
    def _set_resource(self, resource):
        self.set_resource(resource)
        self._resource = resource
    resource = property(_get_resource, _set_resource)

    def _get_perm(self):
        """Permissions specific to this context."""
        from trac.perm import PermissionCache
        if not self._perm:
            self._perm = PermissionCacheProxy(self)
        return self._perm
    perm = property(_get_perm)

    def resource_href(self, path=None, **kwargs):
        """Return a canonical URL for the resource associated to this Context.

        In addition, a relative `path` can be given to refer to another
        resource of the same realm, relative to the current one.

        >>> from trac.test import EnvironmentStub
        >>> c = Context(EnvironmentStub(), None)

        >>> c.resource_href()
        '/trac.cgi'

        >>> c(abs_urls=True).resource_href()
        'http://example.org/trac.cgi'

        >>> c('wiki', 'Main').resource_href()
        '/trac.cgi/wiki/Main'

        Relative references start at the current id:

        >>> c('wiki', 'Main').resource_href('./Sub')
        '/trac.cgi/wiki/Main/Sub'

        >>> c('wiki', 'Main/Sub').resource_href('..')
        '/trac.cgi/wiki/Main'

        >>> c('wiki', 'Main').resource_href('../Other')
        '/trac.cgi/wiki/Other'

        References always stay within the current resource realm:

        >>> main_sub = c('wiki', 'Main/Sub')
        >>> main_sub.resource_href('../..')
        '/trac.cgi/wiki'

        >>> main_sub.resource_href('../../..')
        '/trac.cgi/wiki'
        """
        if path and path[0] == '/': # absolute reference, start at project base
            return self.href(path.lstrip('/'), **kwargs)
        base = unicode(self.id or '').split('/')
        for comp in (path or '').split('/'):
            if comp in ('.', ''):
                continue
            elif comp == '..':
                if base:
                    base.pop()
            else:
                base.append(comp)
        return self.href(self.realm, *base, **kwargs)

    # -- resource descriptors methods

    def permid(self):
        return (self.realm, self.id)
    # TODO: consider using a .depth class property?
    #       This would default to 1 and would be 2 for attachments.
    #       permid() would then be:
    #
    #         parent = self
    #         id = (,)
    #         for i in range(self.depth):
    #             id += (parent.realm, parent.id)
    #         return id

    def name(self):
        return '%s:%s' % (self.realm, self.id)

    def shortname(self):
        return self.name()

    def summary(self):
        summary = self.name()
        if self.version:
            summary += ' at version %s' % self.version
        return summary

class ResourceSystem(Component):
    """Helper component for creating Contexts."""

    context_providers = ExtensionPoint(IContextProvider)

    def __init__(self):
        self._context_map = None

    # Public methods

    def create_context(self, req, realm, *args, **kwargs):
        """Create the appropriate Context for the given `realm`.

        For the remaining arguments, see the Context constructor.
        """
        # build a dict of realm keys to Context subclasses values
        if not self._context_map:
            map = {}
            for provider in self.context_providers:
                for context_class in provider.get_context_classes():
                    map[context_class.realm] = context_class
            self._context_map = map
        context_class = self._context_map.get(realm, Context)
        return context_class(self.env, req, realm, *args, **kwargs)

    def get_known_realms(self):
        realms = []
        for provider in self.context_providers:
            for context_class in provider.get_context_classes():
                realms.append(context_class.realm)
        return realms

class PermissionCacheProxy(object):
    def __init__(self, context):
        self.context = context

    def __contains__(self, action):
        """Checks whether the given `action` is permitted."""
        return self.context.req.perm.__contains__(action, self.context)
    has_permission = __contains__

    def has_all(self, *actions):
        """Return `True` if all the `actions` are permitted."""
        return self.context.req.perm.has_all(actions, self.context)

    def has_any(self, *actions):
        """Return `True` if at least one of the `actions` is permitted."""
        return self.context.req.perm.has_any(actions, self.context)

    def require(self, action):
        """Ensure that the given `action` is permitted."""
        return self.context.req.perm.require(action, self.context)
    assert_permission = require

    def require_all(self, *actions):
        """Ensure that all of the given `actions` are permitted."""
        return self.context.req.perm.require_all(actions, self.context)

    def require_any(self, *actions):
        """Ensure that at least one of the given `actions` is permitted."""
        return self.context.req.perm.require_any(actions, self.context)
<commit_msg>WikiContext: don't store a reference to the db handle in a context.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2007 Edgewall Software
# Copyright (C) 2003-2007 Christian Boos <cboos@neuf.fr>
# Copyright (C) 2003-2007 Alec Thomas <alec@swapoff.org>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Christian Boos <cboos@neuf.fr>
#         Alec Thomas <alec@swapoff.org>

from StringIO import StringIO

from trac.util.html import html
from trac.core import *
from trac.util import reversed

class ResourceError(TracError):
    """Base resource exception."""

class InvalidResourceSelector(ResourceError):
    """Thrown when an invalid resource selector is provided."""

class IContextProvider(Interface):
    """Map between Trac resources and their corresponding contexts."""

    def get_context_classes():
        """Generator yielding a list of `Context` subclasses."""

class Context(object):
    """Base class for Wiki rendering contexts.

    A context specifies ''which'' resource is accessed and ''how''
    this resource is accessed.

    The context is a lightweight resource descriptor. It identifies
    a resource using a `realm` information (e.g. `"wiki"`, `"ticket"`,
    etc.) and a `id` uniquely identifying the resource within its realm.
    If there's a data model associated to the resource, it can be
    retrieved using the `resource` property.
    As a resource is usually versioned, there's also a `version` property
    with the usual convention that `None` refers to the latest version
    of the resource.

    The context also knows about the "access context" of a Wiki content,
    i.e. how the resource is actually accessed. Most of the time, this
    is by the way of a web request (`req`). The parameters of that web
    request will be reused in order to generate correct URLs for hyperlinks
    related to the context (like `resource_href()`).
    If the request is not present in the context, the canonical base URLs
    as configured in the environment will be used.

    This also means that the user accessing the resource is known, and that
    specific access control rules can be enforced.

    Another aspect related to the access context consists of the scope or
    context trail by which the information belonging to a context is
    presented. It is quite usual that contexts are embedded in other
    contexts. This can be known by querying the `parent` context, which
    is automatically set when creating a subcontext from another context.
    This does '''not''' automatically imply that there's any relationship
    between the corresponding resources, though it can be that some
    resources are only fully identified when taking their parent context
    into account (like attachments, see `permid()`).

    For example, when rendering a ticket description from within a
    Custom Query rendered by the TicketQuery macro inside a wiki page,
    the context ''path'' will be:

    `Context(env, req)('wiki', 'CurrentStatus')('query')('ticket', '12')`

    Finally, the context could also know about the expected output MIME type
    which should be used to present the information to the user (TODO)
    """

    realm = None

    def __init__(self, env, req, realm=None, id=None, parent=None,
                 version=None, abs_urls=False, db=None, resource=None):
        if not env:
            raise TracError("Environment not specified for Context")
        self.env = env
        self.req = req
        self.realm = realm
        self.id = id
        self.parent = parent
        self.version = version
        self.abs_urls = abs_urls
        self._db = db
        self._perm = None
        self.resource = resource

    def __repr__(self):
        path = []
        current = self
        while current:
            name = current.realm or '[root]'
            if current.id:
                name += ':' + unicode(current.id) # id can be numerical
            if current.version:
                name += '@' + unicode(current.version)
            path.append(name)
            current = current.parent
        detail = ''
        if self.abs_urls:
            detail += ' [abs]'
        if self._resource:
            detail += ' %r' % self._resource
        return '<Context %s%s%s>' % \
               (', '.join(reversed(path)),
                self.req and ' %r' % self.req or '', detail)

    def __call__(self, realm=None, id=None, version=False, abs_urls=None,
                 resource=None):
        """Create a new Context, usually a child of this Context.

        >>> from trac.test import EnvironmentStub
        >>> c = Context(EnvironmentStub(), None)

        >>> c1 = c('wiki', 'CurrentStatus')
        >>> c1
        <Context [root], wiki:CurrentStatus>

        If both `realm` and `id` are `None`, then the new context will
        actually be a copy of the current context, instead of a child context.

        >>> c2 = c1()
        >>> c2
        <Context [root], wiki:CurrentStatus>

        >>> (c1.parent == c2.parent, c1.parent == c)
        (True, True)

        >>> c(abs_urls=True)('query')('ticket', '12')
        <Context [root], query, ticket:12 [abs]>

        In the case of a copy, if `version` is not specified or `False`,
        the current version will be kept. Setting `version` explicitely
        to `None` will request the latest version.

        >>> c3 = c1(version=3)
        >>> c3
        <Context [root], wiki:CurrentStatus@3>

        >>> c4 = c3()
        >>> c3
        <Context [root], wiki:CurrentStatus@3>

        Only changing the `id` results in another resource in the same realm.

        >>> c5 = c4(id='AnotherOne')
        >>> c5
        <Context [root], wiki:CurrentStatus@3, wiki:AnotherOne>

        """
        abs_urls = [abs_urls, self.abs_urls][abs_urls is None]
        if realm or id:
            # create a child context
            if version is False: # not set, use latest
                version = None
            return ResourceSystem(self.env).create_context(
                self.req, realm or self.realm, id, self,
                version=version, abs_urls=abs_urls, resource=resource)
        else:
            # copy current context
            copy = object.__new__(self.__class__)
            # strict copy
            copy.env = self.env
            copy.req = self.req
            copy.realm = self.realm
            copy.id = self.id
            copy.parent = self.parent
            copy._db = self._db
            copy._perm = self._perm
            # copy + update
            if version is False: # not set, keep existing
                version = self.version
            copy.version = version
            copy.abs_urls = abs_urls
            copy.resource = resource or self._resource
            return copy

    def from_resource(cls, req, resource, *args, **kwargs):
        """Create a new Context from an existing Resource.

        Can take any other argument `__call__` can take, except `realm` and `id`
        which are deduced from the `resource` itself.
        """
        kwargs['resource'] = resource
        res = ResourceSystem(resource.env)
        return res.create_context(req, resource.realm, resource.id,
                                  *args, **kwargs)
    from_resource = classmethod(from_resource)

    def get_resource(self):
        """Return the actual resource this context refers to.

        This should be overridden by subclasses in order to return
        the data model object corresponding to resource specified by
        the context.
        """
        return None

    def set_resource(self, resource):
        """Attach the given resource to this context.

        This could be overridden by subclasses, to ensure that the resource
        descriptor properties of the Context are always correctly reflecting
        the state of the `resource` itself (for the `version` property, for
        example).
        """
        pass

    def _get_db(self):
        return self.env.get_db_cnx()
    db = property(fget=_get_db)

    def _get_href(self):
        """Return an Href instance, adapted to the context."""
        base = self.req or self.env
        if self.abs_urls:
            return base.abs_href
        else:
            return base.href
    href = property(fget=_get_href)

    def _get_resource(self):
        if not self._resource:
            self._resource = self.get_resource()
            if not self._resource:
                raise InvalidResourceSelector("Can't retrieve resource %s:%s" %
                                              (self.realm, self.id))
        return self._resource
    def _set_resource(self, resource):
        self.set_resource(resource)
        self._resource = resource
    resource = property(_get_resource, _set_resource)

    def _get_perm(self):
        """Permissions specific to this context."""
        from trac.perm import PermissionCache
        if not self._perm:
            self._perm = PermissionCacheProxy(self)
        return self._perm
    perm = property(_get_perm)

    def resource_href(self, path=None, **kwargs):
        """Return a canonical URL for the resource associated to this Context.

        In addition, a relative `path` can be given to refer to another
        resource of the same realm, relative to the current one.

        >>> from trac.test import EnvironmentStub
        >>> c = Context(EnvironmentStub(), None)

        >>> c.resource_href()
        '/trac.cgi'

        >>> c(abs_urls=True).resource_href()
        'http://example.org/trac.cgi'

        >>> c('wiki', 'Main').resource_href()
        '/trac.cgi/wiki/Main'

        Relative references start at the current id:

        >>> c('wiki', 'Main').resource_href('./Sub')
        '/trac.cgi/wiki/Main/Sub'

        >>> c('wiki', 'Main/Sub').resource_href('..')
        '/trac.cgi/wiki/Main'

        >>> c('wiki', 'Main').resource_href('../Other')
        '/trac.cgi/wiki/Other'

        References always stay within the current resource realm:

        >>> main_sub = c('wiki', 'Main/Sub')
        >>> main_sub.resource_href('../..')
        '/trac.cgi/wiki'

        >>> main_sub.resource_href('../../..')
        '/trac.cgi/wiki'
        """
        if path and path[0] == '/': # absolute reference, start at project base
            return self.href(path.lstrip('/'), **kwargs)
        base = unicode(self.id or '').split('/')
        for comp in (path or '').split('/'):
            if comp in ('.', ''):
                continue
            elif comp == '..':
                if base:
                    base.pop()
            else:
                base.append(comp)
        return self.href(self.realm, *base, **kwargs)

    # -- resource descriptors methods

    def permid(self):
        return (self.realm, self.id)
    # TODO: consider using a .depth class property?
    #       This would default to 1 and would be 2 for attachments.
    #       permid() would then be:
    #
    #         parent = self
    #         id = (,)
    #         for i in range(self.depth):
    #             id += (parent.realm, parent.id)
    #         return id

    def name(self):
        return '%s:%s' % (self.realm, self.id)

    def shortname(self):
        return self.name()

    def summary(self):
        summary = self.name()
        if self.version:
            summary += ' at version %s' % self.version
        return summary

class ResourceSystem(Component):
    """Helper component for creating Contexts."""

    context_providers = ExtensionPoint(IContextProvider)

    def __init__(self):
        self._context_map = None

    # Public methods

    def create_context(self, req, realm, *args, **kwargs):
        """Create the appropriate Context for the given `realm`.

        For the remaining arguments, see the Context constructor.
        """
        # build a dict of realm keys to Context subclasses values
        if not self._context_map:
            map = {}
            for provider in self.context_providers:
                for context_class in provider.get_context_classes():
                    map[context_class.realm] = context_class
            self._context_map = map
        context_class = self._context_map.get(realm, Context)
        return context_class(self.env, req, realm, *args, **kwargs)

    def get_known_realms(self):
        realms = []
        for provider in self.context_providers:
            for context_class in provider.get_context_classes():
                realms.append(context_class.realm)
        return realms

class PermissionCacheProxy(object):
    def __init__(self, context):
        self.context = context

    def __contains__(self, action):
        """Checks whether the given `action` is permitted."""
        return self.context.req.perm.__contains__(action, self.context)
    has_permission = __contains__

    def has_all(self, *actions):
        """Return `True` if all the `actions` are permitted."""
        return self.context.req.perm.has_all(actions, self.context)

    def has_any(self, *actions):
        """Return `True` if at least one of the `actions` is permitted."""
        return self.context.req.perm.has_any(actions, self.context)

    def require(self, action):
        """Ensure that the given `action` is permitted."""
        return self.context.req.perm.require(action, self.context)
    assert_permission = require

    def require_all(self, *actions):
        """Ensure that all of the given `actions` are permitted."""
        return self.context.req.perm.require_all(actions, self.context)

    def require_any(self, *actions):
        """Ensure that at least one of the given `actions` is permitted."""
        return self.context.req.perm.require_any(actions, self.context)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2005 Edgewall Software
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://projects.edgewall.com/trac/.
#
# Author: Christopher Lenz <cmlenz@gmx.de>

try:
    import threading
except ImportError:
    import dummy_threading as threading
    threading._get_ident = lambda: 0
import time

from trac.db.util import ConnectionWrapper

class TimeoutError(Exception):
    """Exception raised by the connection pool when no connection has become
    available after a given timeout."""

class PooledConnection(ConnectionWrapper):
    """A database connection that can be pooled. When closed, it gets returned
    to the pool.
    """

    def __init__(self, pool, cnx):
        ConnectionWrapper.__init__(self, cnx)
        self._pool = pool

    def close(self):
        if self.cnx:
            self._pool._return_cnx(self.cnx)
            self.cnx = None

    def __del__(self):
        self.close()

class ConnectionPool(object):
    """A very simple connection pool implementation."""

    def __init__(self, maxsize, connector, **kwargs):
        self._dormant = [] # inactive connections in pool
        self._active = {} # active connections by thread ID
        self._available = threading.Condition(threading.Lock())
        self._maxsize = maxsize # maximum pool size
        self._cursize = 0 # current pool size, includes active connections
        self._connector = connector
        self._kwargs = kwargs

    def get_cnx(self, timeout=None):
        start = time.time()
        self._available.acquire()
        try:
            tid = threading._get_ident()
            if tid in self._active:
                self._active[tid][0] += 1
                return PooledConnection(self, self._active[tid][1])
            while True:
                if self._dormant:
                    cnx = self._dormant.pop()
                    break
                elif self._maxsize and self._cursize < self._maxsize:
                    cnx = self._connector.get_connection(**self._kwargs)
                    self._cursize += 1
                    break
                else:
                    if timeout:
                        self._available.wait(timeout)
                        if (time.time() - start) >= timeout:
                            raise TimeoutError, 'Unable to get database ' \
                                                'connection within %d seconds' \
                                                % timeout
                    else:
                        self._available.wait()
            self._active[tid] = [1, cnx]
            return PooledConnection(self, cnx)
        finally:
            self._available.release()

    def _return_cnx(self, cnx):
        self._available.acquire()
        try:
            tid = threading._get_ident()
            if tid in self._active:
                num, cnx_ = self._active.get(tid)
                assert cnx is cnx_
                if num > 1:
                    self._active[tid][0] = num - 1
                else:
                    del self._active[tid]
                    if cnx not in self._dormant:
                        cnx.rollback()
                        if cnx.poolable:
                            self._dormant.append(cnx)
                        else:
                            self._cursize -= 1
                        self._available.notify()
        finally:
            self._available.release()

    def shutdown(self):
        self._available.acquire()
        try:
            for cnx in self._dormant:
                cnx.cnx.close()
        finally:
            self._available.release()
<commit_msg>Handle stale PostgreSQL (or others) more gracefully. Closes #3394. Thanks to flfr at stibo.com for the patch.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2005 Edgewall Software
# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.com/license.html.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://projects.edgewall.com/trac/.
#
# Author: Christopher Lenz <cmlenz@gmx.de>

try:
    import threading
except ImportError:
    import dummy_threading as threading
    threading._get_ident = lambda: 0
import time

from trac.db.util import ConnectionWrapper

class TimeoutError(Exception):
    """Exception raised by the connection pool when no connection has become
    available after a given timeout."""

class PooledConnection(ConnectionWrapper):
    """A database connection that can be pooled. When closed, it gets returned
    to the pool.
    """

    def __init__(self, pool, cnx):
        ConnectionWrapper.__init__(self, cnx)
        self._pool = pool

    def close(self):
        if self.cnx:
            self._pool._return_cnx(self.cnx)
            self.cnx = None

    def __del__(self):
        self.close()

class ConnectionPool(object):
    """A very simple connection pool implementation."""

    def __init__(self, maxsize, connector, **kwargs):
        self._dormant = [] # inactive connections in pool
        self._active = {} # active connections by thread ID
        self._available = threading.Condition(threading.Lock())
        self._maxsize = maxsize # maximum pool size
        self._cursize = 0 # current pool size, includes active connections
        self._connector = connector
        self._kwargs = kwargs

    def get_cnx(self, timeout=None):
        start = time.time()
        self._available.acquire()
        try:
            tid = threading._get_ident()
            if tid in self._active:
                self._active[tid][0] += 1
                return PooledConnection(self, self._active[tid][1])
            while True:
                if self._dormant:
                    cnx = self._dormant.pop()
                    try:
                        cnx.cursor() # check whether the connection is stale
                        break
                    except Exception:
                        cnx.close()
                elif self._maxsize and self._cursize < self._maxsize:
                    cnx = self._connector.get_connection(**self._kwargs)
                    self._cursize += 1
                    break
                else:
                    if timeout:
                        self._available.wait(timeout)
                        if (time.time() - start) >= timeout:
                            raise TimeoutError, 'Unable to get database ' \
                                                'connection within %d seconds' \
                                                % timeout
                    else:
                        self._available.wait()
            self._active[tid] = [1, cnx]
            return PooledConnection(self, cnx)
        finally:
            self._available.release()

    def _return_cnx(self, cnx):
        self._available.acquire()
        try:
            tid = threading._get_ident()
            if tid in self._active:
                num, cnx_ = self._active.get(tid)
                assert cnx is cnx_
                if num > 1:
                    self._active[tid][0] = num - 1
                else:
                    del self._active[tid]
                    if cnx not in self._dormant:
                        cnx.rollback()
                        if cnx.poolable:
                            self._dormant.append(cnx)
                        else:
                            self._cursize -= 1
                        self._available.notify()
        finally:
            self._available.release()

    def shutdown(self):
        self._available.acquire()
        try:
            for cnx in self._dormant:
                cnx.cnx.close()
        finally:
            self._available.release()
<|endoftext|>


<commit_before>"""
raven.contrib.django.client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import

import time
import logging

from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.http import HttpRequest
from django.template import TemplateSyntaxError

try:
    # support Django 1.9
    from django.template.base import Origin
except ImportError:
    # backward compatibility
    from django.template.loader import LoaderOrigin as Origin

from raven.base import Client
from raven.contrib.django.utils import get_data_from_template, get_host
from raven.contrib.django.middleware import SentryLogMiddleware
from raven.utils.wsgi import get_headers, get_environ
from raven.utils import once
from raven import breadcrumbs

__all__ = ('DjangoClient',)

@once
def install_sql_hook():
    """If installed this causes Django's queries to be captured."""
    from django.db.backends.utils import CursorWrapper

    def record_sql(start, sql, params):
        breadcrumbs.record_breadcrumb('query', {
            'query': sql,
            'params': params,
            'duration': time.time() - start,
            'classifier': 'django.db'
        })

    real_execute = CursorWrapper.execute
    real_executemany = CursorWrapper.executemany

    def execute(self, sql, params=None):
        start = time.time()
        try:
            return real_execute(self, sql, params)
        finally:
            record_sql(start, sql, params)

    def executemany(self, sql, param_list):
        start = time.time()
        try:
            return real_executemany(self, sql, param_list)
        finally:
            record_sql(start, sql, param_list)

    CursorWrapper.execute = execute
    CursorWrapper.executemany = executemany
    breadcrumbs.ignore_logger('django.db.backends')

class DjangoClient(Client):
    logger = logging.getLogger('sentry.errors.client.django')

    def __init__(self, *args, **kwargs):
        install_sql_hook = kwargs.pop('install_sql_hook', True)
        Client.__init__(self, *args, **kwargs)
        if install_sql_hook:
            self.install_sql_hook()

    def install_sql_hook(self):
        install_sql_hook()

    def get_user_info(self, user):
        if hasattr(user, 'is_authenticated') and \
           not user.is_authenticated():
            return None

        user_info = {
            'id': user.pk,
        }

        if hasattr(user, 'email'):
            user_info['email'] = user.email

        if hasattr(user, 'get_username'):
            user_info['username'] = user.get_username()
        elif hasattr(user, 'username'):
            user_info['username'] = user.username

        return user_info

    def get_data_from_request(self, request):
        result = {}

        user = getattr(request, 'user', None)
        if user is not None:
            user_info = self.get_user_info(user)
            if user_info:
                result['user'] = user_info

        try:
            uri = request.build_absolute_uri()
        except SuspiciousOperation:
            # attempt to build a URL for reporting as Django won't allow us to
            # use get_host()
            if request.is_secure():
                scheme = 'https'
            else:
                scheme = 'http'
            host = get_host(request)
            uri = '%s://%s%s' % (scheme, host, request.path)

        if request.method not in ('GET', 'HEAD'):
            try:
                data = request.body
            except Exception:
                try:
                    data = request.raw_post_data
                except Exception:
                    # assume we had a partial read.
                    try:
                        data = request.POST or '<unavailable>'
                    except Exception:
                        data = '<unavailable>'
        else:
            data = None

        environ = request.META

        result.update({
            'request': {
                'method': request.method,
                'url': uri,
                'query_string': request.META.get('QUERY_STRING'),
                'data': data,
                'cookies': dict(request.COOKIES),
                'headers': dict(get_headers(environ)),
                'env': dict(get_environ(environ)),
            }
        })

        return result

    def build_msg(self, *args, **kwargs):
        data = super(DjangoClient, self).build_msg(*args, **kwargs)

        for frame in self._iter_frames(data):
            module = frame.get('module')
            if not module:
                continue

            if module.startswith('django.'):
                frame['in_app'] = False

        if not self.site and 'django.contrib.sites' in settings.INSTALLED_APPS:
            try:
                from django.contrib.sites.models import Site
                site = Site.objects.get_current()
                site_name = site.name or site.domain
                data['tags'].setdefault('site', site_name)
            except Exception:
                # Database error? Fallback to the id
                try:
                    data['tags'].setdefault('site', settings.SITE_ID)
                except AttributeError:
                    # SITE_ID wasn't set, so just ignore
                    pass

        return data

    def capture(self, event_type, request=None, **kwargs):
        if 'data' not in kwargs:
            kwargs['data'] = data = {}
        else:
            data = kwargs['data']

        if request is None:
            request = getattr(SentryLogMiddleware.thread, 'request', None)

        is_http_request = isinstance(request, HttpRequest)
        if is_http_request:
            data.update(self.get_data_from_request(request))

        if kwargs.get('exc_info'):
            exc_value = kwargs['exc_info'][1]
            # As of r16833 (Django) all exceptions may contain a
            # ``django_template_source`` attribute (rather than the legacy
            # ``TemplateSyntaxError.source`` check) which describes
            # template information.  As of Django 1.9 or so the new
            # template debug thing showed up.
            if hasattr(exc_value, 'django_template_source') or \
               ((isinstance(exc_value, TemplateSyntaxError) and
                isinstance(getattr(exc_value, 'source', None),
                           (tuple, list)) and
                isinstance(exc_value.source[0], Origin))) or \
               hasattr(exc_value, 'template_debug'):
                source = getattr(exc_value, 'django_template_source',
                                 getattr(exc_value, 'source', None))
                debug = getattr(exc_value, 'template_debug', None)
                if source is None:
                    self.logger.info('Unable to get template source from exception')
                data.update(get_data_from_template(source, debug))

        result = super(DjangoClient, self).capture(event_type, **kwargs)

        if is_http_request and result:
            # attach the sentry object to the request
            request.sentry = {
                'project_id': data.get('project', self.remote.project),
                'id': self.get_ident(result),
            }

        return result
<commit_msg>Also support older django versions for sql hook<commit_after>"""
raven.contrib.django.client
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

from __future__ import absolute_import

import time
import logging

from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.http import HttpRequest
from django.template import TemplateSyntaxError

try:
    # support Django 1.9
    from django.template.base import Origin
except ImportError:
    # backward compatibility
    from django.template.loader import LoaderOrigin as Origin

from raven.base import Client
from raven.contrib.django.utils import get_data_from_template, get_host
from raven.contrib.django.middleware import SentryLogMiddleware
from raven.utils.wsgi import get_headers, get_environ
from raven.utils import once
from raven import breadcrumbs

__all__ = ('DjangoClient',)

@once
def install_sql_hook():
    """If installed this causes Django's queries to be captured."""
    try:
        from django.db.backends.utils import CursorWrapper
    except ImportError:
        from django.db.backends.util import CursorWrapper

    def record_sql(start, sql, params):
        breadcrumbs.record_breadcrumb('query', {
            'query': sql,
            'params': params,
            'duration': time.time() - start,
            'classifier': 'django.db'
        })

    real_execute = CursorWrapper.execute
    real_executemany = CursorWrapper.executemany

    def execute(self, sql, params=None):
        start = time.time()
        try:
            return real_execute(self, sql, params)
        finally:
            record_sql(start, sql, params)

    def executemany(self, sql, param_list):
        start = time.time()
        try:
            return real_executemany(self, sql, param_list)
        finally:
            record_sql(start, sql, param_list)

    CursorWrapper.execute = execute
    CursorWrapper.executemany = executemany
    breadcrumbs.ignore_logger('django.db.backends')

class DjangoClient(Client):
    logger = logging.getLogger('sentry.errors.client.django')

    def __init__(self, *args, **kwargs):
        install_sql_hook = kwargs.pop('install_sql_hook', True)
        Client.__init__(self, *args, **kwargs)
        if install_sql_hook:
            self.install_sql_hook()

    def install_sql_hook(self):
        install_sql_hook()

    def get_user_info(self, user):
        if hasattr(user, 'is_authenticated') and \
           not user.is_authenticated():
            return None

        user_info = {
            'id': user.pk,
        }

        if hasattr(user, 'email'):
            user_info['email'] = user.email

        if hasattr(user, 'get_username'):
            user_info['username'] = user.get_username()
        elif hasattr(user, 'username'):
            user_info['username'] = user.username

        return user_info

    def get_data_from_request(self, request):
        result = {}

        user = getattr(request, 'user', None)
        if user is not None:
            user_info = self.get_user_info(user)
            if user_info:
                result['user'] = user_info

        try:
            uri = request.build_absolute_uri()
        except SuspiciousOperation:
            # attempt to build a URL for reporting as Django won't allow us to
            # use get_host()
            if request.is_secure():
                scheme = 'https'
            else:
                scheme = 'http'
            host = get_host(request)
            uri = '%s://%s%s' % (scheme, host, request.path)

        if request.method not in ('GET', 'HEAD'):
            try:
                data = request.body
            except Exception:
                try:
                    data = request.raw_post_data
                except Exception:
                    # assume we had a partial read.
                    try:
                        data = request.POST or '<unavailable>'
                    except Exception:
                        data = '<unavailable>'
        else:
            data = None

        environ = request.META

        result.update({
            'request': {
                'method': request.method,
                'url': uri,
                'query_string': request.META.get('QUERY_STRING'),
                'data': data,
                'cookies': dict(request.COOKIES),
                'headers': dict(get_headers(environ)),
                'env': dict(get_environ(environ)),
            }
        })

        return result

    def build_msg(self, *args, **kwargs):
        data = super(DjangoClient, self).build_msg(*args, **kwargs)

        for frame in self._iter_frames(data):
            module = frame.get('module')
            if not module:
                continue

            if module.startswith('django.'):
                frame['in_app'] = False

        if not self.site and 'django.contrib.sites' in settings.INSTALLED_APPS:
            try:
                from django.contrib.sites.models import Site
                site = Site.objects.get_current()
                site_name = site.name or site.domain
                data['tags'].setdefault('site', site_name)
            except Exception:
                # Database error? Fallback to the id
                try:
                    data['tags'].setdefault('site', settings.SITE_ID)
                except AttributeError:
                    # SITE_ID wasn't set, so just ignore
                    pass

        return data

    def capture(self, event_type, request=None, **kwargs):
        if 'data' not in kwargs:
            kwargs['data'] = data = {}
        else:
            data = kwargs['data']

        if request is None:
            request = getattr(SentryLogMiddleware.thread, 'request', None)

        is_http_request = isinstance(request, HttpRequest)
        if is_http_request:
            data.update(self.get_data_from_request(request))

        if kwargs.get('exc_info'):
            exc_value = kwargs['exc_info'][1]
            # As of r16833 (Django) all exceptions may contain a
            # ``django_template_source`` attribute (rather than the legacy
            # ``TemplateSyntaxError.source`` check) which describes
            # template information.  As of Django 1.9 or so the new
            # template debug thing showed up.
            if hasattr(exc_value, 'django_template_source') or \
               ((isinstance(exc_value, TemplateSyntaxError) and
                isinstance(getattr(exc_value, 'source', None),
                           (tuple, list)) and
                isinstance(exc_value.source[0], Origin))) or \
               hasattr(exc_value, 'template_debug'):
                source = getattr(exc_value, 'django_template_source',
                                 getattr(exc_value, 'source', None))
                debug = getattr(exc_value, 'template_debug', None)
                if source is None:
                    self.logger.info('Unable to get template source from exception')
                data.update(get_data_from_template(source, debug))

        result = super(DjangoClient, self).capture(event_type, **kwargs)

        if is_http_request and result:
            # attach the sentry object to the request
            request.sentry = {
                'project_id': data.get('project', self.remote.project),
                'id': self.get_ident(result),
            }

        return result
<|endoftext|>


<commit_before>import os

from rmc.config.flask_base import *
import rmc.shared.secrets as s

JS_DIR = 'js_prod'
DEBUG = False
ENV = 'prod'
GA_PROPERTY_ID = 'UA-35073503-1'
LOG_DIR = '/home/rmc/logs'
LOG_PATH = os.path.join(LOG_DIR, 'server/server.log')
FB_APP_ID = '219309734863464'
FB_APP_SECRET = s.FB_APP_SECRET_PROD
<commit_msg>Revert "Revert "do not use minified js on prod"" (=don't minify to fix things)<commit_after>import os

from rmc.config.flask_base import *
import rmc.shared.secrets as s

JS_DIR = 'js'
DEBUG = False
ENV = 'prod'
GA_PROPERTY_ID = 'UA-35073503-1'
LOG_DIR = '/home/rmc/logs'
LOG_PATH = os.path.join(LOG_DIR, 'server/server.log')
FB_APP_ID = '219309734863464'
FB_APP_SECRET = s.FB_APP_SECRET_PROD
<|endoftext|>


<commit_before>"""
spaceshooter.py
Author: Marcus Helble
Credit: Liam Abbate, Wilson Rimberg

Assignment:
Write and submit a program that implements the spacewar game:
https://github.com/HHS-IntroProgramming/Spacewar
"""
import ggame
from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame
from random import random
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 910
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

space_asset= ImageAsset("images/starfield.jpg",)
backg=Sprite(space_asset, (0,0))
backg2=Sprite(space_asset, (512,0))
backg3=Sprite(space_asset,(1024,0))
backg4=Sprite(space_asset, (0,512))
backg5=Sprite(space_asset, (512,512))
backg6=Sprite(space_asset, (1024, 512))
class SpaceShip(Sprite):
    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(227,0,292-227,125), 4, 'vertical')

    def __init__(self, position):
        super().__init__(SpaceShip.asset, position)
        self.vr = 0.01
        self.thrust = 0
        self.thrustframe = 1
        self.rx = 1
        self.ry = -1
        self.rxa = 0
        self.rxb = 0
        self.rya = 0
        self.ryb = 0
        self.hm = 0
        self.vm = 0
        self.visible = True
        SpaceGame.listenKeyEvent("keydown", "space", self.thrustOn)
        SpaceGame.listenKeyEvent("keyup", "space", self.thrustOff)
        SpaceGame.listenKeyEvent("keydown", "left arrow", self.left)
        SpaceGame.listenKeyEvent("keyup", "left arrow", self.stopleft)
        SpaceGame.listenKeyEvent("keydown", "right arrow", self.right)
        SpaceGame.listenKeyEvent("keyup", "right arrow", self.stopright)
        SpaceGame.listenKeyEvent("keydown", "up arrow", self.up)
        SpaceGame.listenKeyEvent("keyup", "up arrow", self.stopup)
        SpaceGame.listenKeyEvent("keydown", "down arrow", self.down)
        SpaceGame.listenKeyEvent("keyup", "down arrow", self.stopdown)

        self.fxcenter = self.fycenter = 0.5

    def step(self):
        self.rotation = 0
        if self.thrust == 1:
            self.setImage(self.thrustframe)
            self.thrustframe += 1
            if self.thrustframe == 4:
                self.thrustframe = 1

        else:
            self.setImage(0)

        if self.rxa == 2 and self.rxb == 2:
            self.x=self.x
            self.hm = 0

        else:
            if self.rx == -5:
                self.x=self.x-10
                self.hm=1
            if self.rx == 5:
                self.x=self.x+10
                self.hm=2

        if self.rya == 2 and self.ryb == 2:
                self.y=self.y
                self.vm = 0

        else:
            if self.ry == -5:
                    self.y=self.y-10
                    self.vm = 1
            if self.ry == 5:
                    self.y=self.y+10
                    self.vm = 2
        if self.hm==0 and self.vm==0:
            self.rotation = 0
            self.thrust = 0
        else:
            self.thrust=1
            if self.hm==1:
                if self.vm==1:
                    self.rotation=(1/4)*pi
                elif self.vm==2:
                    self.rotation=(3/4)*pi
                else:
                    self.rotation=pi/2
            elif self.hm==2:
                if self.vm==1:
                    self.rotation=(7/4)*pi
                elif self.vm==2:
                    self.rotation=(5/4)*pi
                else:
                    self.rotation=(3/2)*pi
            else:
                if self.vm==1:
                    self.rotation=0
                elif self.vm==2:
                    self.rotation=pi
        collision = self.collidingWithSprites(Star)
        if len(collision) > 0:
            self.visible=False

    def thrustOn(self, event):
        self.thrust = 1
    def thrustOff(self, event):
        self.thrust = 0
    def left(self, event):
        self.rx=-5
        self.rxa=0
    def right(self, event):
        self.rx=5
        self.rxb=0
    def up(self, event):
        self.ry=-5
        self.rya=0
    def down(self, event):
        self.ry=5
        self.ryb=0
    def stopleft(self, event):
        self.rxa=2
    def stopright(self, event):
        self.rxb=2
    def stopup(self, event):
        self.ryb=2
    def stopdown(self, event):
        self.rya=2

class Star(Sprite):
    asset=ImageAsset("images/sun.png")
    height=300
    width=300
    def __init__(self, position):
        super().__init__(Star.asset, position)
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.circularCollisionModel()
class SpaceGame(App):
    def step(self):
        for ship in self.getSpritesbyClass(SpaceShip):
            ship.step()
    def __init__(self, width, height):
        super().__init__(width, height)
        SpaceShip((125,100))
        SpaceShip((175,150))
        SpaceShip((75,150))
        Star((600,600))
        Star((850,300))
myapp = SpaceGame(SCREEN_WIDTH, SCREEN_HEIGHT)
myapp.run()<commit_msg>Updated from Brython Server: December 5, 2016 6:17:30 PM EST<commit_after>"""
spaceshooter.py
Author: Marcus Helble
Credit: Liam Abbate, Wilson Rimberg

Assignment:
Write and submit a program that implements the spacewar game:
https://github.com/HHS-IntroProgramming/Spacewar
"""
import ggame
from ggame import App, RectangleAsset, ImageAsset, Sprite, LineStyle, Color, Frame
from random import random
SCREEN_WIDTH = 1500
SCREEN_HEIGHT = 910
pi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
backg_asset= ImageAsset("images/starfield.jpg",)
backg=Sprite(space_asset, (0,0))
backg2=Sprite(space_asset, (512,0))
backg3=Sprite(space_asset,(1024,0))
backg4=Sprite(space_asset, (0,512))
backg5=Sprite(space_asset, (512,512))
backg6=Sprite(space_asset, (1024, 512))
class SpaceShip(Sprite):
    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(227,0,292-227,125), 4, 'vertical')

    def __init__(self, position):
        super().__init__(SpaceShip.asset, position)
        self.vr = 0.01
        self.thrust = 0
        self.thrustframe = 1
        self.rx = 1
        self.ry = -1
        self.rxa = 0
        self.rxb = 0
        self.rya = 0
        self.ryb = 0
        self.hm = 0
        self.vm = 0
        self.visible = True
        SpaceGame.listenKeyEvent("keydown", "space", self.thrustOn)
        SpaceGame.listenKeyEvent("keyup", "space", self.thrustOff)
        SpaceGame.listenKeyEvent("keydown", "left arrow", self.left)
        SpaceGame.listenKeyEvent("keyup", "left arrow", self.stopleft)
        SpaceGame.listenKeyEvent("keydown", "right arrow", self.right)
        SpaceGame.listenKeyEvent("keyup", "right arrow", self.stopright)
        SpaceGame.listenKeyEvent("keydown", "up arrow", self.up)
        SpaceGame.listenKeyEvent("keyup", "up arrow", self.stopup)
        SpaceGame.listenKeyEvent("keydown", "down arrow", self.down)
        SpaceGame.listenKeyEvent("keyup", "down arrow", self.stopdown)

        self.fxcenter = self.fycenter = 0.5

    def step(self):
        self.rotation = 0
        if self.thrust == 1:
            self.setImage(self.thrustframe)
            self.thrustframe += 1
            if self.thrustframe == 4:
                self.thrustframe = 1

        else:
            self.setImage(0)

        if self.rxa == 2 and self.rxb == 2:
            self.x=self.x
            self.hm = 0

        else:
            if self.rx == -5:
                self.x=self.x-10
                self.hm=1
            if self.rx == 5:
                self.x=self.x+10
                self.hm=2

        if self.rya == 2 and self.ryb == 2:
                self.y=self.y
                self.vm = 0

        else:
            if self.ry == -5:
                    self.y=self.y-10
                    self.vm = 1
            if self.ry == 5:
                    self.y=self.y+10
                    self.vm = 2
        if self.hm==0 and self.vm==0:
            self.rotation = 0
            self.thrust = 0
        else:
            self.thrust=1
            if self.hm==1:
                if self.vm==1:
                    self.rotation=(1/4)*pi
                elif self.vm==2:
                    self.rotation=(3/4)*pi
                else:
                    self.rotation=pi/2
            elif self.hm==2:
                if self.vm==1:
                    self.rotation=(7/4)*pi
                elif self.vm==2:
                    self.rotation=(5/4)*pi
                else:
                    self.rotation=(3/2)*pi
            else:
                if self.vm==1:
                    self.rotation=0
                elif self.vm==2:
                    self.rotation=pi
        collision = self.collidingWithSprites(Star)
        if len(collision) > 0:
            self.visible=False

    def thrustOn(self, event):
        self.thrust = 1
    def thrustOff(self, event):
        self.thrust = 0
    def left(self, event):
        self.rx=-5
        self.rxa=0
    def right(self, event):
        self.rx=5
        self.rxb=0
    def up(self, event):
        self.ry=-5
        self.rya=0
    def down(self, event):
        self.ry=5
        self.ryb=0
    def stopleft(self, event):
        self.rxa=2
    def stopright(self, event):
        self.rxb=2
    def stopup(self, event):
        self.ryb=2
    def stopdown(self, event):
        self.rya=2

class Star(Sprite):
    asset=ImageAsset("images/sun.png")
    height=300
    width=300
    def __init__(self, position):
        super().__init__(Star.asset, position)
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.circularCollisionModel()
class SpaceGame(App):
    def step(self):
        for ship in self.getSpritesbyClass(SpaceShip):
            ship.step()
    def __init__(self, width, height):
        super().__init__(width, height)
        SpaceShip((125,100))
        SpaceShip((175,150))
        SpaceShip((75,150))
        Star((600,600))
        Star((850,300))
myapp = SpaceGame(SCREEN_WIDTH, SCREEN_HEIGHT)
myapp.run()<|endoftext|>


<commit_before>from data_collection.management.commands import BaseXpressDemocracyClubCsvImporter

class Command(BaseXpressDemocracyClubCsvImporter):
    council_id = "E07000229"
    addresses_name = "local.2019-05-02/Version 1/Democracy_Club__02May2019worthing.tsv"
    stations_name = "local.2019-05-02/Version 1/Democracy_Club__02May2019worthing.tsv"
    elections = ["local.2019-05-02"]
    csv_delimiter = "\t"

    def address_record_to_dict(self, record):
        rec = super().address_record_to_dict(record)

        if record.addressline6 in ["BN11 2FL", "BN11 2FJ"]:
            rec["accept_suggestion"] = False

        return rec
<commit_msg>Import script for Worthing (closes #2344)<commit_after>from data_collection.management.commands import BaseXpressDemocracyClubCsvImporter

class Command(BaseXpressDemocracyClubCsvImporter):
    council_id = "E07000229"
    addresses_name = "parl.2019-12-12/Version 1/merged.tsv"
    stations_name = "parl.2019-12-12/Version 1/merged.tsv"
    elections = ["parl.2019-12-12"]
    csv_delimiter = "\t"
    allow_station_point_from_postcode = False

    def address_record_to_dict(self, record):
        rec = super().address_record_to_dict(record)

        if record.addressline6 in ["BN11 2FL", "BN11 2FJ", "BN11 1BF"]:
            rec["accept_suggestion"] = False

        return rec
<|endoftext|>