Example #1
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
Example #2
0
def _get_headers(syn: Synapse, entity: Union[File, Folder,
                                             Project]) -> List[dict]:
    """Get wiki headers.

    Args:
        syn: Synapse connection
        entity: A Synapse Entity

    Returns:
        List of wiki headers

    """

    try:
        wiki_headers = syn.getWikiHeaders(entity)
    except SynapseHTTPError:
        raise ValueError(f"{entity.name} has no Wiki. Mirroring wikis "
                         "require that both `entity` and `destination` "
                         "have the same wiki structure. If you want to copy "
                         "a wiki page from `entity` to `destination`, you may "
                         "want to use `synapseutils.copyWiki`")
    return wiki_headers