-
Notifications
You must be signed in to change notification settings - Fork 0
/
describe.py
63 lines (54 loc) · 2.43 KB
/
describe.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
59
60
61
62
63
import sys
import argparse
from pathlib import Path
from api import get_course, split_url, strip_folder_name
def parse_args(args):
# help text and argument parser
# solution based on https://stackoverflow.com/a/24181138/462692
desc = '\n'.join(["Describe the elements of a course.",
"...",
"An optional argument -c/--config_file can be used with the path to the config file. "
"Otherwise the default config file '~/.config/canvasapi.conf' will be used.\n"
])
parser = argparse.ArgumentParser(description=desc)
required_named = parser.add_argument_group('required named arguments')
required_named.add_argument("-u", "--url", help="The url of the course, ending with the course id", required = True)
parser.add_argument("-cf", "--config_file", help="Path to config file", default = '~/.config/canvasapi.conf')
args = parser.parse_args(args)
return args
def is_published_txt(published):
if published:
return "Published"
else:
return "Unpublished"
def describe_module(module):
"""
Extracts information from all items asssociated
with a module and prints them.
"""
for module_item in module.get_module_items():
item_data = []
# collect defaults
item_data.append(module_item.type)
item_data.append(module_item.title)
item_data.append(module_item.position)
item_data.append(is_published_txt(module_item.published))
if module_item.type != "SubHeader":
item_data.append(module_item.html_url)
elif module_item.type in ["ExternalUrl", "ExternalTool"]:
item_data.append(module_item.external_url)
elif module_item.type in ['File', 'Discussion', 'Assignment', 'Quiz', 'ExternalTool']:
# these have a another ID, the content_id
item_data.append(module_item.content_id)
print("\t", "\t".join([str(item) for item in item_data]))
def main(args):
args = parse_args(args)
# extract course information from url and get course
API_URL, course_id, item_name, url_type = split_url(args.url)
course = get_course(API_URL, course_id, args.config_file)
# find the module, if any
for module in course.get_modules():
print(module.name, module.position, is_published_txt(module.published), module.id)
describe_module(module)
if __name__ == "__main__":
main(sys.argv[1:])