Browse Source

Lodel: first draft of Settings class

ArnAud 9 years ago
parent
commit
d854b468e0
4 changed files with 41 additions and 4 deletions
  1. 1
    1
      .gitignore
  2. 2
    2
      DataSource/MySQL/MySQL.py
  3. 37
    0
      Lodel/settings.py
  4. 1
    1
      settings_local.py.example

+ 1
- 1
.gitignore View File

2
 .venv
2
 .venv
3
 .git
3
 .git
4
 .idea
4
 .idea
5
-settings.py
5
+settings_local.py
6
 doc
6
 doc
7
 .*.swp
7
 .*.swp

+ 2
- 2
DataSource/MySQL/MySQL.py View File

1
 # -*- coding: utf8 -*-
1
 # -*- coding: utf8 -*-
2
 
2
 
3
-import settings
3
+from Lodel.settings import Settings
4
 
4
 
5
 ## @brief Manages the accesses to a MySQL datasource
5
 ## @brief Manages the accesses to a MySQL datasource
6
 class MySQL(object):
6
 class MySQL(object):
11
     field_lodel_id = 'lodel_id'
11
     field_lodel_id = 'lodel_id'
12
     class_table_prefix = 'class_'
12
     class_table_prefix = 'class_'
13
     objects_table_name = 'object'
13
     objects_table_name = 'object'
14
-    connections = settings.DATABASE_CONNECTIONS
14
+    connections = Settings.get("datasource")
15
     ## @brief indicates if we want ON DELETE CASCADE on foreign keys
15
     ## @brief indicates if we want ON DELETE CASCADE on foreign keys
16
     # @todo implementation in migration handler
16
     # @todo implementation in migration handler
17
     fk_on_delete_cascade = False
17
     fk_on_delete_cascade = False

+ 37
- 0
Lodel/settings.py View File

1
+#-*- coding: utf-8 -*-
2
+
3
+import settings_local
4
+
5
+class Settings:
6
+    # List of accepted settings
7
+    datasource = None
8
+
9
+    @staticmethod
10
+    # @throw AttributeError if the setting is not defined in this class
11
+    def get(attribute):
12
+        value = None
13
+        # find the value in settings itself, if not set search in settings_local
14
+        value = getattr(Settings, attribute)
15
+        if value is None:
16
+            try:
17
+                value = getattr(settings_local, attribute)
18
+            except AttributeError:
19
+                pass
20
+
21
+        if value is not None:
22
+            try:
23
+                func = getattr(Settings, attribute + '_args')
24
+                value = func(value)
25
+            except AttributeError:
26
+                pass
27
+
28
+        return value
29
+
30
+    @staticmethod
31
+    # @throw AttributeError if the setting is not defined in this class
32
+    def set(attribute, value):
33
+        setattr(Settings, attribute, value)
34
+
35
+    @staticmethod
36
+    def datasource_args(value):
37
+        return value

settings.py.example → settings_local.py.example View File

2
 
2
 
3
 import pymysql
3
 import pymysql
4
 
4
 
5
-DATABASE_CONNECTIONS = {
5
+datasource = {
6
     'default': {
6
     'default': {
7
         'module': pymysql,
7
         'module': pymysql,
8
         'host': '127.0.0.1',
8
         'host': '127.0.0.1',

Loading…
Cancel
Save