forked from vericast/nbconflux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
58 lines (49 loc) · 2.57 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import argparse
import getpass
import os
import sys
from .api import notebook_to_page
def main(argv=None):
"""Command line interface."""
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description='Converts Jupyter Notebooks to Atlassian Confluence pages using nbconvert',
epilog="Collects credentials from the following locations:\n"
"1. CONFLUENCE_USERNAME and CONFLUENCE_PASSWORD environment variables\n"
"2. ~/.nbconflux file in the format username:password\n"
"3. User prompts")
parser.add_argument('notebook', type=str, help='Path to local notebook (ipynb)')
parser.add_argument('url', type=str, help='URL of Confluence page to update')
parser.add_argument('--exclude-toc', action='store_true', help='Do not generate a table of contents')
parser.add_argument('--exclude-ipynb', action='store_true', help='Do not attach the notebook to the page')
parser.add_argument('--exclude-style', action='store_true', help='Do not include the Jupyter base stylesheet')
parser.add_argument('--include-mathjax', action='store_true', help='Enable MathJax on the page')
args = parser.parse_args(argv or sys.argv[1:])
username = os.getenv('CONFLUENCE_USERNAME')
password = os.getenv('CONFLUENCE_PASSWORD')
cfg = os.path.expanduser('~/.nbconflux')
# Prefer credentials in environment variables
if username and password:
print('Using credentials for {} from environment variables'.format(username))
elif os.path.isfile(cfg):
# Fallback on credentials in a well known file location
with open(cfg) as f:
segs = f.read().strip().split(':', 1)
if len(segs) == 2:
username = segs[0]
password = segs[1]
print('Using credentials for {} from configuration file'.format(username))
# Prompt the user for missing credentials
if username is None:
current = getpass.getuser()
current = current[2:] if current.startswith('p-') else current
username = input('Confluence username ({}): '.format(current))
# Use the current username if the user doesn't enter anything
if not username.strip():
username = current
if password is None:
password = getpass.getpass('Confluence password: ')
notebook_to_page(args.notebook, args.url, username, password,
generate_toc=not args.exclude_toc, attach_ipynb=not args.exclude_ipynb,
enable_style=not args.exclude_style, enable_mathjax=args.include_mathjax)
if __name__ == '__main__':
main()