root / rdfalchemy / trunk / rdfalchemy / engine / __init__.py

Revision 142, 5.0 kB (checked in by phil, 5 months ago)

to close ticket: 29

Line 
1import os
2import re
3import urllib
4
5def create_engine(url='', identifier="", create=False):
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        if opts.get('password'):
35            openstr += ',password=%(password)s' % opts
36        if opts.get('port'):
37            openstr += ',port=%(port)s' % opts
38        db.open(openstr)
39    elif url.lower().startswith('sleepycat://'):
40        from rdflib import ConjunctiveGraph
41        db = ConjunctiveGraph('Sleepycat',identifier=identifier)
42        openstr = os.path.abspath(os.path.expanduser(url[12:]))
43        db.open(openstr,create=create)
44    elif url.lower().startswith('sqlite://'):
45        from rdflib import ConjunctiveGraph
46        db = ConjunctiveGraph('SQLite',identifier=identifier)
47        openstr = os.path.abspath(os.path.expanduser(url[9:]))
48        db.open(openstr,create=create)
49    elif url.lower().startswith('zodb://'):
50        import ZODB
51        import transaction
52        from rdflib import ConjunctiveGraph 
53        db = ConjunctiveGraph('ZODB')
54        if url.endswith('.fs'):
55            from ZODB.FileStorage import FileStorage
56            openstr = os.path.abspath(os.path.expanduser(url[7:]))
57            if not os.path.exists(openstr) and not create:
58                raise "File not found: %s"%openstr
59            fs=FileStorage(openstr)
60        else:
61            from ZEO.ClientStorage import ClientStorage
62            schema,opts = _parse_rfc1738_args(url)
63            fs=ClientStorage((opts['host'],int(opts['port'])))
64        # get the Zope Database
65        zdb=ZODB.DB(fs)
66        # open it
67        conn=zdb.open()
68        #get the root
69        root=conn.root()
70        # get the Conjunctive Graph
71        if 'rdflib' not in root and create:
72            root['rdflib'] = ConjunctiveGraph('ZODB')
73        db=root['rdflib']
74    elif url.lower().startswith('sesame://'):
75        from rdfalchemy.sparql.sesame2 import SesameGraph
76        db = SesameGraph("http://"+url[9:])
77    elif url.lower().startswith('sparql://'):
78        from rdfalchemy.sparql import SPARQLGraph
79        db = SPARQLGraph("http://"+url[9:])
80    else:
81        raise "Could not parse  string '%s'" % url
82    return db
83   
84def engine_from_config(configuration, prefix='rdfalchemy.', **kwargs):
85    """Create a new Engine instance using a configuration dictionary.
86   
87    :param configuration: a dictionary, typically produced from a config file
88    where keys are prefixed, such as `rdfalchemy.dburi`, etc. 
89    :param prefix: indicates the prefix to be searched for.
90   
91    """
92
93    options = dict([(key[len(prefix):], configuration[key])
94                 for key in configuration 
95                  if key.startswith(prefix)])
96
97    options.update(kwargs)
98    url = options.pop('dburi')
99    return create_engine(url, **options)
100
101   
102def _parse_rfc1738_args(name):
103    """ parse url str into options
104    code orig from sqlalchemy.engine.url """
105    pattern = re.compile(r'''
106            (\w+)://
107            (?:
108                ([^:/]*)
109                (?::([^/]*))?
110            @)?
111            (?:
112                ([^/:]*)
113                (?::([^/]*))?
114            )?
115            (?:/(.*))?
116            '''
117            , re.X)
118
119    m = pattern.match(name)
120    if m is not None:
121        (name, username, password, host, port, database) = m.group(1, 2, 3, 4, 5, 6)
122        if database is not None:
123            tokens = database.split(r"?", 2)
124            database = tokens[0]
125            query = (len(tokens) > 1 and dict( cgi.parse_qsl(tokens[1]) ) or None)
126            if query is not None:
127                query = dict([(k.encode('ascii'), query[k]) for k in query])
128        else:
129            query = None
130        opts = {'username':username,'password':password,'host':host,'port':port,'database':database, 'query':query}
131        if opts['password'] is not None:
132            opts['password'] = urllib.unquote_plus(opts['password'])
133        return (name, opts)
134    else:
135        raise exceptions.ValueError("Could not parse rfc1738 URL from string '%s'" % name)
Note: See TracBrowser for help on using the browser.