本文整理汇总了Python中properties.Properties.read方法的典型用法代码示例。如果您正苦于以下问题:Python Properties.read方法的具体用法?Python Properties.read怎么用?Python Properties.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类properties.Properties
的用法示例。
在下文中一共展示了Properties.read方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ResourceBundle
# 需要导入模块: from properties import Properties [as 别名]
# 或者: from properties.Properties import read [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)
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from properties import Properties [as 别名]
# 或者: from properties.Properties import read [as 别名]
class InterfaceMain:
#=============================================================================#
# #
# Private methods #
# v #
#=============================================================================#
def __init__(self, filename=None, fullscreen=False, hidemenu=False):
self.filename = None
self.encoding = 'utf-8'
self.line = 0
self.char = 0
self.is_fullscreen = False
self.prev_geometry = (0, 0)
self.properties = Properties()
self.properties.read()
self.recent_files = RecentFiles()
self.recent_files.read()
self.create_widgets()
if filename:
self.filename = filename
else:
self.filename = self.recent_files.filename()
self.line = self.recent_files.line()
self.char = self.recent_files.char()
gobject.idle_add( self.open_file, self.filename )
if fullscreen and gtk.pygtk_version >= (2, 2):
self.on_fullscreen_activate()
self.prev_geometry = (self.properties.window.width,
self.properties.window.height)
if hidemenu:
self.main_menubar.hide()
#self.timeout_cb()
gobject.idle_add(lambda : self.timeout_cb() and False)
gobject.timeout_add(10000, self.timeout_cb)
#self.window.connect('key-release-event', self.key_press_event_cb)
self.window.connect('key-press-event', self.key_press_event_cb)
#=============================================================================#
# #
# Public methods #
# v #
#=============================================================================#
def create_widgets(self):
self.glade_file = GLADE_FILE
self.propsdialog_glade_file = PROPSDIALOG_GLADE_FILE
wTree = glade.XML( self.glade_file, 'main_window')
wTree.signal_autoconnect(self)
self.window = wTree.get_widget('main_window')
top_vbox = wTree.get_widget('top_vbox')
self.main_menubar = wTree.get_widget('main_menubar')
# (un)fullscreen available in PyGTK 2.2 and above
if gtk.pygtk_version < (2, 2):
wTree.get_widget('fullscreen_menu').set_sensitive(False)
#self.open_recent_file_menu = wTree.get_widget('open_recent_file_menu')
self.content_menu = wTree.get_widget('content_menu')
self.background_menu = wTree.get_widget('background_menu')
self.skins_menu = wTree.get_widget('skins_menu')
self.hyphenation_menu = wTree.get_widget('hyphenation_menu')
self.text_widget = ContentArea( self, self.properties )
top_vbox.pack_start( self.text_widget )
self.text_widget.realize()
#text_widget.connect('position-changed', self.position_changed_cb)
#self.text_widget = text_widget
#self.properties.read()
self.window.resize( self.properties.window.width,
self.properties.window.height )
self.update_background_menu()
self.update_skins_menu()
self.update_hyphenation_menu()
self.window.show_all()
self.text_widget.initialize()
#=============================================================================#
def open_file(self, filename):
#print 'open_file', filename
try:
open(filename)
#.........这里部分代码省略.........