X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=spec%2Fedit_spec.py;h=9584b05169f5517217597859624d6cbd73ccad87;hb=ca203bf4cf215ea6e5e1713c5b4140f0bb8d93c8;hp=dec96efc775a3668799953010d411d1bb72ba6e9;hpb=f42e0edd3094808c319a89594ae6f55843a0a3bf;p=swftools.git diff --git a/spec/edit_spec.py b/spec/edit_spec.py old mode 100644 new mode 100755 index dec96ef..9584b05 --- a/spec/edit_spec.py +++ b/spec/edit_spec.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python import wx import wx.lib.scrolledpanel as scrolled import os @@ -16,6 +17,8 @@ class Check: return "pixel at (%d,%d)" % (self.x,self.y) def right(self): return "" + def verifies(self, model): + return True class PixelColorCheck(Check): def __init__(self, x,y, color): @@ -23,6 +26,10 @@ class PixelColorCheck(Check): self.color = color def right(self): return "is of color 0x%06x" % self.color + def verifies(self, model): + p = model.getPixel(self.x,self.y) + val = p[0]<<16 | p[1]<<8 | p[2] + return val == self.color class TwoPixelCheck(Check): def __init__(self, x,y, x2,y2): @@ -32,7 +39,12 @@ class TwoPixelCheck(Check): return "pixel at (%d,%d)" % (self.x2,self.y2) class PixelBrighterThan(TwoPixelCheck): - pass + def verifies(self, model): + p1 = model.getPixel(self.x,self.y) + p2 = model.getPixel(self.x2,self.y2) + val1 = p1[0] + p1[1] + p1[2] + val2 = p2[0] + p2[1] + p2[2] + return val1 > val2 class PixelDarkerThan(TwoPixelCheck): pass @@ -53,7 +65,14 @@ class AreaPlain(AreaCheck): class AreaNotPlain(AreaCheck): pass -checktypes = [PixelColorCheck,PixelBrighterThan,PixelDarkerThan,PixelEqualTo,AreaPlain,AreaNotPlain] +class AreaText(AreaCheck): + def __init__(self, x,y, x2, y2, text=""): + AreaCheck.__init__(self,x,y,x2,y2) + self.text = text + +checktypes = [PixelColorCheck,PixelBrighterThan,PixelDarkerThan,PixelEqualTo,AreaPlain,AreaNotPlain,AreaText] + +global TESTMODE def convert_to_ppm(pdf): print pdf @@ -62,13 +81,21 @@ def convert_to_ppm(pdf): f.close() width,heigth = re.compile(r"Page size:\s*([0-9]+) x ([0-9]+) pts").findall(info)[0] dpi = int(72.0 * 612 / int(width)) - os.system("pdftoppm -r "+str(dpi)+" -f 1 -l 1 "+pdf+" test") + if TESTMODE: + os.system("pdf2swf -s zoom="+str(dpi)+" -p1 "+pdf+" -o test.swf") + os.system("swfrender --legacy test.swf -o test.png") + os.unlink("test.swf") + return "test.png" + else: + os.system("pdftoppm -r "+str(dpi)+" -f 1 -l 1 "+pdf+" test") return "test-000001.ppm" + class Model: - def __init__(self, filename, checks): - self.filename = filename - self.imgfilename = convert_to_ppm(filename) + def __init__(self, specfile, docfile, checks): + self.specfile = specfile + self.docfile = docfile + self.imgfilename = convert_to_ppm(self.docfile) self.bitmap = wx.Bitmap(self.imgfilename) self.image = wx.ImageFromBitmap(self.bitmap) self.width = self.bitmap.GetWidth() @@ -78,6 +105,13 @@ class Model: self.appendListeners = [] self.drawModeListeners = [] self.drawmode = PixelColorCheck + + def close(self): + try: os.unlink(self.imgfilename) + except: pass + + def getPixel(self,x,y): + return (self.image.GetRed(x,y), self.image.GetGreen(x,y), self.image.GetBlue(x,y)) def setdrawmode(self, mode): self.drawmode = mode @@ -102,7 +136,18 @@ class Model: @staticmethod def load(filename): - path = os.path.splitext(filename)[0]+".rb" + # convenience, allow to do "edit_spec.py file.pdf" + p,ext = os.path.splitext(filename) + if ext!=".rb": + path = p+".rb" + if not os.path.isfile(path): + path = p+".spec.rb" + if not os.path.isfile(path): + print "No file %s found, creating new..." % path + return Model(path, filename, []) + else: + path = filename + fi = open(path, "rb") r_file = re.compile(r"^convert_file \"([^\"]*)\"") r_pixelcolor = re.compile(r"^pixel_at\(([0-9]+),([0-9]+)\).should_be_of_color (0x[0-9a-fA-F]+)") @@ -111,6 +156,7 @@ class Model: r_pixelequalto = re.compile(r"^pixel_at\(([0-9]+),([0-9]+)\).should_be_the_same_as pixel_at\(([0-9]+),([0-9]+)\)") r_areaplain = re.compile(r"^area_at\(([0-9]+),([0-9]+),([0-9]+),([0-9]+)\).should_be_plain_colored") r_areanotplain = re.compile(r"^area_at\(([0-9]+),([0-9]+),([0-9]+),([0-9]+)\).should_not_be_plain_colored") + r_areatext = re.compile(r"^area_at\(([0-9]+),([0-9]+),([0-9]+),([0-9]+)\).should_contain_text '(.*)'") r_width = re.compile(r"^width.should be ([0-9]+)") r_height = re.compile(r"^height.should be ([0-9]+)") r_describe = re.compile(r"^describe \"pdf conversion\"") @@ -127,7 +173,7 @@ class Model: if filename: raise Exception("can't load multi-file specs (in line %d)" % (nr+1)) filename = m.group(1); - model = Model(filename, []) + model = Model(path, filename, []) continue m = r_pixelcolor.match(line) if m: model.append(PixelColorCheck(int(m.group(1)),int(m.group(2)),int(m.group(3),16)));continue @@ -141,6 +187,8 @@ class Model: if m: model.append(AreaPlain(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))));continue m = r_areanotplain.match(line) if m: model.append(AreaNotPlain(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4))));continue + m = r_areatext.match(line) + if m: model.append(AreaText(int(m.group(1)),int(m.group(2)),int(m.group(3)),int(m.group(4)),m.group(5)));continue if r_width.match(line) or r_height.match(line): continue # compatibility if r_describe.match(line) or r_end.match(line) or r_header.match(line): @@ -152,10 +200,10 @@ class Model: return model def save(self): - path = os.path.splitext(self.filename)[0]+".rb" + path = self.specfile fi = open(path, "wb") fi.write("require File.dirname(__FILE__) + '/spec_helper'\n\ndescribe \"pdf conversion\" do\n") - fi.write(" convert_file \"%s\" do\n" % self.filename) + fi.write(" convert_file \"%s\" do\n" % self.docfile) for check in self.checks: c = check.__class__ if c == PixelColorCheck: @@ -170,6 +218,8 @@ class Model: fi.write(" area_at(%d,%d,%d,%d).should_be_plain_colored\n" % (check.x,check.y,check.x2,check.y2)) elif c == AreaNotPlain: fi.write(" area_at(%d,%d,%d,%d).should_not_be_plain_colored\n" % (check.x,check.y,check.x2,check.y2)) + elif c == AreaText: + fi.write(" area_at(%d,%d,%d,%d).should_contain_text '%s'\n" % (check.x,check.y,check.x2,check.y2,check.text)) fi.write(" end\n") fi.write("end\n") fi.close() @@ -201,7 +251,7 @@ class ZoomWindow(wx.Window): for xx in range(15): x = self.x+xx-8 if 0<=x