def difference(observatory, old_file, new_file, *args, **keys): """Difference different kinds of CRDS files (mappings, FITS references, etc.) named `old_file` and `new_file` against one another and print out the results on stdout. Returns: 0 no differences 1 some differences 2 errors in subshells """ filetype = config.filetype(old_file) differs = { "mapping" : MappingDifferencer, "asdf" : AsdfDifferencer, "fits" : FitsDifferencer, "text" : TextDifferencer, "yaml" : TextDifferencer, "json" : JsonDifferencer, } differ_class = differs.get(filetype, None) if differ_class is None: log.warning("Cannot difference file of type", repr(filetype), ":", repr(old_file), repr(new_file)) status = 2 # arguably, this should be an error not a warning. wary of changing now. else: differ = differ_class(observatory, old_file, new_file, *args, **keys) status = differ.difference() return status
def get_filetype(filepath, original_name=None): """Determine file type from `original_name` if possible, otherwise attempt to idenitfy based on file contents. """ if original_name is None: original_name = os.path.basename(filepath) # Fast extension-based type determination filetype = config.filetype(original_name) if filetype != "unknown": return filetype with open(filepath, "rb") as handle: first_5 = str(handle.read(5).decode('utf-8')) if first_5 == "#ASDF": return "asdf" elif first_5 == "SIMPL": first_81 = first_5 + handle.read(76).decode('utf-8') if first_81[-1] == '\n': all_chars = first_81 + handle.read().decode('utf-8') lines = all_chars.splitlines() lengths = { len(line) for line in lines } if len(lengths) == 1 and 80 in lengths: return 'geis' else: return 'fits' else: return "fits" try: with open(filepath) as handle: import json json.load(handle) return "json" except Exception: pass try: with open(filepath) as handle: import yaml yaml.safe_load(handle) return "yaml" except Exception: pass return "unknown"
def is_dataset(name): """Returns True IFF `name` is plausible as a dataset. Not a guarantee.""" return config.filetype(name) in ["fits", "asdf", "geis"]