From a89655a2e8ef63e0717529bb5ed1debc0aba0f9f Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Sat, 20 Nov 2010 14:34:36 +0000 Subject: pep8 cleanup --- diff --git a/TurtleArt/RtfParser.py b/TurtleArt/RtfParser.py index 4b1f1dc..9a141a4 100644 --- a/TurtleArt/RtfParser.py +++ b/TurtleArt/RtfParser.py @@ -14,6 +14,7 @@ import sys + class RtfException(Exception): pass @@ -23,6 +24,7 @@ argument = 3 backslash = 4 escapedChar = 5 + class RtfParser(object): def __init__(self, unicode=False): @@ -49,15 +51,15 @@ class RtfParser(object): def putChar(self): pass - def doControl(self,token,arg): + def doControl(self, token, arg): pass - def feed(self,txt): + def feed(self, txt): for c in txt: self.feedChar(c) - def feedChar(self,char): - if self.state == plaintext: # this is just normal user content + def feedChar(self, char): + if self.state == plaintext: # this is just normal user content if char == '\\': self.state = backslash elif char == '{': @@ -66,7 +68,7 @@ class RtfParser(object): self.popState() else: self.putChar(char) - elif self.state == backslash: # a command or escape + elif self.state == backslash: # a command or escape if char == '\\' or char == '{' or char == '}': self.putChar(char) self.state = plaintext @@ -77,28 +79,28 @@ class RtfParser(object): elif char == "'": self.state = escapedChar self.escapedChar = '' - elif char in ['\\', '{','}']: + elif char in ['\\', '{', '}']: self.putChar(char) self.state = plaintext - elif char == "~": # non breaking space + elif char == "~": # non breaking space self.putChar(self.getNonBreakingSpace()) self.state = plaintext else: - raise RtfException,'unexpected %s after \\' % char + raise RtfException(('unexpected %s after \\' % char)) elif self.state == escapedChar: self.escapedChar = self.escapedChar + char if len(self.escapedChar) == 2: - char = self.getChar(int(self.escapedChar,16)) + char = self.getChar(int(self.escapedChar, 16)) self.putChar(char) self.state = plaintext - elif self.state == control: # collecting the command token + elif self.state == control: # collecting the command token if char.isalpha(): self.token = self.token + char - elif char.isdigit() or char== '-': + elif char.isdigit() or char == '-': self.state = argument self.arg = char else: - self.doControl(self.token,self.arg) + self.doControl(self.token, self.arg) self.state = plaintext if char == '\\': self.state = backslash @@ -109,12 +111,12 @@ class RtfParser(object): else: if not char.isspace(): self.putChar(char) - elif self.state == argument: # collecting the optional command argument + elif self.state == argument: # collecting the optional argument if char.isdigit(): self.arg = self.arg + char else: self.state = plaintext - self.doControl(self.token,self.arg) + self.doControl(self.token, self.arg) if char == '\\': self.state = backslash elif char == '{': @@ -127,30 +129,22 @@ class RtfParser(object): class RtfTextOnly(RtfParser): + def __init__(self): RtfParser.__init__(self) self.level = 0 - + def pushState(self): self.level = self.level + 1 def popState(self): self.level = self.level - 1 - def putChar(self,ch): + def putChar(self, ch): if self.par: self.output += ch - def doControl(self,token,arg): + def doControl(self, token, arg): if token[0:3] == 'par': self.par = True pass - -if __name__ == '__main__': - text_only = RtfTextOnly() - if len(sys.argv) != 2: - print 'Usage : %s file.rtf' % sys.argv[0] - else: - for line in open(sys.argv[1], 'r'): - text_only.feed(line) - print text_only.output diff --git a/TurtleArt/taexporthtml.py b/TurtleArt/taexporthtml.py index 4c11437..09042f8 100644 --- a/TurtleArt/taexporthtml.py +++ b/TurtleArt/taexporthtml.py @@ -33,49 +33,49 @@ def save_html(self, tw, embed_flag=True): # A dictionary to define the HTML wrappers around template elements self.html_glue = { - 'doctype': "\n", - 'html': ("\n", "\n"), - 'html_svg': ("\n", - "\n"), - 'head': ("\n\n", "\n"), - 'meta': "\n", - 'title': ("", "\n"), - 'style': ("\n"), - 'body': ("\n", "\n\n"), - 'div': ("
\n", "
\n"), - 'slide': ("\n\n"), - 'h1': ("

", "

\n"), - 'table': ("\n", "
\n"), - 'tr': ("\n", "\n"), - 'td': ("\n", - "\n\n"), - 'img': ("\"Image\"\n"), - 'img2': ("\"Image\"\n"), - 'img3': ("\"Image\"\n"), - 'ul': ("\n", "
\n"), - 'li': ("", "\n")} + 'doctype': '\n', + 'html': ('\n", "\n'), + 'html_svg': ('\n', + '\n'), + 'head': ('\n\n', '\n'), + 'meta': '\n', + 'title': ('', '\n'), + 'style': ('\n'), + 'body': ('\n', '\n\n'), + 'div': ('
\n', '
\n'), + 'slide': ('\n\n'), + 'h1': ('

', '

\n'), + 'table': ('\n', + '\n\n'), + 'img': ('Image\n'), + 'img2': ('Image\n'), + 'img3': ('Image\n'), + 'ul': ('
\n', '
\n'), + 'li': ('', '\n')} - comment = "\n" +-->\n' if self.embed_images == True: - self.html_glue['img'] = (" + \
-                                 \n") - self.html_glue['img2'] = ("\"Image\"\n") + self.html_glue['img'] = (' + \
+                                 \n') + self.html_glue['img2'] = ('Image\n') """ If there are saved_pictures, put them into a .html; otherwise, save a screendump and the turtle project code. """ - code = "" + code = '' if len(tw.saved_pictures) > 0: for i, p in enumerate(tw.saved_pictures): code += self.html_glue['slide'][0] + str(i) @@ -83,7 +83,7 @@ def save_html(self, tw, embed_flag=True): self.html_glue['div'][0] + \ self.html_glue['h1'][0] if self.embed_images == True: - f = open(p, "r") + f = open(p, 'r') imgdata = f.read() f.close() if p.endswith(('.svg')): @@ -96,7 +96,7 @@ def save_html(self, tw, embed_flag=True): tmp += self.html_glue['img2'][1] else: if p.endswith(('.svg')): - f = open(p, "r") + f = open(p, 'r') imgdata = f.read() f.close() tmp = imgdata @@ -109,7 +109,8 @@ def save_html(self, tw, embed_flag=True): self.html_glue['div'][1] else: if self.embed_images == True: - imgdata = image_to_base64(save_picture(self.tw.canvas), tw.activity) + imgdata = image_to_base64(save_picture(self.tw.canvas), + tw.activity) else: imgdata = os.path.join(self.tw.load_save_folder, 'image') self.tw.save_as_image(imgdata) @@ -120,9 +121,9 @@ def save_html(self, tw, embed_flag=True): code += self.html_glue['div'][1] if tw.running_sugar: - title = _("Turtle Art") + " " + tw.activity.metadata['title'] + title = _('Turtle Art') + ' ' + tw.activity.metadata['title'] else: - title = _("Turtle Art") + title = _('Turtle Art') header = self.html_glue['doctype'] + \ self.html_glue['html'][0] diff --git a/TurtleArt/taexportlogo.py b/TurtleArt/taexportlogo.py index bfa7ca8..bd8a1e6 100644 --- a/TurtleArt/taexportlogo.py +++ b/TurtleArt/taexportlogo.py @@ -18,8 +18,8 @@ #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. -IGNORE = ["hideblocks", "showblocks", "fullscreen", "polar", "cartesian", - "sandwichbottom", "id"] +IGNORE = ['hideblocks', 'showblocks', 'fullscreen', 'polar', 'cartesian', + 'sandwichbottom', 'id'] import math try: @@ -27,57 +27,56 @@ try: except: pass - def save_logo(tw): """ Set up the Turtle Art color palette and color processing. """ - color_processing = "\ + color_processing = '\ to tasetpalette :i :r :g :b :myshade \r\ -make \"s ((:myshade - 50) / 50) \r\ +make "s ((:myshade - 50) / 50) \r\ ifelse lessp :s 0 [ \r\ -make \"s (1 + (:s *0.8)) \r\ -make \"r (:r * :s) \r\ -make \"g (:g * :s) \r\ -make \"b (:b * :s) \r\ +make "s (1 + (:s *0.8)) \r\ +make "r (:r * :s) \r\ +make "g (:g * :s) \r\ +make "b (:b * :s) \r\ ] [ \ -make \"s (:s * 0.9) \r\ -make \"r (:r + ((99-:r) * :s)) \r\ -make \"g (:g + ((99-:g) * :s)) \r\ -make \"b (:b + ((99-:b) * :s)) \r\ +make "s (:s * 0.9) \r\ +make "r (:r + ((99-:r) * :s)) \r\ +make "g (:g + ((99-:g) * :s)) \r\ +make "b (:b + ((99-:b) * :s)) \r\ ] \ setpalette :i (list :r :g :b) \r\ end \r\ \ to rgb :myi :mycolors :myshade \r\ -make \"myr first :mycolors \r\ -make \"mycolors butfirst :mycolors \r\ -make \"myg first :mycolors \r\ -make \"mycolors butfirst :mycolors \r\ -make \"myb first :mycolors \r\ -make \"mycolors butfirst :mycolors \r\ +make "myr first :mycolors \r\ +make "mycolors butfirst :mycolors \r\ +make "myg first :mycolors \r\ +make "mycolors butfirst :mycolors \r\ +make "myb first :mycolors \r\ +make "mycolors butfirst :mycolors \r\ tasetpalette :myi :myr :myg :myb :myshade \r\ output :mycolors \r\ end \r\ \ to processcolor :mycolors :myshade \r\ if emptyp :mycolors [stop] \r\ -make \"i :i + 1 \r\ +make "i :i + 1 \r\ processcolor (rgb :i :mycolors :myshade) :myshade \r\ end \r\ \ to tasetshade :shade \r\ -make \"myshade modulo :shade 200 \r\ -if greaterp :myshade 99 [make \"myshade (199-:myshade)] \r\ -make \"i 7 \r\ -make \"mycolors :colors \r\ +make "myshade modulo :shade 200 \r\ +if greaterp :myshade 99 [make "myshade (199-:myshade)] \r\ +make "i 7 \r\ +make "mycolors :colors \r\ processcolor :mycolors :myshade \r\ end \r\ \ to tasetpencolor :c \r\ -make \"color (modulo (round :c) 100) \r\ +make "color (modulo (round :c) 100) \r\ setpencolor :color + 8 \r\ end \r\ \ -make \"colors [ \ +make "colors [ \ 99 0 0 99 5 0 99 10 0 99 15 0 99 20 0 \ 99 25 0 99 30 0 99 35 0 99 40 0 99 45 0 \ 99 50 0 99 55 0 99 60 0 99 65 0 99 70 0 \ @@ -98,11 +97,11 @@ make \"colors [ \ 75 0 99 80 0 99 85 0 99 90 0 99 95 0 99 \ 99 0 99 99 0 90 99 0 80 99 0 70 99 0 60 \ 99 0 50 99 0 40 99 0 30 99 0 20 99 0 10] \r\ -make \"shade 50 \r\ -tasetshade :shade \r" +make "shade 50 \r\ +tasetshade :shade \r' bs = tw.just_blocks() - code = "" + code = '' stack_count = 0 show = 0 @@ -125,7 +124,7 @@ tasetshade :shade \r" Walk through the code, substituting UCB Logo for Turtle Art primitives. """ for b in bs: - this_stack = "" + this_stack = '' data = walk_stack(tw.lc, b, tw.block_list.list) # We need to catch several special cases: stacks, random, etc. stack = False @@ -133,16 +132,16 @@ tasetshade :shade \r" namedbox = False refstack = False refbox = False - myvar = "" + myvar = '' for d in data: if type(d) == type((1, 2)): (d, b) = d if type(d) is float: if namedbox: myvar += str(d) - myvar += " " + myvar += ' ' elif write: - this_stack += "labelsize " + this_stack += 'labelsize ' this_stack += str(d) write = False else: @@ -162,112 +161,112 @@ tasetshade :shade \r" else: # Translate some Turtle Art primitives into UCB Logo if namedstack: - this_stack += "to " + this_stack += 'to ' this_stack += d[2:].replace(' ', '_') - this_stack += "\r" + this_stack += '\r' stack = True namedstack = False elif namedbox: - if d[0:2] == "#s": - this_stack += "make \"" + if d[0:2] == '#s': + this_stack += 'make "' this_stack += d[2:].replace(' ', '_') - this_stack += " " + this_stack += ' ' this_stack += myvar namedbox = False - myvar = "" + myvar = '' else: myvar += d elif refstack: this_stack += d[2:].replace(' ', '_') - this_stack += " " + this_stack += ' ' refstack = False elif refbox: - this_stack += ":" + this_stack += ':' this_stack += d[2:].replace(' ', '_') refbox = False - elif d == "stack": + elif d == 'stack': refstack = True - elif d == "box": + elif d == 'box': refbox = True - elif d == "storeinbox": + elif d == 'storeinbox': namedbox = True - elif d == "storeinbox1": - this_stack += "make \"box1" - elif d == "box1": - this_stack += ":box1" - elif d == "storeinbox2": - this_stack += "make \"box2" - elif d == "box2": - this_stack += ":box2" - elif d == "shade": - this_stack += ":shade" - elif d == "setshade": + elif d == 'storeinbox1': + this_stack += 'make "box1' + elif d == 'box1': + this_stack += ':box1' + elif d == 'storeinbox2': + this_stack += 'make "box2' + elif d == 'box2': + this_stack += ':box2' + elif d == 'shade': + this_stack += ':shade' + elif d == 'setshade': setcolor = True - this_stack += "tasetshade" - elif d == "color": - this_stack += "pencolor" - elif d == "nop": - this_stack += " " - elif d == "start": - this_stack += "to start\r" + this_stack += 'tasetshade' + elif d == 'color': + this_stack += 'pencolor' + elif d == 'nop': + this_stack += ' ' + elif d == 'start': + this_stack += 'to start\r' stack = True - elif d == "nop1": - this_stack += "to stack1\r" + elif d == 'nop1': + this_stack += 'to stack1\r' stack = True - elif d == "nop2": - this_stack += "to stack2\r" + elif d == 'nop2': + this_stack += 'to stack2\r' stack = True - elif d == "nop3": + elif d == 'nop3': namedstack = True - elif d == "stopstack": - this_stack += "stop" - elif d == "clean": - this_stack += "clearscreen" - elif d == "setxy": + elif d == 'stopstack': + this_stack += 'stop' + elif d == 'clean': + this_stack += 'clearscreen' + elif d == 'setxy': setxy = True - this_stack += "tasetxypenup" - elif d == "setxy2": + this_stack += 'tasetxypenup' + elif d == 'setxy2': setxy2 = True - this_stack += "tasetxy" - elif d == "color": - this_stack += ":color" - elif d == "plus": - this_stack += "sum" - elif d == "setcolor": + this_stack += 'tasetxy' + elif d == 'color': + this_stack += ':color' + elif d == 'plus': + this_stack += 'sum' + elif d == 'setcolor': setcolor = True - this_stack += "tasetpencolor" - elif d == "fillscreen": + this_stack += 'tasetpencolor' + elif d == 'fillscreen': fillscreen = True setcolor = True - this_stack += "tasetbackground" - elif d == "random": + this_stack += 'tasetbackground' + elif d == 'random': random = True - this_stack += "tarandom" - elif d == "pensize": + this_stack += 'tarandom' + elif d == 'pensize': pensize = True - this_stack += "tapensize" - elif d == "setpensize": + this_stack += 'tapensize' + elif d == 'setpensize': setpensize = True - this_stack += "tasetpensize" - elif d == "arc": + this_stack += 'tasetpensize' + elif d == 'arc': arc = True - this_stack += "taarc" - elif d == "pop": + this_stack += 'taarc' + elif d == 'pop': heap = True - this_stack += "tapop" - elif d == "push": + this_stack += 'tapop' + elif d == 'push': heap = True - this_stack += "tapush" - elif d == "heap": + this_stack += 'tapush' + elif d == 'heap': heap = True - this_stack += "taprintheap" - elif d == "emptyheap": + this_stack += 'taprintheap' + elif d == 'emptyheap': heap = True - this_stack += "taclearheap" - elif d == "kbinput": - this_stack += "make \"keyboard readchar" - elif d == "keyboard": - this_stack += ":keyboard" + this_stack += 'taclearheap' + elif d == 'kbinput': + this_stack += 'make "keyboard readchar' + elif d == 'keyboard': + this_stack += ':keyboard' elif d == 'insertimage': image = True elif image: @@ -276,84 +275,84 @@ tasetshade :shade \r" elif image == 2: # Skip this arg image = False - elif d[0:2] == "#s": + elif d[0:2] == '#s': # output single characters as a string if len(d[2:]): - this_stack += "\"" + this_stack += '"' this_stack += d[2:] # make a sentence out of everything else else: - this_stack += "sentence " - this_stack += d[2:].replace("\s", " \"") - this_stack += "\r" - elif d == "write": - this_stack += "label" + this_stack += 'sentence ' + this_stack += d[2:].replace('\s', ' "') + this_stack += '\r' + elif d == 'write': + this_stack += 'label' write = True elif d == 'show' or d == 'showaligned': - this_stack += "label" + this_stack += 'label' show = 1 - elif d == "minus2": - this_stack += "taminus" + elif d == 'minus2': + this_stack += 'taminus' minus = True - elif d == "division": - this_stack += "quotient" - elif d == "lpos": + elif d == 'division': + this_stack += 'quotient' + elif d == 'lpos': this_stack += str(-tw.canvas.width / (tw.coord_scale * 2)) - elif d == "rpos": + elif d == 'rpos': this_stack += str(tw.canvas.width / (tw.coord_scale * 2)) - elif d == "bpos": + elif d == 'bpos': this_stack += str(-tw.canvas.height / (tw.coord_scale * 2)) - elif d == "tpos": + elif d == 'tpos': this_stack += str(tw.canvas.height / (tw.coord_scale * 2)) elif d in IGNORE: this_stack += ' ' - elif show == 1 and d[0:2] == "#s": + elif show == 1 and d[0:2] == '#s': this_stack += d[2:] # We don't handle depreciated 'template' blocks else: this_stack += d - this_stack += " " + this_stack += ' ' if stack: stack = False - # if it is not a stack, we need to add a "to ta#" label + # if it is not a stack, we need to add a 'to ta#' label elif len(data) > 0: - this_stack = "to ta" + str(stack_count) + "\r" + this_stack + this_stack = 'to ta' + str(stack_count) + '\r' + this_stack stack_count += 1 if len(data) > 0: code += this_stack - code += "\rend\r" + code += '\rend\r' # We need to define some additional procedures. - if minus: # Logo minus only takes one argument. - code = "to taminus :y :x\routput sum :x minus :y\rend\r" + code - if random: # to avoid negative numbers - code = "to tarandom :min :max\r" + \ - "output (random (:max - :min)) + :min\rend\r" + code - if fillscreen: # Set shade than background color - code = "to tasetbackground :color :shade\r" + \ - "tasetshade :shade\rsetbackground :color\rend\r" + code - if setcolor: # Load the Turtle Art color palette. + if minus: # Logo minus only takes one argument. + code = 'to taminus :y :x\routput sum :x minus :y\rend\r' + code + if random: # to avoid negative numbers + code = 'to tarandom :min :max\r' + \ + 'output (random (:max - :min)) + :min\rend\r' + code + if fillscreen: # Set shade than background color + code = 'to tasetbackground :color :shade\r' + \ + 'tasetshade :shade\rsetbackground :color\rend\r' + code + if setcolor: # Load the Turtle Art color palette. code = color_processing + code - if setpensize: # Set int of pensize - code = "to tasetpensize :a\rsetpensize round :a\rend\r" + code - if pensize: # Return only the first argument. - code = "to tapensize\routput first round pensize\rend\r" + code - if setxy2: # Swap and round arguments - code = "to tasetxy :x :y\rsetxy :x :y\rend\r" + code - if setxy: # Swap and round arguments and add pen up/down - code = "to tasetxy :x :y\rpenup\rsetxy :x :y\rpendown\rend\r" + code - if arc: # Turtle Art 'arc' needs to be redefined. - c = (2 * math.pi)/360 - code = "to taarc :a :r\rrepeat round :a [right 1 forward (" + \ - str(c) + " * :r)]\rend\r" + code - if heap: # Add psuedo 'push' and 'pop' - code = "to tapush :foo\rmake \"taheap fput :foo :taheap\rend\r" + \ - "to tapop\rif emptyp :taheap [stop]\rmake \"tmp first :taheap\r" + \ - "make \"taheap butfirst :taheap\routput :tmp\rend\r" + \ - "to taclearheap\rmake \"taheap []\rend\r" + \ - "to taprintheap \rprint :taheap\rend\r" + \ - "make \"taheap []\r" + code - code = "window\r" + code + if setpensize: # Set int of pensize + code = 'to tasetpensize :a\rsetpensize round :a\rend\r' + code + if pensize: # Return only the first argument. + code = 'to tapensize\routput first round pensize\rend\r' + code + if setxy2: # Swap and round arguments + code = 'to tasetxy :x :y\rsetxy :x :y\rend\r' + code + if setxy: # Swap and round arguments and add pen up/down + code = 'to tasetxy :x :y\rpenup\rsetxy :x :y\rpendown\rend\r' + code + if arc: # Turtle Art 'arc' needs to be redefined. + c = (2 * math.pi) / 360 + code = 'to taarc :a :r\rrepeat round :a [right 1 forward (' + \ + str(c) + ' * :r)]\rend\r' + code + if heap: # Add psuedo 'push' and 'pop' + code = 'to tapush :foo\rmake "taheap fput :foo :taheap\rend\r' + \ + 'to tapop\rif emptyp :taheap [stop]\rmake \'tmp first :taheap\r' +\ + 'make "taheap butfirst :taheap\routput :tmp\rend\r' + \ + 'to taclearheap\rmake "taheap []\rend\r' + \ + 'to taprintheap \rprint :taheap\rend\r' + \ + 'make "taheap []\r' + code + code = 'window\r' + code return code diff --git a/TurtleArt/tagplay.py b/TurtleArt/tagplay.py index 83f9df5..abdfe88 100644 --- a/TurtleArt/tagplay.py +++ b/TurtleArt/tagplay.py @@ -121,29 +121,29 @@ class Gplay(): if lc.tw.running_sugar: self.bin.set_transient_for(lc.tw.activity) - self.bin.move(x, y+108) + self.bin.move(x, y + 108) self.bin.resize(w, h) self.bin.show_all() self._want_document = True def _player_eos_cb(self, widget): - logging.debug("end of stream") + logging.debug('end of stream') pass def _player_error_cb(self, widget, message, detail): self.player.stop() self.player.set_uri(None) - logging.debug("Error: %s - %s" % (message, detail)) + logging.debug('Error: %s - %s' % (message, detail)) def _player_stream_info_cb(self, widget, stream_info): if not len(stream_info) or self.got_stream_info: return GST_STREAM_TYPE_UNKNOWN = 0 - GST_STREAM_TYPE_AUDIO = 1 - GST_STREAM_TYPE_VIDEO = 2 - GST_STREAM_TYPE_TEXT = 3 + GST_STREAM_TYPE_AUDIO = 1 + GST_STREAM_TYPE_VIDEO = 2 + GST_STREAM_TYPE_TEXT = 3 only_audio = True for item in stream_info: @@ -167,7 +167,7 @@ class Gplay(): continue else: result.append('file://' + \ - urllib.quote(os.path.join(self.playpath,x))) + urllib.quote(os.path.join(self.playpath, x))) return result def start(self, uri=None): @@ -176,7 +176,7 @@ class Gplay(): if not uri: return False # FIXME: parse m3u files and extract actual URL - if uri.endswith(".m3u") or uri.endswith(".m3u8"): + if uri.endswith('.m3u') or uri.endswith('.m3u8'): self.playlist.extend(self.getplaylist([line.strip() \ for line in open(uri).readlines()])) elif uri.endswith('.pls'): @@ -184,24 +184,25 @@ class Gplay(): cf.readfp(open(uri)) x = 1 while True: - self.playlist.append(cf.get("playlist",'File'+str(x))) + self.playlist.append(cf.get('playlist', 'File' + str(x))) x += 1 except: #read complete pass else: - self.playlist.append("file://" + urllib.quote(os.path.abspath(uri))) + self.playlist.append('file://' + \ + urllib.quote(os.path.abspath(uri))) if not self.player: # lazy init the player so that videowidget is realized # and has a valid widget allocation self.player = GstPlayer(self.videowidget) - self.player.connect("eos", self._player_eos_cb) - self.player.connect("error", self._player_error_cb) - self.player.connect("stream-info", self._player_stream_info_cb) + self.player.connect('eos', self._player_eos_cb) + self.player.connect('error', self._player_error_cb) + self.player.connect('stream-info', self._player_stream_info_cb) try: if not self.currentplaying: - logging.info("Playing: " + self.playlist[0]) + logging.info('Playing: ' + self.playlist[0]) self.player.set_uri(self.playlist[0]) self.currentplaying = 0 self.play_toggled() @@ -240,10 +241,10 @@ class Gplay(): def scale_value_changed_cb(self, scale): # see seek.c:seek_cb - real = long(scale.get_value() * self.p_duration / 100) # in ns + real = long(scale.get_value() * self.p_duration / 100) # in ns self.player.seek(real) # allow for a preroll - self.player.get_state(timeout=50 * gst.MSECOND) # 50 ms + self.player.get_state(timeout=50 * gst.MSECOND) # 50 ms def scale_button_release_cb(self, widget, event): # see seek.cstop_seek @@ -274,9 +275,8 @@ class Gplay(): class GstPlayer(gobject.GObject): __gsignals__ = { 'error': (gobject.SIGNAL_RUN_FIRST, None, [str, str]), - 'eos' : (gobject.SIGNAL_RUN_FIRST, None, []), - 'stream-info' : (gobject.SIGNAL_RUN_FIRST, None, [object]) - } + 'eos': (gobject.SIGNAL_RUN_FIRST, None, []), + 'stream-info': (gobject.SIGNAL_RUN_FIRST, None, [object])} def __init__(self, videowidget): gobject.GObject.__init__(self) @@ -284,7 +284,7 @@ class GstPlayer(gobject.GObject): self.playing = False self.error = False - self.player = gst.element_factory_make("playbin", "player") + self.player = gst.element_factory_make('playbin', 'player') self.videowidget = videowidget self._init_video_sink() @@ -304,18 +304,18 @@ class GstPlayer(gobject.GObject): if message.structure.get_name() == 'prepare-xwindow-id': self.videowidget.set_sink(message.src) message.src.set_property('force-aspect-ratio', True) - + def on_message(self, bus, message): t = message.type if t == gst.MESSAGE_ERROR: err, debug = message.parse_error() - logging.debug("Error: %s - %s" % (err, debug)) + logging.debug('Error: %s - %s' % (err, debug)) self.error = True - self.emit("eos") + self.emit('eos') self.playing = False - self.emit("error", str(err), str(debug)) + self.emit('error', str(err), str(debug)) elif t == gst.MESSAGE_EOS: - self.emit("eos") + self.emit('eos') self.playing = False elif t == gst.MESSAGE_STATE_CHANGED: old, new, pen = message.parse_state_changed() @@ -327,12 +327,12 @@ class GstPlayer(gobject.GObject): self.bin = gst.Bin() videoscale = gst.element_factory_make('videoscale') self.bin.add(videoscale) - pad = videoscale.get_pad("sink") - ghostpad = gst.GhostPad("sink", pad) + pad = videoscale.get_pad('sink') + ghostpad = gst.GhostPad('sink', pad) self.bin.add_pad(ghostpad) - videoscale.set_property("method", 0) + videoscale.set_property('method', 0) - caps_string = "video/x-raw-yuv, " + caps_string = 'video/x-raw-yuv, ' r = self.videowidget.get_allocation() if r.width > 500 and r.height > 500: # Sigh... xvimagesink on the XOs will scale the video to fit @@ -340,24 +340,24 @@ class GstPlayer(gobject.GObject): # video in Xephyr so that the XO can work right. w = 480 h = float(w) / float(float(r.width) / float(r.height)) - caps_string += "width=%d, height=%d" % (w, h) + caps_string += 'width=%d, height=%d' % (w, h) else: - caps_string += "width=480, height=360" + caps_string += 'width=480, height=360' caps = gst.Caps(caps_string) - self.filter = gst.element_factory_make("capsfilter", "filter") + self.filter = gst.element_factory_make('capsfilter', 'filter') self.bin.add(self.filter) - self.filter.set_property("caps", caps) + self.filter.set_property('caps', caps) - conv = gst.element_factory_make ("ffmpegcolorspace", "conv"); + conv = gst.element_factory_make('ffmpegcolorspace', 'conv') self.bin.add(conv) videosink = gst.element_factory_make('autovideosink') self.bin.add(videosink) gst.element_link_many(videoscale, self.filter, conv, videosink) - self.player.set_property("video-sink", self.bin) + self.player.set_property('video-sink', self.bin) def query_position(self): - "Returns a (position, duration) tuple" + """ Returns a (position, duration) tuple """ try: position, format = self.player.query_position(gst.FORMAT_TIME) except: @@ -383,22 +383,22 @@ class GstPlayer(gobject.GObject): if res: self.player.set_new_stream_time(0L) else: - logging.debug("seek to %r failed" % location) + logging.debug('seek to %r failed' % location) def pause(self): - logging.debug("pausing player") + logging.debug('pausing player') self.player.set_state(gst.STATE_PAUSED) self.playing = False def play(self): - logging.debug("playing player") + logging.debug('playing player') self.player.set_state(gst.STATE_PLAYING) self.playing = True self.error = False - + def stop(self): self.player.set_state(gst.STATE_NULL) - logging.debug("stopped player") + logging.debug('stopped player') def get_state(self, timeout=1): return self.player.get_state(timeout=timeout) @@ -408,6 +408,7 @@ class GstPlayer(gobject.GObject): class VideoWidget(gtk.DrawingArea): + def __init__(self): gtk.DrawingArea.__init__(self) self.set_events(gtk.gdk.EXPOSURE_MASK) @@ -426,24 +427,3 @@ class VideoWidget(gtk.DrawingArea): assert self.window.xid self.imagesink = sink self.imagesink.set_xwindow_id(self.window.xid) - - - -if __name__ == '__main__': - window = gtk.Window() - - view = VideoWidget() - - player = GstPlayer(view) - - window.add(view) - - player.set_uri('http://78.46.73.237:8000/prog') - player.play() - window.show_all() - - - - gtk.main() - - diff --git a/TurtleArt/tautils.py b/TurtleArt/tautils.py index 1845b3c..960b442 100644 --- a/TurtleArt/tautils.py +++ b/TurtleArt/tautils.py @@ -134,6 +134,7 @@ def get_id(connection): return connection.id return None + def json_dump(data): """ Save data using available JSON tools. """ if OLD_SUGAR_SYSTEM is True: -- cgit v0.9.1