m2m模型翻译
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.

117 lines
3.2 KiB

6 months ago
  1. #
  2. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  3. # not use this file except in compliance with the License. You may obtain
  4. # a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  10. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  11. # License for the specific language governing permissions and limitations
  12. # under the License.
  13. import re
  14. from .. import DatumInContext, This
  15. SUB = re.compile(r"sub\(/(.*)/,\s+(.*)\)")
  16. SPLIT = re.compile(r"split\((.),\s+(\d+),\s+(\d+|-1)\)")
  17. STR = re.compile(r"str\(\)")
  18. class DefintionInvalid(Exception):
  19. pass
  20. class Sub(This):
  21. """Regex substituor
  22. Concrete syntax is '`sub(/regex/, repl)`'
  23. """
  24. def __init__(self, method=None):
  25. m = SUB.match(method)
  26. if m is None:
  27. raise DefintionInvalid("%s is not valid" % method)
  28. self.expr = m.group(1).strip()
  29. self.repl = m.group(2).strip()
  30. self.regex = re.compile(self.expr)
  31. self.method = method
  32. def find(self, datum):
  33. datum = DatumInContext.wrap(datum)
  34. value = self.regex.sub(self.repl, datum.value)
  35. if value == datum.value:
  36. return []
  37. else:
  38. return [DatumInContext.wrap(value)]
  39. def __eq__(self, other):
  40. return (isinstance(other, Sub) and self.method == other.method)
  41. def __repr__(self):
  42. return '%s(%r)' % (self.__class__.__name__, self.method)
  43. def __str__(self):
  44. return '`sub(/%s/, %s)`' % (self.expr, self.repl)
  45. class Split(This):
  46. """String splitter
  47. Concrete syntax is '`split(char, segment, max_split)`'
  48. """
  49. def __init__(self, method=None):
  50. m = SPLIT.match(method)
  51. if m is None:
  52. raise DefintionInvalid("%s is not valid" % method)
  53. self.char = m.group(1)
  54. self.segment = int(m.group(2))
  55. self.max_split = int(m.group(3))
  56. self.method = method
  57. def find(self, datum):
  58. datum = DatumInContext.wrap(datum)
  59. try:
  60. value = datum.value.split(self.char, self.max_split)[self.segment]
  61. except Exception:
  62. return []
  63. return [DatumInContext.wrap(value)]
  64. def __eq__(self, other):
  65. return (isinstance(other, Split) and self.method == other.method)
  66. def __repr__(self):
  67. return '%s(%r)' % (self.__class__.__name__, self.method)
  68. def __str__(self):
  69. return '`%s`' % self.method
  70. class Str(This):
  71. """String converter
  72. Concrete syntax is '`str()`'
  73. """
  74. def __init__(self, method=None):
  75. m = STR.match(method)
  76. if m is None:
  77. raise DefintionInvalid("%s is not valid" % method)
  78. self.method = method
  79. def find(self, datum):
  80. datum = DatumInContext.wrap(datum)
  81. value = str(datum.value)
  82. return [DatumInContext.wrap(value)]
  83. def __eq__(self, other):
  84. return (isinstance(other, Str) and self.method == other.method)
  85. def __repr__(self):
  86. return '%s(%r)' % (self.__class__.__name__, self.method)
  87. def __str__(self):
  88. return '`str()`'