Ai
2 Star 0 Fork 0

chromium_develop/chromium_third_party_node

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
clean_json_attrs.py 1.48 KB
一键复制 编辑 原始数据 按行查看 历史
李想 提交于 2022-08-16 16:18 +08:00 . chromium origin init
#!/usr/bin/env python
import json
import os
import re
def Clean(start_dir, attr_pattern, file_pattern):
cleaned = False
def _remove_attrs(json_dict, attr_pattern):
assert isinstance(json_dict, dict)
removed = False
for key, val in json_dict.items():
if isinstance(val, dict):
if _remove_attrs(val, attr_pattern):
removed = True
elif re.search(attr_pattern, key):
del json_dict[key]
removed = True
return removed
for root, dirs, files in os.walk(start_dir):
for f in files:
if not re.search(file_pattern, f):
continue
path = os.path.join(root, f)
json_dict = json.loads(open(path).read())
if not _remove_attrs(json_dict, attr_pattern):
continue
with open(path, 'w') as new_contents:
new_contents.write(json.dumps(json_dict))
cleaned = True
return cleaned
if __name__ == '__main__':
import argparse
import sys
parser = argparse.ArgumentParser(
description='Recursively removes attributes from JSON files')
parser.add_argument('--attr_pattern', type=str, required=True,
help='A regex of attributes to remove')
parser.add_argument('--file_pattern', type=str, required=True,
help='A regex of files to clean')
parser.add_argument('start_dir', type=str,
help='A directory to start scanning')
args = parser.parse_args(sys.argv[1:])
Clean(start_dir=args.start_dir, attr_pattern=args.attr_pattern,
file_pattern=args.file_pattern)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chromium_develop/chromium_third_party_node.git
git@gitee.com:chromium_develop/chromium_third_party_node.git
chromium_develop
chromium_third_party_node
chromium_third_party_node
master

搜索帮助