Documentation
¶
Overview ¶
Package extract is the parse stage: one extractor per media type, each turning an item's bytes into units, such as a thread's post and comments or a book's pages. A registry keyed by media type picks the extractor, so an item parses the same whatever source sent it.
An extractor registers by joining builtin. One written in Go sets Extract. One whose library is not Go is a Program from the worker image, run through isolatedprocess. Each output is keyed by item, extractor and version, so a changed extractor takes a new version and its outputs write beside the old.
Index ¶
Constants ¶
const MediaTypePDF = "application/pdf"
MediaTypePDF is a PDF's media type, as a folder connector lands one.
const ProgramDir = "/usr/local/libexec/ingestion/extractors"
ProgramDir is where the worker image installs each extractor program.
const ProgramTimeout = 30 * time.Minute
ProgramTimeout is a program extractor's Timeout unless its registration sets another.
const PyMuPDFVersion = "1.28.2"
PyMuPDFVersion is the PyMuPDF that release/python-requirements.txt pins, and that the worker image installs for the PDF programs.
Variables ¶
var PDFText = func() Extractor { e := Program("pdf-text", "1+pymupdf-"+PyMuPDFVersion, MediaTypePDF, ProgramDir+"/pdf-text") e.Positional = true return e }()
PDFText reads a PDF's text with PyMuPDF, one unit per PDF page, so unit n is PDF page n, a blank page included. A unit's label is the page's printed label where the PDF declares page labels, and null where it declares none. Its version names PyMuPDF's, so a library upgrade writes its pages beside the old ones.
var ThreadText = Extractor{Name: "thread-text", Version: "1", MediaType: redditexport.MediaTypeSave, Extract: threadText}
ThreadText parses a Reddit save: its post, then each comment of its thread depth first, in the order Reddit sorted them. A comment that its author deleted or a moderator removed is left out, and its replies are kept. Each unit's label is its author.
Functions ¶
func Capability ¶
Capability is the capability of e's parse tasks, so a worker claims only the tasks of extractors it runs.
Types ¶
type Extractor ¶
type Extractor struct {
// Name names the extractor in each output it writes, such as thread-text.
Name string
// Version is written beside each output, so a changed extractor takes a
// new one.
Version string
MediaType string
// Program is the absolute path of the extractor's program, and empty for
// one written in Go. A worker runs it only where the program is installed.
Program string
// Extract reads body and answers its units in order.
Extract func(ctx context.Context, body io.Reader) ([]Unit, error)
// Timeout bounds one run of Extract, and none where it is zero. A run
// past it fails its task, so a program that hangs on one item's bytes
// frees its worker slot and the item waits in review.
Timeout time.Duration
// Positional keeps a unit with no text in its place, so unit n is the
// artifact's own part n, such as a book's PDF page n. Otherwise such a
// unit is dropped and the rest are numbered again.
Positional bool
}
Extractor turns the bytes of one media type into a parse's units.
func Program ¶
Program is an extractor that runs the program at path with args, such as one whose library is Python. The program reads the item's bytes on stdin, and writes one JSON line per unit on stdout, in order:
{"label": "Chapter 1", "text": "It was a bright cold day…"}
label may be null or left out, and any other field is refused. A run that exits non-zero fails its task, with the end of its stderr kept, and so does a run past ProgramTimeout. The program runs through isolatedprocess, so it holds none of the worker's credentials.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry picks the extractor for an item by its media type.
func Builtin ¶
func Builtin() *Registry
Builtin is the registry of every extractor that ingestion holds.
func NewRegistry ¶
NewRegistry registers extractors. It refuses one with no name, version, media type or way to run, and two of one name or one media type.
func (*Registry) Handlers ¶
Handlers is a worker handler for each extractor of r that runs here: each written in Go, and each whose program is installed. A worker claims only these extractors' tasks.
func (*Registry) QueueParse ¶
QueueParse queues a parse task for item where an extractor reads its media type. Landing calls it in the write that adds the item, so every item lands with its parse queued.
func (*Registry) Replayable ¶
func (r *Registry) Replayable() server.Replayable
Replayable is the parse stage as a replay runs it again: a task for each item whose media type an extractor reads.