Skip to content

Core 核心库

概述

open-note-core 是 Open Note 的 opennote 扩展工具的核心抽象层,提供笔记操作的统一接口。

安装

bash
pip install open-note-core

核心概念

Note

python
from open_note_core import Note

note = Note(
    id="uuid",
    title="标题",
    content="内容",
    created_at="2024-01-01T00:00:00Z",
    updated_at="2024-01-01T00:00:00Z"
)

Category

python
from open_note_core import Category

category = Category(
    id="uuid",
    name="分类名称",
    parent_id=None  # 父分类 ID
)

Tag

python
from open_note_core import Tag

tag = Tag(
    id="uuid",
    name="标签名"
)

API 参考

NoteService

笔记操作服务。

create_note

python
def create_note(
    title: str,
    content: str,
    category_id: str | None = None,
    tags: list[str] | None = None
) -> Note

创建新笔记。

get_note

python
def get_note(note_id: str) -> Note | None

获取单个笔记。

update_note

python
def update_note(
    note_id: str,
    title: str | None = None,
    content: str | None = None
) -> Note

更新笔记。

delete_note

python
def delete_note(note_id: str) -> bool

删除笔记。

list_notes

python
def list_notes(
    category_id: str | None = None,
    tags: list[str] | None = None,
    limit: int = 20,
    offset: int = 0
) -> list[Note]

获取笔记列表。

CategoryService

分类操作服务。

create_category

python
def create_category(
    name: str,
    parent_id: str | None = None
) -> Category

get_category_tree

python
def get_category_tree() -> list[CategoryNode]

获取分类树。

TagService

标签操作服务。

add_tag

python
def add_tag(note_id: str, tag_name: str) -> Tag

remove_tag

python
def remove_tag(note_id: str, tag_id: str) -> bool

get_note_tags

python
def get_note_tags(note_id: str) -> list[Tag]

数据模型

Note

字段类型描述
idstr唯一标识
titlestr标题
contentstr内容
created_atdatetime创建时间
updated_atdatetime更新时间
category_idstr | None分类 ID
sourcestr来源(manual/clipboard/url/file)

Category

字段类型描述
idstr唯一标识
namestr名称
parent_idstr | None父分类 ID

Tag

字段类型描述
idstr唯一标识
namestr名称

错误处理

python
from open_note_core import NoteNotFoundError, ValidationError

try:
    note = service.get_note("invalid-id")
except NoteNotFoundError:
    print("笔记不存在")
except ValidationError as e:
    print(f"验证错误: {e}")

示例

完整工作流

python
from open_note_core import OpenNoteCore

# 初始化
core = OpenNoteCore(database_url="sqlite:///notes.db")

# 创建分类
work_category = core.categories.create(name="工作")

# 创建笔记
note = core.notes.create(
    title="会议记录",
    content="讨论内容...",
    category_id=work_category.id,
    tags=["重要", "待办"]
)

# 获取笔记
fetched = core.notes.get_note(note.id)

# 更新笔记
updated = core.notes.update_note(note.id, content="更新后的内容")

# 搜索
results = core.notes.list_notes(tags=["重要"])

# 删除
core.notes.delete_note(note.id)

扩展

实现自定义存储后端:

python
from open_note_core import BaseNoteRepository

class MyRepository(BaseNoteRepository):
    def create(self, note: Note) -> Note:
        # 自定义实现
        pass
    
    def get(self, note_id: str) -> Note | None:
        # 自定义实现
        pass