Beispiel #1
0
 def connect(self, user=None, password=None, url=None):
     
     connectionResult = False
     
     httpProtocol = Neo4jDBInterface.NON_SECURE_PROTOCOL
     targetUrl = Neo4jDBInterface.DEFAULT_NON_SECURE_URL
     
     #Check for the URL first
     if url is not None:
         targetUrl = url
     
     credentialsData = ""
     #Check for the credentials then
     if user is not None and len(user) > 0 and password is not None and len(password) > 0:
         #
         credentialsData = user+":"+password+"@"
         httpProtocol = Neo4jDBInterface.SECURE_PROTOCOL
         if url is None:
             #Change the default port for secure connections
             targetUrl = Neo4jDBInterface.DEFAULT_SECURE_URL
             
         #Perform the authentication first
         authenticate(targetUrl,user,password)
         
     
     #Construct the final URL for connection
     connectionString = httpProtocol+credentialsData+targetUrl
     
     self.targetGraph = Graph(connectionString)
     
     #Update the connection state of the instance
     self.isDBConnected = True
     connectionResult = True
   
     return connectionResult    
Beispiel #2
0
def test_authentication_adds_the_correct_header():
    from py2neo.core import _headers
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    authenticate("localhost:7474", "arthur", "excalibur")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='
Beispiel #3
0
def test_authentication_adds_the_correct_header():
    from py2neo.core import _headers
    _headers.clear()
    _headers.update({None: [("X-Stream", "true")]})
    authenticate("localhost:7474", "arthur", "excalibur")
    headers = _get_headers("localhost:7474")
    assert headers['Authorization'] == 'Basic YXJ0aHVyOmV4Y2FsaWJ1cg=='
Beispiel #4
0
 def _graph(self):
     host_port = "{0}:{1}".format(self._host, self._port)
     uri = "{0}://{1}".format(self._scheme, host_port)
     if self._user and self._password:
         authenticate(host_port, self._user, self._password)
     return ServiceRoot(uri).graph
Beispiel #5
0
def main():
    import sys
    from py2neo.core import ServiceRoot
    script, args = sys.argv[0], sys.argv[1:]
    if not args:
        _help(script)
        sys.exit(0)
    uri = NEO4J_URI.resolve("/")
    service_root = ServiceRoot(uri.string)
    out = sys.stdout
    output_format = None
    command_line = CypherCommandLine(service_root.graph)
    while args:
        arg = args.pop(0)
        if arg.startswith("-"):
            if arg in ("-?", "--help"):
                _help(script)
                sys.exit(0)
            elif arg in ("-A", "--auth"):
                user_name, password = args.pop(0).partition(":")[0::2]
                authenticate(service_root.uri.host_port, user_name, password)
            elif arg in ("-p", "--parameter"):
                key = args.pop(0)
                value = args.pop(0)
                command_line.set_parameter(key, value)
            elif arg in ("-f",):
                command_line.set_parameter_filename(args.pop(0))
            elif arg in ("-h", "--human"):
                output_format = None
            elif arg in ("-j", "--json"):
                output_format = "json"
            elif arg in ("-c", "--csv"):
                output_format = "csv"
            elif arg in ("-t", "--tsv"):
                output_format = "tsv"
            else:
                raise ValueError("Unrecognised option %s" % arg)
        else:
            if not command_line.tx:
                command_line.begin()
            try:
                results = command_line.execute(arg)
            except CypherError as error:
                sys.stderr.write("%s: %s\n\n" % (error.__class__.__name__, error.args[0]))
            else:
                for result in results:
                    if output_format == "json":
                        records = [compact(dict(zip(result.columns, map(dehydrate, record))))
                                   for record in result]
                        out.write(json.dumps(records, ensure_ascii=False, sort_keys=True, indent=2))
                        out.write("\n")
                    elif output_format in ("csv", "tsv"):
                        separator = "," if output_format == "csv" else "\t"
                        out.write(separator.join(json.dumps(column, separators=",:",
                                                            ensure_ascii=False)
                                                 for column in result.columns))
                        out.write("\n")
                        for record in result:
                            out.write(separator.join(json.dumps(dehydrate(value),
                                                                separators=",:",
                                                                ensure_ascii=False,
                                                                sort_keys=True)
                                                     for value in record))
                            out.write("\n")
                    else:
                        out.write(repr(result))
                    out.write("\n")
    if command_line.tx:
        try:
            command_line.commit()
        except CypherTransactionError as error:
            sys.stderr.write(error.args[0])
            sys.stderr.write("\n")
Beispiel #6
0
def main():
    import sys
    from py2neo.core import ServiceRoot
    script, args = sys.argv[0], sys.argv[1:]
    if not args:
        _help(script)
        sys.exit(0)
    uri = NEO4J_URI.resolve("/")
    service_root = ServiceRoot(uri.string)
    out = sys.stdout
    output_format = None
    command_line = CypherCommandLine(service_root.graph)
    while args:
        arg = args.pop(0)
        if arg.startswith("-"):
            if arg in ("-?", "--help"):
                _help(script)
                sys.exit(0)
            elif arg in ("-A", "--auth"):
                user_name, password = args.pop(0).partition(":")[0::2]
                authenticate(service_root.uri.host_port, user_name, password)
            elif arg in ("-p", "--parameter"):
                key = args.pop(0)
                value = args.pop(0)
                command_line.set_parameter(key, value)
            elif arg in ("-f", ):
                command_line.set_parameter_filename(args.pop(0))
            elif arg in ("-h", "--human"):
                output_format = None
            elif arg in ("-j", "--json"):
                output_format = "json"
            elif arg in ("-c", "--csv"):
                output_format = "csv"
            elif arg in ("-t", "--tsv"):
                output_format = "tsv"
            else:
                raise ValueError("Unrecognised option %s" % arg)
        else:
            if not command_line.tx:
                command_line.begin()
            try:
                results = command_line.execute(arg)
            except CypherError as error:
                sys.stderr.write("%s: %s\n\n" %
                                 (error.__class__.__name__, error.args[0]))
            else:
                for result in results:
                    if output_format == "json":
                        records = [
                            compact(
                                dict(
                                    zip(result.columns, map(dehydrate,
                                                            record))))
                            for record in result
                        ]
                        out.write(
                            json.dumps(records,
                                       ensure_ascii=False,
                                       sort_keys=True,
                                       indent=2))
                        out.write("\n")
                    elif output_format in ("csv", "tsv"):
                        separator = "," if output_format == "csv" else "\t"
                        out.write(
                            separator.join(
                                json.dumps(column,
                                           separators=",:",
                                           ensure_ascii=False)
                                for column in result.columns))
                        out.write("\n")
                        for record in result:
                            out.write(
                                separator.join(
                                    json.dumps(dehydrate(value),
                                               separators=",:",
                                               ensure_ascii=False,
                                               sort_keys=True)
                                    for value in record))
                            out.write("\n")
                    else:
                        out.write(repr(result))
                    out.write("\n")
    if command_line.tx:
        try:
            command_line.commit()
        except CypherTransactionError as error:
            sys.stderr.write(error.args[0])
            sys.stderr.write("\n")
Beispiel #7
0
 def _graph(self):
     host_port = "{0}:{1}".format(self._host, self._port)
     uri = "{0}://{1}".format(self._scheme, host_port)
     if self._user and self._password:
         authenticate(host_port, self._user, self._password)
     return ServiceRoot(uri).graph