当前位置: 首页>>代码示例>>Python>>正文


Python Grammar.parseString方法代码示例

本文整理汇总了Python中grammar.Grammar.parseString方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.parseString方法的具体用法?Python Grammar.parseString怎么用?Python Grammar.parseString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在grammar.Grammar的用法示例。


在下文中一共展示了Grammar.parseString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: parse

# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import parseString [as 别名]
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
开发者ID:18sg,项目名称:Panel-O-Matic-SHET-Client,代码行数:18,代码来源:parser.py

示例2: MMParser

# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import parseString [as 别名]
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)
#.........这里部分代码省略.........
开发者ID:rgammans,项目名称:MysteryMachine,代码行数:103,代码来源:MMParser.py


注:本文中的grammar.Grammar.parseString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。