Esempio n. 1
0
    def filerec(self, rel_path, create=False):
        """Return a database record for a file"""

        import sqlalchemy.orm.exc

        s = self.bundle.database.session

        if not rel_path:
            raise ValueError("Must supply rel_path")

        try:
            o = s.query(File).filter(File.path == rel_path).one()
            o._is_new = False
        except sqlalchemy.orm.exc.NoResultFound as e:

            if not create:
                raise e

            a_path = self.filesystem.path(rel_path)
            o = File(
                path=rel_path,
                content_hash=Filesystem.file_hash(a_path),
                modified=os.path.getmtime(a_path),
                process="none",
            )
            s.add(o)
            s.commit()
            o._is_new = True

        except Exception as e:
            return None

        return o
Esempio n. 2
0
    def get_url(self, source_url, create=False):
        """Return a database record for a file"""

        import sqlalchemy.orm.exc

        s = self.bundle.database.session

        try:
            o = s.query(File).filter(File.source_url == source_url).one()

        except sqlalchemy.orm.exc.NoResultFound:
            if create:
                o = File(source_url=source_url, path=source_url, process="none")
                s.add(o)
                s.commit()
            else:
                return None

        o.session = s  # Files have SavableMixin
        return o