mdbq 4.0.22__py3-none-any.whl → 4.0.23__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.
- mdbq/__version__.py +1 -1
- mdbq/myconf/myconf2.py +39 -65
- {mdbq-4.0.22.dist-info → mdbq-4.0.23.dist-info}/METADATA +1 -1
- {mdbq-4.0.22.dist-info → mdbq-4.0.23.dist-info}/RECORD +6 -6
- {mdbq-4.0.22.dist-info → mdbq-4.0.23.dist-info}/WHEEL +0 -0
- {mdbq-4.0.22.dist-info → mdbq-4.0.23.dist-info}/top_level.txt +0 -0
mdbq/__version__.py
CHANGED
@@ -1 +1 @@
|
|
1
|
-
VERSION = '4.0.
|
1
|
+
VERSION = '4.0.23'
|
mdbq/myconf/myconf2.py
CHANGED
@@ -233,7 +233,7 @@ class ConfigParser:
|
|
233
233
|
self._comments_cache.clear()
|
234
234
|
self._section_map.clear()
|
235
235
|
|
236
|
-
def _convert_value(self, value: str, target_type: Type[T]) -> T:
|
236
|
+
def _convert_value(self, value: str, target_type: Type[T], file_path: Optional[Union[str, Path]] = None, key: Optional[str] = None) -> T:
|
237
237
|
"""转换配置值到指定类型"""
|
238
238
|
try:
|
239
239
|
if target_type == bool:
|
@@ -252,13 +252,14 @@ class ConfigParser:
|
|
252
252
|
if sep in value:
|
253
253
|
return tuple(item.strip() for item in value.split(sep) if item.strip())
|
254
254
|
return (value.strip(),)
|
255
|
-
elif target_type == set:
|
255
|
+
elif target_type == set or target_type == frozenset:
|
256
256
|
if not value.strip():
|
257
|
-
return set()
|
257
|
+
return set() if target_type == set else frozenset()
|
258
258
|
for sep in [',', ';', '|', ' ']:
|
259
259
|
if sep in value:
|
260
|
-
|
261
|
-
|
260
|
+
items = [item.strip() for item in value.split(sep) if item.strip()]
|
261
|
+
return set(items) if target_type == set else frozenset(items)
|
262
|
+
return set([value.strip()]) if target_type == set else frozenset([value.strip()])
|
262
263
|
elif target_type == dict:
|
263
264
|
if not value.strip():
|
264
265
|
return {}
|
@@ -268,12 +269,12 @@ class ConfigParser:
|
|
268
269
|
pairs = [pair.strip() for pair in value.split(sep) if pair.strip()]
|
269
270
|
for pair in pairs:
|
270
271
|
if '=' in pair:
|
271
|
-
|
272
|
-
result[
|
272
|
+
key_, val = pair.split('=', 1)
|
273
|
+
result[key_.strip()] = val.strip()
|
273
274
|
return result
|
274
275
|
if '=' in value:
|
275
|
-
|
276
|
-
return {
|
276
|
+
key_, val = value.split('=', 1)
|
277
|
+
return {key_.strip(): val.strip()}
|
277
278
|
return {}
|
278
279
|
elif target_type == int:
|
279
280
|
value = value.strip().lower()
|
@@ -292,10 +293,6 @@ class ConfigParser:
|
|
292
293
|
return value.encode('utf-8')
|
293
294
|
elif target_type == bytearray:
|
294
295
|
return bytearray(value.encode('utf-8'))
|
295
|
-
elif target_type == set:
|
296
|
-
return set(value.split(','))
|
297
|
-
elif target_type == frozenset:
|
298
|
-
return frozenset(value.split(','))
|
299
296
|
elif target_type == range:
|
300
297
|
parts = value.split(':')
|
301
298
|
if len(parts) == 2:
|
@@ -307,8 +304,8 @@ class ConfigParser:
|
|
307
304
|
except (ValueError, TypeError) as e:
|
308
305
|
raise ConfigValueError(
|
309
306
|
f"无法将值 '{value}' 转换为类型 {target_type.__name__}",
|
310
|
-
file_path=
|
311
|
-
key=
|
307
|
+
file_path=file_path,
|
308
|
+
key=key
|
312
309
|
)
|
313
310
|
|
314
311
|
def get_value(self, file_path: Optional[Union[str, Path]] = None, key: str = None,
|
@@ -320,27 +317,21 @@ class ConfigParser:
|
|
320
317
|
file_path = self._current_file
|
321
318
|
if not self._validate_key(key):
|
322
319
|
raise ConfigValueError(f"无效的键名: {key}", file_path=file_path, key=key)
|
323
|
-
|
324
320
|
config = self.read(file_path)
|
325
321
|
section = section or self.options.default_section
|
326
322
|
normalized_section = self._normalize_section(section)
|
327
|
-
|
328
323
|
original_section = self._get_original_section(str(file_path), normalized_section)
|
329
324
|
if original_section is None:
|
330
325
|
if default is not None:
|
331
326
|
return default
|
332
327
|
raise ConfigSectionNotFoundError(file_path, section)
|
333
|
-
|
334
328
|
if key not in config[original_section]:
|
335
329
|
if default is not None:
|
336
330
|
return default
|
337
331
|
raise ConfigKeyNotFoundError(file_path, original_section, key)
|
338
|
-
|
339
332
|
value = config[original_section][key]
|
340
|
-
|
341
333
|
if value_type is not None:
|
342
|
-
return self._convert_value(value, value_type)
|
343
|
-
|
334
|
+
return self._convert_value(value, value_type, file_path=file_path, key=key)
|
344
335
|
return value
|
345
336
|
|
346
337
|
def get_values(self, keys: List[Tuple[str, str]],
|
@@ -414,17 +405,14 @@ class ConfigParser:
|
|
414
405
|
file_path = self._current_file
|
415
406
|
if not self._validate_key(key):
|
416
407
|
raise ConfigValueError(f"无效的键名: {key}", file_path=file_path, key=key)
|
417
|
-
|
408
|
+
section = section or self.options.default_section
|
418
409
|
original_lines = []
|
419
410
|
if file_path.exists():
|
420
411
|
with open(file_path, 'r', encoding=self.options.encoding) as file:
|
421
412
|
original_lines = file.readlines()
|
422
|
-
|
423
413
|
config = self.read(file_path)
|
424
|
-
|
425
414
|
if section not in config:
|
426
415
|
config[section] = {}
|
427
|
-
|
428
416
|
if value_type is not None:
|
429
417
|
try:
|
430
418
|
if value_type == bool:
|
@@ -441,37 +429,28 @@ class ConfigParser:
|
|
441
429
|
section=section,
|
442
430
|
key=key
|
443
431
|
)
|
444
|
-
|
445
432
|
if isinstance(value, bool):
|
446
433
|
value = str(value).lower()
|
447
434
|
else:
|
448
435
|
value = str(value)
|
449
|
-
|
450
436
|
config[section][key] = value
|
451
|
-
|
452
437
|
try:
|
453
438
|
file_path.parent.mkdir(parents=True, exist_ok=True)
|
454
|
-
|
455
439
|
with open(file_path, 'w', encoding=self.options.encoding) as file:
|
456
440
|
current_section = self.options.default_section
|
457
441
|
section_separators = {}
|
458
|
-
|
459
442
|
for line in original_lines:
|
460
443
|
stripped_line = line.strip()
|
461
|
-
|
462
444
|
if not stripped_line:
|
463
445
|
file.write(line)
|
464
446
|
continue
|
465
|
-
|
466
447
|
if stripped_line.startswith('[') and stripped_line.endswith(']'):
|
467
448
|
current_section = stripped_line[1:-1]
|
468
449
|
file.write(line)
|
469
450
|
continue
|
470
|
-
|
471
451
|
if self._is_comment_line(stripped_line):
|
472
452
|
file.write(line)
|
473
453
|
continue
|
474
|
-
|
475
454
|
key_value = self._split_key_value(stripped_line)
|
476
455
|
if key_value:
|
477
456
|
orig_key, orig_value = key_value
|
@@ -479,7 +458,6 @@ class ConfigParser:
|
|
479
458
|
if sep in line:
|
480
459
|
section_separators.setdefault(current_section, {})[orig_key] = sep
|
481
460
|
break
|
482
|
-
|
483
461
|
if current_section == section and orig_key == key:
|
484
462
|
separator = section_separators.get(current_section, {}).get(orig_key, self.options.separators[0])
|
485
463
|
comment = ''
|
@@ -493,13 +471,10 @@ class ConfigParser:
|
|
493
471
|
file.write(line)
|
494
472
|
else:
|
495
473
|
file.write(line)
|
496
|
-
|
497
474
|
if section not in [line.strip()[1:-1] for line in original_lines if line.strip().startswith('[') and line.strip().endswith(']')]:
|
498
475
|
file.write(f'\n[{section}]\n')
|
499
476
|
file.write(f'{key}={value}\n')
|
500
|
-
|
501
477
|
self._clear_cache(str(file_path))
|
502
|
-
|
503
478
|
except Exception as e:
|
504
479
|
raise ConfigWriteError(file_path, e)
|
505
480
|
|
@@ -585,35 +560,34 @@ class ConfigParser:
|
|
585
560
|
def main() -> None:
|
586
561
|
"""示例用法"""
|
587
562
|
config_file = Path('/Users/xigua/spd.txt')
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
563
|
+
try:
|
564
|
+
# 方式1:使用上下文管理器
|
565
|
+
with ConfigParser() as parser:
|
566
|
+
parser.open(config_file)
|
567
|
+
host, port, username, password = parser.get_section_values(
|
568
|
+
keys=['host', 'port', 'username', 'password'],
|
569
|
+
section='mysql'
|
570
|
+
)
|
571
|
+
print("方式1结果:", host, port, username, password)
|
572
|
+
parser.set_value('username', 'root', section='mysql')
|
573
|
+
parser.set_value('port', 3306, section='mysql')
|
574
|
+
# 方式2:链式调用
|
575
|
+
parser = ConfigParser()
|
576
|
+
host, port, username, password = parser.open(config_file).get_section_values(
|
593
577
|
keys=['host', 'port', 'username', 'password'],
|
594
578
|
section='mysql'
|
595
579
|
)
|
596
|
-
print("方式
|
597
|
-
|
598
|
-
parser
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
print("\n方式2结果:", host, port, username, password)
|
608
|
-
|
609
|
-
# 方式3:传统方式
|
610
|
-
parser = ConfigParser()
|
611
|
-
host, port, username, password = parser.get_section_values(
|
612
|
-
file_path=config_file,
|
613
|
-
section='mysql',
|
614
|
-
keys=['host', 'port', 'username', 'password']
|
615
|
-
)
|
616
|
-
print("\n方式3结果:", host, port, username, password)
|
580
|
+
print("\n方式2结果:", host, port, username, password)
|
581
|
+
# 方式3:传统方式
|
582
|
+
parser = ConfigParser()
|
583
|
+
host, port, username, password = parser.get_section_values(
|
584
|
+
file_path=config_file,
|
585
|
+
section='mysql',
|
586
|
+
keys=['host', 'port', 'username', 'password']
|
587
|
+
)
|
588
|
+
print("\n方式3结果:", host, port, username, password)
|
589
|
+
except Exception as e:
|
590
|
+
print(f"配置文件操作异常: {e}")
|
617
591
|
|
618
592
|
|
619
593
|
if __name__ == '__main__':
|
@@ -1,12 +1,12 @@
|
|
1
1
|
mdbq/__init__.py,sha256=Il5Q9ATdX8yXqVxtP_nYqUhExzxPC_qk_WXQ_4h0exg,16
|
2
|
-
mdbq/__version__.py,sha256=
|
2
|
+
mdbq/__version__.py,sha256=DL1LeguYb9YeIqFpwczMwHkRTX7qAUfv5A5WC00M-Po,18
|
3
3
|
mdbq/aggregation/__init__.py,sha256=EeDqX2Aml6SPx8363J-v1lz0EcZtgwIBYyCJV6CcEDU,40
|
4
4
|
mdbq/aggregation/query_data.py,sha256=hdaB0vPh5BcTu9kLViRvM7OAE0b07D4jzAIipqxGI-I,166757
|
5
5
|
mdbq/log/__init__.py,sha256=Mpbrav0s0ifLL7lVDAuePEi1hJKiSHhxcv1byBKDl5E,15
|
6
6
|
mdbq/log/mylogger.py,sha256=9w_o5mYB3FooIxobq_lSa6oCYTKIhPxDFox-jeLtUHI,21714
|
7
7
|
mdbq/myconf/__init__.py,sha256=jso1oHcy6cJEfa7udS_9uO5X6kZLoPBF8l3wCYmr5dM,18
|
8
8
|
mdbq/myconf/myconf.py,sha256=39tLUBVlWQZzQfrwk7YoLEfipo11fpwWjaLBHcUt2qM,33341
|
9
|
-
mdbq/myconf/myconf2.py,sha256=
|
9
|
+
mdbq/myconf/myconf2.py,sha256=LQPNO5c9uATglZ1IrOgGslj6aSuAfpBXgHnPwvCrHmA,25482
|
10
10
|
mdbq/mysql/__init__.py,sha256=A_DPJyAoEvTSFojiI2e94zP0FKtCkkwKP1kYUCSyQzo,11
|
11
11
|
mdbq/mysql/deduplicator.py,sha256=kAnkI_vnN8CchgDQAFzeh0M0vLXE2oWq9SfDPNZZ3v0,73215
|
12
12
|
mdbq/mysql/mysql.py,sha256=pDg771xBugCMSTWeskIFTi3pFLgaqgyG3smzf-86Wn8,56772
|
@@ -25,7 +25,7 @@ mdbq/redis/__init__.py,sha256=YtgBlVSMDphtpwYX248wGge1x-Ex_mMufz4-8W0XRmA,12
|
|
25
25
|
mdbq/redis/getredis.py,sha256=vpBuNc22uj9Vr-_Dh25_wpwWM1e-072EAAIBdB_IpL0,23494
|
26
26
|
mdbq/spider/__init__.py,sha256=RBMFXGy_jd1HXZhngB2T2XTvJqki8P_Fr-pBcwijnew,18
|
27
27
|
mdbq/spider/aikucun.py,sha256=juOqpr_dHeE1RyjCu67VcpzoJAWMO7FKv0i8KiH8WUo,21552
|
28
|
-
mdbq-4.0.
|
29
|
-
mdbq-4.0.
|
30
|
-
mdbq-4.0.
|
31
|
-
mdbq-4.0.
|
28
|
+
mdbq-4.0.23.dist-info/METADATA,sha256=Dx9QagRQoN_ttdDOi6RnTKDgSzfqBqn0bp7ozDrZq7Q,364
|
29
|
+
mdbq-4.0.23.dist-info/WHEEL,sha256=jB7zZ3N9hIM9adW7qlTAyycLYW9npaWKLRzaoVcLKcM,91
|
30
|
+
mdbq-4.0.23.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
|
31
|
+
mdbq-4.0.23.dist-info/RECORD,,
|
File without changes
|
File without changes
|