File: field_ref.py

package info (click to toggle)
python-lunr 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,644 kB
  • sloc: python: 3,811; javascript: 114; makefile: 60
file content (26 lines) | stat: -rw-r--r-- 807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from lunr.exceptions import BaseLunrException


class FieldRef:
    JOINER = "/"

    def __init__(self, doc_ref, field_name, string_value=None):
        self.doc_ref = doc_ref
        self.field_name = field_name
        self._string_value = string_value

    def __repr__(self):
        return '<FieldRef field="{}" ref="{}">'.format(self.field_name, self.doc_ref)

    @classmethod
    def from_string(cls, string):
        if cls.JOINER not in string:
            raise BaseLunrException("Malformed field ref string")
        field_ref, doc_ref = string.split(cls.JOINER, 1)
        return cls(doc_ref, field_ref, string)

    def __str__(self):
        if self._string_value is None:
            self._string_value = self.field_name + self.JOINER + str(self.doc_ref)

        return self._string_value