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


Python Properties.keys方法代码示例

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


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

示例1: mkProps

# 需要导入模块: from properties import Properties [as 别名]
# 或者: from properties.Properties import keys [as 别名]
def mkProps(props, dir):
    path=os.path.join(dir,'mk.cfg')
    if os.path.exists(path):
        p=Properties(path)
        for name in p.keys():
            value=p.get(name)
            if value.startswith('\\'):
                value=props.get(name)+' '+value[1:]
            props.assign(name,value)
    return props
开发者ID:amirgeva,项目名称:coide,代码行数:12,代码来源:genmake.py

示例2: ResourceBundle

# 需要导入模块: from properties import Properties [as 别名]
# 或者: from properties.Properties import keys [as 别名]
class ResourceBundle(PyWebMvcObject):
  """Extends a L{Properties<properties.Properties>} file to allow for
  parameterized substitution and property references (terms). Parameters are in
  the form C{{N}} where N is an zero-based index of the paramters to the
  getMessage function. Terms take the form C{${resourcekey}} and allow a
  resource entry to refer to another resource entry. It is not recommended that
  you place parameters in your terms, but it is allowed since parameter
  substitution is performed after term expansion. Example::

    term.foo=Foo
    term.idea=idea
    term.modifiedIdea={1} ${term.idea}
    message.bar=This is my {0} ${term.foo}.
    message.badIdea=This is a {0} ${term.modifiedIdea}.
    message.fineIdea=This is a {0} {1}.
    message.goodIdea=This is a {0} {1} ${term.idea}.

  >>> bundle["term.foo"]
  'Foo'
  >>> bundle["term.modifiedIdea"]
  '{1} idea'
  >>> bundle.getMessage("message.bar","Silly")
  'This is my Silly Foo.'
  >>> bundle.getMessage("message.badIdea","really", "bad")
  'This is a really bad idea.'
  >>> bundle.getMessage("message.fineIdea","perfectly",
  ...                   bundle.getMessage("term.modifiedIdea","fine"))
  'This is a perfectly fine idea.'
  >>> bundle.getMessage("message.goodIdea","much", "better")
  'This is a much better idea.'
  """
  def __init__(self, propertiesFile = None):
    self.props = Properties()
    if propertiesFile:
      self.addPropertiesFile(propertiesFile)
  def addPropertiesFile(self, propertiesFile):
    if isinstance(propertiesFile, types.StringTypes):
      self.props.read(propertiesFile)
    else:
      self.props.load(propertiesFile)
    self.resolve_terms()
  def addPropertiesFiles(self, propertiesFiles):
    for f in propertiesFiles:
      self.addPropertiesFile(f)
  def resolve_terms(self):
    for key in self.props.keys():
      self.props[key] = self.replaceTerms(self.props[key])
  def has_term(self,msg):
    idx = msg.find("${")
    if idx < 0:
      return False
    else:
      return msg.find("}",idx) >= 0
  def get_term(self,msg,all=False):
    tokens = re.split("(\\$\\{|\\})",msg)
    startCount = 0
    term = ""
    terms = []
    for token in tokens:
      if token == "${":
        startCount += 1
        if startCount > 1:
          term += token
      elif token == "}" and startCount > 0:
        startCount -= 1
        if startCount == 0:
          if all:
            terms.append(term)
            term = ""
          else:
            return term
        else:
          term += token
      elif startCount > 0:
        term += token
    if startCount > 0:
      raise ValueError("Malformed term found in message resource: "+msg)
    if all:
      return terms
    else:
      return None
  def replaceTerms(self, msg, paramMap = {}):
    terms = self.get_term(msg,all=True)
    for term in terms:
      origTerm = term
      try:
        if self.has_term(term):
          term = self.replaceTerms(term, paramMap)
        if paramMap.has_key(term):
          termVal = paramMap[term]
        else:
          termVal = self.getMessage(term,paramMap)
        msg = msg.replace("${"+origTerm+"}",termVal)
      except KeyError:
        pass
    return msg
  def keys(self):
    return self.props.keys()
  def has_key(self, key):
    return self.props.has_key(key)
#.........这里部分代码省略.........
开发者ID:chriseppstein,项目名称:pywebmvc,代码行数:103,代码来源:resourcebundle.py


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