minor changes to parsing process, and map file content to structure.
- Move magic marker for unparsed sections to a constant to matcha against. - Add functions to flatten a heckelement tree into a python structure
This commit is contained in:
		
							
								
								
									
										7
									
								
								TODO
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								TODO
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					Python:
 | 
				
			||||||
 | 
					 - More options for accessing a heck tree.
 | 
				
			||||||
 | 
					 - automated testing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Guile, C (or D), and Haxe:
 | 
				
			||||||
 | 
					 - Initial implementation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -9,6 +9,8 @@ from .exceptions import HeckParseException
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
HeckValue = TypeVar("HeckElement") | str | int | float
 | 
					HeckValue = TypeVar("HeckElement") | str | int | float
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					UNPARSED_MARKER = "%%% UNPARSED %%% "
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class HeckElement:
 | 
					class HeckElement:
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    Container for a tree of HECKformat elements.
 | 
					    Container for a tree of HECKformat elements.
 | 
				
			||||||
@ -30,11 +32,14 @@ class HeckElement:
 | 
				
			|||||||
        self.unparsed = False
 | 
					        self.unparsed = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def flatten(self) -> Mapping:
 | 
					    def flatten(self) -> Mapping:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Convert a hecktree element into a dictionary.
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
        output = {}
 | 
					        output = {}
 | 
				
			||||||
        for elm in self.children:
 | 
					        for elm in self.children:
 | 
				
			||||||
            elmval = []
 | 
					            elmval = []
 | 
				
			||||||
            if elm.unparsed:
 | 
					            if elm.unparsed:
 | 
				
			||||||
                nam = '%%%UNPARSED%%% '+elm.name
 | 
					                nam = UNPARSED_MARKER+elm.name
 | 
				
			||||||
                val = '\n'.join(elm.values)
 | 
					                val = '\n'.join(elm.values)
 | 
				
			||||||
                if nam in output:
 | 
					                if nam in output:
 | 
				
			||||||
                    output[nam] = '\n'.join([output[nam], val])
 | 
					                    output[nam] = '\n'.join([output[nam], val])
 | 
				
			||||||
@ -50,6 +55,37 @@ class HeckElement:
 | 
				
			|||||||
                    output[elm.name] = elmval
 | 
					                    output[elm.name] = elmval
 | 
				
			||||||
        return output
 | 
					        return output
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_flat_value(self) -> Union[List, HeckValue]:
 | 
				
			||||||
 | 
					        if not len(self.values):
 | 
				
			||||||
 | 
					            return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if len(self.values) > 1:
 | 
				
			||||||
 | 
					            return list(self.values)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return self.values[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def flatten_replace(self) -> Mapping:
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Convert a hecktree element into a dictionary (but don't try to merge values, just pretend each key is unique)
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        output = {}
 | 
				
			||||||
 | 
					        for elm in self.children:
 | 
				
			||||||
 | 
					            if elm.unparsed:
 | 
				
			||||||
 | 
					                nam = UNPARSED_MARKER+elm.name
 | 
				
			||||||
 | 
					                val = '\n'.join(elm.values)
 | 
				
			||||||
 | 
					                output[nam] = val
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                elmval = None
 | 
				
			||||||
 | 
					                if len(elm.children):
 | 
				
			||||||
 | 
					                    elmval = {}
 | 
				
			||||||
 | 
					                    elmval['children'] = elm.flatten_replace()
 | 
				
			||||||
 | 
					                    elmval['value'] = elm.get_flat_value()
 | 
				
			||||||
 | 
					                else:
 | 
				
			||||||
 | 
					                    elmval = elm.get_flat_value()
 | 
				
			||||||
 | 
					                output[elm.name] = elmval
 | 
				
			||||||
 | 
					        return output
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __str__(self):
 | 
					    def __str__(self):
 | 
				
			||||||
        k=''
 | 
					        k=''
 | 
				
			||||||
        if self.unparsed:
 | 
					        if self.unparsed:
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user