diff --git a/canvas_sync.py b/canvas_sync.py index 6514a581366de2123b078e6bea3134faf5cddc50..3b39be0324ccf94a43a63281630ccbb02dcff8b8 100644 --- a/canvas_sync.py +++ b/canvas_sync.py @@ -14,37 +14,40 @@ API_URL = "https://canvas.education.lu.se/" API_TOKEN = os.environ["CANVAS_TOKEN"] CANVAS_COURSE_CODE = os.environ["CANVAS_COURSE_CODE"] -canvas = Canvas(API_URL, API_TOKEN) -course = canvas.get_course(CANVAS_COURSE_CODE) +if os.path.isdir(GITLAB_FOLDER_NAME): + canvas = Canvas(API_URL, API_TOKEN) + course = canvas.get_course(CANVAS_COURSE_CODE) -base_folder = next(folder for folder in course.get_folders() - if folder.full_name == CANVAS_BASE_PATH) -pdf_folder = next((folder for folder in course.get_folders() - if folder.full_name == "/".join([CANVAS_BASE_PATH, - CANVAS_FOLDER_NAME])), None) -# Check if folder exists, otherwise create it -if pdf_folder is None: - pdf_folder = base_folder.create_folder(CANVAS_FOLDER_NAME) + base_folder = next(folder for folder in course.get_folders() + if folder.full_name == CANVAS_BASE_PATH) + pdf_folder = next((folder for folder in course.get_folders() + if folder.full_name == "/".join([CANVAS_BASE_PATH, + CANVAS_FOLDER_NAME])), None) + # Check if folder exists, otherwise create it + if pdf_folder is None: + pdf_folder = base_folder.create_folder(CANVAS_FOLDER_NAME) -# Recurse through all subfolder and upload pdfs to corresponding -folders = [(pdf_folder, ())] -while len(folders) > 0: - folder, path = folders.pop() - ospath = os.path.join(GITLAB_FOLDER_NAME, *path) - print("Searching {}".format(ospath)) - for f in os.listdir(ospath): - if f == ".gitkeep": - continue - gitlabpath = os.path.join(ospath, f) - if os.path.isdir(gitlabpath): - canvaspath = "/".join([CANVAS_BASE_PATH, CANVAS_FOLDER_NAME, *path, f]) - nfolder = next((folder for folder in course.get_folders() - if folder.full_name == canvaspath), None) - print("{} is a folder, found {} on canvas".format(gitlabpath, nfolder)) - if nfolder is None: - print("Creating new canvas folder.") - nfolder = folder.create_folder(f) - folders.append((nfolder, tuple([*path, f]))) - else: - print("{} is a file, uploading!".format(gitlabpath)) - folder.upload(gitlabpath) + # Recurse through all subfolder and upload pdfs to corresponding + folders = [(pdf_folder, ())] + while len(folders) > 0: + folder, path = folders.pop() + ospath = os.path.join(GITLAB_FOLDER_NAME, *path) + print("Searching {}".format(ospath)) + for f in os.listdir(ospath): + if f == ".gitkeep": + continue + gitlabpath = os.path.join(ospath, f) + if os.path.isdir(gitlabpath): + canvaspath = "/".join([CANVAS_BASE_PATH, CANVAS_FOLDER_NAME, *path, f]) + nfolder = next((folder for folder in course.get_folders() + if folder.full_name == canvaspath), None) + print("{} is a folder, found {} on canvas".format(gitlabpath, nfolder)) + if nfolder is None: + print("Creating new canvas folder.") + nfolder = folder.create_folder(f) + folders.append((nfolder, tuple([*path, f]))) + else: + print("{} is a file, uploading!".format(gitlabpath)) + folder.upload(gitlabpath) +else: + print("to_canvas directory not found in current repository, skipping sync")