Example #1
0
def _get_wikipages_and_mapping(
        syn: Synapse, entity: Union[File, Folder, Project],
        destination: Union[File, Folder, Project]) -> dict:
    """Get entity/destination pages and mapping of wiki pages

    Args:
        syn: Synapse connection
        entity: Synapse File, Project, Folder Entity or Id with
                Wiki you want to copy
        destination: Synapse File, Project, Folder Entity or Id
                     with Wiki that matches entity

    Returns:
        {'entity_wiki_pages': {'title': synapseclient.Wiki}
         'destination_wiki_pages': {'title': synapseclient.Wiki}
         'wiki_mapping': {'wiki_id': 'dest_wiki_id'}}

    """
    entity_wiki = _get_headers(syn, entity)
    destination_wiki = _get_headers(syn, destination)

    entity_wiki_pages = {}
    for wiki in entity_wiki:
        entity_wiki = syn.getWiki(entity, wiki['id'])
        entity_wiki_pages[wiki['title']] = entity_wiki

    # Mapping dictionary containing wiki page mapping between
    # entity and destination
    wiki_mapping = {}
    destination_wiki_pages = {}
    for wiki in destination_wiki:
        destination_wiki = syn.getWiki(destination, wiki['id'])
        destination_wiki_pages[wiki['title']] = destination_wiki
        # Only map wiki pages that exist in `entity` (source)
        if entity_wiki_pages.get(wiki['title']) is not None:
            wiki_mapping[entity_wiki_pages[wiki['title']].id] = wiki['id']
        else:
            logger.info("Title exists at destination but not in "
                        f"entity: {wiki['title']}")

    return {
        'entity_wiki_pages': entity_wiki_pages,
        'destination_wiki_pages': destination_wiki_pages,
        'wiki_mapping': wiki_mapping
    }
Example #2
0
def push_wiki(syn: Synapse,
              projectid: str,
              workdir: str = "./") -> typing.List[dict]:
    """Pushes Wiki from configuration

    Args:
        syn: Synapse connection
        project: synapseclient.Project or its id
        workdir: Location to download markdown files and wiki_config.json.
                 Defaults to location of where code is being
                 executed.

    Returns:
        Wiki Configuration::

            [
                {
                    "id": "111",
                    "title": "title",
                    "parentId": "33333",
                    "markdown_path": "home.md"
                },
                {...}
            ]

    """
    wiki_config = validate_config(workdir)
    for wiki_header in wiki_config:
        # no markdown path, nothing to update
        markdown_path = wiki_header.get('markdown_path')
        if not wiki_header.get('markdown_path'):
            print(f"Markdown not specified: {wiki_header['title']}")
            continue
        markdown_path = os.path.join(workdir, markdown_path)
        with open(markdown_path, 'r') as md_f:
            markdown = md_f.read()
        # Create new wiki page if id isn't specified
        if wiki_header.get('id') is not None:
            wiki = syn.getWiki(projectid, subpageId=wiki_header['id'])
            # Don't store if the wiki pages are the same
            if wiki.markdown == markdown:
                print(f"no updates: {wiki_header['title']}")
                continue
            print(f"Wiki updated: {wiki_header['title']}")
        else:
            wiki = synapseclient.Wiki(owner=projectid,
                                      title=wiki_header['title'],
                                      parentWikiId=wiki_header['parentId'])
            print(f"Wiki added: {wiki_header['title']}")
        wiki.markdown = markdown
        wiki = syn.store(wiki)
        # If new wiki page is added, must add to wiki_config.json
        wiki_header['id'] = wiki['id']
    return wiki_config
Example #3
0
def pull_wiki(syn: Synapse,
              project: str,
              workdir: str = "./") -> typing.List[dict]:
    """Downloads each wikipage's content into a markdown file and
    stores a configuration file

    Args:
        syn: Synapse connection
        project: synapseclient.Project or its id
        workdir: Location to download markdown files and wiki_config.json
                 into. Defaults to location of where code is being
                 executed.

    Returns:
        Wiki Configuration::

            [
                {
                    "id": "111",
                    "title": "homepage",
                    "markdown_path": "111-homepage.md"
                },
                {...}
            ]

    """
    projectid = synapseclient.core.utils.id_of(project)
    wiki_headers = syn.getWikiHeaders(projectid)
    for wiki_header in wiki_headers:
        wiki = syn.getWiki(projectid, subpageId=wiki_header['id'])
        # Convert all special characters to underscore
        # This way markdown paths don't have special characters
        # and json file can be written without encoding
        clean_title = ''.join(letter for letter in wiki['title']
                              if letter.isalnum())
        # Home page title is always blank
        if clean_title == '':
            clean_title = 'homepage'
            wiki_header['title'] = clean_title
        # The wiki id is added to the markdown path because wiki ids are
        # unique, but wiki titles don't have to be
        markdown_path = os.path.join(workdir, f"{wiki.id}-{clean_title}.md")
        with open(markdown_path, 'w') as md_file:
            md_file.write(wiki['markdown'])
        wiki_header['markdown_path'] = f"{wiki.id}-{clean_title}.md"
    return wiki_headers