re-common 10.0.39__py3-none-any.whl → 10.0.40__py3-none-any.whl

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 (218) hide show
  1. re_common/baselibrary/__init__.py +4 -4
  2. re_common/baselibrary/baseabs/__init__.py +6 -6
  3. re_common/baselibrary/baseabs/baseabs.py +26 -26
  4. re_common/baselibrary/database/mbuilder.py +132 -132
  5. re_common/baselibrary/database/moudle.py +93 -93
  6. re_common/baselibrary/database/msqlite3.py +194 -194
  7. re_common/baselibrary/database/mysql.py +169 -169
  8. re_common/baselibrary/database/sql_factory.py +26 -26
  9. re_common/baselibrary/mthread/MThreadingRun.py +486 -486
  10. re_common/baselibrary/mthread/MThreadingRunEvent.py +349 -349
  11. re_common/baselibrary/mthread/__init__.py +2 -2
  12. re_common/baselibrary/mthread/mythreading.py +695 -695
  13. re_common/baselibrary/pakge_other/socks.py +404 -404
  14. re_common/baselibrary/readconfig/config_factory.py +18 -18
  15. re_common/baselibrary/readconfig/ini_config.py +317 -317
  16. re_common/baselibrary/readconfig/toml_config.py +49 -49
  17. re_common/baselibrary/temporary/envdata.py +36 -36
  18. re_common/baselibrary/tools/all_requests/aiohttp_request.py +118 -118
  19. re_common/baselibrary/tools/all_requests/httpx_requet.py +102 -102
  20. re_common/baselibrary/tools/all_requests/mrequest.py +412 -412
  21. re_common/baselibrary/tools/all_requests/requests_request.py +81 -81
  22. re_common/baselibrary/tools/batch_compre/bijiao_batch.py +31 -31
  23. re_common/baselibrary/tools/contrast_db3.py +123 -123
  24. re_common/baselibrary/tools/copy_file.py +39 -39
  25. re_common/baselibrary/tools/db3_2_sizedb3.py +102 -102
  26. re_common/baselibrary/tools/foreachgz.py +39 -39
  27. re_common/baselibrary/tools/get_attr.py +10 -10
  28. re_common/baselibrary/tools/image_to_pdf.py +61 -61
  29. re_common/baselibrary/tools/java_code_deal.py +139 -139
  30. re_common/baselibrary/tools/javacode.py +79 -79
  31. re_common/baselibrary/tools/mdb_db3.py +48 -48
  32. re_common/baselibrary/tools/merge_file.py +171 -171
  33. re_common/baselibrary/tools/merge_gz_file.py +165 -165
  34. re_common/baselibrary/tools/mhdfstools/down_hdfs_files.py +42 -42
  35. re_common/baselibrary/tools/mhdfstools/hdfst.py +42 -42
  36. re_common/baselibrary/tools/mhdfstools/up_hdfs_files.py +38 -38
  37. re_common/baselibrary/tools/mongo_tools.py +50 -50
  38. re_common/baselibrary/tools/move_file.py +170 -170
  39. re_common/baselibrary/tools/move_mongo/mongo_table_to_file.py +63 -63
  40. re_common/baselibrary/tools/move_mongo/move_mongo_table.py +354 -354
  41. re_common/baselibrary/tools/move_mongo/use_mttf.py +18 -18
  42. re_common/baselibrary/tools/move_mongo/use_mv.py +93 -93
  43. re_common/baselibrary/tools/mpandas/mpandasreadexcel.py +125 -125
  44. re_common/baselibrary/tools/mpandas/pandas_visualization.py +7 -7
  45. re_common/baselibrary/tools/myparsel.py +104 -104
  46. re_common/baselibrary/tools/rename_dir_file.py +37 -37
  47. re_common/baselibrary/tools/sequoiadb_utils.py +398 -398
  48. re_common/baselibrary/tools/split_line_to_many.py +25 -25
  49. re_common/baselibrary/tools/stringtodicts.py +33 -33
  50. re_common/baselibrary/tools/workwechant_bot.py +84 -84
  51. re_common/baselibrary/utils/baseaiohttp.py +296 -296
  52. re_common/baselibrary/utils/baseaiomysql.py +87 -87
  53. re_common/baselibrary/utils/baseallstep.py +191 -191
  54. re_common/baselibrary/utils/baseavro.py +19 -19
  55. re_common/baselibrary/utils/baseboto3.py +291 -291
  56. re_common/baselibrary/utils/basecsv.py +32 -32
  57. re_common/baselibrary/utils/basedict.py +133 -133
  58. re_common/baselibrary/utils/basedir.py +241 -241
  59. re_common/baselibrary/utils/baseencode.py +351 -351
  60. re_common/baselibrary/utils/baseencoding.py +28 -28
  61. re_common/baselibrary/utils/baseesdsl.py +86 -86
  62. re_common/baselibrary/utils/baseexcel.py +264 -264
  63. re_common/baselibrary/utils/baseexcept.py +109 -109
  64. re_common/baselibrary/utils/basefile.py +654 -654
  65. re_common/baselibrary/utils/baseftp.py +214 -214
  66. re_common/baselibrary/utils/basegzip.py +60 -60
  67. re_common/baselibrary/utils/basehdfs.py +135 -135
  68. re_common/baselibrary/utils/basehttpx.py +268 -268
  69. re_common/baselibrary/utils/baseip.py +87 -87
  70. re_common/baselibrary/utils/basejson.py +2 -2
  71. re_common/baselibrary/utils/baselist.py +32 -32
  72. re_common/baselibrary/utils/basemotor.py +190 -190
  73. re_common/baselibrary/utils/basemssql.py +98 -98
  74. re_common/baselibrary/utils/baseodbc.py +113 -113
  75. re_common/baselibrary/utils/basepandas.py +302 -302
  76. re_common/baselibrary/utils/basepeewee.py +11 -11
  77. re_common/baselibrary/utils/basepika.py +180 -180
  78. re_common/baselibrary/utils/basepydash.py +143 -143
  79. re_common/baselibrary/utils/basepymongo.py +230 -230
  80. re_common/baselibrary/utils/basequeue.py +22 -22
  81. re_common/baselibrary/utils/baserar.py +57 -57
  82. re_common/baselibrary/utils/baserequest.py +279 -279
  83. re_common/baselibrary/utils/baseset.py +8 -8
  84. re_common/baselibrary/utils/basesmb.py +403 -403
  85. re_common/baselibrary/utils/basestring.py +382 -382
  86. re_common/baselibrary/utils/basetime.py +320 -320
  87. re_common/baselibrary/utils/baseurl.py +121 -121
  88. re_common/baselibrary/utils/basezip.py +57 -57
  89. re_common/baselibrary/utils/core/__init__.py +7 -7
  90. re_common/baselibrary/utils/core/bottomutils.py +18 -18
  91. re_common/baselibrary/utils/core/mdeprecated.py +327 -327
  92. re_common/baselibrary/utils/core/mlamada.py +16 -16
  93. re_common/baselibrary/utils/core/msginfo.py +25 -25
  94. re_common/baselibrary/utils/core/requests_core.py +103 -103
  95. re_common/baselibrary/utils/fateadm.py +429 -429
  96. re_common/baselibrary/utils/importfun.py +123 -123
  97. re_common/baselibrary/utils/mfaker.py +57 -57
  98. re_common/baselibrary/utils/my_abc/__init__.py +3 -3
  99. re_common/baselibrary/utils/my_abc/better_abc.py +32 -32
  100. re_common/baselibrary/utils/mylogger.py +414 -414
  101. re_common/baselibrary/utils/myredisclient.py +861 -861
  102. re_common/baselibrary/utils/pipupgrade.py +21 -21
  103. re_common/baselibrary/utils/ringlist.py +85 -85
  104. re_common/baselibrary/utils/version_compare.py +36 -36
  105. re_common/baselibrary/utils/ydmhttp.py +126 -126
  106. re_common/facade/lazy_import.py +11 -11
  107. re_common/facade/loggerfacade.py +25 -25
  108. re_common/facade/mysqlfacade.py +467 -467
  109. re_common/facade/now.py +31 -31
  110. re_common/facade/sqlite3facade.py +257 -257
  111. re_common/facade/use/mq_use_facade.py +83 -83
  112. re_common/facade/use/proxy_use_facade.py +19 -19
  113. re_common/libtest/base_dict_test.py +19 -19
  114. re_common/libtest/baseavro_test.py +13 -13
  115. re_common/libtest/basefile_test.py +14 -14
  116. re_common/libtest/basemssql_test.py +77 -77
  117. re_common/libtest/baseodbc_test.py +7 -7
  118. re_common/libtest/basepandas_test.py +38 -38
  119. re_common/libtest/get_attr_test/get_attr_test_settings.py +14 -14
  120. re_common/libtest/get_attr_test/settings.py +54 -54
  121. re_common/libtest/idencode_test.py +53 -53
  122. re_common/libtest/iniconfig_test.py +35 -35
  123. re_common/libtest/ip_test.py +34 -34
  124. re_common/libtest/merge_file_test.py +20 -20
  125. re_common/libtest/mfaker_test.py +8 -8
  126. re_common/libtest/mm3_test.py +31 -31
  127. re_common/libtest/mylogger_test.py +88 -88
  128. re_common/libtest/myparsel_test.py +27 -27
  129. re_common/libtest/mysql_test.py +151 -151
  130. re_common/libtest/pymongo_test.py +21 -21
  131. re_common/libtest/split_test.py +11 -11
  132. re_common/libtest/sqlite3_merge_test.py +5 -5
  133. re_common/libtest/sqlite3_test.py +34 -34
  134. re_common/libtest/tomlconfig_test.py +30 -30
  135. re_common/libtest/use_tools_test/__init__.py +2 -2
  136. re_common/libtest/user/__init__.py +4 -4
  137. re_common/studio/__init__.py +4 -4
  138. re_common/studio/assignment_expressions.py +36 -36
  139. re_common/studio/mydash/test1.py +18 -18
  140. re_common/studio/pydashstudio/first.py +9 -9
  141. re_common/studio/streamlitstudio/first_app.py +65 -65
  142. re_common/studio/streamlitstudio/uber_pickups.py +23 -23
  143. re_common/studio/test.py +18 -18
  144. re_common/v2/baselibrary/business_utils/BusinessStringUtil.py +219 -219
  145. re_common/v2/baselibrary/business_utils/baseencodeid.py +100 -100
  146. re_common/v2/baselibrary/business_utils/full_doi_path.py +116 -116
  147. re_common/v2/baselibrary/business_utils/rel_tools.py +6 -6
  148. re_common/v2/baselibrary/decorators/utils.py +59 -59
  149. re_common/v2/baselibrary/helpers/search_packge/NearestNeighbors_test.py +105 -105
  150. re_common/v2/baselibrary/helpers/search_packge/fit_text_match.py +253 -253
  151. re_common/v2/baselibrary/helpers/search_packge/scikit_learn_text_matcher.py +260 -260
  152. re_common/v2/baselibrary/helpers/search_packge/test.py +1 -1
  153. re_common/v2/baselibrary/s3object/baseboto3.py +230 -230
  154. re_common/v2/baselibrary/tools/WeChatRobot.py +95 -95
  155. re_common/v2/baselibrary/tools/ac_ahocorasick.py +75 -75
  156. re_common/v2/baselibrary/tools/concurrency.py +35 -35
  157. re_common/v2/baselibrary/tools/data_processer/base.py +53 -53
  158. re_common/v2/baselibrary/tools/data_processer/data_processer.py +508 -508
  159. re_common/v2/baselibrary/tools/data_processer/data_reader.py +187 -187
  160. re_common/v2/baselibrary/tools/data_processer/data_writer.py +38 -38
  161. re_common/v2/baselibrary/tools/dict_tools.py +44 -44
  162. re_common/v2/baselibrary/tools/dolphinscheduler.py +187 -187
  163. re_common/v2/baselibrary/tools/hdfs_base_processor.py +204 -204
  164. re_common/v2/baselibrary/tools/hdfs_bulk_processor.py +67 -67
  165. re_common/v2/baselibrary/tools/hdfs_data_processer.py +338 -338
  166. re_common/v2/baselibrary/tools/hdfs_line_processor.py +74 -74
  167. re_common/v2/baselibrary/tools/list_tools.py +69 -69
  168. re_common/v2/baselibrary/tools/resume_tracker.py +94 -94
  169. re_common/v2/baselibrary/tools/search_hash_tools.py +54 -54
  170. re_common/v2/baselibrary/tools/text_matcher.py +326 -326
  171. re_common/v2/baselibrary/tools/unionfind_tools.py +60 -60
  172. re_common/v2/baselibrary/utils/BusinessStringUtil.py +196 -196
  173. re_common/v2/baselibrary/utils/api_net_utils.py +270 -270
  174. re_common/v2/baselibrary/utils/author_smi.py +361 -361
  175. re_common/v2/baselibrary/utils/base_string_similarity.py +158 -158
  176. re_common/v2/baselibrary/utils/basedict.py +37 -37
  177. re_common/v2/baselibrary/utils/basehdfs.py +163 -163
  178. re_common/v2/baselibrary/utils/basepika.py +180 -180
  179. re_common/v2/baselibrary/utils/basetime.py +77 -77
  180. re_common/v2/baselibrary/utils/db.py +156 -156
  181. re_common/v2/baselibrary/utils/elasticsearch.py +46 -0
  182. re_common/v2/baselibrary/utils/json_cls.py +16 -16
  183. re_common/v2/baselibrary/utils/mq.py +83 -83
  184. re_common/v2/baselibrary/utils/n_ary_expression_tree.py +243 -243
  185. re_common/v2/baselibrary/utils/string_bool.py +186 -186
  186. re_common/v2/baselibrary/utils/string_clear.py +246 -246
  187. re_common/v2/baselibrary/utils/string_smi.py +18 -18
  188. re_common/v2/baselibrary/utils/stringutils.py +271 -271
  189. re_common/vip/base_step_process.py +11 -11
  190. re_common/vip/baseencodeid.py +90 -90
  191. re_common/vip/changetaskname.py +28 -28
  192. re_common/vip/core_var.py +24 -24
  193. re_common/vip/mmh3Hash.py +89 -89
  194. re_common/vip/proxy/allproxys.py +127 -127
  195. re_common/vip/proxy/allproxys_thread.py +159 -159
  196. re_common/vip/proxy/cnki_proxy.py +153 -153
  197. re_common/vip/proxy/kuaidaili.py +87 -87
  198. re_common/vip/proxy/proxy_all.py +113 -113
  199. re_common/vip/proxy/update_kuaidaili_0.py +42 -42
  200. re_common/vip/proxy/wanfang_proxy.py +152 -152
  201. re_common/vip/proxy/wp_proxy_all.py +181 -181
  202. re_common/vip/read_rawid_to_txt.py +91 -91
  203. re_common/vip/title/__init__.py +5 -5
  204. re_common/vip/title/transform/TransformBookTitleToZt.py +125 -125
  205. re_common/vip/title/transform/TransformConferenceTitleToZt.py +139 -139
  206. re_common/vip/title/transform/TransformCstadTitleToZt.py +195 -195
  207. re_common/vip/title/transform/TransformJournalTitleToZt.py +203 -203
  208. re_common/vip/title/transform/TransformPatentTitleToZt.py +132 -132
  209. re_common/vip/title/transform/TransformRegulationTitleToZt.py +114 -114
  210. re_common/vip/title/transform/TransformStandardTitleToZt.py +135 -135
  211. re_common/vip/title/transform/TransformThesisTitleToZt.py +135 -135
  212. re_common/vip/title/transform/__init__.py +10 -10
  213. {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/LICENSE +201 -201
  214. {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/METADATA +24 -16
  215. re_common-10.0.40.dist-info/RECORD +249 -0
  216. {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/WHEEL +1 -1
  217. re_common-10.0.39.dist-info/RECORD +0 -248
  218. {re_common-10.0.39.dist-info → re_common-10.0.40.dist-info}/top_level.txt +0 -0
@@ -1,18 +1,18 @@
1
- from re_common.baselibrary.baseabs import BaseAbs
2
-
3
-
4
- class ConfigFactory(BaseAbs):
5
-
6
- @staticmethod
7
- def ini_factory(type='ini'):
8
- if type == 'ini':
9
- from re_common.baselibrary.readconfig.ini_config import IniConfig
10
- return IniConfig()
11
- assert 0, "err sql type please check: %s" % type
12
-
13
- @staticmethod
14
- def toml_factory(type='toml'):
15
- from re_common.baselibrary.readconfig.toml_config import TomlConfig
16
- if type == 'toml':
17
- return TomlConfig()
18
- assert 0, "err sql type please check: %s" % type
1
+ from re_common.baselibrary.baseabs import BaseAbs
2
+
3
+
4
+ class ConfigFactory(BaseAbs):
5
+
6
+ @staticmethod
7
+ def ini_factory(type='ini'):
8
+ if type == 'ini':
9
+ from re_common.baselibrary.readconfig.ini_config import IniConfig
10
+ return IniConfig()
11
+ assert 0, "err sql type please check: %s" % type
12
+
13
+ @staticmethod
14
+ def toml_factory(type='toml'):
15
+ from re_common.baselibrary.readconfig.toml_config import TomlConfig
16
+ if type == 'toml':
17
+ return TomlConfig()
18
+ assert 0, "err sql type please check: %s" % type
@@ -1,317 +1,317 @@
1
- """
2
- 1.基本的读取配置文件
3
-
4
- -read(filename) 直接读取ini文件内容
5
-
6
- -sections() 得到所有的section,并以列表的形式返回
7
-
8
- -options(section) 得到该section的所有option
9
-
10
- -items(section) 得到该section的所有键值对
11
-
12
- -get(section,option) 得到section中option的值,返回为string类型
13
-
14
- -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
15
-
16
- 2.基本的写入配置文件
17
-
18
- -add_section(section) 添加一个新的section
19
-
20
- -set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
21
- 如果配置文件如下
22
- [mysqllocalhost]
23
- host = 127.0.0.1
24
- port = 3306
25
- user = root
26
- passwd = xujiang1994323
27
- db = test.txt
28
- chartset = utf8mb4
29
-
30
-
31
- [mysqlfdfs]
32
- host = xujiang5.xicp.io
33
- port = 3307
34
- user = root
35
- passwd = xujiang1994323
36
- db = fdfs
37
- chartset = utf8
38
-
39
- """
40
- import configparser
41
- # 读取配置文件
42
- import sys
43
- import traceback
44
- import warnings
45
- from abc import ABCMeta, abstractmethod
46
- from collections import OrderedDict
47
- from configparser import DEFAULTSECT
48
-
49
-
50
- class ConfigParserMoudle():
51
- """
52
- config 解析的默认参数 防止以后需要使用 现在全部使用默认
53
- """
54
-
55
- def __init__(self):
56
- self.defaults = None
57
- self.allow_no_value = False
58
- self.delimiters = ('=', ':')
59
- self.comment_prefixes = ('#', ';')
60
- self.inline_comment_prefixes = None
61
- self.strict = True
62
- self.empty_lines_in_values = True
63
- self.default_section = DEFAULTSECT
64
-
65
- def to_dict(self):
66
- return self.__dict__
67
-
68
- def __str__(self):
69
- return str(self.__dict__)
70
-
71
-
72
- class ConfigParserBuilderAbstract:
73
- __metaclass__ = ABCMeta
74
-
75
- @abstractmethod
76
- def build_defaults(self):
77
- pass
78
-
79
- @abstractmethod
80
- def build_allow_no_value(self):
81
- pass
82
-
83
- @abstractmethod
84
- def build_delimiters(self):
85
- pass
86
-
87
- @abstractmethod
88
- def build_comment_prefixes(self):
89
- pass
90
-
91
- @abstractmethod
92
- def use_inline_comment_prefixes(self):
93
- pass
94
-
95
- @abstractmethod
96
- def build_strict(self):
97
- pass
98
-
99
- @abstractmethod
100
- def use_empty_lines_in_values(self):
101
- pass
102
-
103
- @abstractmethod
104
- def build_default_section(self):
105
- pass
106
-
107
- @abstractmethod
108
- def get_moudle(self):
109
- pass
110
-
111
-
112
- default_config_parser_moudle = ConfigParserMoudle()
113
-
114
-
115
- class MyParser(configparser.ConfigParser):
116
- """
117
- 继承了ConfigParser 方便以后重写
118
- """
119
-
120
- def __init__(self, configmoudle: ConfigParserMoudle):
121
- super(MyParser, self).__init__(**configmoudle.to_dict())
122
-
123
-
124
- class IniConfig(object):
125
-
126
- def __init__(self, configpath=''):
127
- # 配置文件路径
128
- self.configpath = configpath
129
- # 指定的moudle格式 无特殊需求不用传入
130
- self.moudle = default_config_parser_moudle
131
- self.keep_keys_case = True
132
- self.encoding = "utf-8"
133
-
134
- def set_encoding(self, encoding="utf-8"):
135
- """
136
- 设置读取文件编码
137
- :param encoding:
138
- :return:
139
- """
140
- self.encoding = encoding
141
- return self
142
-
143
- def set_keep_keys_case(self, keep_keys_case=True):
144
- """
145
- 保持option的大小写
146
- :return:
147
- """
148
- self.keep_keys_case = keep_keys_case
149
- return self
150
-
151
- def set_config_path(self, configPath):
152
- """
153
- 设置config文件路径
154
- :param configPath:
155
- :return:
156
- """
157
- self.configpath = configPath
158
- return self
159
-
160
- def set_configmoudle(self, configmoudle: ConfigParserMoudle):
161
- """
162
- 设置 modudle,一般情况下不需要传入设置 使用默认设置即可
163
- :param configmoudle:
164
- :return:
165
- """
166
- self.moudle = configmoudle
167
- return self
168
-
169
- def read_config(self):
170
- """
171
- 读取配置文件 这样cf本身就是一个字典
172
- :param encoding:
173
- :param configPath:
174
- :return:
175
- """
176
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
177
- try:
178
- self.cf.read(self.configpath, encoding=self.encoding)
179
- except:
180
- print('读取配置失败:' + traceback.format_exc())
181
- sys.exit(-1)
182
- return self
183
-
184
- def read_config_string(self):
185
- """
186
- 读取配置文件 将文件读成一个string传入
187
- :param cfstr:
188
- :return:
189
- """
190
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
191
- try:
192
- with open(self.configpath, mode='rb') as f:
193
- content = f.read()
194
- if content.startswith(b'\xef\xbb\xbf'): # 去掉 utf8 bom 头
195
- content = content[3:]
196
- self.cf.read_string(content.decode(self.encoding))
197
- except:
198
- print('读取配置失败:' + traceback.format_exc())
199
- sys.exit(-1)
200
- return self
201
-
202
- def builder(self, is_read_str=False):
203
- self.cf = MyParser(self.moudle)
204
- if self.keep_keys_case:
205
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
206
- self.cf.optionxform = str
207
- if is_read_str:
208
- self.read_config_string()
209
- else:
210
- self.read_config()
211
- return self
212
-
213
- def get_cf(self):
214
- """
215
- 获取config的对象对其操作
216
- :return:
217
- """
218
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
219
- return self.cf
220
-
221
- def get_all_sections(self) -> list:
222
- """
223
- 传入参数为configparser.ConfigParser
224
- 返回为 list
225
- 该函数得到所有sections 即配置文件中 [mysqlfdfs] 这种
226
- 是配置文件的一级参数
227
- :param cf:
228
- :return:
229
- """
230
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
231
- secs = self.cf.sections()
232
- if not secs:
233
- warnings.warn("sections is null,maybe your are cionfig path is wrong")
234
- return secs
235
-
236
- def get_sesc_options(self, sesc_str: str) -> list:
237
- """
238
- 返回为 list
239
- 该函数得到指定sections的options
240
- 像 host 这种
241
- 是配置文件的一级参数
242
- :param sesc_str:
243
- :return:
244
- """
245
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
246
- opts = self.cf.options(sesc_str)
247
- if not opts:
248
- warnings.warn("opts is null,maybe your are sesc_str is wrong")
249
- return opts
250
-
251
- def get_items(self, sesc: str):
252
- """
253
- :return: [('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('passwd', 'xujiang1994323'), ('db', 'test.txt'), ('chartset', 'utf8mb4')]
254
- """
255
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
256
- kvs = self.cf.items(sesc)
257
- return kvs
258
-
259
- def get_value(self, sec_str: str, opt_key: str) -> str:
260
- """
261
- 获取值 str
262
- :param cf:
263
- :param sec_str:
264
- :param opt_key:
265
- :return:
266
- """
267
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
268
- str_value = self.cf.get(sec_str, opt_key)
269
- return str_value
270
-
271
- def get_value_int(self, sec_str: str, opt_key: str) -> int:
272
- """
273
- 获取配置文件的值按Int形式
274
- :param cf:
275
- :param sec_str:
276
- :param opt_key:
277
- :return:
278
- """
279
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
280
- int_value = self.cf.getint(sec_str, opt_key)
281
- return int_value
282
-
283
- def set_value(self, sec_str: str, opt_key: str, opt_value: str, filePath: str):
284
- """
285
- 设置或者更新值
286
- :param cf:
287
- :param sec_str:
288
- :param opt_key:
289
- :param opt_value:
290
- :return:
291
- """
292
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
293
- self.cf.set(sec_str, opt_key, opt_value)
294
- self.cf.write(open(filePath, "w"))
295
-
296
- def add_new_section(self, secs: str, filePath: str):
297
- """
298
- 设置section
299
- :param secs:
300
- :param filePath:
301
- :return:
302
- """
303
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
304
- self.cf.add_section(secs)
305
- self.cf.write(open(filePath, "w"))
306
-
307
- def get_config_dict(self) -> OrderedDict:
308
- """
309
- 读取文件转dict
310
- :param cf:
311
- :return:
312
- """
313
- assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
314
- d = OrderedDict(self.cf._sections)
315
- for k in d:
316
- d[k] = OrderedDict(d[k])
317
- return d
1
+ """
2
+ 1.基本的读取配置文件
3
+
4
+ -read(filename) 直接读取ini文件内容
5
+
6
+ -sections() 得到所有的section,并以列表的形式返回
7
+
8
+ -options(section) 得到该section的所有option
9
+
10
+ -items(section) 得到该section的所有键值对
11
+
12
+ -get(section,option) 得到section中option的值,返回为string类型
13
+
14
+ -getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
15
+
16
+ 2.基本的写入配置文件
17
+
18
+ -add_section(section) 添加一个新的section
19
+
20
+ -set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。
21
+ 如果配置文件如下
22
+ [mysqllocalhost]
23
+ host = 127.0.0.1
24
+ port = 3306
25
+ user = root
26
+ passwd = xujiang1994323
27
+ db = test.txt
28
+ chartset = utf8mb4
29
+
30
+
31
+ [mysqlfdfs]
32
+ host = xujiang5.xicp.io
33
+ port = 3307
34
+ user = root
35
+ passwd = xujiang1994323
36
+ db = fdfs
37
+ chartset = utf8
38
+
39
+ """
40
+ import configparser
41
+ # 读取配置文件
42
+ import sys
43
+ import traceback
44
+ import warnings
45
+ from abc import ABCMeta, abstractmethod
46
+ from collections import OrderedDict
47
+ from configparser import DEFAULTSECT
48
+
49
+
50
+ class ConfigParserMoudle():
51
+ """
52
+ config 解析的默认参数 防止以后需要使用 现在全部使用默认
53
+ """
54
+
55
+ def __init__(self):
56
+ self.defaults = None
57
+ self.allow_no_value = False
58
+ self.delimiters = ('=', ':')
59
+ self.comment_prefixes = ('#', ';')
60
+ self.inline_comment_prefixes = None
61
+ self.strict = True
62
+ self.empty_lines_in_values = True
63
+ self.default_section = DEFAULTSECT
64
+
65
+ def to_dict(self):
66
+ return self.__dict__
67
+
68
+ def __str__(self):
69
+ return str(self.__dict__)
70
+
71
+
72
+ class ConfigParserBuilderAbstract:
73
+ __metaclass__ = ABCMeta
74
+
75
+ @abstractmethod
76
+ def build_defaults(self):
77
+ pass
78
+
79
+ @abstractmethod
80
+ def build_allow_no_value(self):
81
+ pass
82
+
83
+ @abstractmethod
84
+ def build_delimiters(self):
85
+ pass
86
+
87
+ @abstractmethod
88
+ def build_comment_prefixes(self):
89
+ pass
90
+
91
+ @abstractmethod
92
+ def use_inline_comment_prefixes(self):
93
+ pass
94
+
95
+ @abstractmethod
96
+ def build_strict(self):
97
+ pass
98
+
99
+ @abstractmethod
100
+ def use_empty_lines_in_values(self):
101
+ pass
102
+
103
+ @abstractmethod
104
+ def build_default_section(self):
105
+ pass
106
+
107
+ @abstractmethod
108
+ def get_moudle(self):
109
+ pass
110
+
111
+
112
+ default_config_parser_moudle = ConfigParserMoudle()
113
+
114
+
115
+ class MyParser(configparser.ConfigParser):
116
+ """
117
+ 继承了ConfigParser 方便以后重写
118
+ """
119
+
120
+ def __init__(self, configmoudle: ConfigParserMoudle):
121
+ super(MyParser, self).__init__(**configmoudle.to_dict())
122
+
123
+
124
+ class IniConfig(object):
125
+
126
+ def __init__(self, configpath=''):
127
+ # 配置文件路径
128
+ self.configpath = configpath
129
+ # 指定的moudle格式 无特殊需求不用传入
130
+ self.moudle = default_config_parser_moudle
131
+ self.keep_keys_case = True
132
+ self.encoding = "utf-8"
133
+
134
+ def set_encoding(self, encoding="utf-8"):
135
+ """
136
+ 设置读取文件编码
137
+ :param encoding:
138
+ :return:
139
+ """
140
+ self.encoding = encoding
141
+ return self
142
+
143
+ def set_keep_keys_case(self, keep_keys_case=True):
144
+ """
145
+ 保持option的大小写
146
+ :return:
147
+ """
148
+ self.keep_keys_case = keep_keys_case
149
+ return self
150
+
151
+ def set_config_path(self, configPath):
152
+ """
153
+ 设置config文件路径
154
+ :param configPath:
155
+ :return:
156
+ """
157
+ self.configpath = configPath
158
+ return self
159
+
160
+ def set_configmoudle(self, configmoudle: ConfigParserMoudle):
161
+ """
162
+ 设置 modudle,一般情况下不需要传入设置 使用默认设置即可
163
+ :param configmoudle:
164
+ :return:
165
+ """
166
+ self.moudle = configmoudle
167
+ return self
168
+
169
+ def read_config(self):
170
+ """
171
+ 读取配置文件 这样cf本身就是一个字典
172
+ :param encoding:
173
+ :param configPath:
174
+ :return:
175
+ """
176
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
177
+ try:
178
+ self.cf.read(self.configpath, encoding=self.encoding)
179
+ except:
180
+ print('读取配置失败:' + traceback.format_exc())
181
+ sys.exit(-1)
182
+ return self
183
+
184
+ def read_config_string(self):
185
+ """
186
+ 读取配置文件 将文件读成一个string传入
187
+ :param cfstr:
188
+ :return:
189
+ """
190
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
191
+ try:
192
+ with open(self.configpath, mode='rb') as f:
193
+ content = f.read()
194
+ if content.startswith(b'\xef\xbb\xbf'): # 去掉 utf8 bom 头
195
+ content = content[3:]
196
+ self.cf.read_string(content.decode(self.encoding))
197
+ except:
198
+ print('读取配置失败:' + traceback.format_exc())
199
+ sys.exit(-1)
200
+ return self
201
+
202
+ def builder(self, is_read_str=False):
203
+ self.cf = MyParser(self.moudle)
204
+ if self.keep_keys_case:
205
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
206
+ self.cf.optionxform = str
207
+ if is_read_str:
208
+ self.read_config_string()
209
+ else:
210
+ self.read_config()
211
+ return self
212
+
213
+ def get_cf(self):
214
+ """
215
+ 获取config的对象对其操作
216
+ :return:
217
+ """
218
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
219
+ return self.cf
220
+
221
+ def get_all_sections(self) -> list:
222
+ """
223
+ 传入参数为configparser.ConfigParser
224
+ 返回为 list
225
+ 该函数得到所有sections 即配置文件中 [mysqlfdfs] 这种
226
+ 是配置文件的一级参数
227
+ :param cf:
228
+ :return:
229
+ """
230
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
231
+ secs = self.cf.sections()
232
+ if not secs:
233
+ warnings.warn("sections is null,maybe your are cionfig path is wrong")
234
+ return secs
235
+
236
+ def get_sesc_options(self, sesc_str: str) -> list:
237
+ """
238
+ 返回为 list
239
+ 该函数得到指定sections的options
240
+ 像 host 这种
241
+ 是配置文件的一级参数
242
+ :param sesc_str:
243
+ :return:
244
+ """
245
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
246
+ opts = self.cf.options(sesc_str)
247
+ if not opts:
248
+ warnings.warn("opts is null,maybe your are sesc_str is wrong")
249
+ return opts
250
+
251
+ def get_items(self, sesc: str):
252
+ """
253
+ :return: [('host', '127.0.0.1'), ('port', '3306'), ('user', 'root'), ('passwd', 'xujiang1994323'), ('db', 'test.txt'), ('chartset', 'utf8mb4')]
254
+ """
255
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
256
+ kvs = self.cf.items(sesc)
257
+ return kvs
258
+
259
+ def get_value(self, sec_str: str, opt_key: str) -> str:
260
+ """
261
+ 获取值 str
262
+ :param cf:
263
+ :param sec_str:
264
+ :param opt_key:
265
+ :return:
266
+ """
267
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
268
+ str_value = self.cf.get(sec_str, opt_key)
269
+ return str_value
270
+
271
+ def get_value_int(self, sec_str: str, opt_key: str) -> int:
272
+ """
273
+ 获取配置文件的值按Int形式
274
+ :param cf:
275
+ :param sec_str:
276
+ :param opt_key:
277
+ :return:
278
+ """
279
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
280
+ int_value = self.cf.getint(sec_str, opt_key)
281
+ return int_value
282
+
283
+ def set_value(self, sec_str: str, opt_key: str, opt_value: str, filePath: str):
284
+ """
285
+ 设置或者更新值
286
+ :param cf:
287
+ :param sec_str:
288
+ :param opt_key:
289
+ :param opt_value:
290
+ :return:
291
+ """
292
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
293
+ self.cf.set(sec_str, opt_key, opt_value)
294
+ self.cf.write(open(filePath, "w"))
295
+
296
+ def add_new_section(self, secs: str, filePath: str):
297
+ """
298
+ 设置section
299
+ :param secs:
300
+ :param filePath:
301
+ :return:
302
+ """
303
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
304
+ self.cf.add_section(secs)
305
+ self.cf.write(open(filePath, "w"))
306
+
307
+ def get_config_dict(self) -> OrderedDict:
308
+ """
309
+ 读取文件转dict
310
+ :param cf:
311
+ :return:
312
+ """
313
+ assert self.cf, AttributeError("还没有调用get_configparser为其赋值 请先调用该函数")
314
+ d = OrderedDict(self.cf._sections)
315
+ for k in d:
316
+ d[k] = OrderedDict(d[k])
317
+ return d