Skip to content

Commit

Permalink
update canvasapi
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-yj-yang committed Apr 19, 2023
1 parent 9937721 commit 63dfc3e
Show file tree
Hide file tree
Showing 38 changed files with 4,696 additions and 796 deletions.
29 changes: 27 additions & 2 deletions CanvasGroupy/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,34 @@
'git_url': 'https://github.com/FleischerResearchLab/CanvasGroupy',
'lib_path': 'CanvasGroupy'},
'syms': { 'CanvasGroupy.assign': {'CanvasGroupy.assign.assign_groups': ('groupeng_assign.html#assign_groups', 'CanvasGroupy/assign.py')},
'CanvasGroupy.canvas_group': {},
'CanvasGroupy.canvas_group': { 'CanvasGroupy.canvas_group.CanvasGroup': ( 'canvas_group_creation.html#canvasgroup',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.__init__': ( 'canvas_group_creation.html#canvasgroup.__init__',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.assign_canvas_group': ( 'canvas_group_creation.html#canvasgroup.assign_canvas_group',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.auth_canvas': ( 'canvas_group_creation.html#canvasgroup.auth_canvas',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.create_group': ( 'canvas_group_creation.html#canvasgroup.create_group',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.create_group_category': ( 'canvas_group_creation.html#canvasgroup.create_group_category',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.get_course': ( 'canvas_group_creation.html#canvasgroup.get_course',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.get_group_categories': ( 'canvas_group_creation.html#canvasgroup.get_group_categories',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.join_canvas_group': ( 'canvas_group_creation.html#canvasgroup.join_canvas_group',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.set_course': ( 'canvas_group_creation.html#canvasgroup.set_course',
'CanvasGroupy/canvas_group.py'),
'CanvasGroupy.canvas_group.CanvasGroup.set_group_category': ( 'canvas_group_creation.html#canvasgroup.set_group_category',
'CanvasGroupy/canvas_group.py')},
'CanvasGroupy.core': {},
'CanvasGroupy.gh_group': { 'CanvasGroupy.gh_group.cd_cmd': ('gh_group_creation.html#cd_cmd', 'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group': { 'CanvasGroupy.gh_group.GitHubGroup': ( 'gh_group_creation.html#githubgroup',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.GitHubGroup.__init__': ( 'gh_group_creation.html#githubgroup.__init__',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.cd_cmd': ('gh_group_creation.html#cd_cmd', 'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.check_gh_auth': ( 'gh_group_creation.html#check_gh_auth',
'CanvasGroupy/gh_group.py'),
'CanvasGroupy.gh_group.command': ('gh_group_creation.html#command', 'CanvasGroupy/gh_group.py'),
Expand Down
105 changes: 100 additions & 5 deletions CanvasGroupy/canvas_group.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,107 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/03_canvas_group_creation.ipynb.

# %% auto 0
__all__ = []
__all__ = ['CanvasGroup']

# %% ../nbs/03_canvas_group_creation.ipynb 3
from .assign import *
from .gh_group import *
from canvasapi import Canvas
import canvasapi

# %% ../nbs/03_canvas_group_creation.ipynb 4
#| export
# %% ../nbs/03_canvas_group_creation.ipynb 5
class CanvasGroup():
def __init__(self,
API_URL="https://canvas.ucsd.edu", # the domain name of canvas
API_KEY="", # Integrations' API generated by canvas
course_id="" # Course ID, can be found in the course url
):
"Initialize Canvas Group within a Group Set and its appropriate memberships"
self.API_URL = API_URL
self.canvas = None
self.course = None
self.group_category = None
self.group_categories = None
self.groups = None
self.users = None
self.email_to_canvas_id = None

# initialize by the input parameter
if API_KEY != "":
self.auth_canvas(API_KEY)
if course_id != "":
self.set_course(course_id)
self.get_group_categories()

def auth_canvas(self,
API_key: str # the Authenticator key generated from canvas
):
"Authorize the canvas module with API_KEY"
self.canvas = Canvas(self.API_URL, API_KEY)

def set_course(self,
course_id: int # the course id of the target course
):
"Set the target course by the course ID"
self.course = self.canvas.get_course(course_id)
self.users = list(self.course.get_users())
self.email_to_canvas_id = {u.email.split("@")[0]: u.id for u in self.users}

def set_group_category(self,
category_name: str # the target group category
) -> canvasapi.group.GroupCategory: # target group category object
try:
self.group_category = self.group_categories[category_name]
except KeyError:
raise KeyError(f"{category_name} did not found in the group categories."
f"\n Try to create one with CanvasGroup.create_group_category")
return self.group_category

def get_course(self):
return self.course

def get_group_categories(self) -> dict: # return a name / group category object
"Grab all existing group category (group set) in this course"
categories = list(self.course.get_group_categories())
self.group_categories = {cat.name: cat for cat in categories}
return {cat.name: cat for cat in categories}

def create_group_category(self,
params: dict # the parameter of canvas group category API @ [this link](https://canvas.instructure.com/doc/api/group_categories.html#method.group_categories.create)
) -> canvasapi.group.GroupCategory: # the generated group category object
"Create group category (group set) in this course"
self.group_category = self.course.create_group_category(**params)
return self.group_category

def create_group(self,
params: dict, #the parameter of canvas group create API at [this link](https://canvas.instructure.com/doc/api/groups.html#method.groups.create)
) -> canvasapi.group.Group: # the generated target group object
"Create canvas group under the target group category"
if self.group_category is None:
raise ValueError("Have you specified or create a group category (group set)?")
return self.group_category.create_group(**params)

def join_canvas_group(self,
group: canvasapi.group.Group, # the group that students will join
group_members:[str], # list of group member's SIS Login (email prefix, before the @.)
) -> [str]: # list of unsuccessful join
"Add membership access of each group member into the group"
unsuccessful_join = []
for group_member in group_members:
try:
canvas_id = self.email_to_canvas_id[group_member]
except KeyError:
unsuccessful_join.append(group_member)
print(f"Error adding student {group_member} \n into group {group.name}")
group.create_membership(canvas_id)
return unsuccessful_join

def assign_canvas_group(self,
group_name: str, # group name, display on canvas
group_members:[str], # list of group member's SIS Login
in_group_category: str, # specify which group category the group belongs to
) -> [str]: # list of unsuccessful join
"Create new groups and assign group member into the class in the `self.group_category`"
self.set_group_category(in_group_category)
group = self.create_group({"name": group_name})
unsuccessful_join = self.join_canvas_group(group, group_members)
return unsuccessful_join

4 changes: 0 additions & 4 deletions CanvasGroupy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@
# %% ../nbs/00_core.ipynb 3
from .assign import *
from .gh_group import *

# %% ../nbs/00_core.ipynb 4
#| export

31 changes: 21 additions & 10 deletions CanvasGroupy/gh_group.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/02_gh_group_creation.ipynb.

# %% auto 0
__all__ = ['command', 'check_gh_auth', 'git_creat_repo_cmd', 'git_manage_access_cmd', 'cd_cmd', 'git_rename_cmd', 'git_commit',
'git_add_all', 'delete_directory']
__all__ = ['GitHubGroup', 'command', 'check_gh_auth', 'git_creat_repo_cmd', 'git_manage_access_cmd', 'cd_cmd', 'git_rename_cmd',
'git_commit', 'git_add_all', 'delete_directory']

# %% ../nbs/02_gh_group_creation.ipynb 3
from github import Github

# %% ../nbs/02_gh_group_creation.ipynb 4
class GitHubGroup():
def __init__(self,
token="",
org=""
):
return ...

# %% ../nbs/02_gh_group_creation.ipynb 8
def command(cmd: str # command to call in bash
) -> (bool, str): # success, output
"Execute the given command in shell"
Expand All @@ -21,15 +32,15 @@ def command(cmd: str # command to call in bash
print(output.decode())
return success, output

# %% ../nbs/02_gh_group_creation.ipynb 4
# %% ../nbs/02_gh_group_creation.ipynb 9
def check_gh_auth() -> bool: # whether you have authenticate
"Check whether you have gh auth configured"
msg = command("gh auth status")[1]
if "You are not logged into any GitHub hosts" in str(msg):
return False
return True

# %% ../nbs/02_gh_group_creation.ipynb 5
# %% ../nbs/02_gh_group_creation.ipynb 10
def git_creat_repo_cmd(repo_name:str, # the name of the created repository
repo_org: str, # the GitHub organization name
template_repo:str, # the template repository that the new repo will use
Expand All @@ -44,7 +55,7 @@ def git_creat_repo_cmd(repo_name:str, # the name of the created repository
cmd = f"{cmd} --clone {repo_org}/{repo_name}"
return cmd

# %% ../nbs/02_gh_group_creation.ipynb 7
# %% ../nbs/02_gh_group_creation.ipynb 12
def git_manage_access_cmd(repo_name:str, # the name of the created repository
repo_org: str, # the GitHub organization name that the repo belongs to
collaorator_id: str, # the GitHub id of the collaborator, or team name if `add_team=True`
Expand All @@ -59,30 +70,30 @@ def git_manage_access_cmd(repo_name:str, # the name of the created repository
return cmd


# %% ../nbs/02_gh_group_creation.ipynb 10
# %% ../nbs/02_gh_group_creation.ipynb 15
def cd_cmd(target_dir: str # the target directory we want to change to
) -> str: # the command of cd
"Unix command for change directory"
return f"cd {target_dir}"

# %% ../nbs/02_gh_group_creation.ipynb 11
# %% ../nbs/02_gh_group_creation.ipynb 16
def git_rename_cmd(source: str, # the original name of the file
target: str # the target name of the file
) -> str: # the command of git rename file
"GitHub mv command to rename file in the directory"
return f"git mv {source} {target}"

# %% ../nbs/02_gh_group_creation.ipynb 12
# %% ../nbs/02_gh_group_creation.ipynb 17
def git_commit(commit_msg: str, # Commit Message
) -> str: # the command for git commit
"GitHub commit command"
return f'git commit -m "{commit_msg}"'

# %% ../nbs/02_gh_group_creation.ipynb 13
# %% ../nbs/02_gh_group_creation.ipynb 18
def git_add_all():
return "git add -A"

# %% ../nbs/02_gh_group_creation.ipynb 14
# %% ../nbs/02_gh_group_creation.ipynb 19
def delete_directory(dir_name: str # the directory that we want to delete
) -> str: # the command for delete dir
return f"rm -rf {dir_name}"
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
CanvasGroupy
================
# CanvasGroupy

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Expand Down
32 changes: 32 additions & 0 deletions _proc/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@
"source": [
"<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"language": "python"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"> gh auth status\n",
"usage: gh [-h] [--home] [-p] [-b] [-s] [-r] [-t] [-c] [-w] [-i] [-d] [-v]\n",
"gh: error: unrecognized arguments: auth status\n",
"\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"check_gh_auth()"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 63dfc3e

Please sign in to comment.