GOPHERSPACE.DE - P H O X Y
gophering on gopher.linkerror.com
#!/usr/bin/env python3

import sys
from pprint import pprint

def usage():
    print("Usage: " + sys.argv[0] + " ")

def parse_csv(filename: str):
    with open(filename) as fh:
        content = fh.read()
        lines = content.split("\n")
        line_number = 0
        headers=[]
        rows=[]
        for line in lines:
            fields = line.split(",")
            row = {}
            field_number = 0
            for field in fields:
                if line_number == 0:
                    headers.append(field)
                else:
                    row[headers[field_number]] = field
                field_number += 1
            if line_number != 0:
                rows.append(row)
            line_number += 1
        return rows

def parse_freqstr(str: str) -> list:
    freqs = str.split("/")
    result = []
    for freq in freqs:
        freq = freq.strip()
        if freq != "":
            result.append(freq)
    return result

def parse_freqs(row_data) -> list:
    result = []
    for i in range(1,6):
        field_name = "emmitter_" + str(i) + "_freqs_mhz"
        if field_name in row_data:
            freqs = parse_freqstr(row_data[field_name])
            if len(freqs) > 0:
                for freq in freqs:
                    result.append(float(freq))
    return result

def generate_frequency_database(data: list) -> list:
    result = {}
    for row in data:
        freqs = parse_freqs(row)
        for freq in freqs:
            if freq in result:
                if not row in result[freq]:
                    result[freq].append(row)
            else:
                result[freq] = [row]
    return result

# -----------------------------------------------------------------------------

if (len(sys.argv) != 2):
    usage()
    sys.exit(1)

data = parse_csv(sys.argv[1])
freqdb = generate_frequency_database(data)
for frequency, data in sorted(freqdb.items()):
    print("---------------------------------------------")
    print(frequency)
    print("---------------------------------------------")
    for entry in data:
        print(entry['licensee_entity_name'])