dependencies.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # flake8: noqa: F401
  2. """Imports all optional dependencies for the project.
  3. An attribute "_hypervideo_dl__identifier" may be inserted into the module if it uses an ambiguous namespace"""
  4. try:
  5. import brotlicffi as brotli
  6. except ImportError:
  7. try:
  8. import brotli
  9. except ImportError:
  10. brotli = None
  11. try:
  12. import certifi
  13. except ImportError:
  14. certifi = None
  15. else:
  16. from os.path import exists as _path_exists
  17. # The certificate may not be bundled in executable
  18. if not _path_exists(certifi.where()):
  19. certifi = None
  20. try:
  21. from Cryptodome.Cipher import AES as Cryptodome_AES
  22. except ImportError:
  23. try:
  24. from Crypto.Cipher import AES as Cryptodome_AES
  25. except (ImportError, SyntaxError): # Old Crypto gives SyntaxError in newer Python
  26. Cryptodome_AES = None
  27. else:
  28. try:
  29. # In pycrypto, mode defaults to ECB. See:
  30. # https://www.pycryptodome.org/en/latest/src/vs_pycrypto.html#:~:text=not%20have%20ECB%20as%20default%20mode
  31. Cryptodome_AES.new(b'abcdefghijklmnop')
  32. except TypeError:
  33. pass
  34. else:
  35. Cryptodome_AES._hypervideo_dl__identifier = 'pycrypto'
  36. try:
  37. import mutagen
  38. except ImportError:
  39. mutagen = None
  40. secretstorage = None
  41. try:
  42. import secretstorage
  43. _SECRETSTORAGE_UNAVAILABLE_REASON = None
  44. except ImportError:
  45. _SECRETSTORAGE_UNAVAILABLE_REASON = (
  46. 'as the `secretstorage` module is not installed. '
  47. 'Please install by running `python3 -m pip install secretstorage`')
  48. except Exception as _err:
  49. _SECRETSTORAGE_UNAVAILABLE_REASON = f'as the `secretstorage` module could not be initialized. {_err}'
  50. try:
  51. import sqlite3
  52. except ImportError:
  53. # although sqlite3 is part of the standard library, it is possible to compile python without
  54. # sqlite support. See: https://github.com/hypervideo/hypervideo/issues/544
  55. sqlite3 = None
  56. try:
  57. import websockets
  58. except (ImportError, SyntaxError):
  59. # websockets 3.10 on python 3.6 causes SyntaxError
  60. # See https://github.com/hypervideo/hypervideo/issues/2633
  61. websockets = None
  62. try:
  63. import xattr # xattr or pyxattr
  64. except ImportError:
  65. xattr = None
  66. else:
  67. if hasattr(xattr, 'set'): # pyxattr
  68. xattr._hypervideo_dl__identifier = 'pyxattr'
  69. all_dependencies = {k: v for k, v in globals().items() if not k.startswith('_')}
  70. available_dependencies = {k: v for k, v in all_dependencies.items() if v}
  71. __all__ = [
  72. 'all_dependencies',
  73. 'available_dependencies',
  74. *all_dependencies.keys(),
  75. ]