Merge remote-tracking branch 'origin/encounters-i18n'
[zzz-pokedex.git] / pokedex / db / __init__.py
1 # encoding: utf-8
2 import re
3
4 from sqlalchemy import engine_from_config, orm
5
6 from ..defaults import get_default_db_uri
7 from .tables import Language, metadata
8 from .multilang import MultilangSession, MultilangScopedSession
9
10
11 def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''):
12 """Connects to the requested URI. Returns a session object.
13
14 With the URI omitted, attempts to connect to a default SQLite database
15 contained within the package directory.
16
17 Calling this function also binds the metadata object to the created engine.
18 """
19
20 # If we didn't get a uri, fall back to the default
21 if uri is None:
22 uri = engine_args.get(engine_prefix + 'url', None)
23 if uri is None:
24 uri = get_default_db_uri()
25
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'
32
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'
38
39 ### Connect
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
44
45 all_session_args = dict(autoflush=True, autocommit=False, bind=engine)
46 all_session_args.update(session_args)
47 sm = orm.sessionmaker(class_=MultilangSession, language_class=Language,
48 **all_session_args)
49 session = MultilangScopedSession(sm)
50
51 # Default to English. Warning, magic constant, blah blah. Trying to fetch
52 # English here would kinda break on new databases. TODO still not an ideal
53 # solution, I guess.
54 session._default_language_id = 9
55
56 return session
57
58 def identifier_from_name(name):
59 """Make a string safe to use as an identifier.
60
61 Valid characters are lowercase alphanumerics and "-". This function may
62 raise ValueError if it can't come up with a suitable identifier.
63
64 This function is useful for scripts which add things with names.
65 """
66 if isinstance(name, str):
67 identifier = name.decode('utf-8')
68 else:
69 identifier = name
70 identifier = identifier.lower()
71 identifier = identifier.replace(u'+', u' plus ')
72 identifier = re.sub(u'[ _–]+', u'-', identifier)
73 identifier = re.sub(u"['./;’(),:]", u'', identifier)
74 identifier = identifier.replace(u'é', u'e')
75 identifier = identifier.replace(u'♀', u'-f')
76 identifier = identifier.replace(u'♂', u'-m')
77 if identifier in (u'???', u'????'):
78 identifier = u'unknown'
79 elif identifier == u'!':
80 identifier = u'exclamation'
81 elif identifier == u'?':
82 identifier = u'question'
83
84 if not identifier.replace(u"-", u"").isalnum():
85 raise ValueError(identifier)
86 return identifier