Play images and video from Synology PhotoStation server

console.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. """
  3. kodiswift.cli.console
  4. ----------------------
  5. This module contains code to handle CLI interaction.
  6. :copyright: (c) 2012 by Jonathan Beluch
  7. :license: GPLv3, see LICENSE for more details.
  8. """
  9. from __future__ import print_function, absolute_import
  10. def get_max_len(items):
  11. """Returns the max of the lengths for the provided items"""
  12. try:
  13. return max(len(item) for item in items)
  14. except ValueError:
  15. return 0
  16. def display_listitems(items, url):
  17. """Displays a list of items along with the index to enable a user
  18. to select an item.
  19. Args:
  20. items (list[kodiswift.ListItem]):
  21. url (str):
  22. """
  23. if len(items) == 2 and items[0].label == '..' and items[1].played:
  24. display_video(items)
  25. else:
  26. label_width = get_max_len(item.label for item in items)
  27. num_width = len(str(len(items)))
  28. output = []
  29. for i, item in enumerate(items):
  30. output.append('[%s] %s (%s)' % (
  31. str(i).rjust(num_width),
  32. item.label.ljust(label_width),
  33. item.path))
  34. line_width = get_max_len(output)
  35. output.append('-' * line_width)
  36. header = [
  37. '',
  38. '=' * line_width,
  39. 'Current URL: %s' % url,
  40. '-' * line_width,
  41. '%s %s Path' % ('#'.center(num_width + 2),
  42. 'Label'.ljust(label_width)),
  43. '-' * line_width,
  44. ]
  45. print('\n'.join(header + output))
  46. def display_video(items):
  47. """Prints a message for a playing video and displays the parent
  48. listitem.
  49. """
  50. parent_item, played_item = items
  51. title_line = 'Playing Media %s (%s)' % (played_item.label, played_item.path.encode("utf8"))
  52. parent_line = '[0] %s (%s)' % (parent_item.label, parent_item.path)
  53. line_width = get_max_len([title_line, parent_line])
  54. output = [
  55. '-' * line_width,
  56. title_line,
  57. '-' * line_width,
  58. parent_line.encode("utf8") if isinstance(parent_line, unicode) else parent_line ,
  59. ]
  60. print('\n'.join(output))
  61. def get_user_choice(items):
  62. """Returns the selected item from provided items or None if 'q' was
  63. entered for quit.
  64. """
  65. choice = raw_input('Choose an item or "q" to quit: ')
  66. while choice != 'q':
  67. try:
  68. item = items[int(choice)]
  69. print() # Blank line for readability between interactive views
  70. return item
  71. except ValueError:
  72. # Passed something that couldn't be converted with int()
  73. choice = raw_input('You entered a non-integer. Choice must be an'
  74. ' integer or "q": ')
  75. except IndexError:
  76. # Passed an integer that was out of range of the list of urls
  77. choice = raw_input('You entered an invalid integer. Choice must '
  78. 'be from above url list or "q": ')
  79. return None
  80. def continue_or_quit():
  81. """Prints an exit message and returns False if the user wants to
  82. quit.
  83. """
  84. return raw_input('Enter to continue or "q" to quit') != 'q'