#! /usr/bin/python3 # Last edited on 2026-01-22 11:44:54 by stolfi # Library function that reads the star property table of the SPS. # To be imported by python programs. from process_funcs import basic_line_loop import re, sys from sys import stderr as err def read_table(fname): # Reads the contents of the star property from file {fname}. # # Blank lines and #-comments in file {fname} are ignored. # # There should be a line starting with "PAGE " that is the header # for the table, and one staring with "---" that is the dashes # for that header. # # The other lines must have the format "{fnum} | S{nn} | {props}" # where {fnum} is a page's f-number, "S{nn}" is the code of a star # from that page (where {nn} is the star number {snum} as two digits, # starting from "01") and {props} is the star properties, in any # format. # # Returns strings {hdr1,hdr2} and dict {tbl}, where {tbl[fnum][snum-1] is # the table line for "S{snum}" from the page whose f-number is # {fnum}, and {hdr1,hdr2} are the table header and dash line. # # For now, the description of a star is a single string, unparsed. tbl = dict() fnum_cur = None last_snum = None hdr1 = None hdr2 = None def process_line(nread, line): nonlocal tbl, fnum_cur, last_snum, hdr1, hdr2 line = line.strip() if re.match(r"^[ \011]*([#]|$)", line): return if re.match(r"PAGE ", line): hdr1 = line; return if re.match(r"[-][-][-]", line): hdr2 = line; return m = re.fullmatch(r"(f[0-9a-z]+)[ ]*[|][ ]*(S[0-9][0-9])[ ]*[|][ ](.*)", line) if m == None: err.write(f"{fname}:{nread}: ** invalid line format\n [[{line}]]\n") sys.exit(1) fnum, Snn, props = m.group(1,2,3) if fnum != fnum_cur: if fnum in tbl: err.write(f"{fname}:{nread}: ** repeated page = {fnum}\n [[{line}]]\n") sys.exit(1) tbl[fnum] = [] fnum_cur = fnum last_snum = 0 snum = int(Snn[1:]) if snum != last_snum + 1: err.write(f"{fname}:{nread}: ** unexpected star number = {Snn}\n [[{line}]]\n") sys.exit(1) tbl[fnum].append(line) last_snum = snum return # .................................................................... rd = open(fname, "r") nread = basic_line_loop(rd, process_line) if len(tbl) == 0: err.write(f"{fname}:{nread}: ** no entries in table file {fname}\n [[{line}]]\n") sys.exit(1) rd.close() return hdr1, hdr2, tbl # ----------------------------------------------------------------------