field_ref.py 894 B

1234567891011121314151617181920212223242526272829303132
  1. from __future__ import unicode_literals
  2. import six
  3. from lunr.exceptions import BaseLunrException
  4. @six.python_2_unicode_compatible
  5. class FieldRef:
  6. JOINER = "/"
  7. def __init__(self, doc_ref, field_name, string_value=None):
  8. self.doc_ref = doc_ref
  9. self.field_name = field_name
  10. self._string_value = string_value
  11. def __repr__(self):
  12. return '<FieldRef field="{}" ref="{}">'.format(self.field_name, self.doc_ref)
  13. @classmethod
  14. def from_string(cls, string):
  15. if cls.JOINER not in string:
  16. raise BaseLunrException("Malformed field ref string")
  17. field_ref, doc_ref = string.split(cls.JOINER, 1)
  18. return cls(doc_ref, field_ref, string)
  19. def __str__(self):
  20. if self._string_value is None:
  21. self._string_value = self.field_name + self.JOINER + str(self.doc_ref)
  22. return self._string_value