def cast_graphpath(value, cur):
    if value is None:
        return None

    tokens = []

    # ignore wrapping '[' and ']' characters
    pos = 1
    length = len(value) - 1

    start = pos
    depth = 0
    gid = False

    while pos < length:
        c = value[pos]
        if c == '"':
            if depth > 0:
                # Parse "string".
                # Leave pos unchanged if unmatched right " were found.

                escape = False
                i = pos + 1

                while i < length:
                    c = value[i]
                    if c == '\\':
                        escape = not escape
                    elif c == '"':
                        if escape:
                            escape = False
                        else:
                            pos = i
                            break
                    else:
                        escape = False

                    i += 1
        elif c == '[' and depth == 0:
            gid = True
        elif c == ']' and depth == 0:
            gid = False
        elif c == '{':
            depth += 1
        elif c == '}':
            depth -= 1
            if depth < 0:
                raise InterfaceError("bad graphpath representation: %s" %
                                     value)
        elif c == ',' and depth == 0 and not gid:
            tokens.append(value[start:pos])
            start = pos + 1

        pos += 1

    tokens.append(value[start:pos])

    vertices = [cast_vertex(t, cur) for t in tokens[0::2]]
    edges = [cast_edge(t, cur) for t in tokens[1::2]]
    return Path(vertices, edges)
Beispiel #2
0
def cast_point(value, cur):
    if value is None:
        return None

    # Convert from (f1, f2) syntax using a regular expression.
    m = re.match(r"\(([^)]+),([^)]+)\)", value)
    if m:
        return Point(float(m.group(1)), float(m.group(2)))
    else:
        raise InterfaceError("bad point representation: %r" % value)
Beispiel #3
0
def cast_graphid(value, cur):
    if value is None:
        return None

    m = _pattern.match(value)
    if m:
        labid = int(m.group(1))
        locid = int(m.group(2))
        gid = (labid, locid)
        return GraphId(gid)
    else:
        raise InterfaceError("bad graphid representation: %s" % value)
def cast_vertex(value, cur):
    if value is None:
        return None

    m = _pattern.match(value)
    if m:
        label = m.group(1)
        vid = cast_graphid(m.group(2), cur)
        props = json.loads(m.group(3))
        return Vertex(label, vid, props)
    else:
        raise InterfaceError("bad vertex representation: %s" % value)
def cast_edge(value, cur):
    if value is None:
        return None

    m = _pattern.match(value)
    if m:
        label = m.group(1)
        eid = cast_graphid(m.group(2), cur)
        start = cast_graphid(m.group(3), cur)
        end = cast_graphid(m.group(4), cur)
        props = json.loads(m.group(5))
        return Edge(label, eid, start, end, props)
    else:
        raise InterfaceError("bad edge representation: %s" % value)
Beispiel #6
0
def cast_level(value, cur):  # pylint: disable=unused-argument
    """Generic Level caster for psycopg2"""
    if not InterfaceError:
        raise ImportError('There was a problem importing psycopg2.')
    if value is None:
        return None

    m = re.match(r"([^\()]*)\((\d*)\)", value)
    name = str(m.group(1))
    number = int(m.group(2))

    if m:
        return level(name, number)
    else:
        raise InterfaceError("Bad level representation: {}".format(value))
Beispiel #7
0
    async def get_notify(self):
        """ Remove and return a psycopg2
        :py:class:`Notify <psycopg2.extensions.Notify>` object from the Notify
        queue. If the queue is empty, wait until an item is available.

        The :py:attr:`psycopg2:connection.notifies` attribute is replaced by
        the :py:class:`psycaio.AioConnMixin` with a custom version that plays
        nicely with asyncio. Do not set it to anything else or this method will
        probably break.

        Example:

        .. code-block:: python

            async def test_notify(cn1, cn2):
                cr1 = cn1.cursor()
                await cr1.execute("LISTEN queue")

                cr2 = cn2.cursor()
                await cr2.execute("NOTIFY queue, 'hi'")

                notify = await cn1.get_notify()
                print(notify.payload)

        For more information see the PostgreSQL docs on
        `LISTEN <https://www.postgresql.org/docs/current/sql-listen.html>`_.
        """
        try:
            return self.get_notify_nowait()
        except QueueEmpty:
            pass

        # nothing in the Queue. Start reading until we got one
        if self.closed:
            raise InterfaceError("connection already closed")
        if self._thread_manager is not None:
            with self._selector_thread() as tm:
                tm.call(self._start_reading, self._poll)
                try:
                    return await self.notifies._pop()
                finally:
                    tm.call(self._stop_reading)
        else:
            self._start_reading(self._poll)
            try:
                return await self.notifies._pop()
            finally:
                self._stop_reading()
class CommonErrorStrategyTestCase(TestCase):
    exception_to_vaue_dict = {
        InterfaceError(): (True, TERMINATE),
        DatabaseError(choice(CommonErrorStrategy.BACKOFFABLE_MESSAGE)):
        (True, BACKOFF),
        OperationalError(): (True, TERMINATE),
        Exception(): (False, TERMINATE)
    }

    def setUp(self):
        self.strat = CommonErrorStrategy()

    def test_strategy_returns_correct_values(self):
        for exception, value in self.exception_to_vaue_dict.iteritems():
            return_value = self.strat.handle_exception(exception)
            self.assertEqual(return_value, value)
Beispiel #9
0
class ScriptTestCase(object):
    """Common tests for all scripts."""

    script_args = []
    script_class = None

    @patch('drop_parts.sys.stderr', new=StringIO())
    @patch('create_parts.sys.stderr', new=StringIO())
    def test_init_fail(self):
        """Test init with wrong arguments."""
        with self.assertRaises(SystemExit):
            self.script_class([])
        self.assertRegex(sys.stderr.getvalue().strip(), "^usage: ")
        with self.assertRaises(FatalScriptError):
            with self.assertRaises(ValueError):
                self.script_class(self.script_args + ["-d", "0000-00-00"])

    def test_init_ok(self):
        """Test standard init."""
        self.script_class(self.script_args)

    @patch('builtins.open', mock.mock_open(read_data='foo\nbar\nbaz\n'))
    def test_config_wrong_json(self):
        """Test wrong format of configuration file."""
        with self.assertRaises(FatalScriptError) as err:
            script = self.script_class(self.script_args)
            script.read_config()
        self.assertEqual(type(err.exception.error), json.JSONDecodeError)

    @patch('builtins.open',
           mock.mock_open(
               read_data='{"database": {"host": "host", "user": "******"}}'))
    def test_config_missing_items(self):
        """Test configuration file in right format, but with mandatory items missing."""
        with self.assertRaises(FatalScriptError) as err:
            script = self.script_class(self.script_args)
            script.read_config()
        self.assertEqual(type(err.exception.error), ConfigError)

    @patch(
        'builtins.open',
        mock.mock_open(
            read_data=
            '{"database": {"host": "myhost", "user": "******", "database": "db"}}'
        ))
    def test_config_ok(self):
        """Test that configuration loaded properly."""
        script = self.script_class(
            self.script_args +
            ["--from-date", "2017-06", "--to-date", "2015-01"])
        script.read_config()
        self.assertEqual(script.args.date_from, date(2017, 6, 1))
        self.assertEqual(script.args.date_to, date(2017, 6, 1))
        self.assertEqual(script.config["database"]["host"], "myhost")

    @patch('psycopg2.connect')
    @patch(
        'builtins.open',
        mock.mock_open(
            read_data=
            '{"database": {"host": "myhost", "user": "******", "database": "db"}}'
        ))
    def test_db_ok(self, mock_connect):
        """Test OK connection to database."""
        script = self.script_class(self.script_args)
        script.read_config()
        script.connect_db()

    @patch('psycopg2.connect', side_effect=InterfaceError('if_error'))
    @patch(
        'builtins.open',
        mock.mock_open(
            read_data=
            '{"database": {"host": "myhost", "user": "******", "database": "db"}}'
        ))
    def test_db_error(self, mock_connect):
        """Test error while connecting to database."""
        script = self.script_class(self.script_args)
        script.read_config()
        with self.assertRaises(FatalScriptError) as err:
            script.connect_db()
        self.assertEqual(type(err.exception.error), InterfaceError)
Beispiel #10
0
 def error(self, msg):
     raise InterfaceError(msg)
Beispiel #11
0
 def clear(self):
     getters = self._queue._getters
     while getters:
         getter = getters.popleft()
         if not getter.done():
             getter.set_exception(InterfaceError("connection is closed"))