| Conditions | 6 |
| Total Lines | 97 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | from argparse import ArgumentParser |
||
| 22 | def main() -> None: |
||
| 23 | parser = ArgumentParser() |
||
| 24 | parser.add_argument( |
||
| 25 | "-v", "--verbose", action="store_true", help="increase output verbosity" |
||
| 26 | ) |
||
| 27 | parser.add_argument( |
||
| 28 | "source_directory", |
||
| 29 | help="The absolute path of the directory where the Moodle quiz HTML files are located. " |
||
| 30 | "These HTML files should contain the 'Review' page of the quizzes.", |
||
| 31 | ) |
||
| 32 | parser.add_argument( |
||
| 33 | "parent_article", help="The article name of the course on vik.wiki." |
||
| 34 | ) |
||
| 35 | parser.add_argument( |
||
| 36 | "-n", |
||
| 37 | "--new", |
||
| 38 | action="store_true", |
||
| 39 | help="Create a new quiz on vik.wiki by automatically opening an edit page for the new article.", |
||
| 40 | ) |
||
| 41 | parser.add_argument( |
||
| 42 | "title", |
||
| 43 | help="How the quiz should be named on [vik.wiki](https://vik.wiki/). " |
||
| 44 | "This usually is in the following form: `[course name] kvíz – [exam name]`. " |
||
| 45 | "The hyphen and the part after it can be omitted.", |
||
| 46 | ) |
||
| 47 | parser.add_argument( |
||
| 48 | "-g", |
||
| 49 | "--grading", |
||
| 50 | help="`+` or `-`. See https://vik.wiki/wiki/Segítség:Kvíz#Pontozás for further info.", |
||
| 51 | ) |
||
| 52 | args = parser.parse_args() |
||
| 53 | |||
| 54 | configure_logging(args.verbose) |
||
| 55 | logging.getLogger(__name__).debug("Program started...") |
||
| 56 | |||
| 57 | full_source_directory = args.source_directory |
||
| 58 | if args.grading == "+": |
||
| 59 | grading = GradingType.Kind |
||
| 60 | elif args.grading == "-": |
||
| 61 | grading = GradingType.Strict |
||
| 62 | else: |
||
| 63 | grading = None |
||
| 64 | quiz = Quiz(parent_article=args.parent_article, title=args.title, grading=grading) |
||
| 65 | quiz.import_files(full_source_directory) |
||
| 66 | |||
| 67 | quiz_wikitext = str(quiz) |
||
| 68 | wiki_domain = "https://test.vik.wiki" |
||
| 69 | webbrowser.open_new_tab(f"{wiki_domain}/index.php?title=Speciális:Belépés") |
||
| 70 | input("Please log in to the wiki then press Enter to continue...") |
||
| 71 | parameters_for_opening_edit = { |
||
| 72 | "action": "edit", |
||
| 73 | "summary": "Kvíz létrehozása " |
||
| 74 | "a https://github.com/gy-mate/moodle-to-vikwikiquiz segítségével importált Moodle-kvízekből", |
||
| 75 | } |
||
| 76 | clear_terminal() |
||
| 77 | if args.new: |
||
| 78 | parameters_for_opening_edit_with_paste = parameters_for_opening_edit.copy() |
||
| 79 | parameters_for_opening_edit_with_paste.update( |
||
| 80 | { |
||
| 81 | "preload": "Sablon:Előbetöltés", |
||
| 82 | "preloadparams[]": quiz_wikitext, |
||
| 83 | } |
||
| 84 | ) |
||
| 85 | url = f"{wiki_domain}/wiki/{args.title}?{urlencode(parameters_for_opening_edit_with_paste)}" |
||
| 86 | if len(url) >= 2048: |
||
| 87 | logging.getLogger(__name__).warning( |
||
| 88 | "I can't create the article automatically " |
||
| 89 | "because the URL would be too long for some browsers (or the server)." |
||
| 90 | ) |
||
| 91 | if args.verbose: |
||
| 92 | pyperclip.copy(url) |
||
| 93 | print( |
||
| 94 | "This URL has been copied to the clipboard! " |
||
| 95 | "It will be overwritten but you may recall it later if you use an app like Pastebot." |
||
| 96 | ) |
||
| 97 | wait_for_pastebot_to_recognize_copy() |
||
| 98 | else: |
||
| 99 | webbrowser.open_new_tab(url) |
||
| 100 | print( |
||
| 101 | "The edit page of the new quiz article has been opened in your browser!" |
||
| 102 | ) |
||
| 103 | pyperclip.copy(quiz_wikitext) |
||
| 104 | print( |
||
| 105 | "The wikitext of the quiz has been copied to the clipboard! " |
||
| 106 | "This will be overwritten but you may recall it later if you use an app like Pastebot." |
||
| 107 | ) |
||
| 108 | wait_for_pastebot_to_recognize_copy() |
||
| 109 | pyperclip.copy(url) |
||
| 110 | print("The URL has been copied to the clipboard!") |
||
| 111 | pyperclip.copy(quiz_wikitext) |
||
| 112 | print("The wikitext of the quiz has been copied to the clipboard!") |
||
| 113 | url = f"{wiki_domain}/wiki/{args.title}?{urlencode(parameters_for_opening_edit)}" |
||
| 114 | webbrowser.open_new_tab(url) |
||
| 115 | print( |
||
| 116 | "The edit page of the quiz article has been opened in your browser! Please paste the wikitext there manually." |
||
| 117 | ) |
||
| 118 | logging.getLogger(__name__).debug("...program finished!") |
||
| 119 | |||
| 144 |