Ejemplo n.º 1
0
class FileSaverMixin(SaverMixin):
    """Implements API for saving & loading in a File oriented context"""

    def __init__(self, pseudo, directory):
        SaverMixin.__init__(self, pseudo, directory)
    
    # MENU
    def save(self):
        """fill document with information from .profile file"""
        profile_file = open(self.get_id(), 'w')
        profile_file.write("#%s\n"% self.encoding)
        self.config.write(profile_file)
        profile_file.close()
        
    def load(self,):
        """fill document with information from .profile file"""
        # load profile
        if not os.path.exists(self.get_id()):
            print "profile %s does not exists"% self.get_id()
            return False
        else:
            profile_file = open(self.get_id())
            self.encoding = profile_file.readline()[1:]
            self.config = CustomConfigParser(self.encoding)
            self.config.readfp(profile_file)
            profile_file.close()
            return True

    def to_stream(self):
        """returns a file object containing values"""
        file_obj = tempfile.TemporaryFile()
        file_obj.write("#%s\n"% self.encoding)
        self.config.write(file_obj)
        file_obj.seek(0)
        return file_obj
 def test_config_parser(self):
     writer = CustomConfigParser()
     writer.add_section('TEST')
     writer.set('TEST', "Windows:path", "not a valid linux:path!")
     writer.write(open("generated/config.test", "w"))
     # standard reader
     reader = ConfigParser()
     reader.readfp(open("generated/config.test"))
     self.assert_(reader.has_section('TEST'))
     self.assert_(reader.has_option('TEST', "Windows"))
     self.assertEquals(reader.get('TEST', "Windows"), "path = not a valid linux:path!")
     # custom reader
     reader = CustomConfigParser()
     reader.readfp(open("generated/config.test"))
     self.assert_(reader.has_section('TEST'))
     self.assert_(reader.has_option('TEST', "Windows:path"))
     self.assertEquals(reader.get('TEST', "Windows:path"), "not a valid linux:path!")