PageRenderTime 87ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/dvb.py

https://github.com/Josep/dvb_py
Python | 181 lines | 154 code | 26 blank | 1 comment | 24 complexity | e9a5897eb9bf4e688b15ead810214ab8 MD5 | raw file
  1. import curses
  2. import re
  3. import os
  4. deb=open('debug.log','w')
  5. ch = '.mplayer/channels.conf' # channels for mplayer below ~
  6. command = 'mplayer -ao pulse -vf pp=fd dvb://'
  7. class Nav(object):
  8. li = []
  9. start = stop = high = 0
  10. scrn = None
  11. def __init__(self, li, scrn):
  12. self.li = li
  13. self.scrn = scrn
  14. return
  15. def home(self):
  16. self.high = self.start = 0
  17. self.stop = min(curses.LINES, len(self.li))
  18. self.present()
  19. def end(self):
  20. t = len(self.li)
  21. self.high = t-1
  22. self.start = t - curses.LINES
  23. self.stop = t
  24. self.present()
  25. def arrow(self, c1, c2, d):
  26. if c1:
  27. self.high += d
  28. self.present()
  29. else:
  30. if c2:
  31. self.high += d
  32. self.stop += d
  33. self.start += d
  34. def uparrow(self, pres=True):
  35. c1 = self.high > self.start
  36. c2 = self.high > 0
  37. self.arrow(c1, c2, -1)
  38. if pres: self.present()
  39. def downarrow(self, pres=True):
  40. c1 = self.high < (self.stop-1)
  41. c2 = self.high < (len(self.li)-1)
  42. self.arrow(c1, c2, +1)
  43. if pres: self.present()
  44. def pgdn(self):
  45. for k in range(curses.LINES):
  46. self.downarrow(False)
  47. self.present()
  48. def pgup(self):
  49. for k in range(curses.LINES):
  50. self.uparrow(False)
  51. self.present()
  52. def supr(self):
  53. ex = '(\d+)\t.*'
  54. co = re.compile(ex)
  55. self.scrn.getch()
  56. deb.write(co.search(self.li[self.high]).group(1))
  57. deb.write('\n')
  58. deb.flush()
  59. def subebaja(self, c, start, stop, high):
  60. d = {65:'uparrow', 66:'downarrow', 72:'home', 53:'pgup', 54:'pgdn', 70:'end', 51:'supr'}
  61. self.start = start
  62. self.stop = stop
  63. self.high = high
  64. c = self.scrn.getch()
  65. if c == 91:
  66. c = self.scrn.getch()
  67. if c in d.keys():
  68. getattr(self, d[c])()
  69. else:
  70. deb.write('27+91+'+c+'\n')
  71. deb.flush()
  72. else:
  73. deb.write('27+'+c+'\n')
  74. deb.flush()
  75. return self.start, self.stop, self.high
  76. def present(self):
  77. self.scrn.clear()
  78. k = 0
  79. for ln in self.li[self.start:self.stop]:
  80. self.scrn.addstr(k,0,ln)
  81. k += 1
  82. self.scrn.addstr(self.high-self.start,0,self.li[self.high],curses.A_STANDOUT)
  83. self.scrn.refresh()
  84. class Dvb(object):
  85. start = stop = high = 0
  86. scrn = None
  87. nav = None
  88. li = []
  89. co = None
  90. def initCurses(self):
  91. self.scrn = curses.initscr()
  92. curses.noecho()
  93. curses.cbreak()
  94. def getList(self):
  95. return [str(k+1)+'\t'+self.co.search(i).group() for k,i in enumerate(self.li)]
  96. def ic(self, first, second):
  97. return cmp(first[0].lower(),second[0].lower())
  98. def sort(self):
  99. k = [(self.co.search(i).group(), str(j+1)) for j,i in enumerate(self.li)]
  100. k = sorted(k, self.ic)
  101. k = [i[1]+'\t'+i[0] for i in k]
  102. self.nav = Nav(k, self.scrn)
  103. self.nav.home()
  104. self.high,self.start,self.stop = self.nav.high, self.nav.start, self.nav.stop
  105. def unsort(self):
  106. self.nav = Nav(self.getList(), self.scrn)
  107. self.nav.home()
  108. self.high,self.start,self.stop = self.nav.high, self.nav.start, self.nav.stop
  109. def search(self):
  110. c = self.scrn.getch()
  111. deb.write('searching for :'+str(c)+'\n')
  112. deb.flush()
  113. def processInput(self):
  114. while True:
  115. c = self.scrn.getch()
  116. if c == 113: #'q' quit
  117. break
  118. elif c == 27: #3 byte sequence for navigation, arrows, PgDn, etc.
  119. self.start, self.stop, self.high = self.nav.subebaja(c, self.start, self.stop, self.high)
  120. elif c == 10: #Enter, play selected
  121. ex = '\d+\t(.*)'
  122. co = re.compile(ex)
  123. st = command+'"'+co.search(self.nav.li[self.nav.high]).group(1)+'" >> debug.log 2>&1'
  124. deb.write(st+'\n')
  125. deb.flush()
  126. self.scrn.clear()
  127. self.scrn.refresh()
  128. #curses.endwin()
  129. os.system(st)
  130. self.unsort()
  131. elif c == 115: #'s' sort
  132. self.sort()
  133. elif c == 117: #'u' unsort
  134. self.unsort()
  135. elif c == 47: #'/' search
  136. self.search()
  137. else:
  138. deb.write('Command: '+str(c)+'\n')
  139. deb.flush()
  140. def initstep1(self):
  141. ex = '[^:]+'
  142. self.co = re.compile(ex)
  143. f = open(os.environ['HOME']+"/" + ch, "r")
  144. self.li = f.readlines()
  145. f.close()
  146. def __init__(self):
  147. self.initCurses()
  148. self.initstep1()
  149. self.unsort()
  150. self.processInput()
  151. curses.nocbreak()
  152. curses.echo()
  153. curses.endwin()
  154. if __name__ =='__main__':
  155. Dvb()