File: extract-icao-ranges.py

package info (click to toggle)
dump1090-mutability 1.15~20180310.4a16df3%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,964 kB
  • sloc: ansic: 7,520; javascript: 4,359; sh: 417; python: 406; makefile: 70
file content (66 lines) | stat: -rwxr-xr-x 1,965 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
#!/usr/bin/env python3

import subprocess
import re

command = [
    'pdftotext', 
    '-layout',
    '-f', '187',
    '-l', '191',
    '-enc', 'ASCII7',
    'adsb-AN10_V3_cons.pdf',
    '-'
]
main_line = re.compile(r' ([^*]+?)\s+\*\s+([01-]{4})\s+([01-]{2})\s+([01-]{3})\s+([01-]{3})\s+([01-]{2})\s+([-]{10})\s*')
continuation_line = re.compile(r'  ([^ ].*)\s*')

def scan():
    matches = []

    process = subprocess.Popen(command,
                               stdin=subprocess.DEVNULL,
                               stdout=subprocess.PIPE)
    match = None
    for line in process.stdout:
        line = line.decode('ascii')
        if match:
            cmatch = continuation_line.match(line)
            if cmatch:
                country = match.group(1) + ' ' + cmatch.group(1)
            else:
                country = match.group(1)

            matches.append((country, 
                            match.group(2) + match.group(3) +
                            match.group(4) + match.group(5) +
                            match.group(6) + match.group(7)))

            if cmatch:
                match = None
                continue

        match = main_line.match(line)

    if match:
        matches.append((match.group(1),
                        match.group(2) + match.group(3) +
                        match.group(4) + match.group(5) +
                        match.group(6) + match.group(7)))

    return matches

if __name__ == '__main__':
    matches = scan()

    print ('var ICAO_Ranges = [');

    for country, assignment in matches:
        low = int(assignment.replace('-', '0'), 2)
        high = int(assignment.replace('-', '1'), 2)
        print('        {{ start: 0x{low:06X}, end: 0x{high:06X}, country: "{country}", flag_image: "{flag}" }},'.format(
            low=low,
            high=high,
            country=country,
            flag=country.replace(' ','_').replace("'","").replace('-','_') + '.png'))
    print ('];')