def parse_routing_info(cls, records): """ Parse the records returned from a getServers call and return a new RoutingTable instance. """ if len(records) != 1: raise RoutingProtocolError("Expected exactly one record") record = records[0] routers = [] readers = [] writers = [] try: servers = record["servers"] for server in servers: role = server["role"] addresses = [] for address in server["addresses"]: addresses.append(SocketAddress.parse( address, DEFAULT_PORT)) if role == "ROUTE": routers.extend(addresses) elif role == "READ": readers.extend(addresses) elif role == "WRITE": writers.extend(addresses) ttl = record["ttl"] except (KeyError, TypeError): raise RoutingProtocolError("Cannot parse routing info") else: return cls(routers, readers, writers, ttl)
def main(): parser = ArgumentParser(description="Execute one or more Cypher statements using Bolt.") parser.add_argument("statement", nargs="+") parser.add_argument("-a", "--address", default="localhost:7687", metavar="ADDRESS") parser.add_argument("-k", "--keys", action="store_true") parser.add_argument("-P", "--password") parser.add_argument("-p", "--parameter", action="append", metavar="NAME=VALUE") parser.add_argument("-q", "--quiet", action="store_true") parser.add_argument("-s", "--secure", action="store_true") parser.add_argument("-U", "--user", default="neo4j") parser.add_argument("-v", "--verbose", action="count") parser.add_argument("-x", "--times", type=int, default=1) parser.add_argument("-z", "--summary", action="store_true") args = parser.parse_args() if args.verbose: Watcher("neobolt").watch(logging.DEBUG, stderr) parameters = {} for parameter in args.parameter or []: name, _, value = parameter.partition("=") if value == "" and name in parameters: del parameters[name] else: try: parameters[name] = json_loads(value) except ValueError: parameters[name] = value cx = connect(SocketAddress.parse(args.address, DEFAULT_PORT), auth=(args.user, args.password), encrypted=args.secure) try: for _ in range(args.times): for statement in args.statement: metadata = {} records = [] try: cx.run(statement, parameters, on_success=metadata.update) cx.pull_all(on_records=records.extend, on_success=metadata.update) cx.sync() except CypherError as error: stderr.write("%s: %s\r\n" % (error.code, error.message)) else: if not args.quiet: if args.keys: stdout.write("%s\r\n" % "\t".join(metadata.get("fields", ()))) for i, record in enumerate(records): stdout.write("%s\r\n" % "\t".join(map(repr, record))) if args.summary: for key, value in sorted(metadata): stdout.write("{}: {}\r\n".format(key, value)) stdout.write("\r\n") finally: cx.close()
def test_should_fail_on_non_numeric_port(self): with self.assertRaises(ValueError): _ = SocketAddress.parse("127.0.0.1:X")
def test_should_parse_host_name_and_port(self): parsed = SocketAddress.parse("localhost:7687") self.assertEqual(parsed, ("localhost", 7687))
def test_should_parse_ipv6_address_and_port(self): parsed = SocketAddress.parse("[::1]:7687") self.assertEqual(parsed, ("::1", 7687, 0, 0))
def test_should_parse_ipv4_address_and_port(self): parsed = SocketAddress.parse("127.0.0.1:7687") self.assertEqual(parsed, ("127.0.0.1", 7687))