Browse Source

Nettoyage et mise en conformité PEP 8 de sqlutils.py

Roland Haroutiounian 9 years ago
parent
commit
b774d33fae
1 changed files with 32 additions and 26 deletions
  1. 32
    26
      Database/sqlutils.py

+ 32
- 26
Database/sqlutils.py View File

9
 
9
 
10
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
10
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Lodel.settings")
11
 
11
 
12
-ENGINES = {'mysql': {
13
-                'driver': 'pymysql',
14
-                'encoding': 'utf8'
15
-            },
16
-            'postgresql': {
17
-                'driver': 'psycopg2',
18
-                'encoding': 'utf8',
19
-            },
20
-            'sqlite': {
21
-                'driver': 'pysqlite',
22
-                'encoding': 'utf8',
23
-            },
12
+ENGINES = {
13
+    'mysql': {
14
+        'driver': 'pymysql',
15
+        'encoding': 'utf8'
16
+    },
17
+    'postgresql': {
18
+        'driver': 'psycopg2',
19
+        'encoding': 'utf8',
20
+    },
21
+    'sqlite': {
22
+        'driver': 'pysqlite',
23
+        'encoding': 'utf8',
24
+    },
24
 }
25
 }
25
 
26
 
26
-sqlcfg = settings.LODEL2SQLWRAPPER
27
+sql_config = settings.LODEL2SQLWRAPPER
27
 
28
 
28
 
29
 
29
 ## Returns an engine given dbconf name
30
 ## Returns an engine given dbconf name
31
 # @param ename str: name of an item in django.conf.settings.LODEL2SQLWRAPPER['db']
32
 # @param ename str: name of an item in django.conf.settings.LODEL2SQLWRAPPER['db']
32
 # @param sqlaloggin None|bool: If None leave default value, if true activate sqlalchemy logging
33
 # @param sqlaloggin None|bool: If None leave default value, if true activate sqlalchemy logging
33
 # @return SqlAlchemy engine
34
 # @return SqlAlchemy engine
34
-def get_engine(ename='default',sqlalogging=None):
35
+def get_engine(ename='default', sqlalogging=None):
35
     # Loading confs
36
     # Loading confs
36
-    cfg = sqlcfg['db'][ename]
37
+    cfg = sql_config['db'][ename]
37
 
38
 
38
-    edata = ENGINES[cfg['ENGINE']] # engine infos
39
+    edata = ENGINES[cfg['ENGINE']]  # engine infos
39
     conn_str = ""
40
     conn_str = ""
40
 
41
 
41
-    if cfg['ENGINE'] =='sqlite':
42
+    if cfg['ENGINE'] == 'sqlite':
42
         #SQLite connection string
43
         #SQLite connection string
43
         conn_str = '%s+%s:///%s' % (cfg['ENGINE'], edata['driver'], cfg['NAME'])
44
         conn_str = '%s+%s:///%s' % (cfg['ENGINE'], edata['driver'], cfg['NAME'])
44
     else:
45
     else:
45
         #MySQL and PostgreSQL connection string
46
         #MySQL and PostgreSQL connection string
46
         user = cfg['USER']
47
         user = cfg['USER']
47
-        user += (':'+cfg['PASSWORD'] if 'PASSWORD' in cfg else '')
48
+        user += (':' + cfg['PASSWORD'] if 'PASSWORD' in cfg else '')
48
 
49
 
49
         if 'HOST' not in cfg:
50
         if 'HOST' not in cfg:
50
             logger.info('Not HOST in configuration, using localhost')
51
             logger.info('Not HOST in configuration, using localhost')
52
         else:
53
         else:
53
             host = cfg['HOST']
54
             host = cfg['HOST']
54
 
55
 
55
-        host += (':'+cfg['PORT'] if 'PORT' in cfg else '')
56
+        host += (':' + cfg['PORT'] if 'PORT' in cfg else '')
56
         conn_str = '%s+%s://' % (cfg['ENGINE'], edata['driver'])
57
         conn_str = '%s+%s://' % (cfg['ENGINE'], edata['driver'])
57
         conn_str += '%s@%s/%s' % (user, host, cfg['NAME'])
58
         conn_str += '%s@%s/%s' % (user, host, cfg['NAME'])
58
 
59
 
59
     ret = sqla.create_engine(conn_str, encoding=edata['encoding'], echo=sqlalogging)
60
     ret = sqla.create_engine(conn_str, encoding=edata['encoding'], echo=sqlalogging)
60
-    logger.debug("Getting engine :"+str(ret))
61
+    logger.debug("Getting engine :" + str(ret))
61
 
62
 
62
     return ret
63
     return ret
63
 
64
 
65
 def getEngine(ename='default', sqlalogging=None):
66
 def getEngine(ename='default', sqlalogging=None):
66
     return get_engine(ename=ename, sqlalogging=sqlalogging)
67
     return get_engine(ename=ename, sqlalogging=sqlalogging)
67
 
68
 
69
+
68
 ## Return a sqlalchemy.MetaData object
70
 ## Return a sqlalchemy.MetaData object
69
 # @param engine sqlalchemy.engine : A sqlalchemy engine
71
 # @param engine sqlalchemy.engine : A sqlalchemy engine
70
 # @return an sql alechemy MetaData instance bind to engine
72
 # @return an sql alechemy MetaData instance bind to engine
73
     res.reflect(bind=engine)
75
     res.reflect(bind=engine)
74
     return res
76
     return res
75
 
77
 
78
+
76
 ## Return an sqlalchemy table given an EmComponent child class
79
 ## Return an sqlalchemy table given an EmComponent child class
77
 # @warning Except a class type not an instance
80
 # @warning Except a class type not an instance
78
 # @param cls : An EmComponent child class
81
 # @param cls : An EmComponent child class
79
 # @return An sqlalchemy table
82
 # @return An sqlalchemy table
80
 # @throw TypeError if em_instance is an EmComponent  or not an EmComponent child class (or an instance)
83
 # @throw TypeError if em_instance is an EmComponent  or not an EmComponent child class (or an instance)
81
 def get_table(cls):
84
 def get_table(cls):
82
-    from EditorialModel.components import EmComponent #dirty circula inclusion hack
83
-    if not issubclass(cls, EmComponent) or cls.table == None:
84
-        raise TypeError("Excepting an EmComponent child class not an "+str(cls))
85
+    from EditorialModel.components import EmComponent  # dirty circula inclusion hack
86
+    if not issubclass(cls, EmComponent) or cls.table is None:
87
+        raise TypeError("Excepting an EmComponent child class not an " + str(cls))
85
     engine = cls.db_engine()
88
     engine = cls.db_engine()
86
     return sqla.Table(cls.table, meta(engine))
89
     return sqla.Table(cls.table, meta(engine))
87
-def getTable(cls): return get_table(cls)
90
+
91
+
92
+def getTable(cls):
93
+    return get_table(cls)
94
+
88
 
95
 
89
 ## This function is intended to execute ddl defined in sqlalter
96
 ## This function is intended to execute ddl defined in sqlalter
90
 # @warning There is a dirty workaround here, DDL should returns only one query, but DropColumn for sqlite has to return 4 queries (rename, create, insert, drop). There is a split on the compiled SQL to extract and execute one query at a time
97
 # @warning There is a dirty workaround here, DDL should returns only one query, but DropColumn for sqlite has to return 4 queries (rename, create, insert, drop). There is a split on the compiled SQL to extract and execute one query at a time
96
     req = str(ddl.compile(dialect=db_engine.dialect))
103
     req = str(ddl.compile(dialect=db_engine.dialect))
97
     queries = req.split(';')
104
     queries = req.split(';')
98
     for query in queries:
105
     for query in queries:
99
-        logger.debug("Executing custom raw SQL query : '"+query+"'")
106
+        logger.debug("Executing custom raw SQL query : '" + query + "'")
100
         ret = conn.execute(query)
107
         ret = conn.execute(query)
101
         if not bool(ret):
108
         if not bool(ret):
102
             return False
109
             return False
103
     conn.close()
110
     conn.close()
104
     return True
111
     return True
105
-

Loading…
Cancel
Save