| 1 |
"""Backend providers for Geniusql.""" |
|---|
| 2 |
|
|---|
| 3 |
import re |
|---|
| 4 |
from geniusql import xray |
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
class Version(object): |
|---|
| 8 |
|
|---|
| 9 |
def __init__(self, atoms): |
|---|
| 10 |
if isinstance(atoms, (int, float)): |
|---|
| 11 |
atoms = str(atoms) |
|---|
| 12 |
if isinstance(atoms, basestring): |
|---|
| 13 |
self.atoms = re.split(r'\W', atoms) |
|---|
| 14 |
else: |
|---|
| 15 |
self.atoms = [str(x) for x in atoms] |
|---|
| 16 |
|
|---|
| 17 |
def __str__(self): |
|---|
| 18 |
return ".".join([str(x) for x in self.atoms]) |
|---|
| 19 |
|
|---|
| 20 |
def __cmp__(self, other): |
|---|
| 21 |
cls = self.__class__ |
|---|
| 22 |
if not isinstance(other, cls): |
|---|
| 23 |
|
|---|
| 24 |
other = cls(other) |
|---|
| 25 |
|
|---|
| 26 |
index = 0 |
|---|
| 27 |
while index < len(self.atoms) and index < len(other.atoms): |
|---|
| 28 |
mine, theirs = self.atoms[index], other.atoms[index] |
|---|
| 29 |
if mine.isdigit() and theirs.isdigit(): |
|---|
| 30 |
mine, theirs = int(mine), int(theirs) |
|---|
| 31 |
if mine < theirs: |
|---|
| 32 |
return -1 |
|---|
| 33 |
if mine > theirs: |
|---|
| 34 |
return 1 |
|---|
| 35 |
index += 1 |
|---|
| 36 |
if index < len(other.atoms): |
|---|
| 37 |
return -1 |
|---|
| 38 |
if index < len(self.atoms): |
|---|
| 39 |
return 1 |
|---|
| 40 |
return 0 |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
class _Registry(dict): |
|---|
| 44 |
|
|---|
| 45 |
def open(self, key, **kwargs): |
|---|
| 46 |
opener = self[key] |
|---|
| 47 |
if isinstance(opener, basestring): |
|---|
| 48 |
opener = xray.attributes(opener) |
|---|
| 49 |
return opener(**kwargs) |
|---|
| 50 |
|
|---|
| 51 |
registry = _Registry({ |
|---|
| 52 |
"mysql": "geniusql.providers.mysql.MySQLDatabase", |
|---|
| 53 |
|
|---|
| 54 |
"psycopg": "geniusql.providers.psycopg.PsycoPgDatabase", |
|---|
| 55 |
"psycopg2": "geniusql.providers.psycopg.PsycoPgDatabase", |
|---|
| 56 |
|
|---|
| 57 |
"sqlite": "geniusql.providers.sqlite.SQLiteDatabase", |
|---|
| 58 |
|
|---|
| 59 |
}) |
|---|