Exemplo n.º 1
0
 def apply_observing_policy(self):
     """register the object with the observer"""
     self.observer = PollyObserver()
     self.observer.register(self)
Exemplo n.º 2
0
 def apply_observing_policy(self):
     """register the object with the observer"""
     self.observer = PollyObserver()
     self.observer.register(self)
Exemplo n.º 3
0
class Parser(Observable):
    filename = None
    content = None
    parser = None

    def __init__(self, *args, **kwargs):
        super(Parser, self).__init__(*args, **kwargs)
        self.parser = LineParser()
        self.apply_observing_policy()

    def apply_observing_policy(self):
        """register the object with the observer"""
        self.observer = PollyObserver()
        self.observer.register(self)

    def load(self, filename):
        """Loads a configuration file in the parser"""
        self.filename = filename
        file = open(filename, 'r')
        self.content = file.readlines()
        file.close()
        return self.content

    def dump_values(self):
        """Dumps every directive/value into a key/value dictionary. Newer 
        keys override older ones"""
        dict = {}
        for line in self.content:
            key = self.parser.get_directive(line)
            if key:
                try:
                    value = self.parser.get_value(line)
                    dict[key] = value
                except:
                    dict[key] = "#ERROR"
        return dict

    def get_value(self, name):
        line = self.get_directive(name)
        if line == None: return None
        return self.parser.get_value(line)

    def set_value(self, name, value):
        idx = self._get_last_directive_idx(name)
        if idx == None:
            idx = self._last_line_idx()
            line = name + "\t" + self.parser.value_escape(value) + "\n"
            self.insert_line(self._last_line_idx(), line)
        else:
            line = self.get_line(idx)
            line = self.parser.change_value(line, value)
            self.set_line(idx, line)

    def remove_value(self, name):
        idx = self._get_last_directive_idx(name)
        if idx:
            self.remove_line(idx)
            return True
        return False

    def get_directive(self, name):
        idx = self._get_last_directive_idx(name)
        if (idx == None): return None
        line = self.get_line(idx)
        return line

    """
    def set_directive (self, name, line):
        idx = self._get_last_directive_idx(name)
        self.set_line( idx, line )
    """

    # idx starts from 0 !! it's not a line number
    def _get_last_directive_idx(self, name):
        last_found = None
        for key, line in enumerate(self.get_content()):
            if (self.parser.get_directive(line) == name): last_found = key
        return last_found

    #TODO create if doesn't exist.
    def set_directive(self, name, line):
        idx = self._get_last_directive_idx(name)
        if (idx == None):
            return self.insert_line(self._last_line_idx(),
                                    line.rstrip() + "\n")
        self.set_line(idx, line.rstrip() + "\n")

    def _last_line_idx(self):
        return 999999

    def has_option(self, name, option):
        line = self.get_directive(name)
        if (line == False or line == None): return False
        return self.parser.has_option(line, option)

    def get_options(self, name):
        value = self.get_value(name)
        if value == None: return []
        options = self.parser.parse_options(value)
        return options

    def add_option(self, name, option):
        line = self.get_directive(name)
        if (line == False or line == None):
            line = name
            line = name + "\t" + self.parser.value_escape(option) + "\n"
            self.insert_line(self._last_line_idx(), line)
            return
        line = self.parser.add_option(line, option)
        self.set_directive(name, line)

    def remove_option(self, name, option):
        #we need idx later if we decide to remove the whole line
        idx = self._get_last_directive_idx(name)
        line = self.get_directive(name)
        if (line == False or line == None): return False
        line = self.parser.remove_option(line, option)
        new_value = self.parser.get_value(line)
        if (new_value.strip() == ""):
            self.remove_line(idx)
        else:
            self.set_directive(name, line)

    def get_source(self):
        return "".join(self.get_content())

    def get_content(self):
        return self.content

    def set_content_from_string(self, string):
        self.content = string.split("\n")

    def get_line(self, idx):
        return self.get_content()[idx]

    def set_line(self, idx, line):
        self.content[idx] = line

    def insert_line(self, idx, line):
        """Inserts a line at the given idx"""
        if idx >= len(self.content):  #out of range
            idx = self._last_line_idx()
        self.content.insert(idx, line)
        self.raise_event('row_inserted', {'idx': idx, 'line': line})

    def remove_line(self, idx):
        del self.content[idx]
        self.raise_event('row_removed', {'idx': idx})
Exemplo n.º 4
0
class Parser (Observable):
    filename = None
    content = None
    parser = None
    
    def __init__ (self, *args, **kwargs):
        super (Parser, self).__init__ (*args, **kwargs)
        self.parser = LineParser()
        self.apply_observing_policy()
    
    def apply_observing_policy(self):
        """register the object with the observer"""
        self.observer = PollyObserver()
        self.observer.register(self)
        
    def load(self, filename ):
        """Loads a configuration file in the parser"""
        self.filename = filename
        file = open ( filename, 'r' )
        self.content = file.readlines()
        file.close()
        return self.content
    
    def dump_values (self ):
        """Dumps every directive/value into a key/value dictionary. Newer 
        keys override older ones"""
        dict = {}
        for line in self.content:            
            key = self.parser.get_directive( line )
            if key:                
                try:
                    value = self.parser.get_value(line)
                    dict[ key ] = value
                except :                    
                    dict[ key ] = "#ERROR"
        return dict
    
    def get_value(self, name):
        line = self.get_directive(name)    
        if line == None: return None
        return self.parser.get_value(line)
    def set_value (self, name, value):
        idx = self._get_last_directive_idx(name)
        if idx == None:
            idx = self._last_line_idx()
            line = name+"\t"+self.parser.value_escape( value )+"\n"
            self.insert_line( self._last_line_idx(), line )
        else:
            line = self.get_line( idx )
            line = self.parser.change_value(line, value)
            self.set_line( idx, line )
    def remove_value(self, name):
        idx = self._get_last_directive_idx(name)
        if idx: 
            self.remove_line(idx)
            return True
        return False
    def get_directive(self, name):
        idx = self._get_last_directive_idx(name)
        if ( idx == None ): return None
        line = self.get_line( idx )
        return line
    """
    def set_directive (self, name, line):
        idx = self._get_last_directive_idx(name)
        self.set_line( idx, line )
    """
    # idx starts from 0 !! it's not a line number    
    def _get_last_directive_idx (self, name ):
        last_found = None
        for key, line in enumerate(self.get_content() ):   
            if ( self.parser.get_directive(line) == name ): last_found = key            
        return last_found
    #TODO create if doesn't exist.
    def set_directive (self, name, line):
        idx = self._get_last_directive_idx(name)
        if ( idx == None ):
             return self.insert_line( self._last_line_idx() , line.rstrip()+"\n")
        self.set_line( idx, line.rstrip()+"\n" )
    
    def _last_line_idx (self):
        return 999999
    
    def has_option (self, name, option ):
        line = self.get_directive(name)
        if ( line == False or line == None ): return False
        return self.parser.has_option(line, option)  
    def get_options (self, name):
        value = self.get_value(name)
        if value == None: return []
        options = self.parser.parse_options( value )
        return options
    def add_option (self, name, option ):    
        line = self.get_directive(name)
        if ( line == False or line == None ): 
            line = name
            line = name+"\t"+self.parser.value_escape( option )+"\n"    
            self.insert_line( self._last_line_idx(), line )
            return
        line = self.parser.add_option(line, option)
        self.set_directive(name, line)
    def remove_option (self, name, option ): 
        #we need idx later if we decide to remove the whole line
        idx = self._get_last_directive_idx(name)   
        line = self.get_directive(name)
        if ( line == False or line == None ): return False
        line = self.parser.remove_option(line, option)
        new_value = self.parser.get_value( line )
        if ( new_value.strip() == "" ):
            self.remove_line( idx )
        else:
            self.set_directive(name, line)
    def get_source (self):
        return "".join( self.get_content() )
    def get_content(self):
        return self.content
    def set_content_from_string(self, string):
        self.content = string.split( "\n" )
    def get_line(self, idx):
        return self.get_content()[idx]
    def set_line(self, idx, line):
        self.content[ idx ] = line    
    def insert_line (self, idx, line):
        """Inserts a line at the given idx"""
        if idx >= len( self.content ): #out of range
            idx = self._last_line_idx()        
        self.content.insert( idx, line )
        self.raise_event( 'row_inserted', {'idx':idx, 'line':line } )
    def remove_line(self, idx ):
        del self.content[ idx ]
        self.raise_event( 'row_removed', {'idx':idx } )