mdbq 4.0.30__py3-none-any.whl → 4.0.32__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/myconf/myconf_bak.py DELETED
@@ -1,816 +0,0 @@
1
- import re
2
- from typing import Dict, Any, Optional, Union, List, Tuple, Type, TypeVar
3
- from pathlib import Path
4
- from mdbq.log import mylogger
5
- from dataclasses import dataclass, field
6
- from enum import Enum
7
- import time
8
-
9
- logger = mylogger.MyLogger(
10
- logging_mode='both',
11
- log_level='info',
12
- log_format='json',
13
- max_log_size=50,
14
- backup_count=5,
15
- enable_async=False, # 是否启用异步日志
16
- sample_rate=1, # 采样DEBUG/INFO日志
17
- sensitive_fields=[], # 敏感字段过滤
18
- enable_metrics=False, # 是否启用性能指标
19
- )
20
-
21
- T = TypeVar('T') # 类型变量
22
-
23
-
24
- class ConfigError(Exception):
25
- """配置相关的基础异常类
26
-
27
- Attributes:
28
- message: 错误消息
29
- file_path: 配置文件路径
30
- section: 配置节名称
31
- key: 配置键名称
32
- """
33
- def __init__(self, message: str, file_path: Optional[Union[str, Path]] = None,
34
- section: Optional[str] = None, key: Optional[str] = None):
35
- self.message = message
36
- self.file_path = str(file_path) if file_path else None
37
- self.section = section
38
- self.key = key
39
- super().__init__(self._format_message())
40
-
41
- def _format_message(self) -> str:
42
- """格式化错误消息"""
43
- parts = [self.message]
44
- if self.file_path:
45
- parts.append(f"文件: {self.file_path}")
46
- if self.section:
47
- parts.append(f"节: [{self.section}]")
48
- if self.key:
49
- parts.append(f"键: {self.key}")
50
- return " | ".join(parts)
51
-
52
-
53
- class ConfigFileNotFoundError(ConfigError):
54
- """当指定的配置文件不存在时抛出的异常"""
55
- def __init__(self, file_path: Union[str, Path]):
56
- super().__init__("配置文件不存在", file_path=file_path)
57
-
58
-
59
- class ConfigReadError(ConfigError):
60
- """当读取配置文件失败时抛出的异常
61
-
62
- Attributes:
63
- original_error: 原始错误对象
64
- """
65
- def __init__(self, file_path: Union[str, Path], original_error: Exception):
66
- super().__init__(
67
- f"读取配置文件失败: {str(original_error)}",
68
- file_path=file_path
69
- )
70
- self.original_error = original_error
71
-
72
-
73
- class ConfigWriteError(ConfigError):
74
- """当写入配置文件失败时抛出的异常
75
-
76
- Attributes:
77
- original_error: 原始错误对象
78
- """
79
- def __init__(self, file_path: Union[str, Path], original_error: Exception):
80
- super().__init__(
81
- f"写入配置文件失败: {str(original_error)}",
82
- file_path=file_path
83
- )
84
- self.original_error = original_error
85
-
86
-
87
- class ConfigValueError(ConfigError):
88
- """当配置值无效时抛出的异常"""
89
- def __init__(self, message: str, file_path: Union[str, Path],
90
- section: Optional[str] = None, key: Optional[str] = None):
91
- super().__init__(message, file_path=file_path, section=section, key=key)
92
-
93
-
94
- class ConfigSectionNotFoundError(ConfigError):
95
- """当指定的配置节不存在时抛出的异常"""
96
- def __init__(self, file_path: Union[str, Path], section: str):
97
- super().__init__(
98
- f"配置节不存在",
99
- file_path=file_path,
100
- section=section
101
- )
102
-
103
-
104
- class ConfigKeyNotFoundError(ConfigError):
105
- """当指定的配置键不存在时抛出的异常"""
106
- def __init__(self, file_path: Union[str, Path], section: str, key: str):
107
- super().__init__(
108
- f"配置键不存在",
109
- file_path=file_path,
110
- section=section,
111
- key=key
112
- )
113
-
114
-
115
- class CommentStyle(Enum):
116
- """配置文件支持的注释风格"""
117
- HASH = '#' # Python风格注释
118
- DOUBLE_SLASH = '//' # C风格注释
119
- SEMICOLON = ';' # INI风格注释
120
-
121
-
122
- @dataclass
123
- class ConfigOptions:
124
- """配置解析器的选项类
125
-
126
- Attributes:
127
- comment_styles: 支持的注释风格列表
128
- encoding: 文件编码
129
- auto_create: 是否自动创建不存在的配置文件
130
- strip_values: 是否去除配置值的首尾空白
131
- preserve_comments: 是否保留注释
132
- default_section: 默认配置节名称
133
- separators: 支持的分隔符列表
134
- cache_ttl: 缓存过期时间(秒)
135
- validate_keys: 是否验证键名
136
- key_pattern: 键名正则表达式模式
137
- case_sensitive: 是否区分大小写
138
- """
139
- comment_styles: List[CommentStyle] = field(default_factory=lambda: [CommentStyle.HASH, CommentStyle.DOUBLE_SLASH])
140
- encoding: str = 'utf-8'
141
- auto_create: bool = False
142
- strip_values: bool = True
143
- preserve_comments: bool = True
144
- default_section: str = 'DEFAULT'
145
- separators: List[str] = field(default_factory=lambda: ['=', ':', ':'])
146
- cache_ttl: int = 300 # 5分钟缓存过期
147
- validate_keys: bool = True
148
- key_pattern: str = r'^[a-zA-Z0-9_\-\.]+$'
149
- case_sensitive: bool = False
150
-
151
-
152
- class ConfigParser:
153
- """配置文件解析器,用于读取和写入配置文件
154
-
155
- Attributes:
156
- options: 解析器配置选项
157
- _config_cache: 配置缓存,用于存储已读取的配置
158
- _cache_timestamps: 缓存时间戳,用于管理缓存过期
159
- _comments_cache: 注释缓存,用于存储每个配置节的注释
160
- _section_map: 用于存储大小写映射
161
- _current_file: 当前正在处理的文件路径
162
- """
163
-
164
- def __init__(self, options: Optional[ConfigOptions] = None):
165
- self.options = options or ConfigOptions()
166
- self._config_cache: Dict[str, Dict[str, Any]] = {}
167
- self._cache_timestamps: Dict[str, float] = {}
168
- self._comments_cache: Dict[str, Dict[str, List[str]]] = {}
169
- self._section_map: Dict[str, Dict[str, str]] = {} # 用于存储大小写映射
170
- self._current_file: Optional[Path] = None # 当前正在处理的文件路径
171
-
172
- def __enter__(self) -> 'ConfigParser':
173
- """进入上下文管理器
174
-
175
- Returns:
176
- ConfigParser: 返回当前实例
177
- """
178
- return self
179
-
180
- def __exit__(self, exc_type: Optional[Type[BaseException]],
181
- exc_val: Optional[BaseException],
182
- exc_tb: Optional[Any]) -> None:
183
- """退出上下文管理器
184
-
185
- Args:
186
- exc_type: 异常类型
187
- exc_val: 异常值
188
- exc_tb: 异常追踪信息
189
- """
190
- self._current_file = None
191
-
192
- def open(self, file_path: Union[str, Path]) -> 'ConfigParser':
193
- """打开配置文件
194
-
195
- Args:
196
- file_path: 配置文件路径
197
-
198
- Returns:
199
- ConfigParser: 返回当前实例,支持链式调用
200
-
201
- Raises:
202
- ConfigFileNotFoundError: 当配置文件不存在且未启用自动创建时
203
- """
204
- file_path = Path(file_path)
205
- if not file_path.exists() and not self.options.auto_create:
206
- raise ConfigFileNotFoundError(file_path)
207
- self._current_file = file_path
208
- return self
209
-
210
- def _ensure_file_open(self) -> None:
211
- """确保文件已打开
212
-
213
- Raises:
214
- ConfigError: 当文件未打开时
215
- """
216
- if self._current_file is None:
217
- raise ConfigError("未打开任何配置文件,请先调用 open() 方法")
218
-
219
- def _is_comment_line(self, line: str) -> bool:
220
- """判断一行是否为注释行"""
221
- stripped = line.strip()
222
- return any(stripped.startswith(style.value) for style in self.options.comment_styles)
223
-
224
- def _extract_comment(self, line: str) -> Tuple[str, str]:
225
- """从行中提取注释
226
-
227
- Returns:
228
- Tuple[str, str]: (去除注释后的行内容, 注释内容)
229
- """
230
- for style in self.options.comment_styles:
231
- comment_match = re.search(fr'\s+{re.escape(style.value)}.*$', line)
232
- if comment_match:
233
- return line[:comment_match.start()].strip(), comment_match.group(0)
234
- return line.strip(), ''
235
-
236
- def _split_key_value(self, line: str) -> Optional[Tuple[str, str]]:
237
- """分割配置行为键值对
238
-
239
- Args:
240
- line: 要分割的配置行
241
-
242
- Returns:
243
- Optional[Tuple[str, str]]: 键值对元组,如果无法分割则返回None
244
- """
245
- for sep in self.options.separators:
246
- if sep in line:
247
- key_part, value_part = line.split(sep, 1)
248
- return key_part.strip(), value_part
249
-
250
- for sep in [':', ':']:
251
- if sep in line:
252
- pattern = fr'\s*{re.escape(sep)}\s*'
253
- parts = re.split(pattern, line, 1)
254
- if len(parts) == 2:
255
- return parts[0].strip(), parts[1]
256
-
257
- return None
258
-
259
- def _validate_key(self, key: str) -> bool:
260
- """验证键名是否合法"""
261
- if not self.options.validate_keys:
262
- return True
263
- return bool(re.match(self.options.key_pattern, key))
264
-
265
- def _get_cached_config(self, file_path: str) -> Optional[Dict[str, Any]]:
266
- """获取缓存的配置,如果过期则返回None"""
267
- if file_path not in self._config_cache:
268
- return None
269
-
270
- if file_path not in self._cache_timestamps:
271
- return None
272
-
273
- if time.time() - self._cache_timestamps[file_path] > self.options.cache_ttl:
274
- return None
275
-
276
- return self._config_cache[file_path]
277
-
278
- def _update_cache(self, file_path: str, config: Dict[str, Any]) -> None:
279
- """更新配置缓存"""
280
- self._config_cache[file_path] = config
281
- self._cache_timestamps[file_path] = time.time()
282
-
283
- def _normalize_section(self, section: str) -> str:
284
- """标准化节名称(处理大小写)"""
285
- if self.options.case_sensitive:
286
- return section
287
- return section.lower()
288
-
289
- def _get_original_section(self, file_path: str, normalized_section: str) -> Optional[str]:
290
- """获取原始节名称"""
291
- if self.options.case_sensitive:
292
- return normalized_section
293
- return self._section_map.get(file_path, {}).get(normalized_section)
294
-
295
- def _update_section_map(self, file_path: str, section: str) -> None:
296
- """更新节名称映射"""
297
- if not self.options.case_sensitive:
298
- normalized = self._normalize_section(section)
299
- if file_path not in self._section_map:
300
- self._section_map[file_path] = {}
301
- self._section_map[file_path][normalized] = section
302
-
303
- def _clear_cache(self, file_path: Optional[str] = None) -> None:
304
- """清除配置缓存"""
305
- if file_path:
306
- self._config_cache.pop(file_path, None)
307
- self._cache_timestamps.pop(file_path, None)
308
- self._comments_cache.pop(file_path, None)
309
- self._section_map.pop(file_path, None)
310
- else:
311
- self._config_cache.clear()
312
- self._cache_timestamps.clear()
313
- self._comments_cache.clear()
314
- self._section_map.clear()
315
-
316
- def _convert_value(self, value: str, target_type: Type[T]) -> T:
317
- """转换配置值到指定类型
318
-
319
- Args:
320
- value: 要转换的值
321
- target_type: 目标类型
322
-
323
- Returns:
324
- T: 转换后的值
325
-
326
- Raises:
327
- ConfigValueError: 当值无法转换为指定类型时
328
- """
329
- try:
330
- if target_type == bool:
331
- return bool(value.lower() in ('true', 'yes', '1', 'on'))
332
- elif target_type == list:
333
- # 支持多种分隔符的列表
334
- if not value.strip():
335
- return []
336
- # 尝试不同的分隔符
337
- for sep in [',', ';', '|', ' ']:
338
- if sep in value:
339
- return [item.strip() for item in value.split(sep) if item.strip()]
340
- # 如果没有分隔符,则作为单个元素返回
341
- return [value.strip()]
342
- elif target_type == tuple:
343
- # 支持元组类型
344
- if not value.strip():
345
- return ()
346
- # 尝试不同的分隔符
347
- for sep in [',', ';', '|', ' ']:
348
- if sep in value:
349
- return tuple(item.strip() for item in value.split(sep) if item.strip())
350
- # 如果没有分隔符,则作为单个元素返回
351
- return (value.strip(),)
352
- elif target_type == set:
353
- # 支持集合类型
354
- if not value.strip():
355
- return set()
356
- # 尝试不同的分隔符
357
- for sep in [',', ';', '|', ' ']:
358
- if sep in value:
359
- return {item.strip() for item in value.split(sep) if item.strip()}
360
- # 如果没有分隔符,则作为单个元素返回
361
- return {value.strip()}
362
- elif target_type == dict:
363
- # 支持字典类型,格式:key1=value1,key2=value2
364
- if not value.strip():
365
- return {}
366
- result = {}
367
- # 尝试不同的分隔符
368
- for sep in [',', ';', '|']:
369
- if sep in value:
370
- pairs = [pair.strip() for pair in value.split(sep) if pair.strip()]
371
- for pair in pairs:
372
- if '=' in pair:
373
- key, val = pair.split('=', 1)
374
- result[key.strip()] = val.strip()
375
- return result
376
- # 如果没有分隔符,尝试单个键值对
377
- if '=' in value:
378
- key, val = value.split('=', 1)
379
- return {key.strip(): val.strip()}
380
- return {}
381
- elif target_type == int:
382
- # 支持十六进制、八进制、二进制
383
- value = value.strip().lower()
384
- if value.startswith('0x'):
385
- return int(value, 16)
386
- elif value.startswith('0o'):
387
- return int(value, 8)
388
- elif value.startswith('0b'):
389
- return int(value, 2)
390
- return int(value)
391
- elif target_type == float:
392
- return float(value)
393
- elif target_type == complex:
394
- return complex(value)
395
- elif target_type == bytes:
396
- return value.encode('utf-8')
397
- elif target_type == bytearray:
398
- return bytearray(value.encode('utf-8'))
399
- elif target_type == set:
400
- return set(value.split(','))
401
- elif target_type == frozenset:
402
- return frozenset(value.split(','))
403
- elif target_type == range:
404
- # 支持 range 类型,格式:start:stop:step 或 start:stop
405
- parts = value.split(':')
406
- if len(parts) == 2:
407
- return range(int(parts[0]), int(parts[1]))
408
- elif len(parts) == 3:
409
- return range(int(parts[0]), int(parts[1]), int(parts[2]))
410
- raise ValueError("Invalid range format")
411
- return target_type(value)
412
- except (ValueError, TypeError) as e:
413
- raise ConfigValueError(
414
- f"无法将值 '{value}' 转换为类型 {target_type.__name__}",
415
- file_path=None,
416
- key=None
417
- )
418
-
419
- def get_value(self, file_path: Optional[Union[str, Path]] = None, key: str = None,
420
- section: Optional[str] = None, default: Any = None,
421
- value_type: Optional[Type[T]] = None) -> T:
422
- """获取指定配置项的值
423
-
424
- Args:
425
- file_path: 配置文件路径,如果为None则使用当前打开的文件
426
- key: 配置键
427
- section: 配置节名称,如果为None则使用默认节
428
- default: 当配置项不存在时返回的默认值
429
- value_type: 期望的值的类型
430
-
431
- Returns:
432
- T: 配置值
433
-
434
- Raises:
435
- ConfigSectionNotFoundError: 当指定的节不存在且未提供默认值时
436
- ConfigKeyNotFoundError: 当指定的键不存在且未提供默认值时
437
- ConfigValueError: 当值无法转换为指定类型时
438
- """
439
- if file_path is None:
440
- self._ensure_file_open()
441
- file_path = self._current_file
442
- if not self._validate_key(key):
443
- raise ConfigValueError(f"无效的键名: {key}", file_path=file_path, key=key)
444
-
445
- config = self.read(file_path)
446
- section = section or self.options.default_section
447
- normalized_section = self._normalize_section(section)
448
-
449
- # 获取原始节名称
450
- original_section = self._get_original_section(str(file_path), normalized_section)
451
- if original_section is None:
452
- if default is not None:
453
- return default
454
- raise ConfigSectionNotFoundError(file_path, section)
455
-
456
- if key not in config[original_section]:
457
- if default is not None:
458
- return default
459
- raise ConfigKeyNotFoundError(file_path, original_section, key)
460
-
461
- value = config[original_section][key]
462
-
463
- if value_type is not None:
464
- return self._convert_value(value, value_type)
465
-
466
- return value
467
-
468
- def get_values(self, keys: List[Tuple[str, str]],
469
- file_path: Optional[Union[str, Path]] = None,
470
- defaults: Optional[Dict[str, Any]] = None,
471
- value_types: Optional[Dict[str, Type]] = None) -> Dict[str, Any]:
472
- """批量获取多个配置项的值
473
-
474
- Args:
475
- keys: 配置项列表,每个元素为 (section, key) 元组
476
- file_path: 配置文件路径,如果为None则使用当前打开的文件
477
- defaults: 默认值字典,格式为 {key: default_value}
478
- value_types: 值类型字典,格式为 {key: type}
479
-
480
- Returns:
481
- Dict[str, Any]: 配置值字典,格式为 {key: value}
482
-
483
- Raises:
484
- ConfigSectionNotFoundError: 当指定的节不存在且未提供默认值时
485
- ConfigKeyNotFoundError: 当指定的键不存在且未提供默认值时
486
- ConfigValueError: 当值无法转换为指定类型时
487
- """
488
- if file_path is None:
489
- self._ensure_file_open()
490
- file_path = self._current_file
491
- defaults = defaults or {}
492
- value_types = value_types or {}
493
- result = {}
494
-
495
- for section, key in keys:
496
- try:
497
- value = self.get_value(
498
- file_path=file_path,
499
- key=key,
500
- section=section,
501
- default=defaults.get(key),
502
- value_type=value_types.get(key)
503
- )
504
- result[key] = value
505
- except (ConfigSectionNotFoundError, ConfigKeyNotFoundError) as e:
506
- if key in defaults:
507
- result[key] = defaults[key]
508
- else:
509
- raise e
510
-
511
- return result
512
-
513
- def get_section_values(self, keys: List[str],
514
- section: Optional[str] = None,
515
- file_path: Optional[Union[str, Path]] = None,
516
- defaults: Optional[Dict[str, Any]] = None,
517
- value_types: Optional[Dict[str, Type]] = None) -> Tuple[Any, ...]:
518
- """获取指定节点下多个键的值元组
519
-
520
- Args:
521
- keys: 要获取的键列表
522
- section: 配置节名称,默认为 DEFAULT
523
- file_path: 配置文件路径,如果为None则使用当前打开的文件
524
- defaults: 默认值字典,格式为 {key: default_value}
525
- value_types: 值类型字典,格式为 {key: type}
526
-
527
- Returns:
528
- Tuple[Any, ...]: 按键列表顺序返回的值元组
529
-
530
- Raises:
531
- ConfigSectionNotFoundError: 当指定的节不存在且未提供默认值时
532
- ConfigKeyNotFoundError: 当指定的键不存在且未提供默认值时
533
- ConfigValueError: 当值无法转换为指定类型时
534
- """
535
- if file_path is None:
536
- self._ensure_file_open()
537
- file_path = self._current_file
538
- defaults = defaults or {}
539
- value_types = value_types or {}
540
- result = []
541
-
542
- for key in keys:
543
- try:
544
- value = self.get_value(
545
- file_path=file_path,
546
- key=key,
547
- section=section,
548
- default=defaults.get(key),
549
- value_type=value_types.get(key)
550
- )
551
- result.append(value)
552
- except (ConfigSectionNotFoundError, ConfigKeyNotFoundError) as e:
553
- if key in defaults:
554
- result.append(defaults[key])
555
- else:
556
- raise e
557
-
558
- return tuple(result)
559
-
560
- def set_value(self, key: str, value: Any,
561
- section: Optional[str] = None,
562
- file_path: Optional[Union[str, Path]] = None,
563
- value_type: Optional[Type] = None) -> None:
564
- """设置指定配置项的值,保持原始文件的格式和注释
565
-
566
- Args:
567
- key: 配置键
568
- value: 要设置的值
569
- section: 配置节名称,如果为None则使用默认节
570
- file_path: 配置文件路径,如果为None则使用当前打开的文件
571
- value_type: 值的类型,用于验证和转换
572
-
573
- Raises:
574
- ConfigValueError: 当值无法转换为指定类型时
575
- ConfigError: 当其他配置错误发生时
576
- """
577
- if file_path is None:
578
- self._ensure_file_open()
579
- file_path = self._current_file
580
- if not self._validate_key(key):
581
- raise ConfigValueError(f"无效的键名: {key}", file_path=file_path, key=key)
582
-
583
- # 读取原始文件內容
584
- original_lines = []
585
- if file_path.exists():
586
- with open(file_path, 'r', encoding=self.options.encoding) as file:
587
- original_lines = file.readlines()
588
-
589
- # 读取当前配置
590
- config = self.read(file_path)
591
-
592
- if section not in config:
593
- config[section] = {}
594
-
595
- if value_type is not None:
596
- try:
597
- if value_type == bool:
598
- if isinstance(value, str):
599
- value = value.lower() in ('true', 'yes', '1', 'on')
600
- else:
601
- value = bool(value)
602
- else:
603
- value = value_type(value)
604
- except (ValueError, TypeError) as e:
605
- raise ConfigValueError(
606
- f"无法将值 '{value}' 转换为类型 {value_type.__name__}",
607
- file_path=file_path,
608
- section=section,
609
- key=key
610
- )
611
-
612
- if isinstance(value, bool):
613
- value = str(value).lower()
614
- else:
615
- value = str(value)
616
-
617
- # 更新配置
618
- config[section][key] = value
619
-
620
- # 写入文件,保持原始格式
621
- try:
622
- file_path.parent.mkdir(parents=True, exist_ok=True)
623
-
624
- with open(file_path, 'w', encoding=self.options.encoding) as file:
625
- current_section = self.options.default_section
626
- section_separators = {} # 用于存储每个section使用的分隔符
627
-
628
- # 解析原始文件,提取格式信息
629
- for line in original_lines:
630
- stripped_line = line.strip()
631
-
632
- if not stripped_line:
633
- file.write(line) # 保持空行
634
- continue
635
-
636
- if stripped_line.startswith('[') and stripped_line.endswith(']'):
637
- current_section = stripped_line[1:-1]
638
- file.write(line) # 保持节标记的原始格式
639
- continue
640
-
641
- if self._is_comment_line(stripped_line):
642
- file.write(line) # 保持注释的原始格式
643
- continue
644
-
645
- key_value = self._split_key_value(stripped_line)
646
- if key_value:
647
- orig_key, orig_value = key_value
648
- # 检测使用的分隔符
649
- for sep in self.options.separators:
650
- if sep in line:
651
- section_separators.setdefault(current_section, {})[orig_key] = sep
652
- break
653
-
654
- # 如果是当前要修改的键,则写入新值
655
- if current_section == section and orig_key == key:
656
- separator = section_separators.get(current_section, {}).get(orig_key, self.options.separators[0])
657
- # 提取行尾注释
658
- comment = ''
659
- for style in self.options.comment_styles:
660
- comment_match = re.search(fr'\s+{re.escape(style.value)}.*$', line)
661
- if comment_match:
662
- comment = comment_match.group(0)
663
- break
664
- # 写入新值并保留注释
665
- file.write(f'{key}{separator}{value}{comment}\n')
666
- else:
667
- file.write(line) # 保持其他行的原始格式
668
- else:
669
- file.write(line) # 保持无法解析的行的原始格式
670
-
671
- # 如果section不存在,则添加新的section
672
- if section not in [line.strip()[1:-1] for line in original_lines if line.strip().startswith('[') and line.strip().endswith(']')]:
673
- file.write(f'\n[{section}]\n')
674
- file.write(f'{key}={value}\n')
675
-
676
- self._clear_cache(str(file_path))
677
-
678
- except Exception as e:
679
- raise ConfigWriteError(file_path, e)
680
-
681
- def read(self, file_path: Optional[Union[str, Path]] = None) -> Dict[str, Any]:
682
- """读取配置文件内容
683
-
684
- Args:
685
- file_path: 配置文件路径,如果为None则使用当前打开的文件
686
-
687
- Returns:
688
- Dict[str, Any]: 配置字典,格式为 {section: {key: value}}
689
-
690
- Raises:
691
- ConfigFileNotFoundError: 当配置文件不存在且未启用自动创建时
692
- ConfigReadError: 当读取配置文件失败时
693
- """
694
- if file_path is None:
695
- self._ensure_file_open()
696
- file_path = self._current_file
697
- else:
698
- file_path = Path(file_path) # 确保 file_path 是 Path 对象
699
-
700
- # 检查缓存
701
- cached_config = self._get_cached_config(str(file_path))
702
- if cached_config is not None:
703
- return cached_config
704
-
705
- if not file_path.exists():
706
- if not self.options.auto_create:
707
- raise ConfigFileNotFoundError(file_path)
708
- logger.info(f'配置文件不存在,将创建: {file_path}')
709
- file_path.parent.mkdir(parents=True, exist_ok=True)
710
- file_path.touch()
711
- return {}
712
-
713
- try:
714
- with open(file_path, 'r', encoding=self.options.encoding) as file:
715
- config = {}
716
- current_section = self.options.default_section
717
- section_comments = []
718
-
719
- for line in file:
720
- stripped_line = line.strip()
721
-
722
- if not stripped_line or self._is_comment_line(stripped_line):
723
- if self.options.preserve_comments:
724
- section_comments.append(line.rstrip())
725
- continue
726
-
727
- if stripped_line.startswith('[') and stripped_line.endswith(']'):
728
- current_section = stripped_line[1:-1]
729
- if not self._validate_key(current_section):
730
- raise ConfigValueError(
731
- f"无效的节名: {current_section}",
732
- file_path=file_path,
733
- section=current_section
734
- )
735
- self._update_section_map(str(file_path), current_section)
736
- if current_section not in config:
737
- config[current_section] = {}
738
- if self.options.preserve_comments:
739
- self._comments_cache.setdefault(str(file_path), {}).setdefault(current_section, []).extend(section_comments)
740
- section_comments = []
741
- continue
742
-
743
- key_value = self._split_key_value(stripped_line)
744
- if key_value:
745
- key, value = key_value
746
- if not self._validate_key(key):
747
- raise ConfigValueError(
748
- f"无效的键名: {key}",
749
- file_path=file_path,
750
- section=current_section,
751
- key=key
752
- )
753
- value, comment = self._extract_comment(value)
754
-
755
- if self.options.strip_values:
756
- value = value.strip()
757
-
758
- if current_section not in config:
759
- config[current_section] = {}
760
-
761
- config[current_section][key] = value
762
- if self.options.preserve_comments and comment:
763
- self._comments_cache.setdefault(str(file_path), {}).setdefault(current_section, []).append(comment)
764
-
765
- self._update_cache(str(file_path), config)
766
- return config
767
-
768
- except Exception as e:
769
- raise ConfigReadError(file_path, e)
770
-
771
-
772
- def main() -> None:
773
- # 使用示例
774
- config_file = Path('/Users/xigua/spd.txt')
775
-
776
- # 方式1:使用上下文管理器
777
- with ConfigParser() as parser:
778
- parser.open(config_file)
779
- host, port, username, password = parser.get_section_values(
780
- keys=['host', 'port', 'username', 'password'],
781
- section='mysql'
782
- )
783
- print("方式1结果:", host, port, username, password)
784
-
785
- # 修改配置
786
- parser.set_value('host', 'localhost', section='mysql')
787
- parser.set_value('port', 3306, section='mysql')
788
-
789
- # # 读取整个配置
790
- # config = parser.read()
791
- # print("\n当前配置:")
792
- # for section, items in config.items():
793
- # print(f"\n[{section}]")
794
- # for key, value in items.items():
795
- # print(f"{key} = {value}")
796
-
797
- # 方式2:链式调用
798
- parser = ConfigParser()
799
- host, port, username, password = parser.open(config_file).get_section_values(
800
- keys=['host', 'port', 'username', 'password'],
801
- section='mysql'
802
- )
803
- print("\n方式2结果:", host, port, username, password)
804
-
805
- # 方式3:传统方式
806
- parser = ConfigParser()
807
- host, port, username, password = parser.get_section_values(
808
- file_path=config_file,
809
- section='mysql',
810
- keys=['host', 'port', 'username', 'password']
811
- )
812
- print("\n方式3结果:", host, port, username, password)
813
-
814
-
815
- if __name__ == '__main__':
816
- main()