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.

__init__.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python3
  2. import os, os.path
  3. import sys
  4. import socket
  5. import time
  6. # Used to gracefully aborting stuff.. like a hippo exiting a stage
  7. class AbandonShip(BaseException):
  8. pass
  9. # Default config values.
  10. _config = {
  11. 'host': 'api.anidb.net',
  12. 'port': '9000',
  13. 'session': '~/.kiara.session',
  14. 'database': '~/.kiara.db',
  15. }
  16. def _config_items(file):
  17. for line in map(lambda s: s.strip(), file.readlines()):
  18. if line.startswith('#') or not line:
  19. continue
  20. yield line.split(None, 1)
  21. def load_config_file(file_name):
  22. global _config
  23. try:
  24. with open(file_name, 'r') as fp:
  25. _config.update(_config_items(fp))
  26. except: pass
  27. load_config_file('/etc/kiararc')
  28. load_config_file(os.path.expanduser('~/.kiararc'))
  29. def check_config():
  30. config_ok = True
  31. for key in 'host port user pass database session ' \
  32. 'basepath_movie basepath_series'.split():
  33. if not key in _config:
  34. print('ERROR: Missing config variable:', key, file=sys.stderr)
  35. config_ok = False
  36. return config_ok
  37. def _send(msg):
  38. def inner():
  39. client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  40. client.connect(os.path.expanduser(_config['session']))
  41. client.sendall(bytes(msg, 'UTF-8'))
  42. data = ''
  43. while True:
  44. data += str(client.recv(1024), 'UTF-8')
  45. if data == '---end---':
  46. client.close()
  47. return
  48. if '\n\n' in data:
  49. item, data = data.split('\n\n', 1)
  50. if '\n' in item:
  51. yield item.split('\n')
  52. else:
  53. yield item
  54. try:
  55. for i in inner():
  56. yield i
  57. except socket.error:
  58. if msg == '- kill':
  59. # We were unable to contact the backend, good.
  60. yield ['status', 'no_backend_running']
  61. else:
  62. # Normal procedure
  63. yield ['status', 'backend_start']
  64. if os.fork():
  65. # Wait for it...
  66. time.sleep(2)
  67. # Then try the command again. If it fails again, something we
  68. # cannot fix is wrong
  69. for i in inner():
  70. yield i
  71. return
  72. yield ['error', 'backend_start_failed']
  73. else:
  74. from libkiara import backend
  75. backend.serve(_config)
  76. def ping():
  77. wah = False
  78. for l in _send('- ping'):
  79. print(l)
  80. wah = l == 'pong'
  81. return wah
  82. # Backend actions:
  83. # a Add file
  84. # c Copy file instead of moving
  85. # o Organize file
  86. # u Get new file info from anidb when the cache is old
  87. # w Mark file watched
  88. # x Overwrite existing files
  89. # - Extra commands
  90. def process(file,
  91. update_info=True, watch=False, organize=False, organize_copy=False,
  92. organize_overwrite=False):
  93. q = 'a'
  94. if update_info:
  95. q += 'u'
  96. if watch:
  97. q += 'w'
  98. if organize:
  99. q += 'o'
  100. if organize_copy:
  101. q += 'c'
  102. if organize_overwrite:
  103. q += 'x'
  104. for line in _send(q + ' ' + file):
  105. yield line
  106. def find_duplicates():
  107. for line in _send('- dups'):
  108. yield line
  109. def forget(*fids):
  110. for line in _send('- forget ' + ' '.join(list(map(str, fids)))):
  111. yield line
  112. def kill():
  113. for line in _send('- kill'):
  114. yield line