crawlo 1.1.0__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.
- crawlo/__init__.py +33 -24
- crawlo/__version__.py +1 -1
- crawlo/cli.py +40 -40
- crawlo/commands/__init__.py +13 -13
- crawlo/commands/check.py +594 -155
- crawlo/commands/genspider.py +125 -110
- crawlo/commands/list.py +147 -119
- crawlo/commands/run.py +285 -170
- crawlo/commands/startproject.py +111 -101
- crawlo/commands/stats.py +188 -167
- crawlo/core/__init__.py +2 -2
- crawlo/core/engine.py +158 -158
- crawlo/core/processor.py +40 -40
- crawlo/core/scheduler.py +57 -57
- crawlo/crawler.py +494 -492
- crawlo/downloader/__init__.py +78 -78
- crawlo/downloader/aiohttp_downloader.py +199 -199
- crawlo/downloader/cffi_downloader.py +242 -277
- crawlo/downloader/httpx_downloader.py +246 -246
- crawlo/event.py +11 -11
- crawlo/exceptions.py +78 -78
- crawlo/extension/__init__.py +31 -31
- crawlo/extension/log_interval.py +49 -49
- crawlo/extension/log_stats.py +44 -44
- crawlo/extension/logging_extension.py +34 -34
- crawlo/filters/__init__.py +37 -37
- crawlo/filters/aioredis_filter.py +150 -150
- crawlo/filters/memory_filter.py +202 -202
- crawlo/items/__init__.py +23 -23
- crawlo/items/base.py +21 -21
- crawlo/items/fields.py +53 -53
- crawlo/items/items.py +104 -104
- crawlo/middleware/__init__.py +21 -21
- crawlo/middleware/default_header.py +32 -32
- crawlo/middleware/download_delay.py +28 -28
- crawlo/middleware/middleware_manager.py +135 -135
- crawlo/middleware/proxy.py +245 -245
- crawlo/middleware/request_ignore.py +30 -30
- crawlo/middleware/response_code.py +18 -18
- crawlo/middleware/response_filter.py +26 -26
- crawlo/middleware/retry.py +90 -90
- crawlo/network/__init__.py +7 -7
- crawlo/network/request.py +203 -203
- crawlo/network/response.py +166 -166
- crawlo/pipelines/__init__.py +13 -13
- crawlo/pipelines/console_pipeline.py +39 -39
- crawlo/pipelines/mongo_pipeline.py +116 -116
- crawlo/pipelines/mysql_batch_pipline.py +272 -272
- crawlo/pipelines/mysql_pipeline.py +195 -195
- crawlo/pipelines/pipeline_manager.py +56 -56
- crawlo/project.py +153 -0
- crawlo/settings/__init__.py +7 -7
- crawlo/settings/default_settings.py +166 -168
- crawlo/settings/setting_manager.py +99 -99
- crawlo/spider/__init__.py +129 -129
- crawlo/stats_collector.py +59 -59
- crawlo/subscriber.py +106 -106
- crawlo/task_manager.py +27 -27
- crawlo/templates/crawlo.cfg.tmpl +10 -10
- crawlo/templates/project/__init__.py.tmpl +3 -3
- crawlo/templates/project/items.py.tmpl +17 -17
- crawlo/templates/project/middlewares.py.tmpl +75 -75
- crawlo/templates/project/pipelines.py.tmpl +63 -63
- crawlo/templates/project/settings.py.tmpl +54 -54
- crawlo/templates/project/spiders/__init__.py.tmpl +5 -5
- crawlo/templates/spider/spider.py.tmpl +31 -31
- crawlo/utils/__init__.py +7 -7
- crawlo/utils/date_tools.py +233 -233
- crawlo/utils/db_helper.py +343 -343
- crawlo/utils/func_tools.py +82 -82
- crawlo/utils/log.py +128 -128
- crawlo/utils/pqueue.py +173 -173
- crawlo/utils/request.py +267 -267
- crawlo/utils/spider_loader.py +62 -62
- crawlo/utils/system.py +11 -11
- crawlo/utils/tools.py +4 -4
- crawlo/utils/url.py +39 -39
- crawlo-1.1.1.dist-info/METADATA +220 -0
- crawlo-1.1.1.dist-info/RECORD +100 -0
- examples/__init__.py +7 -0
- examples/baidu_spider/__init__.py +7 -0
- examples/baidu_spider/demo.py +94 -0
- examples/baidu_spider/items.py +46 -0
- examples/baidu_spider/middleware.py +49 -0
- examples/baidu_spider/pipeline.py +55 -0
- examples/baidu_spider/run.py +27 -0
- examples/baidu_spider/settings.py +121 -0
- examples/baidu_spider/spiders/__init__.py +7 -0
- examples/baidu_spider/spiders/bai_du.py +61 -0
- examples/baidu_spider/spiders/miit.py +159 -0
- examples/baidu_spider/spiders/sina.py +79 -0
- tests/__init__.py +7 -7
- tests/test_proxy_health_check.py +32 -32
- tests/test_proxy_middleware_integration.py +136 -136
- tests/test_proxy_providers.py +56 -56
- tests/test_proxy_stats.py +19 -19
- tests/test_proxy_strategies.py +59 -59
- crawlo/utils/concurrency_manager.py +0 -125
- crawlo/utils/project.py +0 -197
- crawlo-1.1.0.dist-info/METADATA +0 -49
- crawlo-1.1.0.dist-info/RECORD +0 -97
- examples/gxb/__init__.py +0 -0
- examples/gxb/items.py +0 -36
- examples/gxb/run.py +0 -16
- examples/gxb/settings.py +0 -72
- examples/gxb/spider/__init__.py +0 -2
- examples/gxb/spider/miit_spider.py +0 -180
- examples/gxb/spider/telecom_device.py +0 -129
- {crawlo-1.1.0.dist-info → crawlo-1.1.1.dist-info}/WHEEL +0 -0
- {crawlo-1.1.0.dist-info → crawlo-1.1.1.dist-info}/entry_points.txt +0 -0
- {crawlo-1.1.0.dist-info → crawlo-1.1.1.dist-info}/top_level.txt +0 -0
crawlo/settings/__init__.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
#!/usr/bin/python
|
|
2
|
-
# -*- coding:UTF-8 -*-
|
|
3
|
-
"""
|
|
4
|
-
# @Time : 2025-05-11 11:08
|
|
5
|
-
# @Author : oscar
|
|
6
|
-
# @Desc : None
|
|
7
|
-
"""
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding:UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
# @Time : 2025-05-11 11:08
|
|
5
|
+
# @Author : oscar
|
|
6
|
+
# @Desc : None
|
|
7
|
+
"""
|
|
@@ -1,169 +1,167 @@
|
|
|
1
|
-
#!/usr/bin/python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
"""
|
|
4
|
-
==================================
|
|
5
|
-
Crawlo 项目配置文件
|
|
6
|
-
==================================
|
|
7
|
-
说明:
|
|
8
|
-
- 所有配置项均已按功能模块分类。
|
|
9
|
-
- 支持通过环境变量覆盖部分敏感配置(如 Redis、MySQL 密码等)。
|
|
10
|
-
- 可根据需求启用/禁用组件(如 MySQL、Redis、Proxy 等)。
|
|
11
|
-
"""
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
#
|
|
20
|
-
|
|
21
|
-
#
|
|
22
|
-
DOWNLOADER = "crawlo.downloader.
|
|
23
|
-
|
|
24
|
-
#
|
|
25
|
-
|
|
26
|
-
#
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
#
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
#
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
#
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
#
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
#
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
#
|
|
86
|
-
|
|
87
|
-
#
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
#
|
|
104
|
-
'crawlo.middleware.
|
|
105
|
-
'crawlo.middleware.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
#
|
|
110
|
-
'crawlo.middleware.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
#
|
|
116
|
-
|
|
117
|
-
#
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
#
|
|
124
|
-
|
|
125
|
-
'crawlo.extension.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
#
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
#
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
#
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
"
|
|
158
|
-
"
|
|
159
|
-
|
|
160
|
-
"
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
|
|
168
|
-
'(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
==================================
|
|
5
|
+
Crawlo 项目配置文件
|
|
6
|
+
==================================
|
|
7
|
+
说明:
|
|
8
|
+
- 所有配置项均已按功能模块分类。
|
|
9
|
+
- 支持通过环境变量覆盖部分敏感配置(如 Redis、MySQL 密码等)。
|
|
10
|
+
- 可根据需求启用/禁用组件(如 MySQL、Redis、Proxy 等)。
|
|
11
|
+
"""
|
|
12
|
+
import os
|
|
13
|
+
|
|
14
|
+
# ============================== 核心信息 ==============================
|
|
15
|
+
PROJECT_NAME = 'crawlo'
|
|
16
|
+
|
|
17
|
+
# ============================== 网络请求配置 ==============================
|
|
18
|
+
|
|
19
|
+
# 下载器选择(三选一)
|
|
20
|
+
# DOWNLOADER = "crawlo.downloader.aiohttp_downloader.AioHttpDownloader"
|
|
21
|
+
DOWNLOADER = "crawlo.downloader.cffi_downloader.CurlCffiDownloader" # 支持浏览器指纹
|
|
22
|
+
# DOWNLOADER = "crawlo.downloader.httpx_downloader.HttpXDownloader"
|
|
23
|
+
|
|
24
|
+
# 请求超时与安全
|
|
25
|
+
DOWNLOAD_TIMEOUT = 30 # 下载超时时间(秒)
|
|
26
|
+
VERIFY_SSL = True # 是否验证 SSL 证书
|
|
27
|
+
USE_SESSION = True # 是否使用持久化会话(aiohttp 特有)
|
|
28
|
+
|
|
29
|
+
# 请求延迟控制
|
|
30
|
+
DOWNLOAD_DELAY = 1.0 # 基础延迟(秒)
|
|
31
|
+
RANDOM_RANGE = (0.8, 1.2) # 随机延迟系数范围
|
|
32
|
+
RANDOMNESS = True # 是否启用随机延迟
|
|
33
|
+
|
|
34
|
+
# 重试策略
|
|
35
|
+
MAX_RETRY_TIMES = 3 # 最大重试次数
|
|
36
|
+
RETRY_PRIORITY = -1 # 重试请求的优先级调整
|
|
37
|
+
RETRY_HTTP_CODES = [408, 429, 500, 502, 503, 504, 522, 524] # 触发重试的状态码
|
|
38
|
+
IGNORE_HTTP_CODES = [403, 404] # 直接标记成功、不重试的状态码
|
|
39
|
+
ALLOWED_CODES = [] # 允许的状态码(空表示不限制)
|
|
40
|
+
|
|
41
|
+
# 连接与响应大小限制
|
|
42
|
+
CONNECTION_POOL_LIMIT = 50 # 最大并发连接数(连接池大小)
|
|
43
|
+
DOWNLOAD_MAXSIZE = 10 * 1024 * 1024 # 最大响应体大小(10MB)
|
|
44
|
+
DOWNLOAD_WARN_SIZE = 1024 * 1024 # 响应体警告阈值(1MB)
|
|
45
|
+
DOWNLOAD_RETRY_TIMES = MAX_RETRY_TIMES # 下载器内部重试次数(复用全局)
|
|
46
|
+
|
|
47
|
+
# ============================== 并发与调度 ==============================
|
|
48
|
+
|
|
49
|
+
CONCURRENCY = 8 # 单个爬虫的并发请求数
|
|
50
|
+
INTERVAL = 5 # 日志统计输出间隔(秒)
|
|
51
|
+
DEPTH_PRIORITY = 1 # 深度优先策略优先级
|
|
52
|
+
MAX_RUNNING_SPIDERS = 3 # 最大同时运行的爬虫数
|
|
53
|
+
|
|
54
|
+
# ============================== 数据存储配置 ==============================
|
|
55
|
+
|
|
56
|
+
# --- MySQL 配置 ---
|
|
57
|
+
MYSQL_HOST = '127.0.0.1'
|
|
58
|
+
MYSQL_PORT = 3306
|
|
59
|
+
MYSQL_USER = 'root'
|
|
60
|
+
MYSQL_PASSWORD = '123456'
|
|
61
|
+
MYSQL_DB = 'crawl'
|
|
62
|
+
MYSQL_TABLE = 'crawlo'
|
|
63
|
+
MYSQL_BATCH_SIZE = 100 # 批量插入条数
|
|
64
|
+
|
|
65
|
+
# MySQL 连接池
|
|
66
|
+
MYSQL_FLUSH_INTERVAL = 5 # 缓存刷新间隔(秒)
|
|
67
|
+
MYSQL_POOL_MIN = 5
|
|
68
|
+
MYSQL_POOL_MAX = 20
|
|
69
|
+
MYSQL_ECHO = False # 是否打印 SQL 日志
|
|
70
|
+
|
|
71
|
+
# --- MongoDB 配置 ---
|
|
72
|
+
MONGO_URI = 'mongodb://user:password@host:27017'
|
|
73
|
+
MONGO_DATABASE = 'scrapy_data'
|
|
74
|
+
MONGO_COLLECTION = 'crawled_items'
|
|
75
|
+
MONGO_MAX_POOL_SIZE = 200
|
|
76
|
+
MONGO_MIN_POOL_SIZE = 20
|
|
77
|
+
|
|
78
|
+
# ============================== 去重过滤配置 ==============================
|
|
79
|
+
|
|
80
|
+
# 请求指纹存储目录(文件过滤器使用)
|
|
81
|
+
REQUEST_DIR = '.'
|
|
82
|
+
|
|
83
|
+
# 去重过滤器类(二选一)
|
|
84
|
+
FILTER_CLASS = 'crawlo.filters.memory_filter.MemoryFilter'
|
|
85
|
+
# FILTER_CLASS = 'crawlo.filters.redis_filter.AioRedisFilter' # 分布式去重
|
|
86
|
+
|
|
87
|
+
# --- Redis 过滤器配置 ---
|
|
88
|
+
REDIS_HOST = os.getenv('REDIS_HOST', '127.0.0.1')
|
|
89
|
+
REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
|
|
90
|
+
REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', 'oscar&0503')
|
|
91
|
+
REDIS_URL = f'redis://:{REDIS_PASSWORD or ""}@{REDIS_HOST}:{REDIS_PORT}/0'
|
|
92
|
+
REDIS_KEY = 'request_fingerprint' # Redis 中存储指纹的键名
|
|
93
|
+
REDIS_TTL = 0 # 指纹过期时间(0 表示永不过期)
|
|
94
|
+
CLEANUP_FP = 0 # 程序结束时是否清理指纹(0=不清理)
|
|
95
|
+
FILTER_DEBUG = True # 是否开启去重调试日志
|
|
96
|
+
DECODE_RESPONSES = True # Redis 返回是否解码为字符串
|
|
97
|
+
|
|
98
|
+
# ============================== 中间件配置 ==============================
|
|
99
|
+
|
|
100
|
+
MIDDLEWARES = [
|
|
101
|
+
# === 请求预处理阶段 ===
|
|
102
|
+
'crawlo.middleware.request_ignore.RequestIgnoreMiddleware', # 1. 忽略无效请求
|
|
103
|
+
'crawlo.middleware.download_delay.DownloadDelayMiddleware', # 2. 控制请求频率
|
|
104
|
+
'crawlo.middleware.default_header.DefaultHeaderMiddleware', # 3. 添加默认请求头
|
|
105
|
+
'crawlo.middleware.proxy.ProxyMiddleware', # 4. 设置代理
|
|
106
|
+
|
|
107
|
+
# === 响应处理阶段 ===
|
|
108
|
+
'crawlo.middleware.retry.RetryMiddleware', # 5. 失败请求重试
|
|
109
|
+
'crawlo.middleware.response_code.ResponseCodeMiddleware', # 6. 处理特殊状态码
|
|
110
|
+
'crawlo.middleware.response_filter.ResponseFilterMiddleware', # 7. 响应内容过滤
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
# ============================== 扩展与管道 ==============================
|
|
114
|
+
|
|
115
|
+
# 数据处理管道(启用的存储方式)
|
|
116
|
+
PIPELINES = [
|
|
117
|
+
'crawlo.pipelines.console_pipeline.ConsolePipeline', # 控制台输出
|
|
118
|
+
# 'crawlo.pipelines.mysql_pipeline.AsyncmyMySQLPipeline', # MySQL 存储(可选)
|
|
119
|
+
]
|
|
120
|
+
|
|
121
|
+
# 扩展组件(监控与日志)
|
|
122
|
+
EXTENSIONS = [
|
|
123
|
+
'crawlo.extension.log_interval.LogIntervalExtension', # 定时日志
|
|
124
|
+
'crawlo.extension.log_stats.LogStats', # 统计信息
|
|
125
|
+
'crawlo.extension.logging_extension.CustomLoggerExtension', # 自定义日志
|
|
126
|
+
]
|
|
127
|
+
|
|
128
|
+
# ============================== 日志与监控 ==============================
|
|
129
|
+
|
|
130
|
+
LOG_LEVEL = 'INFO' # 日志级别: DEBUG/INFO/WARNING/ERROR
|
|
131
|
+
STATS_DUMP = True # 是否周期性输出统计信息
|
|
132
|
+
LOG_FILE = f'logs/{PROJECT_NAME}.log' # 日志文件路径
|
|
133
|
+
LOG_FORMAT = '%(asctime)s - [%(name)s] - %(levelname)s: %(message)s'
|
|
134
|
+
LOG_ENCODING = 'utf-8'
|
|
135
|
+
|
|
136
|
+
# ============================== 代理配置 ==============================
|
|
137
|
+
|
|
138
|
+
PROXY_ENABLED = False # 是否启用代理
|
|
139
|
+
PROXY_API_URL = "https://api.proxyprovider.com/get" # 代理获取接口(请替换为真实地址)
|
|
140
|
+
|
|
141
|
+
# 代理提取方式(支持字段路径或函数)
|
|
142
|
+
PROXY_EXTRACTOR = "proxy" # 如返回 {"proxy": "http://1.1.1.1:8080"}
|
|
143
|
+
|
|
144
|
+
# 代理刷新控制
|
|
145
|
+
PROXY_REFRESH_INTERVAL = 60 # 代理刷新间隔(秒)
|
|
146
|
+
PROXY_API_TIMEOUT = 10 # 请求代理 API 超时时间
|
|
147
|
+
|
|
148
|
+
# ============================== Curl-Cffi 特有配置 ==============================
|
|
149
|
+
|
|
150
|
+
# 浏览器指纹模拟(仅 CurlCffi 下载器有效)
|
|
151
|
+
CURL_BROWSER_TYPE = "chrome" # 可选: chrome, edge, safari, firefox 或版本如 chrome136
|
|
152
|
+
|
|
153
|
+
# 自定义浏览器版本映射(可覆盖默认行为)
|
|
154
|
+
CURL_BROWSER_VERSION_MAP = {
|
|
155
|
+
"chrome": "chrome136",
|
|
156
|
+
"edge": "edge101",
|
|
157
|
+
"safari": "safari184",
|
|
158
|
+
"firefox": "firefox135",
|
|
159
|
+
# 示例:旧版本测试
|
|
160
|
+
# "chrome_legacy": "chrome110",
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
# 默认请求头(可被 Spider 覆盖)
|
|
164
|
+
DEFAULT_REQUEST_HEADERS = {
|
|
165
|
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
|
|
166
|
+
'(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
|
|
169
167
|
}
|
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
#!/usr/bin/python
|
|
2
|
-
# -*- coding: UTF-8 -*-
|
|
3
|
-
import json
|
|
4
|
-
from copy import deepcopy
|
|
5
|
-
from importlib import import_module
|
|
6
|
-
from collections.abc import MutableMapping
|
|
7
|
-
|
|
8
|
-
from crawlo.settings import default_settings
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
class SettingManager(MutableMapping):
|
|
12
|
-
|
|
13
|
-
def __init__(self, values=None):
|
|
14
|
-
self.attributes = {}
|
|
15
|
-
self.set_settings(default_settings)
|
|
16
|
-
self.update_attributes(values)
|
|
17
|
-
|
|
18
|
-
def get(self, key, default=None):
|
|
19
|
-
"""安全获取值,不触发递归"""
|
|
20
|
-
value = self.attributes.get(key, default)
|
|
21
|
-
return value if value is not None else default
|
|
22
|
-
|
|
23
|
-
def get_int(self, key, default=0):
|
|
24
|
-
return int(self.get(key, default=default))
|
|
25
|
-
|
|
26
|
-
def get_float(self, key, default=0.0):
|
|
27
|
-
return float(self.get(key, default=default))
|
|
28
|
-
|
|
29
|
-
def get_bool(self, key, default=False):
|
|
30
|
-
got = self.get(key, default=default)
|
|
31
|
-
if isinstance(got, bool):
|
|
32
|
-
return got
|
|
33
|
-
if isinstance(got, (int, float)):
|
|
34
|
-
return bool(got)
|
|
35
|
-
got_lower = str(got).strip().lower()
|
|
36
|
-
if got_lower in ('1', 'true'):
|
|
37
|
-
return True
|
|
38
|
-
if got_lower in ('0', 'false'):
|
|
39
|
-
return False
|
|
40
|
-
raise ValueError(
|
|
41
|
-
f"Unsupported value for boolean setting: {got}. "
|
|
42
|
-
"Supported values are: 0/1, True/False, '0'/'1', 'True'/'False' (case-insensitive)."
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
def get_list(self, key, default=None):
|
|
46
|
-
values = self.get(key, default or [])
|
|
47
|
-
if isinstance(values, str):
|
|
48
|
-
return [v.strip() for v in values.split(',') if v.strip()]
|
|
49
|
-
try:
|
|
50
|
-
return list(values)
|
|
51
|
-
except TypeError:
|
|
52
|
-
return [values]
|
|
53
|
-
|
|
54
|
-
def get_dict(self, key, default=None):
|
|
55
|
-
value = self.get(key, default or {})
|
|
56
|
-
if isinstance(value, str):
|
|
57
|
-
value = json.loads(value)
|
|
58
|
-
try:
|
|
59
|
-
return dict(value)
|
|
60
|
-
except TypeError:
|
|
61
|
-
return value
|
|
62
|
-
|
|
63
|
-
def set(self, key, value):
|
|
64
|
-
self.attributes[key] = value
|
|
65
|
-
|
|
66
|
-
def set_settings(self, module):
|
|
67
|
-
if isinstance(module, str):
|
|
68
|
-
module = import_module(module)
|
|
69
|
-
for key in dir(module):
|
|
70
|
-
if key.isupper():
|
|
71
|
-
self.set(key, getattr(module, key))
|
|
72
|
-
|
|
73
|
-
# 实现 MutableMapping 必须的方法
|
|
74
|
-
def __getitem__(self, item):
|
|
75
|
-
return self.attributes[item]
|
|
76
|
-
|
|
77
|
-
def __setitem__(self, key, value):
|
|
78
|
-
self.set(key, value)
|
|
79
|
-
|
|
80
|
-
def __delitem__(self, key):
|
|
81
|
-
del self.attributes[key]
|
|
82
|
-
|
|
83
|
-
def __iter__(self):
|
|
84
|
-
return iter(self.attributes)
|
|
85
|
-
|
|
86
|
-
def __len__(self):
|
|
87
|
-
return len(self.attributes)
|
|
88
|
-
|
|
89
|
-
def __str__(self):
|
|
90
|
-
return f'<Settings: {self.attributes}>'
|
|
91
|
-
|
|
92
|
-
__repr__ = __str__
|
|
93
|
-
|
|
94
|
-
def update_attributes(self, attributes):
|
|
95
|
-
if attributes is not None:
|
|
96
|
-
for key, value in attributes.items():
|
|
97
|
-
self.set(key, value)
|
|
98
|
-
|
|
99
|
-
def copy(self):
|
|
1
|
+
#!/usr/bin/python
|
|
2
|
+
# -*- coding: UTF-8 -*-
|
|
3
|
+
import json
|
|
4
|
+
from copy import deepcopy
|
|
5
|
+
from importlib import import_module
|
|
6
|
+
from collections.abc import MutableMapping
|
|
7
|
+
|
|
8
|
+
from crawlo.settings import default_settings
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class SettingManager(MutableMapping):
|
|
12
|
+
|
|
13
|
+
def __init__(self, values=None):
|
|
14
|
+
self.attributes = {}
|
|
15
|
+
self.set_settings(default_settings)
|
|
16
|
+
self.update_attributes(values)
|
|
17
|
+
|
|
18
|
+
def get(self, key, default=None):
|
|
19
|
+
"""安全获取值,不触发递归"""
|
|
20
|
+
value = self.attributes.get(key, default)
|
|
21
|
+
return value if value is not None else default
|
|
22
|
+
|
|
23
|
+
def get_int(self, key, default=0):
|
|
24
|
+
return int(self.get(key, default=default))
|
|
25
|
+
|
|
26
|
+
def get_float(self, key, default=0.0):
|
|
27
|
+
return float(self.get(key, default=default))
|
|
28
|
+
|
|
29
|
+
def get_bool(self, key, default=False):
|
|
30
|
+
got = self.get(key, default=default)
|
|
31
|
+
if isinstance(got, bool):
|
|
32
|
+
return got
|
|
33
|
+
if isinstance(got, (int, float)):
|
|
34
|
+
return bool(got)
|
|
35
|
+
got_lower = str(got).strip().lower()
|
|
36
|
+
if got_lower in ('1', 'true'):
|
|
37
|
+
return True
|
|
38
|
+
if got_lower in ('0', 'false'):
|
|
39
|
+
return False
|
|
40
|
+
raise ValueError(
|
|
41
|
+
f"Unsupported value for boolean setting: {got}. "
|
|
42
|
+
"Supported values are: 0/1, True/False, '0'/'1', 'True'/'False' (case-insensitive)."
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
def get_list(self, key, default=None):
|
|
46
|
+
values = self.get(key, default or [])
|
|
47
|
+
if isinstance(values, str):
|
|
48
|
+
return [v.strip() for v in values.split(',') if v.strip()]
|
|
49
|
+
try:
|
|
50
|
+
return list(values)
|
|
51
|
+
except TypeError:
|
|
52
|
+
return [values]
|
|
53
|
+
|
|
54
|
+
def get_dict(self, key, default=None):
|
|
55
|
+
value = self.get(key, default or {})
|
|
56
|
+
if isinstance(value, str):
|
|
57
|
+
value = json.loads(value)
|
|
58
|
+
try:
|
|
59
|
+
return dict(value)
|
|
60
|
+
except TypeError:
|
|
61
|
+
return value
|
|
62
|
+
|
|
63
|
+
def set(self, key, value):
|
|
64
|
+
self.attributes[key] = value
|
|
65
|
+
|
|
66
|
+
def set_settings(self, module):
|
|
67
|
+
if isinstance(module, str):
|
|
68
|
+
module = import_module(module)
|
|
69
|
+
for key in dir(module):
|
|
70
|
+
if key.isupper():
|
|
71
|
+
self.set(key, getattr(module, key))
|
|
72
|
+
|
|
73
|
+
# 实现 MutableMapping 必须的方法
|
|
74
|
+
def __getitem__(self, item):
|
|
75
|
+
return self.attributes[item]
|
|
76
|
+
|
|
77
|
+
def __setitem__(self, key, value):
|
|
78
|
+
self.set(key, value)
|
|
79
|
+
|
|
80
|
+
def __delitem__(self, key):
|
|
81
|
+
del self.attributes[key]
|
|
82
|
+
|
|
83
|
+
def __iter__(self):
|
|
84
|
+
return iter(self.attributes)
|
|
85
|
+
|
|
86
|
+
def __len__(self):
|
|
87
|
+
return len(self.attributes)
|
|
88
|
+
|
|
89
|
+
def __str__(self):
|
|
90
|
+
return f'<Settings: {self.attributes}>'
|
|
91
|
+
|
|
92
|
+
__repr__ = __str__
|
|
93
|
+
|
|
94
|
+
def update_attributes(self, attributes):
|
|
95
|
+
if attributes is not None:
|
|
96
|
+
for key, value in attributes.items():
|
|
97
|
+
self.set(key, value)
|
|
98
|
+
|
|
99
|
+
def copy(self):
|
|
100
100
|
return deepcopy(self)
|