Source code for geeViz.eeCatalogHelpers.table_metadata
"""Table schema inference from CSV files."""from__future__importannotationsfromcollections.abcimportIterable,MappingimportcsvfrompathlibimportPathimportrefrom.jsonnetimportJsonnetLiteral__all__=["TABLE_FIELD_TYPES","infer_table_field_type","build_table_schema_from_csv"]TABLE_FIELD_TYPES=frozenset({"int","double","string"})# Plain decimal text only: Python's int()/float() also accept "1_000", "nan",# "inf" and non-ASCII digits, none of which Earth Engine tables parse as numbers._INT_PATTERN=re.compile(r"[+-]?[0-9]+",re.ASCII)_DOUBLE_PATTERN=re.compile(r"[+-]?(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?",re.ASCII)
[docs]definfer_table_field_type(values:Iterable[str])->str:"""Infer a table field type from all populated CSV values."""populated=[value.strip()forvalueinvaluesifvalue.strip()]ifnotpopulated:return"string"ifall(_INT_PATTERN.fullmatch(value)forvalueinpopulated):return"int"ifall(_DOUBLE_PATTERN.fullmatch(value)forvalueinpopulated):return"double"return"string"
[docs]defbuild_table_schema_from_csv(csv_path:Path,field_descriptions:Mapping[str,str],*,type_overrides:Mapping[str,str]|None=None,)->list[dict[str,object]]:"""Build a Jsonnet-ready ``gee:schema`` from a CSV table. Descriptions are required for every CSV field. Type overrides accept ``int``, ``double``, and ``string``; other fields are inferred from all populated CSV values. """overrides=dict(type_overridesor{})withcsv_path.open(encoding="utf-8-sig",newline="")ashandle:reader=csv.DictReader(handle)fieldnames=reader.fieldnamesifnotfieldnames:raiseValueError(f"CSV table has no header: {csv_path}")iflen(fieldnames)!=len(set(fieldnames)):raiseValueError(f"CSV table has duplicate field names: {csv_path}")rows=list(reader)fields=set(fieldnames)missing_descriptions=[namefornameinfieldnamesifnotfield_descriptions.get(name,"").strip()]ifmissing_descriptions:raiseValueError("Missing table field descriptions: "+", ".join(missing_descriptions))unknown_descriptions=sorted(set(field_descriptions)-fields)ifunknown_descriptions:raiseValueError("Descriptions reference fields not present in the CSV: "+", ".join(unknown_descriptions))unknown_overrides=sorted(set(overrides)-fields)ifunknown_overrides:raiseValueError("Type overrides reference fields not present in the CSV: "+", ".join(unknown_overrides))invalid_types=sorted({field_typeforfield_typeinoverrides.values()iffield_typenotinTABLE_FIELD_TYPES})ifinvalid_types:raiseValueError("Unsupported table field types: "+", ".join(invalid_types)+"; expected int, double, or string")schema:list[dict[str,object]]=[]fornameinfieldnames:field_type=overrides.get(name,infer_table_field_type(row.get(name,"")or""forrowinrows),)schema.append({"name":name,"description":field_descriptions[name].strip(),"type":JsonnetLiteral(f"ee_const.var_type.{field_type}"),})returnschema