コード例 #1
0
def parse(string):
	parsed = Grammar.parseString(string, parseAll = True)
	
	bindings = []
	
	for mode_block_p in parsed:
		mode = parse_mode(mode_block_p)
		
		for binding_p in mode_block_p["bindings"][0]:
			press = parse_button_press(binding_p, mode)
			
			call = binding_p["action"]["url"]
			args = list(binding_p["action"]["args"])
			bindings.append(Binding(press, call, args, "property" in binding_p))
	
	return bindings
コード例 #2
0
ファイル: MMParser.py プロジェクト: rgammans/MysteryMachine
class MMParser (object):

  """
   This class handles the macro language which allows references between attributes
   and objects within a MMsystems. The parser resovle references to objects and
   attributes.

  :version:
  :author:
  """

  du_defaults = {'file_insertion_enabled': 0,
              'raw_enabled': 0,
              '_disable_config': 1,
              'doctitle_xform': False}

  def __init__(self,obj):
        self.myobject = obj
        self.grammar  = Grammar(obj)
        self.logger   = logging.getLogger("MysteryMachine.parsetools.MMParser")
#        self.publisher= MMPublisher(obj)

  def evaluate(self,expr):
        self.logger.debug( "\n--evaling--\n%s\n----\n" % expr)
        value=self.grammar.parseString(expr) 
        self.logger.debug( "Parsed as->%s" % repr(value))
        newval=value[0]
        for part in value[1:]:
            newval = newval + part
        self.logger.debug( "\--evalled to --\n%s\n----\n" % newval.__repr__() )
        return newval

  def ProcessRawRst(self,rst_string,src=None,src_stack=[]):
    #Define the options and content for the role
    # this defines the system context and expansion stack
    # used to detect cycles.
    #
    # This is a bit ugly and slow - but it is a re-entrant way of
    #  passing context etc, to our interpreted role.
    # First - prepent system details to src..
    if src is None:
        src =repr(self.myobject)+":unknown"

    #Disabled to make source paths more readable now we
    # don't need to find the global system..
    #src = repr(self.myobject.owner)+":"+src
   
    self.logger.debug( "processed src-> %s" % src)
    self.logger.debug( "raw+IN->%s<-" % rst_string)

    docutils_stack.push( [ self.myobject ,  src_stack ] )

    settings =  copy.copy(MMParser.du_defaults)
    #Determine input encoding, if not a unicode<>
    settings["input_encoding"] = self.myobject.get_root().get_encoding()

    result =   publish_doctree(rst_string,source_path=src,
                               settings_overrides=settings
               
                )

    docutils_stack.pop() 
    self.logger.debug( "pnodelist-->%s<-" % result)
    
    try:
        source = result[0]
        source = source.source
    except:
        self.logger.info("Can't resolve docutils to find source, using src") 
        source = src
    self.logger.debug( "source[ => %s" % source)
    
    #Strip  document header and main containing paragraph.
    # so we get a simple result.
    result =   result.children 
    #self.logger.debug( "nodelist-->%s<-" % result)
    if len(result) ==1:
        self.logger.debug( "MMP-PRST: class is %s" % str(result[0].__class__))
        if result[0].__class__  == docutils.nodes.paragraph:
             result = result[0].children
    #Update source attrib in node.
    for docnode in result:
        if not source in docnode:
           docnode.source=source
    #self.logger.debug( "nodelist-->%s<-" % str(result))
    #self.logger.debug( "String[0]->%s<" % str(result[0]))
    return result
   
  def GetString(self,rst_string,src="unknown",src_stack=[]):
    #FIXME: THis is incredibly niave - we really need to use a rst 
    #       writer here.
    nodes = self.ProcessRawRst(rst_string,src,src_stack)
    result = ""
    for n in nodes:
      result += str(n)
    self.logger.debug( "String->'%s',len %s"  % (result ,len(nodes)))
    if len(nodes) == 1:
      #Supress outer xml container.
      result =re.sub("<(\w+)>([^>]*)</\\1>","\\2",result)
    self.logger.debug( "string->%s<-" %result)
    return result