count.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. count.py
  3. Writes to MongoDB Collection: counts
  4. """
  5. import logging
  6. logger = logging.getLogger(__name__)
  7. from . import abstract as abst
  8. import sefaria.datatype.jagged_array as ja
  9. from sefaria.system.exceptions import BookNameError
  10. class Count(abst.AbstractMongoRecord):
  11. """
  12. """
  13. collection = 'counts'
  14. required_attrs = [
  15. "textComplete",
  16. "percentAvailable",
  17. "availableCounts"
  18. ]
  19. optional_attrs = [
  20. "categories",
  21. "availableTexts",
  22. "title",
  23. "linksCount",
  24. "estimatedCompleteness",
  25. "flags",
  26. "allVersionCounts"
  27. ]
  28. def _set_derived_attributes(self):
  29. from . import text
  30. if getattr(self, "title", None):
  31. try:
  32. indx = text.library.get_index(self.title)
  33. attrs = indx.contents()
  34. #del attrs["_id"]
  35. self.index_attr_keys = attrs.keys()
  36. self.__dict__.update(attrs)
  37. except BookNameError as e:
  38. logger.warning("Count object failed to get Index for {} : {} Normal right after Index name change.".format(self.title, e))
  39. #todo: this needs to be considered. What happens when the data is modified? etc.
  40. if getattr(self, "allVersionCounts", None) is not None:
  41. self._allVersionCountsJA = ja.JaggedIntArray(self.allVersionCounts)
  42. #remove uneccesary and dangerous categories attr from text counts
  43. #This assumes that category nodes have no title element
  44. #todo: review this. Do we need to subclass text and category counts?
  45. def _saveable_attr_keys(self):
  46. attrs = super(Count, self)._saveable_attr_keys()
  47. if getattr(self, "title", None):
  48. attrs.remove("categories")
  49. return attrs
  50. def contents(self, **kwargs):
  51. attrs = super(Count, self).contents()
  52. for key in self.index_attr_keys:
  53. attrs[key] = getattr(self, key, None)
  54. return attrs
  55. #deprecated - use JA directly
  56. def next_address(self, starting_points=None):
  57. starting_points = starting_points or []
  58. if len(starting_points) > 0:
  59. starting_points[-1] += 1
  60. return self._allVersionCountsJA.next_index(starting_points)
  61. #deprecated - use JA directly
  62. def prev_address(self, starting_points=None):
  63. starting_points = starting_points or []
  64. if len(starting_points) > 0:
  65. starting_points[-1] -= 1
  66. return self._allVersionCountsJA.prev_index(starting_points)
  67. #deprecated - use JA directly
  68. def section_length(self, section_numbers):
  69. """
  70. :param section_numbers: The list of 1-based (E.g. Chapter 5 is section_number 5) section numbers
  71. :return: The length of that section
  72. """
  73. return self._allVersionCountsJA.sub_array_length([s - 1 for s in section_numbers])
  74. class CountSet(abst.AbstractMongoSet):
  75. recordClass = Count
  76. def process_index_delete_in_counts(indx, **kwargs):
  77. CountSet({"title":indx.title}).delete()