""" A grammar supporting C language boosters (or possibly other mechanisms bypassing internal dbtable). These actually bypass most of our machinery and should only be used if performance is paramount. Otherwise, CustomGrammars play much nicer with the rest of the DC software. Currently, only one kind of DirectGrammar is supported: C boosters. """ #c Copyright 2008-2025, the GAVO project #c #c This program is free software, covered by the GNU GPL. See the #c COPYING file in the source distribution. import os from gavo import pkg_resources import re import shutil import subprocess from gavo import base from gavo import utils from gavo import rscdef from gavo.grammars import common class CBooster(object): """is a wrapper for an import booster written in C using the DC booster infrastructure. Warning: If you change the booster description, you'll need to touch the source to recompile. """ silence_for_test = False def __init__(self, srcName, dataDesc, recordSize=4000, gzippedInput=False, autoNull=None, preFilter=None, ignoreBadRecords=False, customFlags=""): self.dataDesc = dataDesc self.recordSize = recordSize self.resdir = dataDesc.rd.resdir self.srcName = os.path.join(self.resdir, srcName) self.autoNull, self.preFilter = autoNull, preFilter self.ignoreBadRecords = ignoreBadRecords self.gzippedInput = gzippedInput self.customFlags = customFlags self.bindir = os.path.join(self.resdir, "bin") self.binaryName = os.path.join(self.bindir, os.path.splitext(os.path.basename(srcName))[0]+"-"+base.getConfig( "platform")) self._ensureBinary() def _copySources(self, srcDir): def getResource(src, dest): inF = pkg_resources.resource_stream('gavo', src) with open(dest, "wb") as f: f.write(inF.read()) inF.close() getResource("resources/src/boosterskel.c", "boosterskel.c") getResource("resources/src/boosterskel.h", "boosterskel.h") shutil.copyfile(os.path.join(srcDir, self.srcName), "func.c") def _build(self): callArgs = {} if self.silence_for_test: # test instrumentation callArgs["stdout"] = utils.devnull() if subprocess.call("make", **callArgs): raise base.ReportableError("Booster build failed, messages above.") def _getMakefile(self, dd): res = [] if dd.grammar.type=="fits": res.append("LDFLAGS += -lcfitsio") if dd.grammar.type.startswith("hdf5"): flags = subprocess.check_output( ["pkg-config", "--cflags", "hdf5"]).strip().decode("ascii") if flags: res.append(f"CFLAGS += {flags}") flags = subprocess.check_output( ["pkg-config", "--libs", "hdf5"]).strip().decode("ascii") if flags: res.append(f"LDFLAGS += {flags}") query_n_pars = len(dd.makes[0].table.columns) res.append("LDFLAGS += -lm\n" "CFLAGS += -Wall -DINPUT_LINE_MAX=%d -DQUERY_N_PARS=%s"%( self.recordSize, query_n_pars)) if self.autoNull: res.append("CFLAGS += -DAUTO_NULL='%s'"%self.autoNull.replace( "\\", "\\\\")) if self.ignoreBadRecords: res.append("CFLAGS += -DIGNORE_BAD_RECORDS") res.append("CFLAGS += -g\n") res.append("booster: boosterskel.c func.c\n" "\t$(CC) $(CFLAGS) %s -o booster $^ $(LDFLAGS)"%self.customFlags) return "\n".join(res) def _ensureBinary(self): """makes sure the booster binary exists and is up-to-date. """ if not os.path.exists(self.bindir): os.makedirs(self.bindir) try: if os.path.getmtime(self.srcName)2) { die(USAGE); } if (argc==2) { if (!(inF = fopen(argv[1], "r"))) { die(strerror(errno)); } } else { inF = stdin; } /* /seekable main intro */ """ COMMON_MAIN_INTRO = """ /* common main intro */ writeHeader(destination); /* /common main intro */ """ LOOP_BODY_INTRO = """ Field *tuple; context = inputLine; if (!setjmp(ignoreRecord)) { """ LOOP_BODY_FOOT = """ if (!tuple) { handleBadRecord("Bad input line at record %d", recCount); } writeTuple(tuple, QUERY_N_PARS, destination); context = NULL; recCount ++; if (!(recCount%1000)) { fprintf(stderr, "%08d\\r", recCount); fflush(stderr); } } """ COMMON_MAIN_FOOT = """ writeEndMarker(destination); fprintf(stderr, "%08d records done.\\n", recCount); } """ def _getMakeMacro(item): """returns a maker macro for a column object. """ try: return { "integer": "MAKE_INT", "smallint": "MAKE_SHORT", "double precision": "MAKE_DOUBLE", "real": "MAKE_FLOAT", "char": "MAKE_CHAR_NULL", "bytea": "MAKE_BYTE", "text": "MAKE_TEXT", "bigint": "MAKE_BIGINT", }[item.type] except KeyError: # not a simple case; this could be a place for doing arrays and such return "MAKE_somethingelse" class _CodeGenerator(object): """a base class for code generators. You will at least have to override getItemParser and getFooter if this is to do anything useful. """ def __init__(self, grammar, tableDef): self.grammar, self.tableDef = grammar, tableDef def getSetupCode(self): """returns a sequence of C lines for code between an item parser. """ return [] def getItemParser(self, item, index): """returns code that parses item (a Column instance) at column index index. You're free to ignore index. """ return [] def getPreamble(self): """returns a list of lines that make up the top of the booster. """ return [ '#include ', '#include ', '#include ', '#include ', '#include "boosterskel.h"', '', '#define USAGE "Usage: don\'t."',] def getPrototype(self): """returns the prototype of the getTuple function. """ return "Field *getTuple(char *inputLine, int recNo)" def getFooter(self): """returns the code for the createDumpfile method. You want to use the C fragments above for that. The default returns something that bombs out. """ return '#error "No getFooter defined in the code generator"' class _LineBasedCodeGenerator(_CodeGenerator): """a base class for code generators for reading line-based text files. """ def getFooter(self): """returns the main function of the parser (and possibly other stuff) in a string. This default implementation works for line-based parsers. """ return (COMMON_MAIN_HEADER +NONSEEK_MAIN_INTRO +COMMON_MAIN_INTRO +""" while (fgets(inputLine, INPUT_LINE_MAX, inF)) {""" +LOOP_BODY_INTRO +""" tuple = getTuple(inputLine, recCount);""" +LOOP_BODY_FOOT +"}\n" +COMMON_MAIN_FOOT) class ColCodeGenerator(_LineBasedCodeGenerator): def getItemParser(self, item, index): t = item.type if "smallint" in t: func = "parseShort" elif "int" in t: func = "parseInt" elif t in ["real", "float"]: func = "parseFloat" elif "double" in t: func = "parseDouble" elif "char" in t: func = "parseString" elif "bool" in t: func = "parseBlankBoolean" else: func = "parseWhatever" return ["%s(inputLine, F(%s), start, len);"%(func, getNameForItem(item))] def getPrototype(self): """returns the prototype of the getTuple function. """ return "Field *getTuple(char *inputLine, int recNo)" class SplitCodeGenerator(_LineBasedCodeGenerator): """a code generator for parsing files with lineas and separators. """ def __init__(self, grammar, tableDef): self.splitChar = getattr(grammar, "splitChar", "|") _CodeGenerator.__init__(self, grammar, tableDef) def getPreamble(self): return _LineBasedCodeGenerator.getPreamble(self)+[ "/* delete the next line for POSIX strtok */", "#define strtok strtok_u"] def getSetupCode(self): return _LineBasedCodeGenerator.getSetupCode(self)+[ 'char *curCont;', 'curCont = strtok(inputLine, "%s");'%self.splitChar] def getItemParser(self, item, index): t = item.type fi = getNameForItem(item) if t=="text": parse = ["F(%s)->type = VAL_TEXT;"%fi, "F(%s)->length = strlen(curCont);"%fi, "F(%s)->val.c_ptr = curCont;"%fi,] else: if t=="smallint": cType = "VAL_SHORT" elif t=="bigint": cType = "VAL_BIGINT" elif "int" in t: cType = "VAL_INT" elif t in ["real", "float"]: cType = "VAL_FLOAT" elif "double" in t: cType = "VAL_DOUBLE" elif "char"==t: cType = "VAL_CHAR" elif "char" in t: cType = "VAL_TEXT" elif "bool" in t: cType = "VAL_BOOL" else: cType = "###No appropriate type###" parse = ["fieldscanf(curCont, %s, %s);"%(fi, cType)] parse.append('curCont = strtok(NULL, "%s");'%self.splitChar) return parse class BinCodeGenerator(_CodeGenerator): """a code generator for reading fixed-length binary records. """ def getItemParser(self, item, index): t = item.type if t=="integer": pline = "%s(%s, *(int32_t*)(inputLine+));" elif t=="smallint": pline = "%s(%s, *(int16_t*)(inputLine+ ));" elif t=="double precision": pline = "%s(%s, *(double*)(inputLine+ ));" elif t=="real": pline = "%s(%s, *(float*)(inputLine+ ));" elif t=="char": pline = "%s(%s, *(char*)(inputLine+ ), '');" elif t=="bytea": pline = "%s(%s, *(double*)(inputLine+ ), '');" else: pline = "%s %s" return ["/* %s (%s) */"%(item.description, t), pline%(_getMakeMacro(item), getNameForItem(item))] def getPreamble(self): return _CodeGenerator.getPreamble(self)+[ "#define FIXED_RECORD_SIZE %d"%self.grammar.recordSize] def getFooter(self): return (COMMON_MAIN_HEADER +" int bytesRead = 0;\n" +NONSEEK_MAIN_INTRO +COMMON_MAIN_INTRO +""" while (1) { bytesRead = fread(inputLine, 1, FIXED_RECORD_SIZE, inF); if (bytesRead==0) { break; } else if (bytesRead!=FIXED_RECORD_SIZE) { die("Short record: Only %d bytes read.", bytesRead); } """ +LOOP_BODY_INTRO +""" tuple = getTuple(inputLine, recCount);""" +LOOP_BODY_FOOT +"}\n" +COMMON_MAIN_FOOT) class _NumpyMetaCodeGenerator(_CodeGenerator): """A base class for code generators built using numpy-compatible metadata. """ fitsTypes = { "B": ("TBYTE", "char"), "A": ("TSTRING", "char *"), "I": ("TSHORT", "short"), "J": ("TLONG", "long"), "K": ("TLONGLONG", "long long"), "E": ("TFLOAT", "float"), "L": ("TLOGICAL", "unsigned char"), "D": ("TDOUBLE", "double")} numpyTypes = { "byte": "char", "int8": "char", "short": "short", "int16": "short", "cint": "int", "int32": "int", "int64": "int64_t", "ubyte": "unsigned char", "uint8": "unsigned char", "ushort": "unsigned short", "uint16": "unsigned char", "uintc": "unsigned int", "ulonglong": "unsigned long long", "uint64": "uint64_t", "single": "float", "float16": "float", "float32": "float", "double": "double", "float64": "double", } makers = { "bigint": "MAKE_BIGINT", "smallint": "MAKE_SHORT", "bytea": "MAKE_BYTE", "text": "MAKE_TEXT", "integer": "MAKE_INT", "real": "MAKE_FLOAT", "double precision": "MAKE_DOUBLE", } def _parseFITSFormat(self, format, colName): """returns length and typecode for the supported FITS table types. All others raise errors. """ mat = re.match(r"(\d*)(.)$", format) if not mat: raise base.ReportableError("FITS type code '%s' of %s not handled" " by gavo mkboost; add handling if you can."%(format, colName)) if not mat.group(2) in self.fitsTypes: raise base.ReportableError("FITS type '%s' of %s not handled" " by gavo mkboost; add handling if you can."%( mat.group(2), colName)) return int(mat.group(1) or "1"), mat.group(2) class FITSCodeGenerator(_NumpyMetaCodeGenerator): """A code generator for reading from FITS binary tables. """ def __init__(self, grammar, tableDef): from gavo.utils import pyfits _CodeGenerator.__init__(self, grammar, tableDef) self.forExtension = grammar.extension # now pull the table schema from the first source. if self.grammar.parent.sources is None: raise base.StructureError("Cannot make FITS bintable booster without" " a sources element on the embedding data.") try: hdus = pyfits.open( next(self.grammar.parent.sources.iterSources())) self.fitsTable = hdus[self.forExtension] except StopIteration: raise base.StructureError("Building a FITS bintable booster requires" " at least one matching source.") hdus.close() self._computeMatches() def _computeMatches(self): """adds .fitsIndexForCol and .colForFITSIndex attributes. These are matches based on the respective column names, where we do a case-insensitive matching for now. Nones mean that no corresponding column is present; for FITS columns, this means they are ignored. For table columns, this means that stand-in code is generated for filling out later. """ tableColumns = dict((col.name.lower(), col) for col in self.tableDef) if len(tableColumns)!=len(self.tableDef.columns): raise base.StructureError("Table unsuitable for FITS boosting as" " column names identical after case folding are present.", hint="Use mapKeys to adapt FITS table names to resolve" " the ambiguity") self.colForFITSIndex = {} for index, fitsCol in enumerate(self.fitsTable.columns): columnName = self.grammar.keyMap.get(fitsCol.name, fitsCol.name).lower() self.colForFITSIndex[index] = tableColumns.get(columnName) self.fitsIndexForColName = {} for index, col in self.colForFITSIndex.items(): if col is None: continue self.fitsIndexForColName[col.name.lower()] = index def getItemParser(self, item, index): try: fitsIndex = self.fitsIndexForColName[item.name.lower()] fitsCol = self.fitsTable.columns[fitsIndex] castTo = self.fitsTypes[ self._parseFITSFormat(fitsCol.format, fitsCol.name)[1] ][1] return [ "/* %s (%s) */"%(item.description, item.type), "if (nulls[%d][rowIndex]) {"%fitsIndex, " MAKE_NULL(%s);"%getNameForItem(item), "} else {", " %s(%s, ((%s*)(data[%d]))[rowIndex]);"%( self.makers[item.type], getNameForItem(item), castTo, fitsIndex), "}",] except KeyError: # no FITS table source column return ["MAKE_NULL(%s); /* %s(%s, FILL IN VALUE); */"%( getNameForItem(item), _getMakeMacro(item), getNameForItem(item))] def getPreamble(self): return _CodeGenerator.getPreamble(self)+[ "#include ", "#include ", "#define FITSCATCH(x) if (x) {fatalFitsError(status);}", "void fatalFitsError(int status) {", " if (status==0) {", " return;", " }", " fits_report_error(stderr, status);", " abort();", "}", ] def getPrototype(self): return "Field *getTuple(void *data[], char *nulls[], int rowIndex)" def _parseFITSFormat(self, format, colName): """returns length and typecode for the supported FITS table types. All others raise errors. """ mat = re.match(r"(\d*)(.)$", format) if not mat: raise base.ReportableError("FITS type code '%s' of %s not handled" " by gavo mkboost; add handling if you can."%(format, colName)) if not mat.group(2) in self.fitsTypes: raise base.ReportableError("FITS type '%s' of %s not handled" " by gavo mkboost; add handling if you can."%( mat.group(2), colName)) return int(mat.group(1) or "1"), mat.group(2) def _getColDescs(self): """returns a C initializer for an array of FITSColDescs. """ res = [] for index, fcd in enumerate(self.fitsTable.columns): col = self.colForFITSIndex[index] if col is None: # table column not part of FITS table, suppress reading # by having .cSize=0 res.append("{.cSize = 0, .fitsType = 0, .index=0, .arraysize=0}") continue length, typecode = self._parseFITSFormat(fcd.format, fcd.name) if typecode=="A": # special handling for strings, as we need their size # var length strings have been rejected above res.append( "{.cSize = %d, .fitsType = TSTRING, .index=%d, .arraysize=1}"%( length, index+1)) else: res.append( "{.cSize = sizeof(%s), .fitsType = %s, .index=%d, .arraysize=%d}"%( self.fitsTypes[typecode][1], self.fitsTypes[typecode][0], index+1, length)) return res def getFooter(self): colDescs = self._getColDescs() infoDict = { "nCols": len(colDescs), "colDescs": "{\n%s\n}"%",\n".join(colDescs), "extension": self.forExtension, } return (""" typedef struct FITSColDesc_s { size_t cSize; int fitsType; int index; /* in the FITS columns */ size_t arraysize; } FITSColDesc; FITSColDesc COL_DESCS[%(nCols)d] = %(colDescs)s; """%infoDict+COMMON_MAIN_HEADER +""" fitsfile *fitsInput; int ignored, i; int status = 0; int hdutype = 0; long nRows = 0; void *data[%(nCols)d]; char *nulls[%(nCols)d]; if (argc>2) { die(USAGE); } if (argc==2) { FITSCATCH(fits_open_table(&fitsInput, argv[1], READONLY, &status)); } else { die("FITS tables cannot be read from stdin."); } FITSCATCH(fits_movabs_hdu(fitsInput, %(extension)s+1, &hdutype, &status)); assert(hdutype==BINARY_TBL); FITSCATCH(fits_get_num_rows(fitsInput, &nRows, &status)); for (i=0; i<%(nCols)d; i++) { if (COL_DESCS[i].cSize==0) { /* Column not used */ continue; } else if (COL_DESCS[i].fitsType==TSTRING) { char *stringHoldings = NULL; if (!(data[i] = malloc(nRows*sizeof(char*))) || !(stringHoldings = malloc(nRows*(COL_DESCS[i].cSize+1)))) { die("out of memory"); } else { int k; /* Initialize the char* in the data array */ for (k=0; k") (opts, args) = parser.parse_args() if len(args)!=1: parser.print_help() sys.exit(1) return opts, args[0] def getSource(grammarId): """returns a bytestring containing C source to parse grammarId. """ grammar, td = getGrammarAndTable(grammarId) src = buildSource(grammar, td) return src def main(): from gavo import rscdesc #noflake: cache registration try: opts, grammarId = parseCmdLine() print(getSource(grammarId)) except SystemExit as msg: sys.exit(msg.code)