def set_cache_size(self, cache_size): """Set a new value for the maximum number of database entries in the cache. Use the value ``float('inf')`` to disable caching. If the new cache is smaller than the previous one and cannot contain all the entries anymore, entries are removed from the cache until the number of entries equals the cache size. Which entries are removed depends on the implementation of the cache (e.g. :class:`sunpy.database.caching.LRUCache`, :class:`sunpy.database.caching.LFUCache`). """ cmds = CompositeOperation() # remove items from the cache if the given argument is lower than the # current cache size while cache_size < self.cache_size: # remove items from the cache until cache_size == maxsize of the # cache entry_id, entry = self._cache.to_be_removed cmd = commands.RemoveEntry(self.session, entry) if self._enable_history: cmds.add(cmd) else: cmd() del self._cache[entry_id] self._cache.maxsize = cache_size if cmds: self._command_manager.do(cmds)
def add_many(self, database_entries, ignore_already_added=False): """Add a row of database entries "at once". If this method is used, only one entry is saved in the undo history. Parameters ---------- database_entries : iterable of sunpy.database.tables.DatabaseEntry The database entries that will be added to the database. ignore_already_added : bool, optional See Database.add """ cmds = CompositeOperation() for database_entry in database_entries: # use list(self) instead of simply self because __contains__ checks # for existence in the database and not only all attributes except # ID. if database_entry in list(self) and not ignore_already_added: raise EntryAlreadyAddedError(database_entry) cmd = commands.AddEntry(self.session, database_entry) if self._enable_history: cmds.add(cmd) else: cmd() if database_entry.id is None: self._cache.append(database_entry) else: self._cache[database_entry.id] = database_entry if cmds: self._command_manager.do(cmds)
def tag(self, database_entry, *tags): """Assign the given database entry the given tags. Raises ------ TypeError If no tags are given. sunpy.database.TagAlreadyAssignedError If at least one of the given tags is already assigned to the given database entry. """ if not tags: raise TypeError('at least one tag must be given') # avoid duplicates tag_names = set(tags) cmds = CompositeOperation() for tag_name in tag_names: try: tag = self.get_tag(tag_name) if tag in database_entry.tags: raise TagAlreadyAssignedError(database_entry, tag_names) except NoSuchTagError: # tag does not exist yet -> create it tag = tables.Tag(tag_name) cmd = commands.AddTag(self.session, database_entry, tag) if self._enable_history: cmds.add(cmd) else: cmd() if cmds: self._command_manager.do(cmds)
def add_from_dir(self, path, recursive=False, pattern='*', ignore_already_added=False, time_string_parse_format=None): """ Search the given directory for FITS files and use their FITS headers to add new entries to the database. Note that one entry in the database is assigned to a list of FITS headers, so not the number of FITS headers but the number of FITS files which have been read determine the number of database entries that will be added. FITS files are detected by reading the content of each file, the ``pattern`` argument may be used to avoid reading entire directories if one knows that all FITS files have the same filename extension. Parameters ---------- path : str The directory where to look for FITS files. recursive : bool, optional If True, the given directory will be searched recursively. Otherwise, only the given directory and no subdirectories are searched. The default is `False`, i.e. the given directory is not searched recursively. pattern : str, optional The pattern can be used to filter the list of filenames before the files are attempted to be read. The default is to collect all files. This value is passed to the function :func:`fnmatch.filter`, see its documentation for more information on the supported syntax. ignore_already_added : bool, optional See :meth:`sunpy.database.Database.add`. time_string_parse_format : str, optional Fallback timestamp format which will be passed to `~astropy.time.Time.strptime` if `sunpy.time.parse_time` is unable to automatically read the ``date-obs`` metadata. """ cmds = CompositeOperation() entries = tables.entries_from_dir( path, recursive, pattern, self.default_waveunit, time_string_parse_format=time_string_parse_format) for database_entry, filepath in entries: if database_entry in list(self) and not ignore_already_added: raise EntryAlreadyAddedError(database_entry) cmd = commands.AddEntry(self.session, database_entry) if self._enable_history: cmds.add(cmd) else: cmd() self._cache.append(database_entry) if cmds: self._command_manager.do(cmds)
def add_from_dir(self, path, recursive=False, pattern='*', ignore_already_added=False, time_string_parse_format=None): """Search the given directory for FITS files and use their FITS headers to add new entries to the database. Note that one entry in the database is assigned to a list of FITS headers, so not the number of FITS headers but the number of FITS files which have been read determine the number of database entries that will be added. FITS files are detected by reading the content of each file, the `pattern` argument may be used to avoid reading entire directories if one knows that all FITS files have the same filename extension. Parameters ---------- path : string The directory where to look for FITS files. recursive : bool, optional If True, the given directory will be searched recursively. Otherwise, only the given directory and no subdirectories are searched. The default is `False`, i.e. the given directory is not searched recursively. pattern : string, optional The pattern can be used to filter the list of filenames before the files are attempted to be read. The default is to collect all files. This value is passed to the function :func:`fnmatch.filter`, see its documentation for more information on the supported syntax. ignore_already_added : bool, optional See :meth:`sunpy.database.Database.add`. time_string_parse_format : str, optional Fallback timestamp format which will be passed to `~datetime.datetime.strftime` if `sunpy.time.parse_time` is unable to automatically read the `date-obs` metadata. """ cmds = CompositeOperation() entries = tables.entries_from_dir( path, recursive, pattern, self.default_waveunit, time_string_parse_format=time_string_parse_format) for database_entry, filepath in entries: if database_entry in list(self) and not ignore_already_added: raise EntryAlreadyAddedError(database_entry) cmd = commands.AddEntry(self.session, database_entry) if self._enable_history: cmds.add(cmd) else: cmd() self._cache.append(database_entry) if cmds: self._command_manager.do(cmds)
def clear(self): """Remove all entries from the database. This operation can be undone using the :meth:`undo` method. """ cmds = CompositeOperation() for entry in self: for tag in entry.tags: cmds.add(commands.RemoveTag(self.session, entry, tag)) # TODO: also remove all FITS header entries and all FITS header # comments from each entry before removing the entry itself! # remove all entries from all helper tables database_tables = [ tables.JSONDump, tables.Tag, tables.FitsHeaderEntry, tables.FitsKeyComment ] for table in database_tables: for entry in self.session.query(table): cmds.add(commands.RemoveEntry(self.session, entry)) for entry in self: cmds.add(commands.RemoveEntry(self.session, entry)) del self._cache[entry.id] if self._enable_history: self._command_manager.do(cmds) else: cmds()
def clear(self): """Remove all entries from the database. This operation can be undone using the :meth:`undo` method. """ cmds = CompositeOperation() for entry in self: for tag in entry.tags: cmds.add(commands.RemoveTag(self.session, entry, tag)) # TODO: also remove all FITS header entries and all FITS header # comments from each entry before removing the entry itself! # remove all entries from all helper tables database_tables = [ tables.JSONDump, tables.Tag, tables.FitsHeaderEntry, tables.FitsKeyComment] for table in database_tables: for entry in self.session.query(table): cmds.add(commands.RemoveEntry(self.session, entry)) for entry in self: cmds.add(commands.RemoveEntry(self.session, entry)) del self._cache[entry.id] if self._enable_history: self._command_manager.do(cmds) else: cmds()
def remove_many(self, database_entries): """Remove a row of database entries "at once". If this method is used, only one entry is saved in the undo history. Parameters ---------- database_entries : iterable of sunpy.database.tables.DatabaseEntry The database entries that will be removed from the database. """ cmds = CompositeOperation() for database_entry in database_entries: cmd = commands.RemoveEntry(self.session, database_entry) if self._enable_history: cmds.add(cmd) else: cmd() try: del self._cache[database_entry.id] except KeyError: pass if cmds: self._command_manager.do(cmds)
def test_undo_redo_multiple_cmds_at_once(session, command_manager): assert command_manager.undo_commands == [] command_manager.do(CompositeOperation([ AddEntry(session, DatabaseEntry()), AddEntry(session, DatabaseEntry()), AddEntry(session, DatabaseEntry())])) assert len(command_manager.undo_commands) == 1 assert session.query(DatabaseEntry).count() == 3 command_manager.undo() assert command_manager.undo_commands == [] assert session.query(DatabaseEntry).count() == 0 command_manager.redo() assert command_manager.redo_commands == [] assert session.query(DatabaseEntry).count() == 3
def remove_tag(self, database_entry, tag_name): """Remove the given tag from the database entry. If the tag is not connected to any entry after this operation, the tag itself is removed from the database as well. Raises ------ sunpy.database.NoSuchTagError If the tag is not connected to the given entry. """ tag = self.get_tag(tag_name) cmds = CompositeOperation() remove_tag_cmd = commands.RemoveTag(self.session, database_entry, tag) remove_tag_cmd() if self._enable_history: cmds.add(remove_tag_cmd) if not tag.data: remove_entry_cmd = commands.RemoveEntry(self.session, tag) remove_entry_cmd() if self._enable_history: cmds.add(remove_entry_cmd) if self._enable_history: self._command_manager.push_undo_command(cmds)