def pacu_session(db: orm.session.Session): query: orm.Query = db.query(PacuSession) assert query.count() == 0 pacu_session = PacuSession() db.add(pacu_session) yield pacu_session
def activate(self, database: orm.session.Session) -> None: for other_session in database.query(PacuSession).filter(PacuSession.id != self.id): other_session.is_active = False database.add(other_session) self.is_active = True database.add(self) database.commit()
def ExportDatabase(session: orm.session.Session, export_path: pathlib.Path) -> None: """Export the contents of a database to a directory.""" query = session.query(contentfiles.ContentFile) logging.info('Exporting %s files to %s ...', humanize.intcomma(query.count()), export_path) for contentfile in query: path = export_path / (contentfile.sha256_hex + '.txt') logging.debug(path) with open(path, 'w') as f: f.write(contentfile.text)
def unmake(cls: Type[T], session: o.session.Session, **kwargs) -> None: """ Find the item with the specified name, and delete it if it exists. :param session: The session to be used in the query and creation. :param kwargs: Arguments to use in the :meth:`~sqlalchemy.orm.query.Query.filter_by` clause and in the item constructor. """ # Find the item item = session.query(cls).filter_by(**kwargs).one_or_none() # Delete the item if item is not None: session.delete(item)
def IsInDatabase( cls, session: orm.session.Session, proto: scrape_repos_pb2.GitHubRepoMetadata, ) -> bool: # TODO(cec): At some point it would be nice to have an expiration date for # scraped repos, after which the contents are considered "stale" and the # repo is re-scraped. This would require extending the behaviour of # ShouldImportRepo() to check the expiry date. instance = ( session.query(cls.clone_from_url) .filter(cls.clone_from_url == proto.clone_from_url) .first() ) return True if instance else False
def ExportDatabase( session: orm.session.Session, export_path: pathlib.Path ) -> None: """Export the contents of a database to a directory.""" query = session.query(contentfiles.ContentFile) app.Log( 1, "Exporting %s files to %s ...", humanize.Commas(query.count()), export_path, ) for contentfile in query: path = export_path / (contentfile.sha256 + ".txt") app.Log(2, path) with open(path, "w") as f: f.write(contentfile.text)
def make(cls: Type[T], session: o.session.Session, **kwargs) -> T: """ Find the item with the specified name, or create it if it doesn't exist. :param session: The session to be used in the query and creation. :param kwargs: Arguments to use in the :meth:`~sqlalchemy.orm.query.Query.filter_by` clause and in the item constructor. :return: The retrieved or created item. """ # Find the item item = session.query(cls).filter_by(**kwargs).one_or_none() # Create the item if item is None: item = cls(**kwargs) session.add(item) # Return the item return item
def IsInDatabase(cls, session: orm.session.Session, proto: scrape_repos_pb2.GitHubRepoMetadata) -> bool: instance = session.query(cls).filter_by( **cls._GetArgsFromProto(proto)).first() return True if instance else False
def get_all_as_dict(cls, session: orm.session.Session) -> typing.Dict: return {script.code: script for script in session.query(cls).all()}
def get_module_pages(cls, session: orm.session.Session) -> typing.List[Page]: return session.query(cls).filter_by(namespace=828).filter(cls.title.notlike('%/documentation')).all()