Skip to content

Commit

Permalink
py: bookmark import progress
Browse files Browse the repository at this point in the history
  • Loading branch information
haxscramper committed Mar 18, 2024
1 parent d1efa80 commit 3dcf77e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from sqlalchemy.orm import declarative_base, sessionmaker, Session
from sqlalchemy import Column, select
from datetime import datetime
from py_scriptutils.rich_utils import render_rich_pprint

CAT = Path(__file__).name

Expand All @@ -37,10 +38,12 @@ class BookmarkRecord(BaseModel, extra="forbid"):
book: str
text: str
dateadd: datetime
datelast: datetime
dateedit: datetime


Base = declarative_base()


class Bookmarks(Base):
__tablename__ = "bookmarks"
id = IdColumn()
Expand Down Expand Up @@ -89,8 +92,9 @@ def get_bookmarks(session: Session) -> List[BookmarkRecord]:
)

for item in session.execute(expr).mappings():
log(CAT).info(item)
result.append(BookmarkRecord(**item))
result.append(
BookmarkRecord.model_validate(
{k: item[k] for k in BookmarkRecord.model_fields}))

return result

Expand All @@ -109,7 +113,13 @@ def cli(ctx: click.Context, config: str, **kwargs) -> None:
# node = parseCachedFile(opts., opts.cachedir)
engine = open_sqlite(opts.infile)
session = sessionmaker()(bind=engine)
get_bookmarks(session)
bookmarks = get_bookmarks(session)

for it in bookmarks[:5]:
log(CAT).info("\n" + render_rich_pprint(
it,
max_string=80,
))


if __name__ == "__main__":
Expand Down
22 changes: 22 additions & 0 deletions scripts/py_scriptutils/py_scriptutils/rich_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from rich.console import Console
from rich.pretty import pprint


def render_rich(it) -> str:
Expand All @@ -7,3 +8,24 @@ def render_rich(it) -> str:
console.print(it)

return capture.get()


def render_rich_pprint(
obj,
width: int = 80,
color: bool = True,
max_string: int | None = None,
) -> str:

console = Console(record=True, width=width, force_terminal=color)

pprint(
obj,
console=console,
indent_guides=False,
max_length=width,
max_string=max_string,
)

return console.export_text()

0 comments on commit 3dcf77e

Please sign in to comment.