Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_sentence.py 3.9KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Generic sentence handling tools: hopefully reusable.
  5. """
  6. from typing import Set
  7. class _BaseSentence:
  8. """
  9. A base sentence class for a particular protocol.
  10. Using this base class, specific sentence classes can almost automatically
  11. be created for a particular protocol.
  12. To do this, fill the ALLOWED_ATTRIBUTES class attribute using
  13. the C{getSentenceAttributes} class method of the producer::
  14. class FooSentence(BaseSentence):
  15. \"\"\"
  16. A sentence for integalactic transmodulator sentences.
  17. @ivar transmogrificationConstant: The value used in the
  18. transmogrifier while producing this sentence, corrected for
  19. gravitational fields.
  20. @type transmogrificationConstant: C{Tummy}
  21. \"\"\"
  22. ALLOWED_ATTRIBUTES = FooProtocol.getSentenceAttributes()
  23. @ivar presentAttributes: An iterable containing the names of the
  24. attributes that are present in this sentence.
  25. @type presentAttributes: iterable of C{str}
  26. @cvar ALLOWED_ATTRIBUTES: A set of attributes that are allowed in this
  27. sentence.
  28. @type ALLOWED_ATTRIBUTES: C{set} of C{str}
  29. """
  30. ALLOWED_ATTRIBUTES: Set[str] = set()
  31. def __init__(self, sentenceData):
  32. """
  33. Initializes a sentence with parsed sentence data.
  34. @param sentenceData: The parsed sentence data.
  35. @type sentenceData: C{dict} (C{str} -> C{str} or L{None})
  36. """
  37. self._sentenceData = sentenceData
  38. @property
  39. def presentAttributes(self):
  40. """
  41. An iterable containing the names of the attributes that are present in
  42. this sentence.
  43. @return: The iterable of names of present attributes.
  44. @rtype: iterable of C{str}
  45. """
  46. return iter(self._sentenceData)
  47. def __getattr__(self, name):
  48. """
  49. Gets an attribute of this sentence.
  50. """
  51. if name in self.ALLOWED_ATTRIBUTES:
  52. return self._sentenceData.get(name, None)
  53. else:
  54. className = self.__class__.__name__
  55. msg = f"{className} sentences have no {name} attributes"
  56. raise AttributeError(msg)
  57. def __repr__(self) -> str:
  58. """
  59. Returns a textual representation of this sentence.
  60. @return: A textual representation of this sentence.
  61. @rtype: C{str}
  62. """
  63. items = self._sentenceData.items()
  64. data = [f"{k}: {v}" for k, v in sorted(items) if k != "type"]
  65. dataRepr = ", ".join(data)
  66. typeRepr = self._sentenceData.get("type") or "unknown type"
  67. className = self.__class__.__name__
  68. return f"<{className} ({typeRepr}) {{{dataRepr}}}>"
  69. class _PositioningSentenceProducerMixin:
  70. """
  71. A mixin for certain protocols that produce positioning sentences.
  72. This mixin helps protocols that store the layout of sentences that they
  73. consume in a C{_SENTENCE_CONTENTS} class variable provide all sentence
  74. attributes that can ever occur. It does this by providing a class method,
  75. C{getSentenceAttributes}, which iterates over all sentence types and
  76. collects the possible sentence attributes.
  77. """
  78. @classmethod
  79. def getSentenceAttributes(cls):
  80. """
  81. Returns a set of all attributes that might be found in the sentences
  82. produced by this protocol.
  83. This is basically a set of all the attributes of all the sentences that
  84. this protocol can produce.
  85. @return: The set of all possible sentence attribute names.
  86. @rtype: C{set} of C{str}
  87. """
  88. attributes = {"type"}
  89. for attributeList in cls._SENTENCE_CONTENTS.values():
  90. for attribute in attributeList:
  91. if attribute is None:
  92. continue
  93. attributes.add(attribute)
  94. return attributes