2
0

textutil.py 583 B

1234567891011121314151617181920212223242526
  1. # -*- coding: UTF-8 -*-
  2. import hashlib
  3. import base64
  4. def ensure_unicode(s):
  5. if type(s) == str:
  6. return s
  7. elif type(s) == bytes:
  8. return s.decode('utf-8')
  9. raise TypeError(f"type of string is {type(s)}")
  10. def md5(s):
  11. m = hashlib.md5()
  12. m.update(s)
  13. return m.hexdigest()
  14. def get_file_b64(fname):
  15. data = open(fname, 'rb').read()
  16. return base64.b64encode(data).decode('ascii')
  17. def safe_filename(fname):
  18. filename = ensure_unicode(fname)
  19. return "".join(
  20. [c for c in filename if c.isalpha() or c.isdigit() or c==' ']).rstrip()