Example #1
0
def changeValue(key, value, text):
    """
    Return text which value of given key is changed.
    """
    text = text.strip()
    text, changes = re2.subn(u"(?m)^\* %s: (.*?)$" % re2.escape(key),
                            u"* %s: %s" % (re2.escape(key), value),
                            text)
    # don't check that newtext is equal to oldtext because if newvalue is 
    # equal oldvalue, it will duplicate that key.
    if changes == 0:
        if text:
            text = text + u"\n* " + key + u": " + value
        else:
            text = u"* " + key + u": " + value
    
    return text
Example #2
0
def extract(key, text):
    """
    Extract key from text. Note that both key and text must be enunicode
    strings.
    """
    if key:
        try:
            dat = preload.enunicode(re2.find(u"(?m)^\* " + re2.escape(key) + 
                                    u": (.*?)$", text, 1))
        except AttributeError:
            dat = None
    else:
        lines = text.strip().split(u"\n")
        dat = {}
        for line in lines:
            key, value = line.split(u": ")
            dat[key] = value
            
    return dat