-
-
Save mini4/b3a8a55694abbc2725e9d7156be3e841 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from yaml import load, dump, FullLoader | |
import sys | |
file_name = sys.argv[1] | |
def process_ua(data): | |
for node in data: | |
if 'family_replacement' not in node: | |
node['family_replacement'] = '$1' | |
if 'v1_replacement' not in node: | |
node['v1_replacement'] = '$2' | |
if 'v2_replacement' not in node: | |
node['v2_replacement'] = '$3' | |
with open('ua.yaml', 'w') as stream: | |
dump(data, stream) | |
def process_os(data): | |
for node in data: | |
if 'os_replacement' not in node: | |
node['os_replacement'] = '$1' | |
if 'os_v1_replacement' not in node: | |
node['os_v1_replacement'] = '$2' | |
if 'os_v2_replacement' not in node: | |
node['os_v2_replacement'] = '$3' | |
if 'os_v3_replacement' not in node: | |
node['os_v3_replacement'] = '$4' | |
if 'os_v4_replacement' not in node: | |
node['os_v4_replacement'] = '$5' | |
with open('os.yaml', 'w') as stream: | |
dump(data, stream) | |
def process_device(data): | |
for node in data: | |
if 'regex_flag' in node: | |
node['regex'] = f'(?{node["regex_flag"]}:{node["regex"]})' | |
if 'device_replacement' not in node: | |
node['device_replacement'] = '$1' | |
#if 'brand_replacement' not in node: | |
# node['brand_replacement'] = '$2' | |
if 'model_replacement' not in node: | |
node['model_replacement'] = '$1' | |
with open('device.yaml', 'w') as stream: | |
dump(data, stream) | |
with open(file_name, 'r') as file: | |
data = file.read() | |
yml = load(data, Loader=FullLoader) | |
process_ua(yml['user_agent_parsers']) | |
process_os(yml['os_parsers']) | |
process_device(yml['device_parsers']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment