crawlo 1.0.9__py3-none-any.whl → 1.1.1__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.

Potentially problematic release.


This version of crawlo might be problematic. Click here for more details.

Files changed (111) hide show
  1. crawlo/__init__.py +33 -24
  2. crawlo/__version__.py +1 -1
  3. crawlo/cli.py +40 -40
  4. crawlo/commands/__init__.py +13 -13
  5. crawlo/commands/check.py +594 -106
  6. crawlo/commands/genspider.py +125 -110
  7. crawlo/commands/list.py +147 -92
  8. crawlo/commands/run.py +286 -181
  9. crawlo/commands/startproject.py +111 -101
  10. crawlo/commands/stats.py +188 -59
  11. crawlo/core/__init__.py +2 -2
  12. crawlo/core/engine.py +158 -158
  13. crawlo/core/processor.py +40 -40
  14. crawlo/core/scheduler.py +57 -57
  15. crawlo/crawler.py +494 -492
  16. crawlo/downloader/__init__.py +78 -78
  17. crawlo/downloader/aiohttp_downloader.py +199 -199
  18. crawlo/downloader/cffi_downloader.py +242 -277
  19. crawlo/downloader/httpx_downloader.py +246 -246
  20. crawlo/event.py +11 -11
  21. crawlo/exceptions.py +78 -78
  22. crawlo/extension/__init__.py +31 -31
  23. crawlo/extension/log_interval.py +49 -49
  24. crawlo/extension/log_stats.py +44 -44
  25. crawlo/extension/logging_extension.py +34 -34
  26. crawlo/filters/__init__.py +37 -37
  27. crawlo/filters/aioredis_filter.py +150 -150
  28. crawlo/filters/memory_filter.py +202 -202
  29. crawlo/items/__init__.py +23 -23
  30. crawlo/items/base.py +21 -21
  31. crawlo/items/fields.py +53 -53
  32. crawlo/items/items.py +104 -104
  33. crawlo/middleware/__init__.py +21 -21
  34. crawlo/middleware/default_header.py +32 -32
  35. crawlo/middleware/download_delay.py +28 -28
  36. crawlo/middleware/middleware_manager.py +135 -135
  37. crawlo/middleware/proxy.py +245 -245
  38. crawlo/middleware/request_ignore.py +30 -30
  39. crawlo/middleware/response_code.py +18 -18
  40. crawlo/middleware/response_filter.py +26 -26
  41. crawlo/middleware/retry.py +90 -90
  42. crawlo/network/__init__.py +7 -7
  43. crawlo/network/request.py +203 -203
  44. crawlo/network/response.py +166 -166
  45. crawlo/pipelines/__init__.py +13 -13
  46. crawlo/pipelines/console_pipeline.py +39 -39
  47. crawlo/pipelines/mongo_pipeline.py +116 -116
  48. crawlo/pipelines/mysql_batch_pipline.py +272 -272
  49. crawlo/pipelines/mysql_pipeline.py +195 -195
  50. crawlo/pipelines/pipeline_manager.py +56 -56
  51. crawlo/project.py +153 -0
  52. crawlo/settings/__init__.py +7 -7
  53. crawlo/settings/default_settings.py +166 -168
  54. crawlo/settings/setting_manager.py +99 -99
  55. crawlo/spider/__init__.py +129 -129
  56. crawlo/stats_collector.py +59 -59
  57. crawlo/subscriber.py +106 -106
  58. crawlo/task_manager.py +27 -27
  59. crawlo/templates/crawlo.cfg.tmpl +10 -10
  60. crawlo/templates/project/__init__.py.tmpl +3 -3
  61. crawlo/templates/project/items.py.tmpl +17 -17
  62. crawlo/templates/project/middlewares.py.tmpl +75 -75
  63. crawlo/templates/project/pipelines.py.tmpl +63 -63
  64. crawlo/templates/project/settings.py.tmpl +54 -54
  65. crawlo/templates/project/spiders/__init__.py.tmpl +5 -5
  66. crawlo/templates/spider/spider.py.tmpl +31 -31
  67. crawlo/utils/__init__.py +7 -7
  68. crawlo/utils/date_tools.py +233 -233
  69. crawlo/utils/db_helper.py +343 -343
  70. crawlo/utils/func_tools.py +82 -82
  71. crawlo/utils/log.py +128 -128
  72. crawlo/utils/pqueue.py +173 -173
  73. crawlo/utils/request.py +267 -267
  74. crawlo/utils/spider_loader.py +62 -62
  75. crawlo/utils/system.py +11 -11
  76. crawlo/utils/tools.py +4 -4
  77. crawlo/utils/url.py +39 -39
  78. crawlo-1.1.1.dist-info/METADATA +220 -0
  79. crawlo-1.1.1.dist-info/RECORD +100 -0
  80. examples/__init__.py +7 -0
  81. examples/baidu_spider/__init__.py +7 -0
  82. examples/baidu_spider/demo.py +94 -0
  83. examples/baidu_spider/items.py +46 -0
  84. examples/baidu_spider/middleware.py +49 -0
  85. examples/baidu_spider/pipeline.py +55 -0
  86. examples/baidu_spider/run.py +27 -0
  87. examples/baidu_spider/settings.py +121 -0
  88. examples/baidu_spider/spiders/__init__.py +7 -0
  89. examples/baidu_spider/spiders/bai_du.py +61 -0
  90. examples/baidu_spider/spiders/miit.py +159 -0
  91. examples/baidu_spider/spiders/sina.py +79 -0
  92. tests/__init__.py +7 -7
  93. tests/test_proxy_health_check.py +32 -32
  94. tests/test_proxy_middleware_integration.py +136 -136
  95. tests/test_proxy_providers.py +56 -56
  96. tests/test_proxy_stats.py +19 -19
  97. tests/test_proxy_strategies.py +59 -59
  98. crawlo/utils/concurrency_manager.py +0 -125
  99. crawlo/utils/project.py +0 -197
  100. crawlo-1.0.9.dist-info/METADATA +0 -49
  101. crawlo-1.0.9.dist-info/RECORD +0 -97
  102. examples/gxb/__init__.py +0 -0
  103. examples/gxb/items.py +0 -36
  104. examples/gxb/run.py +0 -16
  105. examples/gxb/settings.py +0 -72
  106. examples/gxb/spider/__init__.py +0 -0
  107. examples/gxb/spider/miit_spider.py +0 -180
  108. examples/gxb/spider/telecom_device.py +0 -129
  109. {crawlo-1.0.9.dist-info → crawlo-1.1.1.dist-info}/WHEEL +0 -0
  110. {crawlo-1.0.9.dist-info → crawlo-1.1.1.dist-info}/entry_points.txt +0 -0
  111. {crawlo-1.0.9.dist-info → crawlo-1.1.1.dist-info}/top_level.txt +0 -0
@@ -1,202 +1,202 @@
1
- #!/usr/bin/python
2
- # -*- coding:UTF-8 -*-
3
- import os
4
- import threading
5
- from weakref import WeakSet
6
- from typing import Set, TextIO, Optional
7
-
8
- from crawlo import Request
9
- from crawlo.filters import BaseFilter
10
- from crawlo.utils.log import get_logger
11
- from crawlo.utils.request import request_fingerprint
12
-
13
-
14
- class MemoryFilter(BaseFilter):
15
- """基于内存的高效请求去重过滤器,适用于单机爬虫"""
16
-
17
- def __init__(self, crawler):
18
- """
19
- 初始化内存过滤器
20
-
21
- :param crawler: 爬虫实例,用于获取配置
22
- """
23
- self.fingerprints: Set[str] = set() # 主指纹存储
24
- self._temp_weak_refs = WeakSet() # 弱引用临时存储(可选)
25
-
26
- debug = crawler.settings.get_bool('FILTER_DEBUG', False)
27
- logger = get_logger(
28
- self.__class__.__name__, # 使用类名替代字符串
29
- crawler.settings.get('LOG_LEVEL', 'INFO')
30
- )
31
- super().__init__(logger, crawler.stats, debug)
32
-
33
- # 性能计数器
34
- self._dupe_count = 0
35
- self._unique_count = 0
36
-
37
- def add_fingerprint(self, fp: str) -> None:
38
- """
39
- 添加请求指纹
40
-
41
- :param fp: 请求指纹字符串
42
- :raises TypeError: 如果指纹不是字符串类型
43
- """
44
- if not isinstance(fp, str):
45
- raise TypeError(f"指纹必须是字符串类型,得到 {type(fp)}")
46
-
47
- self.fingerprints.add(fp)
48
- self._unique_count += 1
49
- # self.logger.debug(f"添加指纹: {fp[:10]}...") # 日志截断防止过长
50
-
51
- def requested(self, request: Request) -> bool:
52
- """
53
- 检查请求是否重复(主要接口)
54
-
55
- :param request: 请求对象
56
- :return: 是否重复
57
- """
58
- fp = request_fingerprint(request)
59
- if fp in self:
60
- self._dupe_count += 1
61
- # self.logger.debug(f"发现重复请求: {fp[:10]}...")
62
- return True
63
-
64
- self.add_fingerprint(fp)
65
- return False
66
-
67
- def __contains__(self, item: str) -> bool:
68
- """
69
- 支持 in 操作符检查
70
-
71
- :param item: 要检查的指纹
72
- :return: 是否已存在
73
- """
74
- return item in self.fingerprints
75
-
76
- @property
77
- def stats_summary(self) -> dict:
78
- """获取过滤器统计信息"""
79
- return {
80
- 'capacity': len(self.fingerprints),
81
- 'duplicates': self._dupe_count,
82
- 'uniques': self._unique_count,
83
- 'memory_usage': self._estimate_memory()
84
- }
85
-
86
- def _estimate_memory(self) -> str:
87
- """估算内存使用量(近似值)"""
88
- avg_item_size = sum(len(x) for x in self.fingerprints) / max(1, len(self.fingerprints))
89
- total = len(self.fingerprints) * (avg_item_size + 50) # 50字节额外开销
90
- return f"{total / (1024 * 1024):.2f} MB"
91
-
92
- def clear(self) -> None:
93
- """清空所有指纹数据"""
94
- self.fingerprints.clear()
95
- self._dupe_count = 0
96
- self._unique_count = 0
97
-
98
- def close(self) -> None:
99
- """关闭过滤器(清理资源)"""
100
- self.clear()
101
-
102
- # 兼容旧版异步接口
103
- async def closed(self):
104
- """兼容异步接口"""
105
- self.close()
106
-
107
-
108
- class MemoryFileFilter(BaseFilter):
109
- """基于内存的请求指纹过滤器,支持原子化文件持久化"""
110
-
111
- def __init__(self, crawler):
112
- """
113
- 初始化过滤器
114
- :param crawler: Scrapy Crawler对象,用于获取配置
115
- """
116
- self.fingerprints: Set[str] = set() # 主存储集合
117
- self._lock = threading.RLock() # 线程安全锁
118
- self._file: Optional[TextIO] = None # 文件句柄
119
-
120
- debug = crawler.settings.get_bool("FILTER_DEBUG", False)
121
- logger = get_logger(
122
- self.__class__.__name__, # 使用类名作为日志标识
123
- crawler.settings.get("LOG_LEVEL", "INFO")
124
- )
125
- super().__init__(logger, crawler.stats, debug)
126
-
127
- # 初始化文件存储
128
- request_dir = crawler.settings.get("REQUEST_DIR")
129
- if request_dir:
130
- self._init_file_store(request_dir)
131
-
132
- def _init_file_store(self, request_dir: str) -> None:
133
- """原子化初始化文件存储"""
134
- with self._lock:
135
- try:
136
- os.makedirs(request_dir, exist_ok=True)
137
- file_path = os.path.join(request_dir, 'request_fingerprints.txt')
138
-
139
- # 原子化操作:读取现有指纹
140
- if os.path.exists(file_path):
141
- with open(file_path, 'r', encoding='utf-8') as f:
142
- self.fingerprints.update(
143
- line.strip() for line in f
144
- if line.strip()
145
- )
146
-
147
- # 以追加模式打开文件
148
- self._file = open(file_path, 'a+', encoding='utf-8')
149
- self.logger.info(f"Initialized fingerprint file: {file_path}")
150
-
151
- except Exception as e:
152
- self.logger.error(f"Failed to init file store: {str(e)}")
153
- raise
154
-
155
- def add_fingerprint(self, fp: str) -> None:
156
- """
157
- 线程安全的指纹添加操作
158
- :param fp: 请求指纹字符串
159
- """
160
- with self._lock:
161
- if fp not in self.fingerprints:
162
- self.fingerprints.add(fp)
163
- self._persist_fp(fp)
164
-
165
- def _persist_fp(self, fp: str) -> None:
166
- """持久化指纹到文件(需在锁保护下调用)"""
167
- if self._file:
168
- try:
169
- self._file.write(f"{fp}\n")
170
- self._file.flush()
171
- os.fsync(self._file.fileno()) # 确保写入磁盘
172
- except IOError as e:
173
- self.logger.error(f"Failed to persist fingerprint: {str(e)}")
174
-
175
- def __contains__(self, item: str) -> bool:
176
- """
177
- 线程安全的指纹检查
178
- :param item: 要检查的指纹
179
- :return: 是否已存在
180
- """
181
- with self._lock:
182
- return item in self.fingerprints
183
-
184
- def close(self) -> None:
185
- """安全关闭资源(同步方法)"""
186
- with self._lock:
187
- if self._file and not self._file.closed:
188
- try:
189
- self._file.flush()
190
- os.fsync(self._file.fileno())
191
- finally:
192
- self._file.close()
193
- self.logger.info(f"Closed fingerprint file: {self._file.name}")
194
-
195
- def __del__(self):
196
- """析构函数双保险"""
197
- self.close()
198
-
199
- # 兼容异步接口
200
- async def closed(self):
201
- """标准的关闭入口"""
202
- self.close()
1
+ #!/usr/bin/python
2
+ # -*- coding:UTF-8 -*-
3
+ import os
4
+ import threading
5
+ from weakref import WeakSet
6
+ from typing import Set, TextIO, Optional
7
+
8
+ from crawlo import Request
9
+ from crawlo.filters import BaseFilter
10
+ from crawlo.utils.log import get_logger
11
+ from crawlo.utils.request import request_fingerprint
12
+
13
+
14
+ class MemoryFilter(BaseFilter):
15
+ """基于内存的高效请求去重过滤器,适用于单机爬虫"""
16
+
17
+ def __init__(self, crawler):
18
+ """
19
+ 初始化内存过滤器
20
+
21
+ :param crawler: 爬虫实例,用于获取配置
22
+ """
23
+ self.fingerprints: Set[str] = set() # 主指纹存储
24
+ self._temp_weak_refs = WeakSet() # 弱引用临时存储(可选)
25
+
26
+ debug = crawler.settings.get_bool('FILTER_DEBUG', False)
27
+ logger = get_logger(
28
+ self.__class__.__name__, # 使用类名替代字符串
29
+ crawler.settings.get('LOG_LEVEL', 'INFO')
30
+ )
31
+ super().__init__(logger, crawler.stats, debug)
32
+
33
+ # 性能计数器
34
+ self._dupe_count = 0
35
+ self._unique_count = 0
36
+
37
+ def add_fingerprint(self, fp: str) -> None:
38
+ """
39
+ 添加请求指纹
40
+
41
+ :param fp: 请求指纹字符串
42
+ :raises TypeError: 如果指纹不是字符串类型
43
+ """
44
+ if not isinstance(fp, str):
45
+ raise TypeError(f"指纹必须是字符串类型,得到 {type(fp)}")
46
+
47
+ self.fingerprints.add(fp)
48
+ self._unique_count += 1
49
+ # self.logger.debug(f"添加指纹: {fp[:10]}...") # 日志截断防止过长
50
+
51
+ def requested(self, request: Request) -> bool:
52
+ """
53
+ 检查请求是否重复(主要接口)
54
+
55
+ :param request: 请求对象
56
+ :return: 是否重复
57
+ """
58
+ fp = request_fingerprint(request)
59
+ if fp in self:
60
+ self._dupe_count += 1
61
+ # self.logger.debug(f"发现重复请求: {fp[:10]}...")
62
+ return True
63
+
64
+ self.add_fingerprint(fp)
65
+ return False
66
+
67
+ def __contains__(self, item: str) -> bool:
68
+ """
69
+ 支持 in 操作符检查
70
+
71
+ :param item: 要检查的指纹
72
+ :return: 是否已存在
73
+ """
74
+ return item in self.fingerprints
75
+
76
+ @property
77
+ def stats_summary(self) -> dict:
78
+ """获取过滤器统计信息"""
79
+ return {
80
+ 'capacity': len(self.fingerprints),
81
+ 'duplicates': self._dupe_count,
82
+ 'uniques': self._unique_count,
83
+ 'memory_usage': self._estimate_memory()
84
+ }
85
+
86
+ def _estimate_memory(self) -> str:
87
+ """估算内存使用量(近似值)"""
88
+ avg_item_size = sum(len(x) for x in self.fingerprints) / max(1, len(self.fingerprints))
89
+ total = len(self.fingerprints) * (avg_item_size + 50) # 50字节额外开销
90
+ return f"{total / (1024 * 1024):.2f} MB"
91
+
92
+ def clear(self) -> None:
93
+ """清空所有指纹数据"""
94
+ self.fingerprints.clear()
95
+ self._dupe_count = 0
96
+ self._unique_count = 0
97
+
98
+ def close(self) -> None:
99
+ """关闭过滤器(清理资源)"""
100
+ self.clear()
101
+
102
+ # 兼容旧版异步接口
103
+ async def closed(self):
104
+ """兼容异步接口"""
105
+ self.close()
106
+
107
+
108
+ class MemoryFileFilter(BaseFilter):
109
+ """基于内存的请求指纹过滤器,支持原子化文件持久化"""
110
+
111
+ def __init__(self, crawler):
112
+ """
113
+ 初始化过滤器
114
+ :param crawler: Scrapy Crawler对象,用于获取配置
115
+ """
116
+ self.fingerprints: Set[str] = set() # 主存储集合
117
+ self._lock = threading.RLock() # 线程安全锁
118
+ self._file: Optional[TextIO] = None # 文件句柄
119
+
120
+ debug = crawler.settings.get_bool("FILTER_DEBUG", False)
121
+ logger = get_logger(
122
+ self.__class__.__name__, # 使用类名作为日志标识
123
+ crawler.settings.get("LOG_LEVEL", "INFO")
124
+ )
125
+ super().__init__(logger, crawler.stats, debug)
126
+
127
+ # 初始化文件存储
128
+ request_dir = crawler.settings.get("REQUEST_DIR")
129
+ if request_dir:
130
+ self._init_file_store(request_dir)
131
+
132
+ def _init_file_store(self, request_dir: str) -> None:
133
+ """原子化初始化文件存储"""
134
+ with self._lock:
135
+ try:
136
+ os.makedirs(request_dir, exist_ok=True)
137
+ file_path = os.path.join(request_dir, 'request_fingerprints.txt')
138
+
139
+ # 原子化操作:读取现有指纹
140
+ if os.path.exists(file_path):
141
+ with open(file_path, 'r', encoding='utf-8') as f:
142
+ self.fingerprints.update(
143
+ line.strip() for line in f
144
+ if line.strip()
145
+ )
146
+
147
+ # 以追加模式打开文件
148
+ self._file = open(file_path, 'a+', encoding='utf-8')
149
+ self.logger.info(f"Initialized fingerprint file: {file_path}")
150
+
151
+ except Exception as e:
152
+ self.logger.error(f"Failed to init file store: {str(e)}")
153
+ raise
154
+
155
+ def add_fingerprint(self, fp: str) -> None:
156
+ """
157
+ 线程安全的指纹添加操作
158
+ :param fp: 请求指纹字符串
159
+ """
160
+ with self._lock:
161
+ if fp not in self.fingerprints:
162
+ self.fingerprints.add(fp)
163
+ self._persist_fp(fp)
164
+
165
+ def _persist_fp(self, fp: str) -> None:
166
+ """持久化指纹到文件(需在锁保护下调用)"""
167
+ if self._file:
168
+ try:
169
+ self._file.write(f"{fp}\n")
170
+ self._file.flush()
171
+ os.fsync(self._file.fileno()) # 确保写入磁盘
172
+ except IOError as e:
173
+ self.logger.error(f"Failed to persist fingerprint: {str(e)}")
174
+
175
+ def __contains__(self, item: str) -> bool:
176
+ """
177
+ 线程安全的指纹检查
178
+ :param item: 要检查的指纹
179
+ :return: 是否已存在
180
+ """
181
+ with self._lock:
182
+ return item in self.fingerprints
183
+
184
+ def close(self) -> None:
185
+ """安全关闭资源(同步方法)"""
186
+ with self._lock:
187
+ if self._file and not self._file.closed:
188
+ try:
189
+ self._file.flush()
190
+ os.fsync(self._file.fileno())
191
+ finally:
192
+ self._file.close()
193
+ self.logger.info(f"Closed fingerprint file: {self._file.name}")
194
+
195
+ def __del__(self):
196
+ """析构函数双保险"""
197
+ self.close()
198
+
199
+ # 兼容异步接口
200
+ async def closed(self):
201
+ """标准的关闭入口"""
202
+ self.close()
crawlo/items/__init__.py CHANGED
@@ -1,23 +1,23 @@
1
- #!/usr/bin/python
2
- # -*- coding: UTF-8 -*-
3
- """
4
- crawlo.items 包
5
- ===============
6
- 提供 Item 和 Field 类用于数据定义和验证。
7
- """
8
- from .items import Item
9
- from .fields import Field
10
- from .base import ItemMeta
11
-
12
- from crawlo.exceptions import ItemInitError, ItemAttributeError
13
-
14
- __all__ = [
15
- 'Item',
16
- 'Field',
17
- 'ItemMeta',
18
- 'ItemInitError',
19
- 'ItemAttributeError'
20
- ]
21
-
22
-
23
-
1
+ #!/usr/bin/python
2
+ # -*- coding: UTF-8 -*-
3
+ """
4
+ crawlo.items 包
5
+ ===============
6
+ 提供 Item 和 Field 类用于数据定义和验证。
7
+ """
8
+ from .items import Item
9
+ from .fields import Field
10
+ from .base import ItemMeta
11
+
12
+ from crawlo.exceptions import ItemInitError, ItemAttributeError
13
+
14
+ __all__ = [
15
+ 'Item',
16
+ 'Field',
17
+ 'ItemMeta',
18
+ 'ItemInitError',
19
+ 'ItemAttributeError'
20
+ ]
21
+
22
+
23
+
crawlo/items/base.py CHANGED
@@ -1,22 +1,22 @@
1
- #!/usr/bin/python
2
- # -*- coding: UTF-8 -*-
3
- """
4
- 基础元类定义
5
- """
6
- from abc import ABCMeta
7
- from .fields import Field
8
-
9
- class ItemMeta(ABCMeta):
10
- def __new__(mcs, name, bases, attrs):
11
- fields = {}
12
- cls_attrs = {}
13
-
14
- for attr_name, attr_value in attrs.items():
15
- if isinstance(attr_value, Field):
16
- fields[attr_name] = attr_value
17
- else:
18
- cls_attrs[attr_name] = attr_value
19
-
20
- cls_instance = super().__new__(mcs, name, bases, cls_attrs)
21
- cls_instance.FIELDS = fields
1
+ #!/usr/bin/python
2
+ # -*- coding: UTF-8 -*-
3
+ """
4
+ 基础元类定义
5
+ """
6
+ from abc import ABCMeta
7
+ from .fields import Field
8
+
9
+ class ItemMeta(ABCMeta):
10
+ def __new__(mcs, name, bases, attrs):
11
+ fields = {}
12
+ cls_attrs = {}
13
+
14
+ for attr_name, attr_value in attrs.items():
15
+ if isinstance(attr_value, Field):
16
+ fields[attr_name] = attr_value
17
+ else:
18
+ cls_attrs[attr_name] = attr_value
19
+
20
+ cls_instance = super().__new__(mcs, name, bases, cls_attrs)
21
+ cls_instance.FIELDS = fields
22
22
  return cls_instance
crawlo/items/fields.py CHANGED
@@ -1,54 +1,54 @@
1
- #!/usr/bin/python
2
- # -*- coding: UTF-8 -*-
3
- """
4
- Field 类定义
5
- """
6
-
7
- from typing import Any, Optional, Type
8
-
9
-
10
- class Field:
11
- """
12
- 字段定义类,用于定义 Item 的字段属性和验证规则
13
- """
14
- def __init__(
15
- self,
16
- nullable: bool = True,
17
- *,
18
- default: Any = None,
19
- field_type: Optional[Type] = None,
20
- max_length: Optional[int] = None,
21
- description: str = ""
22
- ):
23
- self.nullable = nullable
24
- self.default = default
25
- self.field_type = field_type
26
- self.max_length = max_length
27
- self.description = description
28
-
29
- def validate(self, value: Any, field_name: str = "") -> Any:
30
- """
31
- 验证字段值是否符合规则
32
- """
33
- if value is None or (isinstance(value, str) and value.strip() == ""):
34
- if self.default is not None:
35
- return self.default
36
- elif not self.nullable:
37
- raise ValueError(
38
- f"字段 '{field_name}' 不允许为空。"
39
- )
40
-
41
- if value is not None and not (isinstance(value, str) and value.strip() == ""):
42
- if self.field_type and not isinstance(value, self.field_type):
43
- raise TypeError(
44
- f"字段 '{field_name}' 类型错误:期望类型 {self.field_type}, 得到 {type(value)},值:{value!r}"
45
- )
46
- if self.max_length and len(str(value)) > self.max_length:
47
- raise ValueError(
48
- f"字段 '{field_name}' 长度超限:最大长度 {self.max_length},当前长度 {len(str(value))},值:{value!r}"
49
- )
50
-
51
- return value
52
-
53
- def __repr__(self):
1
+ #!/usr/bin/python
2
+ # -*- coding: UTF-8 -*-
3
+ """
4
+ Field 类定义
5
+ """
6
+
7
+ from typing import Any, Optional, Type
8
+
9
+
10
+ class Field:
11
+ """
12
+ 字段定义类,用于定义 Item 的字段属性和验证规则
13
+ """
14
+ def __init__(
15
+ self,
16
+ nullable: bool = True,
17
+ *,
18
+ default: Any = None,
19
+ field_type: Optional[Type] = None,
20
+ max_length: Optional[int] = None,
21
+ description: str = ""
22
+ ):
23
+ self.nullable = nullable
24
+ self.default = default
25
+ self.field_type = field_type
26
+ self.max_length = max_length
27
+ self.description = description
28
+
29
+ def validate(self, value: Any, field_name: str = "") -> Any:
30
+ """
31
+ 验证字段值是否符合规则
32
+ """
33
+ if value is None or (isinstance(value, str) and value.strip() == ""):
34
+ if self.default is not None:
35
+ return self.default
36
+ elif not self.nullable:
37
+ raise ValueError(
38
+ f"字段 '{field_name}' 不允许为空。"
39
+ )
40
+
41
+ if value is not None and not (isinstance(value, str) and value.strip() == ""):
42
+ if self.field_type and not isinstance(value, self.field_type):
43
+ raise TypeError(
44
+ f"字段 '{field_name}' 类型错误:期望类型 {self.field_type}, 得到 {type(value)},值:{value!r}"
45
+ )
46
+ if self.max_length and len(str(value)) > self.max_length:
47
+ raise ValueError(
48
+ f"字段 '{field_name}' 长度超限:最大长度 {self.max_length},当前长度 {len(str(value))},值:{value!r}"
49
+ )
50
+
51
+ return value
52
+
53
+ def __repr__(self):
54
54
  return f"<Field nullable={self.nullable} type={self.field_type} default={self.default}>"