Contents.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. class Contents(object):
  4. text:list
  5. addStats:dict
  6. actions:list
  7. tags:list
  8. def __init__(self, item:list = None):
  9. self.tags = list()
  10. self.addStats = dict()
  11. self.actions = list()
  12. self.text = list()
  13. print(item)
  14. if item!=None:
  15. if "tags" in item:
  16. self.tags = self.__argumentToTagList(item["tags"])
  17. if "mood_add" in item:
  18. for x,y in item["mood_add"].items():
  19. self.addStats[x]=y
  20. if "actions" in item:
  21. for x in item["actions"]:
  22. self.actions.append(x)
  23. if "text" in item:
  24. for x in item["text"]:
  25. self.text.append(x)
  26. def __del__(self):
  27. del self.text
  28. del self.addStats
  29. del self.actions
  30. del self.Tags
  31. def hasTags(self, tagList):
  32. return self._hasTags(self.__argumentToTagList(tagList))
  33. # TagList Info:
  34. # inside the .json File, when a Tag starts with "!", this ist a must have Tag
  35. # inside the TagList, when a tag has a "+", this tag must be in the .json entry
  36. # inside the TagList, when a tag has a "-", this tag must not be in the .json entry
  37. def _hasTags(self, tagList):
  38. # check High Required Tags
  39. for e in self.tags:
  40. if e[0:1]=="!":
  41. found=False
  42. for t in tagList:
  43. if t == e[1:] or t=="+"+e[1:]:
  44. found=True
  45. break
  46. if found==False:
  47. print("JSON-Tag not found: "+e)
  48. return 0
  49. # check required Tags
  50. for t in tagList:
  51. tag = t
  52. found=False
  53. if t[0:1]=="+":
  54. for e in self.tags:
  55. if t[1:] == e or ("!"+t[1:])==e:
  56. found = True
  57. break
  58. if found==False:
  59. print("Script-Tag not found: "+t+" ... " + t[1:] + " | " + "!"+t[1:])
  60. return 0
  61. elif t[0:1]=="-":
  62. found=False
  63. for e in self.tags:
  64. if t[1:] == e:
  65. found = True
  66. break
  67. if found==False:
  68. return 0
  69. if found==True:
  70. break
  71. # get num matching tags
  72. numFound = 0
  73. for t in tagList:
  74. tag1 = t
  75. if tag1[0:1]=="+" or tag1[0:1]=="-" or tag1[0:1]=="*" or tag1[0:1]=="!" or tag1[0:1]=="~":
  76. tag1=tag1[1:]
  77. for e in self.tags:
  78. tag2 = e
  79. if tag2[0:1]=="+" or tag2[0:1]=="-" or tag2[0:1]=="*" or tag2[0:1]=="!" or tag2[0:1]=="~":
  80. tag2=tag2[1:]
  81. if tag1==tag2:
  82. numFound+=1
  83. break
  84. return numFound
  85. def __argumentToTagList(self, arg):
  86. res = list()
  87. if isinstance(arg,list):
  88. for i in arg:
  89. tmp = self.__argumentToTagList(i)
  90. for j in tmp:
  91. res.append(j)
  92. else:
  93. tmp = arg.split(",")
  94. for i in tmp:
  95. res.append(i.strip().lower())
  96. return res