1#! /usr/bin/env python 2#coding=utf-8 3import os 4 5class Base: 6 SIGN = "" 7 def __init__(self, io=None): 8 if io: 9 self.load(io) 10 11 def load(self, io): 12 if io.read(len(self.SIGN)) != self.SIGN: 13 raise TypeError 14 15 self._load(io) 16 17 def _load(self, io): 18 raise NotImplementedError 19 20 def save(self, io): 21 io.write(self.SIGN) 22 self._save(io) 23 24 def _save(self, io): 25 raise NotImplementedError 26 27def BaseFactory(io, class_list): 28 pos = io.tell() 29 for c in class_list: 30 sign = io.read(len(c.SIGN)) 31 io.seek(pos, os.SEEK_SET) 32 if sign == c.SIGN: 33 return c(io)