vericast / nbconflux

nbconflux converts Jupyter Notebooks to Atlassian Confluence pages
BSD 3-Clause "New" or "Revised" License
119 stars 33 forks source link

Find page by space and title #29

Closed danizen closed 5 years ago

danizen commented 5 years ago

I have an idea to find the id of the page by space (key or title) and by page title. For a new page, the parent title could be given.

This would add a dependence (if I were to implement it) on jmespath. A search would have the following inputs:

A method for an exact search:

def find_page(session, baseurl, **kwargs):
    cql_query_template = (
         'type=page'
         ' and (space="{space:s}" OR space.title~"{space:s}")'
         ' and title="{title:s}"')
    cql_query = cql_query_template.format(**kwargs)
    response = session.get(baseurl + '/search', params={'cql': cql_query})
    if not response.ok:
        raise Something()
    response_data = response.json()
    if response_data['size'] != 1:
        return None
    page_id = jmespath.search('results[0].content.id', response_data)
    page_link = baseurl + '/content/' + page_id
    return page_link

This could be generalized to support multiple "strategies", each a function taking the session, baseurl, and kwargs. This makes the search function more maintainable:

def cql_search_strategy(cql_query_template):
    def _strategy(session, baseurl, **kwargs):
        nonlocal cql_query_template
        cql_query = cql_query_template.format(**kwargs)
        response = session.get(baseurl + '/search', params={'cql': cql_query})
        # ....
    return _strategy

exact_title_strategy = cql_search_strategy('type=page and (space="{space:s}" OR space.title~"{space:s}") and title="{title:s}"')
title_contains_startegy = cql_search_strategy('type=page and (space="{space:s}" OR space.title~"{space:s}") and title~"{title:s}"')
danizen commented 5 years ago

Now that I am more up-to-speed on the existing issues #3 and #4, I think it is more natural to have a wrapper script that calls nbconflux.api.notebook_to_page appropriately for my environment.

The search strategies would not be simple at all - what should the parent page be? In my corporate environment I may be able to specify the parent page for all my notebooks, but what about in some other environment.