Beispiel #1
0
    def save_user_edits(self, changes):
        """
        :param collections.abc.Iterable[ModEntry] changes: an iterable of ModEntry objects
        """

        rows_to_delete = [(m.ordinal,) for m in changes]

        # a generator that creates tuples of values by sorting the
        # values of the modentry according the order defined in
        # constants._db_fields
        dbrowgen = (
            tuple([getattr(mod, field)
                   for field in sorted(mod._fields,
                                       key=lambda f: _db_fields.index(
                                           f))
                   ])
            for mod in changes)

        # using the context manager may allow deferrable foreign
        # to go unsatisfied for a moment

        with self._dman.conn:
            # delete the row with the given ordinal
            self._dman.delete("mods", "ordinal=?", rows_to_delete, True)

            # and reinsert

            self._dman.insert(len(_db_fields), "mods", *_db_fields,
                              params=dbrowgen)

        # And finally save changes to disk
        self.save_mod_list()
Beispiel #2
0
    def to_row_tuple(pairs):
        """
        Used as object_pair_hook for json.load(). Takes the mod
        information loaded from the json file and converts it
        to a tuple of just the field values in the
        correct order for feeding to the sqlite database.

        :param typing.Sequence[tuple[str, Any]] pairs:
        :return: Tuple containing just the values of the fields
        """

        return (next(_mcount),) + tuple(
            s[1] for s in sorted(pairs,
                                 key=lambda p: db_fields.index(p[0])))