4 from sqlalchemy
import MetaData
, Table
, engine_from_config
, orm
6 from ..defaults
import get_default_db_uri
7 from .tables
import metadata
8 from .multilang
import MultilangSession
, MultilangScopedSession
11 def connect(uri
=None, session_args
={}, engine_args
={}, engine_prefix
=''):
12 """Connects to the requested URI. Returns a session object.
14 With the URI omitted, attempts to connect to a default SQLite database
15 contained within the package directory.
17 Calling this function also binds the metadata object to the created engine.
20 # If we didn't get a uri, fall back to the default
22 uri
= engine_args
.get(engine_prefix
+ 'url', None)
24 uri
= get_default_db_uri()
26 ### Do some fixery for MySQL
27 if uri
.startswith('mysql:'):
28 # MySQL uses latin1 for connections by default even if the server is
29 # otherwise oozing with utf8; charset fixes this
30 if 'charset' not in uri
:
31 uri
+= '?charset=utf8'
33 # Tables should be InnoDB, in the event that we're creating them, and
34 # use UTF-8 goddammit!
35 for table
in metadata
.tables
.values():
36 table
.kwargs
['mysql_engine'] = 'InnoDB'
37 table
.kwargs
['mysql_charset'] = 'utf8'
40 engine_args
[engine_prefix
+ 'url'] = uri
41 engine
= engine_from_config(engine_args
, prefix
=engine_prefix
)
42 conn
= engine
.connect()
43 metadata
.bind
= engine
45 all_session_args
= dict(autoflush
=True, autocommit
=False, bind
=engine
)
46 all_session_args
.update(session_args
)
47 sm
= orm
.sessionmaker(class_
=MultilangSession
, **all_session_args
)
48 session
= MultilangScopedSession(sm
)
52 def identifier_from_name(name
):
53 """Make a string safe to use as an identifier.
55 Valid characters are lowercase alphanumerics and "-". This function may
56 raise ValueError if it can't come up with a suitable identifier.
58 This function is useful for scripts which add things with names.
60 if isinstance(name
, str):
61 identifier
= name
.decode('utf-8')
64 identifier
= identifier
.lower()
65 identifier
= identifier
.replace(u
'+', u
' plus ')
66 identifier
= re
.sub(u
'[ _–]+', u
'-', identifier
)
67 identifier
= re
.sub(u
"['./;’(),:]", u
'', identifier
)
68 identifier
= identifier
.replace(u
'é', u
'e')
69 identifier
= identifier
.replace(u
'♀', u
'-f')
70 identifier
= identifier
.replace(u
'♂', u
'-m')
71 if identifier
in (u
'???', u
'????'):
72 identifier
= u
'unknown'
73 elif identifier
== u
'!':
74 identifier
= u
'exclamation'
75 elif identifier
== u
'?':
76 identifier
= u
'question'
78 if not identifier
.replace(u
"-", u
"").isalnum():
79 raise ValueError(identifier
)