Kodi plugin to to play various online streams (mostly Latvian)

console.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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,
  52. played_item.path)
  53. parent_line = '[0] %s (%s)' % (parent_item.label,
  54. parent_item.path)
  55. line_width = get_max_len([title_line, parent_line])
  56. output = [
  57. '-' * line_width,
  58. title_line,
  59. '-' * line_width,
  60. parent_line.encode("utf8") if isinstance(parent_line, unicode) else parent_line ,
  61. ]
  62. print('\n'.join(output))
  63. def get_user_choice(items):
  64. """Returns the selected item from provided items or None if 'q' was
  65. entered for quit.
  66. """
  67. choice = raw_input('Choose an item or "q" to quit: ')
  68. while choice != 'q':
  69. try:
  70. item = items[int(choice)]
  71. print() # Blank line for readability between interactive views
  72. return item
  73. except ValueError:
  74. # Passed something that couldn't be converted with int()
  75. choice = raw_input('You entered a non-integer. Choice must be an'
  76. ' integer or "q": ')
  77. except IndexError:
  78. # Passed an integer that was out of range of the list of urls
  79. choice = raw_input('You entered an invalid integer. Choice must '
  80. 'be from above url list or "q": ')
  81. return None
  82. def continue_or_quit():
  83. """Prints an exit message and returns False if the user wants to
  84. quit.
  85. """
  86. return raw_input('Enter to continue or "q" to quit') != 'q'