Ejemplo n.º 1
0
 def test_model_to_portaltype(self):
     """Model types to legacy portal type names."""
     from ..publish import _model_to_portaltype as target
     document = cnxepub.Document.__new__(cnxepub.Document)
     self.assertEqual(target(document), 'Module')
     binder = cnxepub.Binder('fooBinder')  # need init to set metadata
     self.assertEqual(target(binder), 'Collection')
     ugly = object()
     with self.assertRaises(ValueError):
         target(ugly)
Ejemplo n.º 2
0
def tree_to_nodes(tree, context=None, metadata=None):
    """Assembles ``tree`` nodes into object models.
    If ``context`` is supplied, it will be used to contextualize
    the contents of the nodes. Metadata will pass non-node identifying
    values down to child nodes, if not overridden (license, timestamps, etc)
    """
    nodes = []
    for item in tree['contents']:
        if 'contents' in item:
            sub_nodes = tree_to_nodes(item, context=context, metadata=metadata)
            if metadata is None:
                metadata = {}
            else:
                metadata = metadata.copy()
                for key in ('title', 'id', 'shortid', 'cnx-archive-uri',
                            'cnx-archive-shortid'):
                    if key in metadata:
                        metadata.pop(key)

            for key in ('title', 'id', 'shortId'):
                if item.get(key):
                    metadata[key] = item[key]
                    if item[key] != 'subcol':
                        if key == 'id':
                            metadata['cnx-archive-uri'] = item[key]
                        elif key == 'shortId':
                            metadata['cnx-archive-shortid'] = item[key]

            titles = _title_overrides_from_tree(item)
            if item.get('id') is not None:
                tbinder = cnxepub.Binder(item.get('id'),
                                         sub_nodes,
                                         metadata=metadata,
                                         title_overrides=titles)
            else:
                tbinder = cnxepub.TranslucentBinder(sub_nodes,
                                                    metadata=metadata,
                                                    title_overrides=titles)
            nodes.append(tbinder)
        else:
            doc = document_factory(item['id'], context=context)
            for key in ('title', 'id', 'shortId'):
                if item.get(key):
                    doc.metadata[key] = item[key]
                    if key == 'id':
                        doc.metadata['cnx-archive-uri'] = item[key]
                    elif key == 'shortId':
                        doc.metadata['cnx-archive-shortid'] = item[key]
            nodes.append(doc)
    return nodes
Ejemplo n.º 3
0
def binder_factory(ident_hash, baked=False):
    metadata = get_metadata(ident_hash)
    tree = get_tree(ident_hash, baked=baked)
    if baked:
        context = tree['id']
    else:
        context = None
    nodes = tree_to_nodes(tree, context, metadata)
    titles = _title_overrides_from_tree(tree)
    resources = [
        resource_factory(h, ident_hash)
        for h in get_registered_files(ident_hash)
    ]
    binder = cnxepub.Binder(metadata['id'],
                            nodes=nodes,
                            metadata=metadata,
                            resources=resources,
                            title_overrides=titles)
    return binder
Ejemplo n.º 4
0
def tree_to_nodes(tree, context=None):
    """Assembles ``tree`` nodes into object models.
    If ``context`` is supplied, it will be used to contextualize
    the contents of the nodes.

    """
    nodes = []
    for item in tree['contents']:
        if 'contents' in item:
            sub_nodes = tree_to_nodes(item, context=context)
            titles = _title_overrides_from_tree(item)
            metadata = {}
            for key in ('title', 'id', 'shortId'):
                if item.get(key):
                    metadata[key] = item[key]
                    if item[key] != 'subcol':
                        if key == 'id':
                            metadata['cnx-archive-uri'] = item[key]
                        elif key == 'shortId':
                            metadata['cnx-archive-shortid'] = item[key]
            if item.get('id') is not None:
                tbinder = cnxepub.Binder(item.get('id'),
                                         sub_nodes,
                                         metadata=metadata,
                                         title_overrides=titles)
            else:
                tbinder = cnxepub.TranslucentBinder(sub_nodes,
                                                    metadata=metadata,
                                                    title_overrides=titles)
            nodes.append(tbinder)
        else:
            doc = document_factory(item['id'], context=context)
            for key in ('title', 'id', 'shortId'):
                if item.get(key):
                    doc.metadata[key] = item[key]
                    if key == 'id':
                        doc.metadata['cnx-archive-uri'] = item[key]
                    elif key == 'shortId':
                        doc.metadata['cnx-archive-shortid'] = item[key]
            nodes.append(doc)
    return nodes
Ejemplo n.º 5
0
BOOK = cnxepub.Binder(
    id='94f4d0f5@draft',
    metadata={
        u'title':
        u'Book of Infinity',
        u'created':
        u'2013/03/19 15:01:16 -0500',
        u'revised':
        u'2013/03/19 15:01:16 -0500',
        u'keywords': [],
        u'language':
        u'en',
        u'license_text':
        u'CC-By 4.0',
        u'license_url':
        u'http://creativecommons.org/licenses/by/4.0/',
        u'subjects': [
            u'Mathematics and Statistics',
            u'Science and Technology',
        ],
        u'authors': [{
            u'id': u'marknewlyn',
            u'name': u'Mark Horner',
            u'type': u'cnx-id'
        }, {
            u'id': u'charrose',
            u'name': u'Charmaine St. Rose',
            u'type': u'cnx-id'
        }],
        u'copyright_holders': [{
            u'id': u'ream',
            u'name': u'Ream',
            u'type': u'cnx-id'
        }],
        u'editors': [{
            u'id': u'impicky',
            u'name': u'I. M. Picky',
            u'type': u'cnx-id'
        }],
        u'illustrators': [{
            u'id': u'frahablar',
            u'name': u'Francis Hablar',
            u'type': u'cnx-id'
        }],
        u'publishers': [{
            u'id': u'ream',
            u'name': u'Ream',
            u'type': u'cnx-id'
        }, {
            u'id': u'rings',
            u'name': u'Rings',
            u'type': u'cnx-id'
        }],
        u'translators': [{
            u'id': u'frahablar',
            u'name': u'Francis Hablar',
            u'type': u'cnx-id'
        }],
        u'summary':
        "<span xmlns='http://www.w3.org/1999/xhtml'>Book summary</span>",
        u'print_style':
        None,
    },
    nodes=[
        cnxepub.TranslucentBinder(
            metadata={u'title': u'Part One'},
            nodes=[
                cnxepub.TranslucentBinder(
                    metadata={u'title': u'Chapter One'},
                    title_overrides=['Document One'],
                    nodes=[
                        cnxepub.Document(
                            id=u'2cf4d7d3@draft',
                            data=
                            u'<p class="para">If you finish the book, there will be cake.</p><img src="../resources/{}">'
                            .format(RESOURCE_ONE_FILENAME),
                            resources=[
                                cnxepub.Resource(
                                    RESOURCE_ONE_FILENAME,
                                    _read_file(RESOURCE_ONE_FILEPATH, 'rb'),
                                    'image/png',
                                    filename=RESOURCE_ONE_FILENAME)
                            ],
                            metadata={
                                u'title':
                                u'Document One of Infinity',
                                u'created':
                                u'2013/03/19 15:01:16 -0500',
                                u'revised':
                                u'2013/03/19 15:01:16 -0500',
                                u'keywords': [u'South Africa'],
                                u'subjects': [
                                    u'Mathematics and Statistics',
                                ],
                                u'summary':
                                u"<span xmlns='http://www.w3.org/1999/xhtml'>descriptive text</span>",
                                u'language':
                                u'en',
                                u'license_text':
                                u'CC-By 4.0',
                                u'license_url':
                                u'http://creativecommons.org/licenses/by/4.0/',
                                u'authors': [{
                                    u'id': u'marknewlyn',
                                    u'name': u'Mark Horner',
                                    u'type': u'cnx-id'
                                }, {
                                    u'id': u'charrose',
                                    u'name': u'Charmaine St. Rose',
                                    u'type': u'cnx-id'
                                }, {
                                    u'id': u'sarblyth',
                                    u'name': u'Sarah Blyth',
                                    u'type': u'cnx-id'
                                }],
                                u'copyright_holders': [{
                                    u'id': u'ream',
                                    u'name': u'Ream',
                                    u'type': u'cnx-id'
                                }],
                                u'editors': [{
                                    u'id': u'impicky',
                                    u'name': u'I. M. Picky',
                                    u'type': u'cnx-id'
                                }],
                                u'illustrators': [{
                                    u'id': u'frahablar',
                                    u'name': u'Francis Hablar',
                                    u'type': u'cnx-id'
                                }],
                                u'publishers': [{
                                    u'id': u'ream',
                                    u'name': u'Ream',
                                    u'type': u'cnx-id'
                                }, {
                                    u'id': u'rings',
                                    u'name': u'Rings',
                                    u'type': u'cnx-id'
                                }],
                                u'translators': [{
                                    u'id': u'frahablar',
                                    u'name': u'Francis Hablar',
                                    u'type': u'cnx-id'
                                }],
                                u'print_style':
                                u'*print style*',
                            }),
                    ]),
            ]),
    ])