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.

37 lines
922 B

  1. __author__ = 'Chris'
  2. import os
  3. import time
  4. import hashlib
  5. from itertools import dropwhile
  6. import sys
  7. sys.path.append(os.path.dirname(__file__))
  8. def generate_pyfilename():
  9. '''
  10. generates a random filename by hashing the current time stamp
  11. Leading numbers are dropped from the filename for import compatibility
  12. '''
  13. hash = hashlib.md5(str(time.time())).hexdigest()
  14. return ''.join(dropwhile(lambda c: c.isdigit(), hash))
  15. def load(module_source):
  16. tmp_filename = generate_pyfilename()
  17. tmp_filedir = os.path.dirname(__file__)
  18. tmp_filepath = os.path.join(tmp_filedir, tmp_filename)
  19. tmp_py_file = tmp_filepath + '.py'
  20. tmp_pyc_file = tmp_filepath + '.pyc'
  21. try:
  22. with open(tmp_py_file, 'w') as f:
  23. f.write(module_source)
  24. return __import__(tmp_filename)
  25. finally:
  26. os.remove(tmp_py_file)
  27. os.remove(tmp_pyc_file)
  28. if __name__ == '__main__':
  29. pass