| 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, Namespace |
||
| 22 | args = parse_arguments() |
||
| 23 | configure_logging(args.verbose) |
||
| 24 | logging.getLogger(__name__).debug("Program started...") |
||
| 25 | |||
| 26 | full_source_directory, grading = get_arguments(args) |
||
| 27 | quiz = Quiz(parent_article=args.parent_article, title=args.title, grading=grading) |
||
| 28 | quiz.import_files(full_source_directory) |
||
| 29 | |||
| 30 | quiz_wikitext = str(quiz) |
||
| 31 | wiki_domain = "https://test.vik.wiki" |
||
| 32 | webbrowser.open_new_tab(f"{wiki_domain}/index.php?title=Speciális:Belépés") |
||
| 33 | input("Please log in to the wiki then press Enter to continue...") |
||
| 34 | parameters_for_opening_edit = { |
||
| 35 | "action": "edit", |
||
| 36 | "summary": "Kvíz bővítése " |
||
| 37 | "a https://github.com/gy-mate/moodle-to-vikwikiquiz segítségével importált Moodle-kvízekből", |
||
| 38 | } |
||
| 39 | clear_terminal() |
||
| 40 | create_article(args, parameters_for_opening_edit, quiz_wikitext, wiki_domain) |
||
| 41 | logging.getLogger(__name__).debug("Program finished!") |
||
| 42 | |||
| 43 | |||
| 44 | def create_article( |
||
| 45 | args: Namespace, |
||
| 46 | parameters_for_opening_edit: dict, |
||
| 47 | quiz_wikitext: str, |
||
| 48 | wiki_domain: str, |
||
| 49 | ) -> None: |
||
| 50 | if args.new: |
||
| 51 | parameters_for_opening_edit_with_paste = parameters_for_opening_edit.copy() |
||
| 52 | parameters_for_opening_edit_with_paste.update( |
||
| 53 | { |
||
| 54 | "preload": "Sablon:Előbetöltés", |
||
| 55 | "preloadparams[]": quiz_wikitext, |
||
| 56 | } |
||
| 57 | ) |
||
| 58 | parameters_for_opening_edit_with_paste["summary"] = ( |
||
| 59 | parameters_for_opening_edit_with_paste["summary"].replace( |
||
| 60 | "bővítése", "létrehozása" |
||
| 61 | ) |
||
| 62 | ) |
||
| 63 | url = f"{wiki_domain}/wiki/{args.title}?{urlencode(parameters_for_opening_edit_with_paste)}" |
||
| 64 | if len(url) >= 2048: |
||
| 65 | logging.getLogger(__name__).warning( |
||
| 66 | "I can't create the article automatically " |
||
| 67 | "because the URL would be too long for some browsers (or the server)." |
||
| 68 | ) |
||
| 69 | if args.verbose: |
||
| 70 | pyperclip.copy(url) |
||
| 71 | print( |
||
| 72 | "This URL has been copied to the clipboard! " |
||
| 73 | "It will be overwritten but you may recall it later if you use an app like Pastebot." |
||
| 74 | ) |
||
| 75 | wait_for_pastebot_to_recognize_copy() |
||
| 76 | parameters_for_opening_edit["summary"] = parameters_for_opening_edit[ |
||
| 77 | "summary" |
||
| 78 | ].replace("bővítése", "létrehozása") |
||
| 79 | else: |
||
| 80 | pyperclip.copy(quiz_wikitext) |
||
| 81 | print( |
||
| 82 | "The wikitext of the quiz has been copied to the clipboard! " |
||
| 83 | "This will be overwritten but you may recall it later if you use an app like Pastebot." |
||
| 84 | ) |
||
| 85 | wait_for_pastebot_to_recognize_copy() |
||
| 86 | if args.verbose: |
||
| 87 | pyperclip.copy(url) |
||
| 88 | print("The URL has been copied to the clipboard!") |
||
| 89 | webbrowser.open_new_tab(url) |
||
| 90 | print( |
||
| 91 | "The edit page of the new quiz article has been opened in your browser with the wikitext pre-filled!" |
||
| 92 | ) |
||
| 93 | return |
||
| 94 | pyperclip.copy(quiz_wikitext) |
||
| 95 | print("The wikitext of the quiz has been copied to the clipboard!") |
||
| 96 | url = f"{wiki_domain}/wiki/{args.title}?{urlencode(parameters_for_opening_edit)}" |
||
| 97 | webbrowser.open_new_tab(url) |
||
| 98 | print( |
||
| 99 | "The edit page of the quiz article has been opened in your browser! Please paste the wikitext there manually." |
||
| 100 | ) |
||
| 101 | |||
| 102 | |||
| 103 | def get_arguments(args: Namespace) -> tuple[str, GradingType]: |
||
| 104 | full_source_directory = args.source_directory |
||
| 105 | match args.grading: |
||
| 106 | case "+": |
||
| 107 | grading = GradingType.Kind |
||
| 108 | case "-": |
||
| 109 | grading = GradingType.Strict |
||
| 110 | case _: |
||
| 111 | grading = None |
||
| 112 | return full_source_directory, grading |
||
| 113 | |||
| 114 | |||
| 115 | def parse_arguments() -> Namespace: |
||
| 116 | parser = ArgumentParser() |
||
| 117 | parser.add_argument( |
||
| 118 | "-v", "--verbose", action="store_true", help="increase output verbosity" |
||
| 119 | ) |
||
| 172 |