示例#1
0
文件: test_dal.py 项目: sysid/twbm
def dal_pristine():
    dal = DAL(env_config=config)
    dal.sim_results_db_url = "sqlite:///:memory:"
    with dal as dal:
        # noinspection PyProtectedMember
        metadata.create_all(dal._sql_alchemy_db_engine)
        yield dal
示例#2
0
def tags(
        # ctx: typer.Context,
        tag: str = typer.
    Argument(
        None,
        help=(
            "tag for which related tags should be shown. No input: all tags are"
            " printed."),
    ),
        verbose: bool = typer.Option(False, "-v", "--verbose"),
):
    """
    No parameter: Show all tags

    With tag as parameter:
    Show related tags, i.e. tags which are used in combination with tag.
    """
    if verbose:
        typer.echo(f"Using DB: {config.twbm_db_url}", err=True)

    if tag is not None:
        tag = tag.strip(",").strip().lower()

    with DAL(env_config=config) as dal:
        if tag is None:
            tags = dal.get_all_tags()
        else:
            tags = dal.get_related_tags(tag=tag)
        for t in tags:
            typer.echo(f"{t[1]:>4}: {t[0]}", err=True)
示例#3
0
def _update_tags(
    ids: Sequence[int],
    tags: Optional[Sequence[str]] = None,
    tags_not: Optional[Sequence[str]] = None,
    force: bool = False,
):
    bms = Bookmarks(fts_query="").bms
    if tags is None:
        tags = ()
    if tags_not is None:
        tags_not = ()

    with DAL(env_config=config) as dal:
        # to allow for redefinition: set -> list
        # (https://mypy.readthedocs.io/en/stable/common_issues.html#redefinitions-with-incompatible-types)
        new_tags: Iterable
        for id in ids:
            bm = bms[id - 1]
            if force:
                new_tags = set(tags)
            else:
                new_tags = (set(bm.split_tags) | set(tags)) - set(tags_not)
            new_tags = sorted(new_tags)
            new_tags_str = f",{','.join(new_tags)},"
            _log.debug(f"{new_tags_str=}")
            bm.tags = new_tags_str
            dal.update_bookmark(bm)

            show_bms((bm, ))
示例#4
0
def dal(init_db):
    dal = DAL(env_config=config)
    with dal as dal:
        sql_files_path = Path(__file__).parent.absolute() / "sql"
        aiosql_queries = aiosql.from_path(f"{sql_files_path}", "sqlite3")
        aiosql_queries.load_testdata(dal.conn.connection)
        dal.conn.connection.commit()
        yield dal
示例#5
0
def show(
        # ctx: typer.Context,
        id_: int = typer.Argument(..., help="id to print"),
        verbose: bool = typer.Option(False, "-v", "--verbose"),
):
    if verbose:
        typer.echo(f"Using DB: {config.twbm_db_url}", err=True)

    # _ = BukuDb(dbfile=config.dbfile).print_rec(index=id_)  # noqa E800
    with DAL(env_config=config) as dal:
        bm = dal.get_bookmark_by_id(id_=id_)
        show_bms((bm, ))
示例#6
0
def delete(
        # ctx: typer.Context,
        id_: int = typer.Argument(..., help="id to delete"),
        verbose: bool = typer.Option(False, "-v", "--verbose"),
):
    if verbose:
        typer.echo(f"Using DB: {config.twbm_db_url}", err=True)

    # use buku because of DB compactdb
    with DAL(env_config=config) as dal:
        bm = dal.get_bookmark_by_id(id_=id_)
        show_bms((bm, ))
    _ = BukuDb(dbfile=config.dbfile).delete_rec(index=id_, delay_commit=False)
示例#7
0
def open(
        # ctx: typer.Context,
        ids: str = typer.Argument(
            None, help="list of ids, separated by comma, no blanks"),
        verbose: bool = typer.Option(False, "-v", "--verbose"),
):
    """
    Opens bookmarks

    Gotcha:
    in order to allow for piped input, ids must be separated by comma with no blanks.

    Example for using piped input:

        twbm search xxxxx | twbm open
    """
    if verbose:
        typer.echo(f"Using DB: {config.twbm_db_url}", err=True)

    # Gotcha: running from IDE looks like pipe
    is_pipe = not isatty(sys.stdin.fileno())

    if is_pipe:
        ids = sys.stdin.readline()

    try:
        ids = [int(x.strip()) for x in ids.split(",")]  # type: ignore
    except ValueError:
        typer.secho(f"-E- Wrong input format.", fg=typer.colors.RED, err=True)
        raise typer.Abort()

    print(ids)
    with DAL(env_config=config) as dal:
        for id_ in ids:
            bm = dal.get_bookmark_by_id(id_=id_)
            show_bms((bm, ))
            # TODO: generalize opening URI
            if bm.URL.startswith("http"):
                webbrowser.open(bm.URL, new=2)
            else:
                open_it(bm.URL)
示例#8
0
文件: bookmarks.py 项目: sysid/twbm
    def __init__(self, fts_query: str):
        self.fts_query = fts_query

        with DAL(env_config=config) as dal:
            self.bms = dal.get_bookmarks(fts_query=fts_query)
        _ = None
示例#9
0
文件: bookmarks.py 项目: sysid/twbm
def check_tags(tags: Sequence[str]) -> Sequence[str]:
    with DAL(env_config=config) as dal:
        all_tags = set([r[0] for r in dal.get_all_tags()])
        return sorted((set(tags) - all_tags))
示例#10
0
                          desc="")
        if result is not None:
            url, title, tags_in, desc_in = result
        else:
            raise typer.Abort()

    id_ = BukuDb(dbfile=config.dbfile).add_rec(
        url=url,
        title_in=title,
        tags_in=tags_in,
        desc=desc,
        immutable=0,
        delay_commit=False,
        fetch=(not nofetch),
    )
    with DAL(env_config=config) as dal:
        bm = dal.get_bookmark_by_id(id_=id_)
        show_bms((bm, ))


@app.command()
def show(
        # ctx: typer.Context,
        id_: int = typer.Argument(..., help="id to print"),
        verbose: bool = typer.Option(False, "-v", "--verbose"),
):
    if verbose:
        typer.echo(f"Using DB: {config.twbm_db_url}", err=True)

    # _ = BukuDb(dbfile=config.dbfile).print_rec(index=id_)  # noqa E800
    with DAL(env_config=config) as dal: