Skip to content

Instantly share code, notes, and snippets.

@hanfei1991
Created January 26, 2023 10:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hanfei1991/6a6dae16f1f3fc5d778450da7f874e10 to your computer and use it in GitHub Desktop.
Save hanfei1991/6a6dae16f1f3fc5d778450da7f874e10 to your computer and use it in GitHub Desktop.
#!/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 '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'])
@mini4
Copy link

mini4 commented Mar 13, 2023

You don't processed regex_flag key of device_parsers, example, here.
You need to add the following code, like here:

...
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'
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment