def generateEmbeddedPool(cropperPoolDir, holderPoolDir, embeddedPoolDir, holderPerCropper): cropperImageNames = os.listdir(cropperPoolDir) holderImageNames = os.listdir(holderPoolDir) count = 1 for cropperImageName in cropperImageNames: for i in xrange(holderPerCropper): holderImageName = random.choice(holderImageNames) holderImage = os.path.join(holderPoolDir, holderImageName) cropperImage = os.path.join(cropperPoolDir, cropperImageName) outputImage = os.path.join(embeddedPoolDir, '_'.join([holderImageName.split('.')[0], cropperImageName.split('.')[0], str(i)]) + '.jpg') xstart, xend, ystart, yend = getRectBoxByImage(holderImage) replace(holderImage, cropperImage, outputImage, xstart, xend, ystart, yend) count += 1
def commit(self): index = [d.a(d.img( src=makeThumb(which),title=", ".join(tags)),href=makeLink(which)) for which,tags in enumerate(self.index)] with replace(oj(dest,"index.html")) as out: out.write(makePage( d.h1(self.title), d.p(*index), d.p(self.description), d.p(", ".join(self.tags)), title=self.title))
def generateEmbeddedPool(cropperPoolDir, holderPoolDir, embeddedPoolDir, holderPerCropper): cropperImageNames = os.listdir(cropperPoolDir) holderImageNames = os.listdir(holderPoolDir) count = 1 for cropperImageName in cropperImageNames: for i in xrange(holderPerCropper): holderImageName = random.choice(holderImageNames) holderImage = os.path.join(holderPoolDir, holderImageName) cropperImage = os.path.join(cropperPoolDir, cropperImageName) outputImage = os.path.join( embeddedPoolDir, '_'.join([ holderImageName.split('.')[0], cropperImageName.split('.')[0], str(i) ]) + '.jpg') xstart, xend, ystart, yend = getRectBoxByImage(holderImage) replace(holderImage, cropperImage, outputImage, xstart, xend, ystart, yend) count += 1
def advance(self,which,mediumf,tags): tags=[cleanupTag(tag) for tag in tags] self.index.append(tags) link = makeLink(which) head = [] links = [] if which > 0: prev = makeLink(which-1) head.append(d.link(rel="prev",href=prev)) links.append(d.a("Previous",href=prev)) next = "./index.html" if which < self.pages-1: next = makeLink(which+1) head.append(d.link(rel="next",href=next)) links.append(d.a("Next",href=next)) links.append(d.a("Contents",href="./index.html")) with replace(oj(dest,link)) as out: out.write(makePage(d.p(d.a(d.img(class_='page',src=mediumf),href=next)), d.p(*zip(links,repeat(' '))), d.p("Tags: ",", ".join(tags)), head=head, title="{} page {:x}".format( self.title,which)))
def test_empty_template(): template = "" mapping = {} expected = "" assert replace(template, mapping) == expected
def test_dict_not_empty(): template = "$b$" mapping = {"a":"c"} expected = "$b$" assert replace(template, mapping) == expected
def test_different_mapping(): template = "$a$" mapping = {"a": "c"} expected = "c" assert replace(template, mapping) == expected
def test_mapping(): template = "$a$" mapping = {"a": "b"} expected = "b" assert replace(template, mapping) == expected
def test_template_without_placeholder(): template = "no placeholders here" mapping = {} expected = "no placeholders here" assert replace(template, mapping) == expected
from reader import Program import replacer import expander import sys if __name__ == '__main__': with open(sys.argv[1], 'r', encoding='utf-8') as fd: string_program = fd.read() prgrm = Program(string_program) shifted_prgrm = replacer.replace(prgrm) new_string = expander.prgrm_to_text(shifted_prgrm) print(new_string) print('\n') exec(new_string)
from replacer import replace import my_mod if __name__ == "__main__": from getter import get_do_something func = get_do_something() replace(func) print(my_mod.do_something())
def test_empty_replacement2(): template = "$c$" mapping = {"c": ""} expected = "" assert replace(template, mapping) == expected
def test_dict_not_empty(): template = "$b$" mapping = {"a": "c"} expected = "$b$" assert replace(template, mapping) == expected
def test_empty_mapping_makes_no_replacement(): template = "$replaceme$" mapping = {} expected = "$replaceme$" assert replace(template, mapping) == expected
def test_empty_replacement2(): template = "$c$" mapping = {"c":""} expected = "" assert replace(template, mapping) == expected
def test_empty_variable(): template = "$$" mapping = {"": "banana"} expected = "banana" assert replace(template, mapping) == expected
import replacer # my types for clear types = ["js", "php"] # get file in dir jsFiles = replacer.getFilesInCurrentDir(types) # clear replacer.replace(jsFiles)
dest = sys.argv[2] try: os.mkdir(dest) except OSError: pass style = """ .page { max-width: 95%; } body { text-align: center; }""" with replace(oj(dest,"style.css")) as out: out.write(style.encode('utf-8')) def makePage(*content,head=(),title='???'): return str(d.html(d.head(d.title(title),d.link(rel='stylesheet',href='style.css'),*head), d.body(content))).encode('utf-8') def makeLink(which): return '{:x}.html'.format(which) def makeThumb(which): return '{:x}-thumb.jpg'.format(which) def cleanupTag(tag): if tag.startswith('general:'): return tag[len('general:'):] return tag