Skip to content

Commit

Permalink
un hardcode the URL opttion + bug fix (#12)
Browse files Browse the repository at this point in the history
* fixed case sensitivity issue in boatwain

* updated url option

* fixes
  • Loading branch information
j-hui authored Jun 24, 2019
1 parent 7c13c2e commit 3ccce4c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ Boatswain will be expanded to support arbitrary arguments in this config file
which could save you some typing when running commands, but for now just accept
that it's a little overkill.

If you don't need either one of Canvas or GitHub integration, you can just
answer "no" when it asks if you'd like to configure one or the other. If you
answer "yes", make sure you have the respective auth tokens ready.

When configuring Canvas, it will also ask for a URL. You should pass in the URL
that corresponds to the host name where Canvas is hosted. For example, for
Columbia, you should enter:

https://courseworks2.columbia.edu/

If you are using a version of Canvas Wrangler from before May 14, 2019, you may
also add the url configuration to your `boatswain.ini` manually, e.g.:

[canvas]
token = <token>
url = https://courseworks2.columbia.edu/


Canvas Wrangler
===============
Expand Down
53 changes: 43 additions & 10 deletions boatswain_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3

import configparser
import os
Expand All @@ -10,6 +10,7 @@

CANVAS_SECTION = 'canvas'
CANVAS_TOKEN = 'token'
CANVAS_URL = 'url'

GITHUB_SECTION = 'github'
GITHUB_TOKEN = 'token'
Expand All @@ -33,6 +34,9 @@ def __init__(self, path=DEFAULT_INI_PATH):
def canvasToken(self):
return self.get(CANVAS_SECTION, CANVAS_TOKEN)

def canvasUrl(self):
return self.get(CANVAS_SECTION, CANVAS_URL)

def githubToken(self):
return self.get(GITHUB_SECTION, GITHUB_TOKEN)

Expand All @@ -50,6 +54,11 @@ def canvasToken(self):
return self.config.canvasToken()
return self.canvas_token

def canvasUrl(self):
if self.canvas_url is None:
return self.config.canvasUrl()
return self.canvas_url

def githubToken(self):
if self.github_token is None:
return self.config.githubToken()
Expand Down Expand Up @@ -161,9 +170,6 @@ def ParseOption(
help='debug mode; enable all output',
)




config = BoatswainConfig(config_path)

if req_canvas:
Expand All @@ -175,15 +181,36 @@ def ParseOption(
help='override Boatswain Canvas token',
metavar='<canvas-token>',
)
# handle incomplete config; don't handle NoSectionError because we
# assume that the canvas and github sections at least exist
except configparser.NoOptionError:
print('[WARN]: You appear to be missing a Canvas token in your '
+ 'Boatswain configuration ({}). Please add this information '
+ 'to your config file under section [canvas] with key "token".'
+ '\n'.format(config_path))
parser.add_argument('canvas_token',
type=str,
help='Canvas LMS auth token',
metavar='<canvas-token>',
)

try:
canvasUrl = config.canvasUrl()
parser.add_argument('--canvas-url',
default=canvasUrl,
type=str,
help='override Boatswain Canvas URL',
metavar='<canvas-url>',
)
except configparser.NoOptionError:
print('[WARN]: You appear to be missing the Canvas URL in your '
+ 'Boatswain configuration ({}). Please add this information '
+ 'to your config file under section [canvas] with key "url".'
+ '\n'.format(config_path))
parser.add_argument('canvas_url',
type=str,
help='Canvas LMS URL',
metavar='<canvas-token>',
)

if req_github:
try:
githubToken = config.githubToken()
Expand All @@ -193,9 +220,11 @@ def ParseOption(
help='override Boatswain GitHub token',
metavar='<github-token>',
)
# handle incomplete config; don't handle NoSectionError because we
# assume that the canvas and github sections at least exist
except configparser.NoOptionError:
print('[WARN]: You appear to be missing a GitHub token in your '
+ 'Boatswain configuration ({}). Please add this information '
+ 'to your config file under section [github] with key "token".'
+ '\n'.format(config_path))
parser.add_argument('github_token',
type=str,
help='GitHub auth token',
Expand Down Expand Up @@ -241,15 +270,19 @@ def newPopulatedConfigInteractive():
config.add_section(CANVAS_SECTION)
config.add_section(GITHUB_SECTION)

i = itv.promptSelect('Configure Canvas token?', ['n'], default='y')
i = itv.promptSelect('Configure Canvas?', ['n'], default='y')
if i == 'y':
itv.output('You can generate a new Canvas token by going to your '
'Canvas Profile -> Settings -> Approved Integrations '
'and clicking on "+New Access Token"')

canvasUrl = itv.promptInput('Enter Canvas URL')
config.set(CANVAS_SECTION, CANVAS_URL, canvasUrl)

canvasToken = itv.promptInput('Enter Canvas auth token')
config.set(CANVAS_SECTION, CANVAS_TOKEN, canvasToken)

i = itv.promptSelect('Configure GitHub token?', ['n'], default='y')
i = itv.promptSelect('Configure GitHub?', ['n'], default='y')
if i == 'y':
githubToken = itv.promptInput('Enter GitHub auth token')
config.set(GITHUB_SECTION, GITHUB_TOKEN, githubToken)
Expand Down
4 changes: 1 addition & 3 deletions canvas-wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def wrangle_canvas(opt):

grade_data = build_grade_data(grades, student_i, grade_i, comment_i, opt)

canvasURL = 'https://courseworks2.columbia.edu/' # TODO: don't hard

canvas = Canvas(canvasURL, opt.canvasToken())
canvas = Canvas(opt.canvasUrl(), opt.canvasToken())
course = canvas.get_course(opt.course_id)
assignment = course.get_assignment(opt.assignment_id)

Expand Down
26 changes: 13 additions & 13 deletions interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def promptInput(prompt, fmt='', default=''):

while True:
inp = input(promptf)
inp = inp.lower()
if inp != '':
return inp
elif default != '' and inp == '':
Expand All @@ -35,6 +34,18 @@ def promptValidate(prompt, validator, fmt='', default=''):
output('Invalid input (case-insensitive): {}'.format(v))


def selectorValidator(options, default=''):
def validator(inp):
i = inp.lower()
if default != '':
if i == default.lower() or i == '':
return ''
if i in options:
return ''
return i
return validator


'''
e.g. promptInput('Do you want to continue?', ['n'], default='y')
input with validation
Expand All @@ -52,18 +63,7 @@ def promptSelect(prompt, alts, default=''):
validator = selectorValidator(options, default=default)
inp = promptValidate(prompt, validator, fmt=fmt, default=default)

return inp


def selectorValidator(options, default=''):
def validator(inp):
if default != '':
if inp == default.lower() or inp == '':
return ''
if inp in options:
return ''
return inp
return validator
return inp.lower()


def newFileValidator():
Expand Down

0 comments on commit 3ccce4c

Please sign in to comment.