Exemplo n.º 1
0
def set_new_metadata(data, other, idlst, metatag, opfdata):
    newmd = []
    datalst = data.split(_RS)
    if datalst[-1] == "":
        datalst = datalst[0:-1]
    pos = 0
    cnt = len(datalst)
    while pos < cnt:
        # always starts with a parent who may or may not have any children
        line = datalst[pos]
        mname = None
        mcontent = None
        id = None
        mattr = OrderedDict()
        (name, value) = line.split(_US)
        mname = name.strip()
        mcontent = value.strip()
        # handle meta tags
        if mname not in _recognized_dc:
            name = mname
            mname = "meta"
            mattr["name"] = name
            mattr["content"] = mcontent
            mcontent = None
        pos += 1
        # process any children
        while pos < cnt:
            line = datalst[pos]
            if not line.startswith(_IN):
                break
            (name, value) = line.split(_US)
            name = name.strip()
            value = value.strip()
            if name in [
                    "id", "xml:lang", "dir", "opf:scheme", "opf:role",
                    "opf:file-as"
            ]:
                if name == "id":
                    id = valid_id(value, idlst)
                    mattr["id"] = id
                else:
                    mattr[name] = value
            else:
                # not sure what this attribute is for so simply add it
                mattr[name] = value
            pos += 1

        # add in the metadata element itself
        newmd.append((mname, mcontent, mattr))

    # rebuild entire metadata section of opf
    res = []
    res.append(metatag)
    for mentry in newmd:
        res.append('  ' + buildxml(mentry))
    res.append(other)
    res.append('</metadata>\n')
    newmetadata = "".join(res)
    newopfdata = _metadata_pattern.sub(newmetadata, opfdata)
    return newopfdata
Exemplo n.º 2
0
def set_new_metadata(data, other, idlst, metatag, opfdata):
    newmd = []
    datalst = data.split(_RS)
    if datalst[-1] == "":
        datalst = datalst[0:-1]
    pos = 0
    cnt = len(datalst)
    while pos < cnt:
        # always starts with a parent who may or may not have any children
        line = datalst[pos]
        mname = None
        mcontent = None
        id = None
        mattr = {}
        (name, value) = line.split(_US)
        mname = name.strip()
        mcontent = value.strip()
        # handle meta tags
        if mname not in _recognized_dc:
            name = mname
            mname = "meta"
            mattr["name"] = name
            mattr["content"] = mcontent
            mcontent = ""
        pos += 1
        # process any children
        while pos < cnt:
            line = datalst[pos]
            if not line.startswith(_IN):
                break
            (name, value) = line.split(_US)
            name = name.strip()
            value = value.strip()
            if name in ["id", "xml:lang", "dir", "opf:scheme", "opf:role", "opf:file-as"]:
                if name == "id":
                    id = valid_id(value, idlst)
                    mattr["id"] = id
                else:
                    mattr[name] = value
            else: 
                # not sure what this attribute is for so simply add it
                mattr[name] = value
            pos += 1

        # add in the metadata element itself
        newmd.append((mname, mcontent, mattr))

    # rebuild entire metadata section of opf
    res = []
    res.append(metatag)
    for mentry in newmd:
        res.append('  ' + buildxml(mentry))
    res.append(other)
    res.append('</metadata>\n')
    newmetadata = "".join(res)
    newopfdata = _metadata_pattern.sub(newmetadata,opfdata)
    return newopfdata
Exemplo n.º 3
0
def set_new_metadata(data, other, idlst, metatag, opfdata):
    newmd = []
    datalst = data.split(_RS)
    if datalst[-1] == "":
        datalst = datalst[0:-1]
    pos = 0
    cnt = len(datalst)
    while pos < cnt:
        # always starts with a parent who may or may not have any children
        line = datalst[pos]
        mname = None
        mcontent = None
        id = None
        mattr = {}
        refines = {}
        (name, value) = line.split(_US)
        mname = name.strip()
        mcontent = value.strip()
        # handle primary metadata
        if mname in _recognized_meta:
            property = mname
            mname = "meta"
            mattr["property"] = property
        pos += 1
        # process any children
        while pos < cnt:
            line = datalst[pos]
            if not line.startswith(_IN):
                break
            (name, value) = line.split(_US)
            name = name.strip()
            value = value.strip()
            if name in ["id", "xml:lang", "dir"]:
                if name == "id":
                    id = valid_id(value, idlst)
                    mattr["id"] = id
                else:
                    mattr[name] = value
            else: 
                # refinement
                refines[name] = value
            pos += 1

        # now ready to rebuild the metadata with separate refinements if needed
        # make sure if refinements needed that a valid id exists
        if refines:
            if not "id" in mattr:
                root = _rec2root.get(mname, "num")
                id = valid_id(root, idlst)
                mattr["id"] = id

        # add in the metadata element itself
        newmd.append((mname, mcontent, mattr))

        # add any needed refinements
        for prop in refines.keys():
            if prop == "scheme":
                continue
            if prop == "altlang":
                continue
            rattr = {}
            rattr["refines"] = "#" + id
            rattr["property"] = prop
            rname = "meta"
            rcontent = refines[prop]
            if prop == "alternate-script":
                if "altlang" in refines:
                    rattr["xml:lang"] = refines["altlang"]
            if prop in ["role", "identifier-type", "title-type","collection-type"]:
                if "scheme" in refines:
                    rattr["scheme"] = refines["scheme"]
            newmd.append((rname, rcontent, rattr))
            
    # rebuild entire metadata section
    res = []
    res.append(metatag)
    for mentry in newmd:
        res.append('  ' + buildxml(mentry))
    res.append(other)
    res.append('</metadata>\n')
    newmetadata = "".join(res)
    newopfdata = _metadata_pattern.sub(newmetadata,opfdata)
    return newopfdata
Exemplo n.º 4
0
def set_new_metadata(data, other, idlst, metatag, opfdata):
    newmd = []
    datalst = data.split(_RS)
    if datalst[-1] == "":
        datalst = datalst[0:-1]
    pos = 0
    cnt = len(datalst)
    while pos < cnt:
        # always starts with a parent who may or may not have any children
        line = datalst[pos]
        mname = None
        mcontent = None
        id = None
        mattr = {}
        refines = {}
        (name, value) = line.split(_US)
        mname = name.strip()
        mcontent = value.strip()
        # handle primary metadata
        if mname in _recognized_meta:
            property = mname
            mname = "meta"
            mattr["property"] = property
        pos += 1
        # process any children
        while pos < cnt:
            line = datalst[pos]
            if not line.startswith(_IN):
                break
            (name, value) = line.split(_US)
            name = name.strip()
            value = value.strip()
            if name in ["id", "xml:lang", "dir"]:
                if name == "id":
                    id = valid_id(value, idlst)
                    mattr["id"] = id
                else:
                    mattr[name] = value
            else:
                # refinement
                refines[name] = value
            pos += 1

        # now ready to rebuild the metadata with separate refinements if needed
        # make sure if refinements needed that a valid id exists
        if refines:
            if not "id" in mattr:
                root = _rec2root.get(mname, "num")
                id = valid_id(root, idlst)
                mattr["id"] = id

        # add in the metadata element itself
        newmd.append((mname, mcontent, mattr))

        # add any needed refinements
        for prop in refines.keys():
            if prop == "scheme":
                continue
            if prop == "altlang":
                continue
            rattr = {}
            rattr["refines"] = "#" + id
            rattr["property"] = prop
            rname = "meta"
            rcontent = refines[prop]
            if prop == "alternate-script":
                if "altlang" in refines:
                    rattr["xml:lang"] = refines["altlang"]
            if prop in [
                    "role", "identifier-type", "title-type", "collection-type"
            ]:
                if "scheme" in refines:
                    rattr["scheme"] = refines["scheme"]
            newmd.append((rname, rcontent, rattr))

    # rebuild entire metadata section
    res = []
    res.append(metatag)
    for mentry in newmd:
        res.append('  ' + buildxml(mentry))
    res.append(other)
    res.append('</metadata>\n')
    newmetadata = "".join(res)
    newopfdata = _metadata_pattern.sub(newmetadata, opfdata)
    return newopfdata