Browse Source

genlink.py: refactor to expose some kind of device info API

Reviewed-by: Karl Palsson <karlp@tweak.au>
pull/1526/head
Mattia Maldini 4 years ago
committed by Karl Palsson
parent
commit
0817f9175e
  1. 99
      scripts/genlink.py

99
scripts/genlink.py

@ -23,14 +23,8 @@ import sys
import re
import os
if len(sys.argv) != 4:
print("usage: %s <device-data-file> <device> <mode>" % sys.argv[0], file=sys.stderr)
sys.exit(1)
data_file_path = sys.argv[1]
find = sys.argv[2].lower()
mode = sys.argv[3].upper()
def get_device_data(data_file_path, device_id):
device = {
'info': {},
'defs': [],
@ -53,7 +47,7 @@ with open(data_file_path, 'r') as data_file:
pattern, parent, data = parts[0], parts[1], parts[2:]
# skip line if pattern did not match first element
if not fnmatch.fnmatch(find, pattern):
if not fnmatch.fnmatch(device_id, pattern):
continue
# extract data
@ -80,14 +74,14 @@ with open(data_file_path, 'r') as data_file:
continue
# device family
device['family'].append(find)
device['family'].append(device_id)
# break if this was the last line in this chain
if parent == 'END':
break
# look for the parent
find = parent
device_id = parent
# reverse device list
device['family'] = device['family'][::-1]
@ -96,30 +90,95 @@ device['family'] = device['family'][::-1]
if len(device['family']) == 0:
sys.exit(1)
return device
def get_device_family_flags(data_file_path, device_id, device=None):
if not device:
device = get_device_data(data_file_path, device_id)
return ' '.join('-D%s' % d.upper() for d in device['family'])
def get_device_defines(data_file_path, device_id, device=None):
if not device:
device = get_device_data(data_file_path, device_id)
defs = ' '.join('-D_%s=%s' % d for d in device['defs'])
return defs
def get_device_family(data_file_path, device_id, device=None):
if not device:
device = get_device_data(data_file_path, device_id)
if len(device['family']) > 0:
return device['family'][0]
else:
return ''
def get_device_subfamily(data_file_path, device_id, device=None):
if not device:
device = get_device_data(data_file_path, device_id)
if len(device['family']) > 1:
return device['family'][1]
else:
return ''
def get_device_info(data_file_path, device_id, info, device=None):
if not device:
device = get_device_data(data_file_path, device_id)
info = info.lower()
if info in device['info']:
return device['info'][info]
else:
return ''
def main():
if len(sys.argv) != 4:
print("usage: %s <device-data-file> <device> <mode>" % sys.argv[0],
file=sys.stderr)
sys.exit(1)
data_file_path = sys.argv[1]
find = sys.argv[2].lower()
mode = sys.argv[3].upper()
device = get_device_data(data_file_path, find)
# for CPPFLAGS and DEFS, define device family
if mode in ('CPPFLAGS', 'DEFS'):
sys.stdout.write(' '.join('-D%s' % d.upper() for d in device['family']))
sys.stdout.write(
get_device_family_flags(data_file_path, find, device=device))
# defines
if mode == 'DEFS':
if len(device['defs']) > 0:
defs = ' '.join('-D_%s=%s' % d for d in device['defs'])
defs = get_device_defines(data_file_path, find, device=device)
if defs:
sys.stdout.write(' ' + defs)
# device family
elif mode == 'FAMILY':
if len(device['family']) > 0:
sys.stdout.write(device['family'][0])
sys.stdout.write(get_device_family(data_file_path, find,
device=device))
# device subfamily
elif mode == 'SUBFAMILY':
if len(device['family']) > 1:
sys.stdout.write(device['family'][1])
sys.stdout.write(
get_device_subfamily(data_file_path, find, device=device))
# device info
else:
info = mode.lower()
if info in device['info']:
sys.stdout.write(device['info'][info])
sys.stdout.write(
get_device_info(data_file_path, find, mode, device=device))
sys.stdout.flush()
if __name__ == '__main__':
main()
Loading…
Cancel
Save