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 save(self, path):
     self.config = CustomConfigParser(self.encoding)
     for name, a_filter in self.filters.items():
         section_name = a_filter.PREFIX + (a_filter.filter_or and "OR_" or "AND_") + name
         if not self.config.has_section(section_name):
             self.config.add_section(section_name)
         for prop_name, value in a_filter.as_dict().items():
             self.config.set(section_name, prop_name, value)
     DocSaverMixin.save(self, path)
 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 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!")
class FilterSaverMixin(DocSaverMixin):
    """Implements API for saving & loading in a File oriented context"""

    FILTERS = [PeerFilter, FileFilter]

    def __init__(self, encoding=ENCODING):
        DocSaverMixin.__init__(self, encoding)

    # menu
    def save(self, path):
        self.config = CustomConfigParser(self.encoding)
        for name, a_filter in self.filters.items():
            section_name = a_filter.PREFIX + (a_filter.filter_or and "OR_" or "AND_") + name
            if not self.config.has_section(section_name):
                self.config.add_section(section_name)
            for prop_name, value in a_filter.as_dict().items():
                self.config.set(section_name, prop_name, value)
        DocSaverMixin.save(self, path)

    def load(self, path):
        """fill document with information from .profile file"""
        # load config
        if not DocSaverMixin.load(self, path):
            return False
        # synchronize cache
        for section in self.config.sections():
            props = self.config.items(section)
            for filter_class in self.FILTERS:
                name, new_filter = filter_class.from_str(section, props)
                if new_filter is None:
                    continue
                else:
                    self.filters[name] = new_filter
                    break
        # add match all
        self._create_match_all()