From a7dbc53a6657ad1e115f096e3072dc927847d853 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Tue, 03 Feb 2009 00:01:01 +0000 Subject: Mix in Char with OOP --- (limited to 'document.py') diff --git a/document.py b/document.py index 72e571e..d9227fa 100644 --- a/document.py +++ b/document.py @@ -14,6 +14,7 @@ import os import gtk +from zipfile import ZipFile from xml.etree.ElementTree import Element, SubElement, tostring, fromstring import theme @@ -21,24 +22,23 @@ from sound import Sound from ground import Ground from utils import * +from char import Frame + class Document: tape = [] ground = None sound = None - class Tape: - def __init__(self): - self.clean() - - def clean(self): - self.orig = theme.EMPTY_ORIG - self.filename = theme.EMPTY_FILENAME for i in range(theme.TAPE_COUNT): - tape.append(Tape()) + tape.append(Frame(None, theme.EMPTY)) + +def clean(index): + from char import Frame + Document.tape[index] = Frame(None, theme.EMPTY) def save(filepath): - zip = Zip(filepath, 'w') + zip = ZipFile(filepath, 'w') manifest = Element('memorize') def _save(node, arcname, value): @@ -54,20 +54,24 @@ def save(filepath): _save(SubElement(manifest, 'ground'), 'ground.png', Document.ground) _save(SubElement(manifest, 'sound'), 'sound', Document.sound) - saved = {} + arcfiles = {} + for i, frame in enumerate( + [i for i in set(Document.tape) if not i.empty() and i.custom()]): + arcfiles[frame] = 'frame%03d.png' % i + zip.writestr(arcfiles[frame], frame.read()) + tape = SubElement(manifest, 'tape') - for i in range(theme.TAPE_COUNT): - frame = SubElement(tape, 'frame') - if Document.tape[i].filename: - frame.attrib['preinstalled'] = '1' - frame.attrib['filename'] = Document.tape[i].filename + for i, frame in enumerate(Document.tape): + if frame.empty(): + continue + node = SubElement(tape, 'frame') + if frame.custom(): + node.attrib['custom'] = '1' + node.attrib['filename'] = arcfiles[frame] else: - frame.attrib['preinstalled'] = '0' - arcname = saved.get(Document.tape[i].orig) - if not arcname: - arcname = saved[Document.tape[i].orig] = 'frame%03d.png' % i - zip.write_pixbuf(arcname, Document.tape[i].orig) - frame.attrib['filename'] = arcname + node.attrib['custom'] = '0' + node.attrib['filename'] = frame.filename() + node.attrib['index'] = str(i) zip.writestr('MANIFEST.xml', tostring(manifest, encoding='utf-8')) zip.close() @@ -76,7 +80,7 @@ def save(filepath): shutil.copy(filepath, '/tmp/foo.zip') def load(filepath): - zip = Zip(filepath, 'r') + zip = ZipFile(filepath, 'r') manifest = fromstring(zip.read('MANIFEST.xml')) def _load(node, klass): @@ -91,21 +95,18 @@ def load(filepath): Document.sound = _load(manifest.find('sound'), Sound) loaded = {} - for i, frame in enumerate(manifest.findall('tape/frame')): + for node in manifest.findall('tape/frame'): + i = int(node.attrib['index']) if i >= theme.TAPE_COUNT: continue - if int(frame.attrib['preinstalled']): - if frame.attrib['filename'] == theme.EMPTY_FILENAME: - Document.tape[i].orig = theme.EMPTY_ORIG - else: - Document.tape[i].orig = theme.pixbuf(frame.attrib['filename']) - Document.tape[i].filename = frame.attrib['filename'] + filename = node.attrib['filename'] + if int(node.attrib['custom']): + frame = loaded.get(filename) + if not frame: + frame = loaded[filename] = Frame( + zip.read(filename), theme.RESTORED) + Document.tape[i] = frame else: - pixbuf = loaded.get(frame.attrib['filename']) - if not pixbuf: - pixbuf = zip.read_pixbuf(frame.attrib['filename']) - loaded[frame.attrib['filename']] = pixbuf - Document.tape[i].orig = pixbuf - Document.tape[i].filename = None + Document.tape[i] = Frame(filename, theme.PREINSTALLED) zip.close() -- cgit v0.9.1