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.
 
 
 

127 lines
4.1 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 os
  20. import ast
  21. import hashlib
  22. from datetime import datetime, timedelta
  23. from collections import OrderedDict
  24. from nacl.bindings import (
  25. crypto_secretbox,
  26. crypto_secretbox_open,
  27. )
  28. from . import base_context
  29. def simple_encrypt(key, msg):
  30. nonce = os.urandom(24)
  31. cipher = crypto_secretbox(msg, nonce, key)
  32. return b"%s%s" % (nonce, cipher)
  33. def simple_decrypt(key, cipher):
  34. nonce = cipher[:24]
  35. return crypto_secretbox_open(cipher[24:], nonce, key)
  36. def catsoop_loc_hash():
  37. return hashlib.md5(base_context.cs_url_root.encode("utf-8")).hexdigest()
  38. _literal_eval_funcs = {
  39. "OrderedDict": OrderedDict,
  40. "frozenset": frozenset,
  41. "set": set,
  42. "dict": dict,
  43. "list": list,
  44. "tuple": tuple,
  45. "datetime": datetime,
  46. "timedelta": timedelta,
  47. }
  48. def literal_eval(node_or_string):
  49. """
  50. Helper function to read a log entry and return the associated Python
  51. object. Forked from Python 3.5's ast.literal_eval function:
  52. Safely evaluate an expression node or a string containing a Python
  53. expression. The string or node provided may only consist of the following
  54. Python literal structures: strings, bytes, numbers, tuples, lists, dicts,
  55. sets, booleans, and None.
  56. Modified for CAT-SOOP to include collections.OrderedDict.
  57. """
  58. if isinstance(node_or_string, str):
  59. node_or_string = ast.parse(node_or_string, mode="eval")
  60. if isinstance(node_or_string, ast.Expression):
  61. node_or_string = node_or_string.body
  62. def _convert(node):
  63. if isinstance(node, (ast.Str, ast.Bytes)):
  64. return node.s
  65. elif isinstance(node, ast.Num):
  66. return node.n
  67. elif isinstance(node, ast.Tuple):
  68. return tuple(map(_convert, node.elts))
  69. elif isinstance(node, ast.List):
  70. return list(map(_convert, node.elts))
  71. elif isinstance(node, ast.Set):
  72. return set(map(_convert, node.elts))
  73. elif isinstance(node, ast.Dict):
  74. return dict(
  75. (_convert(k), _convert(v)) for k, v in zip(node.keys, node.values)
  76. )
  77. elif isinstance(node, ast.NameConstant):
  78. return node.value
  79. elif (
  80. isinstance(node, ast.UnaryOp)
  81. and isinstance(node.op, (ast.UAdd, ast.USub))
  82. and isinstance(node.operand, (ast.Num, ast.UnaryOp, ast.BinOp))
  83. ):
  84. operand = _convert(node.operand)
  85. if isinstance(node.op, ast.UAdd):
  86. return +operand
  87. else:
  88. return -operand
  89. elif (
  90. isinstance(node, ast.BinOp)
  91. and isinstance(node.op, (ast.Add, ast.Sub))
  92. and isinstance(node.right, (ast.Num, ast.UnaryOp, ast.BinOp))
  93. and isinstance(node.left, (ast.Num, ast.UnaryOp, ast.BinOp))
  94. ):
  95. left = _convert(node.left)
  96. right = _convert(node.right)
  97. if isinstance(node.op, ast.Add):
  98. return left + right
  99. else:
  100. return left - right
  101. elif (
  102. isinstance(node, ast.Call)
  103. and isinstance(node.func, ast.Name)
  104. and node.func.id in _literal_eval_funcs
  105. ):
  106. return _literal_eval_funcs[node.func.id](*(_convert(i) for i in node.args))
  107. raise ValueError("malformed node or string: " + repr(node))
  108. return _convert(node_or_string)