CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
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.
 
 
 

110 lines
3.8 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Extra utilities that don't have a home anywhere else
  18. """
  19. import ast
  20. import hashlib
  21. from collections import OrderedDict
  22. from datetime import datetime, timedelta
  23. from . import base_context
  24. def catsoop_loc_hash():
  25. return hashlib.md5(base_context.cs_url_root.encode("utf-8")).hexdigest()
  26. _literal_eval_funcs = {
  27. "OrderedDict": OrderedDict,
  28. "frozenset": frozenset,
  29. "set": set,
  30. "dict": dict,
  31. "list": list,
  32. "tuple": tuple,
  33. "datetime": datetime,
  34. "timedelta": timedelta,
  35. }
  36. def literal_eval(node_or_string):
  37. """
  38. Helper function to read a log entry and return the associated Python
  39. object. Forked from Python 3.5's ast.literal_eval function:
  40. Safely evaluate an expression node or a string containing a Python
  41. expression. The string or node provided may only consist of the following
  42. Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
  43. sets, booleans, and None.
  44. Modified for CAT-SOOP to include collections.OrderedDict.
  45. """
  46. if isinstance(node_or_string, str):
  47. node_or_string = ast.parse(node_or_string, mode="eval")
  48. if isinstance(node_or_string, ast.Expression):
  49. node_or_string = node_or_string.body
  50. def _convert(node):
  51. if isinstance(node, (ast.Str, ast.Bytes)):
  52. return node.s
  53. elif isinstance(node, ast.Num):
  54. return node.n
  55. elif isinstance(node, ast.Tuple):
  56. return tuple(map(_convert, node.elts))
  57. elif isinstance(node, ast.List):
  58. return list(map(_convert, node.elts))
  59. elif isinstance(node, ast.Set):
  60. return set(map(_convert, node.elts))
  61. elif isinstance(node, ast.Dict):
  62. return dict(
  63. (_convert(k), _convert(v)) for k, v in zip(node.keys, node.values)
  64. )
  65. elif isinstance(node, ast.NameConstant):
  66. return node.value
  67. elif (
  68. isinstance(node, ast.UnaryOp)
  69. and isinstance(node.op, (ast.UAdd, ast.USub))
  70. and isinstance(node.operand, (ast.Num, ast.UnaryOp, ast.BinOp))
  71. ):
  72. operand = _convert(node.operand)
  73. if isinstance(node.op, ast.UAdd):
  74. return +operand
  75. else:
  76. return -operand
  77. elif (
  78. isinstance(node, ast.BinOp)
  79. and isinstance(node.op, (ast.Add, ast.Sub))
  80. and isinstance(node.right, (ast.Num, ast.UnaryOp, ast.BinOp))
  81. and isinstance(node.left, (ast.Num, ast.UnaryOp, ast.BinOp))
  82. ):
  83. left = _convert(node.left)
  84. right = _convert(node.right)
  85. if isinstance(node.op, ast.Add):
  86. return left + right
  87. else:
  88. return left - right
  89. elif (
  90. isinstance(node, ast.Call)
  91. and isinstance(node.func, ast.Name)
  92. and node.func.id in _literal_eval_funcs
  93. ):
  94. return _literal_eval_funcs[node.func.id](*(_convert(i) for i in node.args))
  95. raise ValueError("malformed node or string: " + repr(node))
  96. return _convert(node_or_string)