buildz 0.3.8__tar.gz → 0.4.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. {buildz-0.3.8/buildz.egg-info → buildz-0.4.1}/PKG-INFO +1 -1
  2. buildz-0.4.1/buildz/demo/ioc/test.py +37 -0
  3. buildz-0.4.1/buildz/demo/ioc/test_conf/base.js +9 -0
  4. buildz-0.4.1/buildz/demo/ioc/test_conf/data1.js +43 -0
  5. buildz-0.4.1/buildz/demo/ioc/test_conf/data2.js +21 -0
  6. buildz-0.4.1/buildz/demo/ioc/test_conf/data3.js +21 -0
  7. buildz-0.4.1/buildz/ioc/__init__.py +3 -0
  8. buildz-0.4.1/buildz/ioc/init.py +14 -0
  9. buildz-0.4.1/buildz/ioc/ioc/base.py +34 -0
  10. buildz-0.4.1/buildz/ioc/ioc/conf.py +118 -0
  11. buildz-0.4.1/buildz/ioc/ioc/confs.py +286 -0
  12. buildz-0.4.1/buildz/ioc/ioc_deal/base.py +149 -0
  13. buildz-0.4.1/buildz/ioc/ioc_deal/call.py +39 -0
  14. buildz-0.4.1/buildz/ioc/ioc_deal/conf/call_defaults.js +5 -0
  15. buildz-0.4.1/buildz/ioc/ioc_deal/conf/call_lists.js +20 -0
  16. buildz-0.4.1/buildz/ioc/ioc_deal/conf/deals.js +37 -0
  17. buildz-0.4.1/buildz/ioc/ioc_deal/conf/env_lists.js +18 -0
  18. buildz-0.4.1/buildz/ioc/ioc_deal/conf/mcall_defaults.js +5 -0
  19. buildz-0.4.1/buildz/ioc/ioc_deal/conf/mcall_lists.js +24 -0
  20. buildz-0.4.1/buildz/ioc/ioc_deal/conf/obj_cst_lists.js +12 -0
  21. buildz-0.4.1/buildz/ioc/ioc_deal/conf/obj_defaults.js +8 -0
  22. buildz-0.4.1/buildz/ioc/ioc_deal/conf/obj_lists.js +32 -0
  23. buildz-0.4.1/buildz/ioc/ioc_deal/conf/obj_set_lists.js +10 -0
  24. buildz-0.4.1/buildz/ioc/ioc_deal/conf/ovar_lists.js +22 -0
  25. buildz-0.4.1/buildz/ioc/ioc_deal/conf/ref_lists.js +22 -0
  26. buildz-0.4.1/buildz/ioc/ioc_deal/conf/var_lists.js +18 -0
  27. buildz-0.4.1/buildz/ioc/ioc_deal/demo.py +15 -0
  28. buildz-0.4.1/buildz/ioc/ioc_deal/env.py +17 -0
  29. buildz-0.4.1/buildz/ioc/ioc_deal/mcall.py +49 -0
  30. buildz-0.4.1/buildz/ioc/ioc_deal/obj.py +92 -0
  31. buildz-0.4.1/buildz/ioc/ioc_deal/ovar.py +40 -0
  32. buildz-0.4.1/buildz/ioc/ioc_deal/ref.py +17 -0
  33. buildz-0.4.1/buildz/ioc/ioc_deal/val.py +16 -0
  34. buildz-0.4.1/buildz/ioc/ioc_deal/var.py +33 -0
  35. buildz-0.4.1/buildz/pyz.py +47 -0
  36. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/strz.py +7 -3
  37. buildz-0.4.1/buildz/xf/mapz.py +54 -0
  38. {buildz-0.3.8 → buildz-0.4.1/buildz.egg-info}/PKG-INFO +1 -1
  39. {buildz-0.3.8 → buildz-0.4.1}/buildz.egg-info/SOURCES.txt +44 -10
  40. {buildz-0.3.8 → buildz-0.4.1}/setup.py +1 -1
  41. buildz-0.3.8/buildz/xf/mapz.py +0 -20
  42. {buildz-0.3.8 → buildz-0.4.1}/LICENSE +0 -0
  43. {buildz-0.3.8 → buildz-0.4.1}/MANIFEST.in +0 -0
  44. {buildz-0.3.8 → buildz-0.4.1}/README.md +0 -0
  45. {buildz-0.3.8 → buildz-0.4.1}/buildz/__init__.py +0 -0
  46. {buildz-0.3.8 → buildz-0.4.1}/buildz/__main__.py +0 -0
  47. {buildz-0.3.8 → buildz-0.4.1}/buildz/base.py +0 -0
  48. {buildz-0.3.8 → buildz-0.4.1}/buildz/build.py +0 -0
  49. {buildz-0.3.8 → buildz-0.4.1}/buildz/confz.py +0 -0
  50. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/demo.confz +0 -0
  51. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/demo.py +0 -0
  52. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/run.confz +0 -0
  53. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/test.confz +0 -0
  54. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/test.py +0 -0
  55. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/testc.py +0 -0
  56. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/testf.py +0 -0
  57. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/testz.py +0 -0
  58. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/value.confz +0 -0
  59. {buildz-0.3.8/buildz/demo → buildz-0.4.1/buildz/demo/x}/x.py +0 -0
  60. {buildz-0.3.8 → buildz-0.4.1}/buildz/dj.txt +0 -0
  61. {buildz-0.3.8 → buildz-0.4.1}/buildz/keys.py +0 -0
  62. {buildz-0.3.8 → buildz-0.4.1}/buildz/test_loader copy.py +0 -0
  63. {buildz-0.3.8 → buildz-0.4.1}/buildz/test_loader.py +0 -0
  64. {buildz-0.3.8 → buildz-0.4.1}/buildz/test_writer.py +0 -0
  65. {buildz-0.3.8 → buildz-0.4.1}/buildz/tools.py +0 -0
  66. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/__init__.py +0 -0
  67. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/base.py +0 -0
  68. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/buff.py +0 -0
  69. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fc.py +0 -0
  70. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fc_item.py +0 -0
  71. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fc_list.py +0 -0
  72. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fc_map.py +0 -0
  73. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fc_set.py +0 -0
  74. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fc_str.py +0 -0
  75. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fc_type.py +0 -0
  76. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/file.py +0 -0
  77. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fmt_base.py +0 -0
  78. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fmt_str.py +0 -0
  79. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/fmt_type.py +0 -0
  80. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/mg.py +0 -0
  81. {buildz-0.3.8 → buildz-0.4.1}/buildz/xconfz/mg_fmt.py +0 -0
  82. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/__init__.py +0 -0
  83. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/file.py +0 -0
  84. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/base.py +0 -0
  85. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/buffer.py +0 -0
  86. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/listz.py +0 -0
  87. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/lr.py +0 -0
  88. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/lrval.py +0 -0
  89. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/mapz.py +0 -0
  90. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/nextz.py +0 -0
  91. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/reval.py +0 -0
  92. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/setz.py +0 -0
  93. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/spc.py +0 -0
  94. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/deal/spt.py +0 -0
  95. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/exp.py +0 -0
  96. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/item.py +0 -0
  97. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/mg.py +0 -0
  98. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/loader/pos.py +0 -0
  99. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/read.py +0 -0
  100. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/test_write.py +0 -0
  101. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/write.py +0 -0
  102. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/base.py +0 -0
  103. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/conf.py +0 -0
  104. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/deal/jsonval.py +0 -0
  105. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/deal/listz.py +0 -0
  106. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/deal/mapz.py +0 -0
  107. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/deal/reval.py +0 -0
  108. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/deal/strz.py +0 -0
  109. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/itemz.py +0 -0
  110. {buildz-0.3.8 → buildz-0.4.1}/buildz/xf/writer/mg.py +0 -0
  111. {buildz-0.3.8 → buildz-0.4.1}/buildz.egg-info/dependency_links.txt +0 -0
  112. {buildz-0.3.8 → buildz-0.4.1}/buildz.egg-info/top_level.txt +0 -0
  113. {buildz-0.3.8 → buildz-0.4.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: buildz
3
- Version: 0.3.8
3
+ Version: 0.4.1
4
4
  Summary: a json-like file format's read and write code by python, and codes to read and product object from configure file in such format
5
5
  Home-page: https://github.com/buildCodeZ/buildz
6
6
  Author: Zzz
@@ -0,0 +1,37 @@
1
+ #
2
+ from buildz.ioc import build
3
+ from buildz import xf
4
+ import os
5
+ join = os.path.join
6
+ class Test:
7
+ def __init__(self, *args, **maps):
8
+ print(f"Test init args: {args}, maps: {maps}")
9
+ self.args = args
10
+ self.maps = maps
11
+ def run(self):
12
+ print(f"get obj:{self}")
13
+ print("get_obj.obj:", self.obj)
14
+
15
+ pass
16
+ fps = xf.loads("[data1.js, data2.js, data3.js]")[:2]
17
+
18
+ fps = [join('test_conf', fp) for fp in fps]
19
+ def show():
20
+ print("run show")
21
+
22
+ pass
23
+ def test():
24
+ confs = build()
25
+ for fp in fps:
26
+ confs.add_fp(fp)
27
+ #obj = confs.get("data1.test")
28
+ confs.get("data1.run")
29
+ obj = confs.get("data1.test.obj")
30
+ print(f"obj: {obj}")
31
+
32
+ pass
33
+
34
+ if __name__=="__main__":
35
+ test()
36
+
37
+ pass
@@ -0,0 +1,9 @@
1
+
2
+ {
3
+ env_spt: "." // split on env id
4
+ spt: "." // split on id
5
+ default_type: 'test' // default type of data
6
+ deep_first: false // 全局查id的时候是从最上层开始找还是从最下层开始找(每一层都可能有配置文件)
7
+ global_env: true //True=环境变量env都是全局的(全局查找),否则优先每个配置文件里查环境变量,查不到才查全局
8
+ global_deal: true //True=类型处理函数deal都是全局的(全局查找),否则优先每个配置文件里查处理函数,查不到才查全局
9
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ envs: {
3
+ env.test: 123
4
+ }
5
+ namespace: data1
6
+ default_type: default
7
+ datas: [
8
+ {
9
+ id: test
10
+ type: object
11
+ source: test.Test
12
+ single: 1
13
+ construct: {
14
+ args: [
15
+ [env, env.test]
16
+ [val, 123]
17
+ ],
18
+ maps: {
19
+ key:[ref, data2.test]
20
+ }
21
+ },
22
+ sets: [
23
+ [obj, [ref, test]]
24
+ [obj1, [ovar, null, args]]
25
+ ],
26
+ calls: [
27
+ [mcall, null, run, [], {}]
28
+ ]
29
+ }
30
+ {
31
+ id: run
32
+ type: mcall
33
+ source: test
34
+ method: run
35
+ args: []
36
+ maps: {}
37
+ }
38
+ {
39
+ id: test.obj
40
+ val: test.show
41
+ }
42
+ ]
43
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+
3
+ namespace: data2
4
+ default_type: obj
5
+ datas: [
6
+ {
7
+ id: test
8
+ type: object
9
+ source: test.Test
10
+ construct: {
11
+ args: [
12
+ [env, env.test]
13
+ [env, path]
14
+ ]
15
+ maps: {
16
+
17
+ }
18
+ }
19
+ }
20
+ ]
21
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ envs: {
3
+ env.name: "data3"
4
+ }
5
+ namespace: data3
6
+ default_type: obj
7
+ datas: [
8
+ {
9
+ id: test
10
+ type: obj
11
+ args: [1,2,3]
12
+ maps: {4:5,6:7}
13
+ ref: test1
14
+ }
15
+ {
16
+ id: test1
17
+ type: obj
18
+ args: ['no ref']
19
+ }
20
+ ]
21
+ }
@@ -0,0 +1,3 @@
1
+ #coding=utf-8
2
+
3
+ from .init import build
@@ -0,0 +1,14 @@
1
+ #
2
+
3
+ from .ioc import confs
4
+ from .ioc_deal.base import default_deals
5
+
6
+ def build(fp_init = None, add_default_deals = True):
7
+ obj = confs.Confs()
8
+ if (fp_init is not None):
9
+ obj.init_fp(fp_init)
10
+ if add_default_deals:
11
+ obj.add_fp(default_deals)
12
+ return obj
13
+
14
+ pass
@@ -0,0 +1,34 @@
1
+ #coding=utf-8
2
+ from buildz import xf, pyz
3
+ from buildz.xf import g as xg
4
+ import json
5
+ class Base:
6
+ def update_maps(self, maps, src):
7
+ xf.deep_update(maps, src)
8
+ def __init__(self, *args, **maps):
9
+ self.init(*args, **maps)
10
+ def init(self, *args, **maps):
11
+ pass
12
+ def __call__(self, *args, **maps):
13
+ return self.deal(*args, **maps)
14
+ def deal(self, *args, **maps):
15
+ return None
16
+
17
+ pass
18
+ class EncapeData(Base):
19
+ """
20
+ 包含data id对应的配置,配置文件id,配置文件对象
21
+ [object.test, call, ]
22
+ """
23
+ def __init__(self, data, conf, local = False, type = None, src = None):
24
+ self.data = data
25
+ self.sid = conf.id
26
+ self.src = src
27
+ self.conf = conf
28
+ self.confs = conf.confs
29
+ self.local = local
30
+ if type is None:
31
+ type = conf.confs.get_data_type(data, 0, conf.default_type())
32
+ self.type = type
33
+
34
+ pass
@@ -0,0 +1,118 @@
1
+ #coding=utf-8
2
+ from buildz import xf, pyz
3
+ from buildz.xf import g as xg
4
+ import json
5
+ from .base import Base, EncapeData
6
+ class Conf(Base):
7
+ def get_key(self, obj, key = 'id', index=0):
8
+ if type(obj)==dict:
9
+ return obj[key]
10
+ id = obj[index]
11
+ if type(id) in [list, tuple]:
12
+ return id[0]
13
+ return id
14
+ def map(self, arr, fc_key):
15
+ return {fc_key(obj): obj for obj in arr}
16
+ def init(self, conf, confs):
17
+ """
18
+ {
19
+ deals:[{build: fc_path,args: [],maps: {}}]
20
+ envs: {id: val}
21
+ id: default null
22
+ namespace: default null
23
+ datas: [{id:val, type: val, ...}]
24
+ locals: [like datas]
25
+ default_type: default null
26
+ }
27
+ """
28
+ id = xf.g(conf, id=None)
29
+ if id is None:
30
+ id = confs.conf_id()
31
+ self.id = id
32
+ self.namespace = xf.g(conf, namespace=None)
33
+ self.conf = conf
34
+ self.confs = confs
35
+ self.locals = self.map(xf.g(conf, locals=[]), self.confs.get_data_id)
36
+ self.datas = self.map(xf.g(conf, datas=[]), self.confs.get_data_id)
37
+ self.deals = self.map(xf.g(conf, deals = []), self.confs.get_deal_type)
38
+ self._default_type = xf.g(conf, default_type = None)
39
+ self.envs = xf.g(conf, envs = {})
40
+ self.confs.flush_env(self.envs)
41
+ for _type in list(self.deals.keys()):
42
+ conf = self.deals[_type]
43
+ if type(conf) in [list, tuple]:
44
+ maps = {}
45
+ maps['type'] = conf[0]
46
+ maps['build'] = conf[1]
47
+ arr = conf[2:]
48
+ if len(arr)>0:
49
+ maps['args'] = arr.pop(0)
50
+ if len(arr)>0:
51
+ maps['maps'] = arr.pop(0)
52
+ conf = maps
53
+ fc = pyz.load(conf["build"])
54
+ args = xf.g(conf, args=[])
55
+ maps = xf.g(conf, maps={})
56
+ deal = fc(*args, **maps)
57
+ self.deals[_type] = deal
58
+ aliases = xf.g(conf, aliases = [])
59
+ for alias in aliases:
60
+ self.deals[alias] = deal
61
+ def get_env(self, id, search_confs = True):
62
+ if self.confs.global_env and search_confs:
63
+ return self.confs.get_env(id, self.id)
64
+ ids = self.confs.env_ids(id)
65
+ envs = self.envs
66
+ find = None
67
+ for id in ids:
68
+ if type(envs)!=dict:
69
+ envs = None
70
+ break
71
+ if id not in envs:
72
+ envs = None
73
+ break
74
+ envs = envs[id]
75
+ if envs is not None:
76
+ return envs
77
+ if not search_confs:
78
+ return None
79
+ return self.confs.get_env(id, self.id)
80
+ def get_deal(self, type, search_confs = True):
81
+ if self.confs.global_deal and search_confs:
82
+ return self.confs.get_deal(type, self.id)
83
+ if type in self.deals:
84
+ return self.deals[type]
85
+ if not search_confs:
86
+ return None
87
+ return self.confs.get_deal(type, self.id)
88
+ def get_data(self, id, local = True, search_confs = True):
89
+ if id in self.datas:
90
+ obj = self.datas[id]
91
+ return EncapeData(obj, self, local = False)
92
+ if not local:
93
+ return None
94
+ if id in self.locals:
95
+ obj = self.locals[id]
96
+ return EncapeData(obj, self, local = True)
97
+ if not search_confs:
98
+ return None
99
+ return self.confs.get_data(id, self.id)
100
+ def get(self, *args, **maps):
101
+ return self.get_obj(*args, **maps)
102
+ def default_type(self):
103
+ if self._default_type is None:
104
+ return self.confs.default_type
105
+ return self._default_type
106
+ def get_obj(self, id):
107
+ """
108
+ 根据data id获取data对象,处理逻辑:根据data id查配置,根据配置的type查deal,返回deal处理过的配置
109
+ """
110
+ conf = self.get_data(id)
111
+ if conf is None:
112
+ return None
113
+ deal = self.get_deal(conf.type)
114
+ if deal is None:
115
+ return None
116
+ return deal(conf)
117
+
118
+ pass
@@ -0,0 +1,286 @@
1
+ #coding=utf-8
2
+ from buildz import xf, pyz
3
+ from buildz.xf import g as xg
4
+ import json
5
+ from .base import Base, EncapeData
6
+ from .conf import Conf
7
+ import os
8
+ class ConfsNode(Base):
9
+ def init(self):
10
+ self.confs = []
11
+ self.ids = {}
12
+
13
+ pass
14
+
15
+
16
+ class Confs(Base):
17
+ """
18
+
19
+ """
20
+ def flush_env(self, envs):
21
+ """
22
+ a.b.c:d -> a:{b:{c:d}}
23
+ """
24
+ for key in list(envs.keys()):
25
+ val = envs[key]
26
+ if type(val)==dict:
27
+ self.flush_env(val)
28
+ ids = self.env_ids(key)
29
+ if len(ids)>1:
30
+ del envs[key]
31
+ pids = ids[:-1]
32
+ id = ids[-1]
33
+ tmp = envs
34
+ for pid in pids:
35
+ if pid not in tmp:
36
+ tmp[pid] = {}
37
+ tmp = tmp[pid]
38
+ if id not in tmp:
39
+ tmp[id] = val
40
+ continue
41
+ tval = tmp[id]
42
+ if type(tval) == dict and type(val)==dict:
43
+ self.update_maps(tval, val)
44
+ else:
45
+ tmp[id] = val
46
+ def env_ids(self, id):
47
+ return id.split(self.env_spt)
48
+ def env_id(self, ids):
49
+ return self.env_spt.join(ids)
50
+ def get_env(self, id, sid=None):
51
+ if sid is not None and not self.global_env:
52
+ val = self.confs[sid].get_env(id, False)
53
+ if val is not None:
54
+ return val
55
+ sysdt = os.getenv(id)
56
+ if sysdt is not None:
57
+ return sysdt
58
+ ids = self.env_ids(id)
59
+ envs = self.envs
60
+ for id in ids:
61
+ if type(envs)!=dict:
62
+ envs = None
63
+ break
64
+ if id not in envs:
65
+ envs = None
66
+ break
67
+ envs = envs[id]
68
+ return envs
69
+ def set_env(self, id, val):
70
+ obj = {id:val}
71
+ self.flush_env(obj)
72
+ self.update_maps(self.envs, obj)
73
+ def set_deal(self, type, fc):
74
+ self.deals[type] = fc
75
+ def init_fp(self, fp):
76
+ conf = self.loads(xf.fread(fp))
77
+ self.init(conf, self.loads)
78
+ def by_json(self):
79
+ self.by('json')
80
+ def by_xf(self):
81
+ self.by('xf')
82
+ def by(self, type = 'xf'):
83
+ if type == 'xf':
84
+ self.loads = xf.loads
85
+ elif type == 'json':
86
+ self.loads = json.loads
87
+ else:
88
+ raise Exception("only 'xf' and 'json' impl now")
89
+ def get_key(self, obj, key = 'id', index=0):
90
+ if type(obj)==dict:
91
+ return obj[key]
92
+ id = obj[index]
93
+ if type(id) in [list, tuple]:
94
+ return id[0]
95
+ return id
96
+ def init(self, conf={}, loads = None):
97
+ """
98
+ {
99
+ // 环境变量的分隔符
100
+ // default = '.'
101
+ env_spt: .
102
+ // 数据id的分隔符
103
+ // default = "."
104
+ spt: .
105
+ // 数据的默认类型(处理方式)
106
+ // default = 'default'
107
+ default_type: default
108
+ // 全局查id的时候是从最上层开始找还是从最下层开始找(每一层都可能有配置文件)
109
+ // default = false
110
+ deep_first: false
111
+ // true=环境变量env都是全局的(全局查找),否则优先每个配置文件里查环境变量,查不到才查全局
112
+ // default = true
113
+ global_env: true
114
+ // true=类型处理函数deal都是全局的(全局查找),否则优先每个配置文件里查处理函数,查不到才查全局
115
+ // default = true
116
+ global_deal: true
117
+ // 数据的id字段名
118
+ // default = 'id'
119
+ data_key_id: id
120
+ // 数据的type字段名
121
+ // default = 'type'
122
+ data_key_type: type
123
+ // 数据配置参数是数组的时候,数据id的位置
124
+ // default=[0,0]
125
+ data_index_id: [0,0]
126
+ // 数据配置参数是数组的时候,数据type的位置
127
+ // default=[0,1]
128
+ data_index_type: [0,1]
129
+ // 处理deal的type字段名
130
+ // default = 'type'
131
+ deal_key_type: type
132
+ // deal配置参数是数组的时候,数据type的位置
133
+ // default=0
134
+ deal_index_type: 0
135
+ }
136
+ """
137
+ if loads is None:
138
+ loads = xf.loads
139
+ self.loads = loads
140
+ if type(conf) in [bytes, str]:
141
+ conf = self.loads(conf)
142
+ self.spt = xf.g(conf, spt = ".")
143
+ self.env_spt = xf.g(conf, env_spt = ".")
144
+ self.default_type = xf.g(conf, default_type='default')
145
+ self.deep_first = xf.g(conf, deep_first=False)
146
+ self.global_env = xf.g(conf, global_env = True)
147
+ self.global_deal = xf.g(conf, global_deal = True)
148
+ self.data_key_id = xf.g(conf, data_key_id = 'id')
149
+ self.data_key_type = xf.g(conf, data_key_type = 'type')
150
+ self.data_index_id = xf.g(conf, data_index_id = [0,0])
151
+ self.data_index_type = xf.g(conf, data_index_type = [0,1])
152
+ self.deal_key_type = xf.g(conf, deal_key_type = 'type')
153
+ self.deal_index_type = xf.g(conf, deal_index_type = 0)
154
+ self._conf_id = 0
155
+ self.conf = conf
156
+ self.node = ConfsNode()
157
+ self.confs = {}
158
+ self.deals = {}
159
+ self.envs = {}
160
+ def get_deal_type(self, obj):
161
+ if type(obj)==dict:
162
+ return obj[self.deal_key_type]
163
+ return obj[self.deal_index_type]
164
+ def get_data_id(self, obj):
165
+ if type(obj)==dict:
166
+ return obj[self.data_key_id]
167
+ obj = obj[self.data_index_id[0]]
168
+ if type(obj) in [list, tuple]:
169
+ obj = obj[self.data_index_id[1]]
170
+ return obj
171
+ def get_data_type(self, obj, type_first = 1, default = None):
172
+ if type(obj)==dict:
173
+ if self.data_key_type not in obj:
174
+ return default
175
+ return obj[self.data_key_type]
176
+ obj = obj[self.data_index_type[0]]
177
+ if type(obj) in [list, tuple]:
178
+ return obj[self.data_index_type[1]]
179
+ if type_first:
180
+ return obj
181
+ return default
182
+ def conf_id(self):
183
+ """
184
+ 给每个配置文件加一个id,外部不调用
185
+ """
186
+ id = self._conf_id
187
+ self._conf_id+=1
188
+ return id
189
+ def ids(self, id):
190
+ if id is None:
191
+ return []
192
+ """
193
+ data的id转id列表,外部不调用
194
+ 例: id = 'a.b.c', spt = ".", ids = ['a','b','c']
195
+ """
196
+ return id.split(self.spt)
197
+ def id(self, ids):
198
+ """
199
+ data的id列表转id,外部不调用
200
+ 例: ids = ['a','b','c'], spt = ".", id = 'a.b.c',
201
+ """
202
+ return self.spt.join(ids)
203
+ def add_fp(self, fp):
204
+ conf = self.loads(xf.fread(fp))
205
+ return self.add(conf)
206
+ def add(self, conf):
207
+ """
208
+ {
209
+ deals:[{build: fc_path,args: [],maps: {}}]
210
+ envs: {id: val}
211
+ id: default null
212
+ namespace: default null
213
+ datas: [{id:val, type: val, ...}]
214
+ locals: [like datas]
215
+ }
216
+ """
217
+ if type(conf) in [bytes, str]:
218
+ conf = self.loads(conf)
219
+ obj = Conf(conf, self)
220
+ id = xf.g(conf, namespace=None)
221
+ ids = self.ids(id)
222
+ node = self.node
223
+ for id in ids:
224
+ if id not in node.ids:
225
+ node.ids[id] = ConfsNode()
226
+ node = node.ids[id]
227
+ node.confs.append(obj)
228
+ self.confs[obj.id] = obj
229
+ for k in obj.deals:
230
+ self.deals[k] = obj.deals[k]
231
+ self.update_maps(self.envs, obj.envs)
232
+ def get(self, *args, **maps):
233
+ return self.get_obj(*args, **maps)
234
+ def get_obj(self, id, sid = None):
235
+ """
236
+ 根据data id获取data对象,处理逻辑:根据data id查配置,根据配置的type查deal,返回deal处理过的配置
237
+ """
238
+ conf = self.get_data(id, sid)
239
+ if conf is None:
240
+ return None
241
+ deal = self.get_deal(conf.type, sid)
242
+ if deal is None:
243
+ return None
244
+ #print(f"get_obj: {id}({sid}), conf: {conf}, deal: {deal}, type: {conf.type}")
245
+ return deal(conf)
246
+ def get_deal(self, type, sid=None):
247
+ """
248
+ 根据type类型查处理函数deal,sid(配置文件id)不为空并且global_deal=False则先查局部
249
+ """
250
+ if sid is not None and not self.global_deal:
251
+ deal = self.confs[sid].get_deal(type, False)
252
+ if deal is not None:
253
+ return deal
254
+ if type in self.deals:
255
+ return self.deals[type]
256
+ return None
257
+ def get_confs(self, ids):
258
+ """
259
+ 根据ids查所有对应的配置文件列表
260
+ """
261
+ node = self.node
262
+ confs = []
263
+ for i in range(len(ids)):
264
+ id = ids[i]
265
+ confs.append([node.confs,i])
266
+ if id not in node.ids:
267
+ break
268
+ node = node.ids[id]
269
+ return confs
270
+ def get_data(self, id, sid=None):
271
+ """
272
+ 根据id查对应的data配置
273
+ """
274
+ ids = self.ids(id)
275
+ arr = self.get_confs(ids)
276
+ if self.deep_first:
277
+ arr.reverse()
278
+ for confs,i in arr:
279
+ id = self.id(ids[i:])
280
+ for conf in confs:
281
+ conf = conf.get_data(id, sid==conf.id, False)
282
+ if conf is not None:
283
+ return conf
284
+ return None
285
+
286
+ pass