1 import os
2 import re
3 import urllib
4
6 """
7 :returns: returns an open rdflib ConjunctiveGraph
8
9 :param url: a string of the url
10 :param identifier: URIRef of the default context for writing
11 e.g.:
12
13 - create_engine('mysql://myname@localhost/rdflibdb')
14 - create_engine('sleepycat://~/working/rdf_db')
15 - create_engine('zodb:///var/rdflib/Data.fs')
16 - create_engine('zodb://localhost:8672')
17 - create_engine('sesame://www.example.com:8080/openrdf-sesame/repositories/Test')
18 - create_engine('sparql://www.example.com:2020/sparql')
19
20 for zodb:
21
22 the key in the Zope database is hardcoded as 'rdflib'
23 urls ending in `.fs` indicate FileStorage
24 otherwise ClientStoreage is assumed which requires
25 a ZEO Server to be running"""
26 if url=='' or url.startswith('IOMemory'):
27 from rdflib import ConjunctiveGraph
28 db = ConjunctiveGraph('IOMemory')
29 elif url.lower().startswith('mysql://'):
30 from rdflib import ConjunctiveGraph
31 db = ConjunctiveGraph('MySQL',identifier)
32 schema,opts = _parse_rfc1738_args(url)
33 openstr= 'db=%(database)s,host=%(host)s,user=%(username)s'%opts
34 db.open(openstr)
35 elif url.lower().startswith('sleepycat://'):
36 from rdflib import ConjunctiveGraph
37 db = ConjunctiveGraph('Sleepycat',identifier=identifier)
38 openstr = os.path.abspath(os.path.expanduser(url[12:]))
39 db.open(openstr,create=create)
40 elif url.lower().startswith('sqlite://'):
41 from rdflib import ConjunctiveGraph
42 db = ConjunctiveGraph('SQLite',identifier=identifier)
43 openstr = os.path.abspath(os.path.expanduser(url[9:]))
44 db.open(openstr,create=create)
45 elif url.lower().startswith('zodb://'):
46 import ZODB
47 import transaction
48 db = ConjunctiveGraph('ZODB')
49 if url.endswith('.fs'):
50 from ZODB.FileStorage import FileStorage
51 openstr = os.path.abspath(os.path.expanduser(url[7:]))
52 if not os.path.exists(openstr) and not create:
53 raise "File not found: %s"%openstr
54 fs=FileStorage(openstr)
55 else:
56 from ZEO.ClientStorage import ClientStorage
57 schema,opts = _parse_rfc1738_args(url)
58 fs=ClientStorage((opts['host'],int(opts['port'])))
59
60 zdb=ZODB.DB(fs)
61
62 conn=zdb.open()
63
64 root=conn.root()
65
66 if 'rdflib' not in root and create:
67 root['rdflib'] = ConjunctiveGraph('ZODB')
68 db=root['rdflib']
69 elif url.lower().startswith('sesame://'):
70 from rdfalchemy.sparql.sesame2 import SesameGraph
71 db = SesameGraph("http://"+url[9:])
72 elif url.lower().startswith('sparql://'):
73 from rdfalchemy.sparql import SPARQLGraph
74 db = SPARQLGraph("http://"+url[9:])
75 else:
76 raise "Could not parse string '%s'" % url
77 return db
78
80 """Create a new Engine instance using a configuration dictionary.
81
82 :param configuration: a dictionary, typically produced from a config file
83 where keys are prefixed, such as `rdfalchemy.dburi`, etc.
84 :param prefix: indicates the prefix to be searched for.
85
86 """
87
88 options = dict([(key[len(prefix):], configuration[key])
89 for key in configuration
90 if key.startswith(prefix)])
91
92 options.update(kwargs)
93 url = options.pop('dburi')
94 return create_engine(url, **options)
95
96
98 """ parse url str into options
99 code orig from sqlalchemy.engine.url """
100 pattern = re.compile(r'''
101 (\w+)://
102 (?:
103 ([^:/]*)
104 (?::([^/]*))?
105 @)?
106 (?:
107 ([^/:]*)
108 (?::([^/]*))?
109 )?
110 (?:/(.*))?
111 '''
112 , re.X)
113
114 m = pattern.match(name)
115 if m is not None:
116 (name, username, password, host, port, database) = m.group(1, 2, 3, 4, 5, 6)
117 if database is not None:
118 tokens = database.split(r"?", 2)
119 database = tokens[0]
120 query = (len(tokens) > 1 and dict( cgi.parse_qsl(tokens[1]) ) or None)
121 if query is not None:
122 query = dict([(k.encode('ascii'), query[k]) for k in query])
123 else:
124 query = None
125 opts = {'username':username,'password':password,'host':host,'port':port,'database':database, 'query':query}
126 if opts['password'] is not None:
127 opts['password'] = urllib.unquote_plus(opts['password'])
128 return (name, opts)
129 else:
130 raise exceptions.ValueError("Could not parse rfc1738 URL from string '%s'" % name)
131