def get_file_properties(filename): """Figure out (instrument, filekind, serial) based on `filename` which should be a mapping or FITS reference file. >> get_file_properties("./hst_acs_biasfile_0001.rmap") ('acs', 'biasfile') >> get_file_properties("./hst_acs_biasfile_0001.pmap") Traceback (most recent call last): ... AssertionError: Invalid .pmap filename './hst_acs_biasfile_0001.pmap' >> get_file_properties("test_data/s7g1700gl_dead.fits") """ if config.is_mapping(filename): try: return decompose_newstyle_name(filename)[2:4] except Exception: # NOTE: load_mapping more conservative than fetch_mapping used in properties_from_mapping mapping = rmap.load_mapping(filename) return mapping.instrument, mapping.filekind elif config.is_reference(filename): result = get_reference_properties(filename)[2:4] else: try: result = properties_inside_mapping(filename) except Exception as exc: result = get_reference_properties(filename)[2:4] assert result[0] in INSTRUMENTS+[""], "Bad instrument " + \ repr(result[0]) + " in filename " + repr(filename) assert result[1] in FILEKINDS+[""], "Bad filekind " + \ repr(result[1]) + " in filename " + repr(filename) return result
def get_flex_uri(filename, observatory=None): """If environment variables define the base URI for `filename`, append filename and return the combined URI. If no environment override has been specified, obtain the base URI from the server_info config, append filename, and return the combined URI. If `filename` is a config file and no environment override is defined, return "none". """ if observatory is None: observatory = get_default_observatory() uri = config.get_uri(filename) if uri == "none": info = get_server_info() if config.is_config(filename): uri = _unpack_info(info, "config_url", observatory) elif config.is_pickle(filename): uri = _unpack_info(info, "pickle_url", observatory) elif config.is_mapping(filename): uri = _unpack_info(info, "mapping_url", observatory) elif config.is_reference(filename): uri = _unpack_info(info, "reference_url", observatory) else: raise CrdsError("Can't identify file type for:", srepr(filename)) if uri == "none": return uri if not uri.endswith("/"): uri += "/" uri += filename return uri
def get_file_properties(filename): """Figure out (instrument, filekind) based on `filename` which should be a mapping or FITS reference file. >>> get_file_properties("./hst_acs_biasfile_0001.rmap") ('acs', 'biasfile') >> get_file_properties("./hst_acs_biasfile_0001.pmap") Traceback (most recent call last): ... IOError: [Errno 2] No such file or directory: './hst_acs_biasfile_0001.pmap' >> get_file_properties("test_data/s7g1700gl_dead.fits") """ if data_file.is_geis_data(filename): # determine GEIS data file properties from corresponding header file. filename = filename[:-1] + "h" if config.is_mapping(filename): try: return decompose_newstyle_name(filename)[2:4] except Exception: return properties_inside_mapping(filename) elif config.is_reference(filename): result = get_reference_properties(filename)[2:4] else: try: result = properties_inside_mapping(filename) except Exception: result = get_reference_properties(filename)[2:4] assert result[0] in INSTRUMENTS+[""], "Bad instrument " + \ repr(result[0]) + " in filename " + repr(filename) assert result[1] in FILEKINDS+[""], "Bad filekind " + \ repr(result[1]) + " in filename " + repr(filename) return result
def main(self): """Generate names corrsponding to files listed on the command line.""" if self.args.standard: self.args.add_keywords = True self.args.verify_file = True if not self.args.files: return for filename in self.files: assert config.is_reference(filename), \ "File " + repr(filename) + " does not appear to be a reference file. Only references can be renamed." uniqname = naming.generate_unique_name(filename, self.observatory) if self.args.dry_run: log.info("Would rename", self.format_file(filename), "-->", self.format_file(uniqname)) else: self.rewrite(filename, uniqname) if self.args.remove_original: os.remove(filename) # XXXX script returns filename result not suitable as program exit status # XXXX filename result is insufficient if multiple files are specified. # XXXX filename result supports embedded use on web server returning new name. return uniqname
def _cat_file(self, path): """Print out information on a single reference or mapping at `path`.""" self._cat_banner("File:", os.path.abspath(path), delim="#", bottom_delim="-") if config.is_reference(path): self._cat_reference(path) else: self._cat_text(path) if self._file_info: self._cat_catalog_info(path)
def remove_checksum(file_): """Remove checksums from `file_`.""" log.info("Removing checksum for", repr(file_)) if config.is_reference(file_): data_file.remove_checksum(file_) elif rmap.is_mapping(file_): raise exceptions.CrdsError("Mapping checksums cannot be removed for:", repr(file_)) else: raise exceptions.CrdsError( "File", repr(file_), "does not appear to be a CRDS reference or mapping file.")
def add_checksum(file_): """Add checksums to file_.""" log.info("Adding checksum for", repr(file_)) if config.is_reference(file_): with log.error_on_exception("Failed updating checksum for", repr(file_)): data_file.add_checksum(file_) elif rmap.is_mapping(file_): update_mapping_checksum(file_) else: raise exceptions.CrdsError( "File", repr(file_), "does not appear to be a CRDS reference or mapping file.")
def verify_checksum(file_): """Verify checksums in `file_`.""" log.info("Verifying checksum for", repr(file_)) if config.is_reference(file_): data_file.verify_checksum(file_) elif rmap.is_mapping(file_): if config.CRDS_IGNORE_MAPPING_CHECKSUM.get(): log.warning( "Mapping checksums are disabled by config.CRDS_IGNORE_MAPPING_CHECKSM." ) rmap.load_mapping(file_) else: raise exceptions.CrdsError( "File", repr(file_), "does not appear to be a CRDS reference or mapping file.")
def get_file_properties(filename): """Figure out (instrument, filekind, serial) based on `filename` which should be a mapping or ASDF reference file. >>> get_file_properties('tests/data/roman_wfi16_f158_flat_small.asdf') ('wfi', 'flat') >>> get_file_properties('tests/data/roman_wfi_flat_0004.rmap') ('wfi', 'flat') >>> get_file_properties('tests/data/roman_0001.pmap') ('', '') >>> get_file_properties('tests/data/ascii_tab.csv') # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: string indices must be integers """ if config.is_mapping(filename): try: return decompose_newstyle_name(filename)[2:4] except Exception: # NOTE: load_mapping more conservative than fetch_mapping used in properties_from_mapping mapping = rmap.load_mapping(filename) return mapping.instrument, mapping.filekind elif config.is_reference(filename): result = get_reference_properties(filename)[2:4] else: try: result = properties_inside_mapping(filename) except Exception as exc: result = get_reference_properties(filename)[2:4] assert result[0] in INSTRUMENTS+[""], "Bad instrument " + \ repr(result[0]) + " in filename " + repr(filename) assert result[1] in FILEKINDS+[""], "Bad filekind " + \ repr(result[1]) + " in filename " + repr(filename) return result