예제 #1
0
def clean(namespace):
    """
    Returns a clean VERSION 1.1 rendition of a namespace.

    Data is seperated into four blocks: X, LN, PATTERN, and NS.
    Within blocks, original order is preserved.
    Standard X keys are handled particularly and intelligently.
    X LAST-CHANGED is updated to the present time.
    """
    text = []

    def show_wo_repeating(key):
        """Write X records, but don't repeat any values."""
        if key not in namespace["X"]:
            return
        vals = namespace["X"][key]
        shown = []
        for val in vals:
            if val not in shown:
                shown.append(val)
                text.append(u'X %s %s\n' % (key, lnparser.escape(val)))
    
    text.append(u'X VERSION 1.1\n')
    text.append(u'X LAST-CHANGED %s\n' % isotime())
    show_wo_repeating("PREFERRED-NAME")
    show_wo_repeating("AUTHOR")
    show_wo_repeating("AUTHOR-FOAF")
    if "FINAL" in namespace["X"]:
        final = lnparser.escape(namespace["X"]["FINAL"][0])
        text.append(u'X FINAL %s\n' % final)

    for record_type in ["X", "LN", "PATTERN", "NS"]:
        covered = []
        for line_num, second, third in namespace[record_type+"-raw"]:
            if record_type == "X":
                if second in standard_X_keys:
                    continue
            elif second in covered:
                continue
            covered.append(second)
            text.append(u'%s %s %s\n' % (record_type,
                                         lnparser.escape(second),
                                         lnparser.escape(third)))
        if len(namespace[record_type+"-raw"]) > 0:
            text.append(u'\n')

    return ''.join(text)
예제 #2
0
 def show_wo_repeating(key):
     """Write X records, but don't repeat any values."""
     if key not in namespace["X"]:
         return
     vals = namespace["X"][key]
     shown = []
     for val in vals:
         if val not in shown:
             shown.append(val)
             text.append(u'X %s %s\n' % (key, lnparser.escape(val)))