Пример #1
0
def _deal_with_result(args, table_rows, docstr_loc, ipynb_files):
    if not args.no_generate_diff and table_rows:
        print_results_as_table(table_rows)
    elif args.force and docstr_loc:
        _apply_diff(docstr_loc, args.no_generate_args_list, ipynb_files,
                    args.docstring_style)
        _remove_converted_python_files(ipynb_files)
    elif args.no_generate_diff and docstr_loc:
        if args.no_print_report:
            choice = query_yes_no(
                "The diff has been generated, do you want to see the suggestions for missing Docstrings?"
            )
            if choice:
                print_results_as_table(table_rows)
                choice = query_yes_no("Do you want to apply the suggestions?")
            else:
                choice = query_yes_no("Do you want to apply the suggestions?")
        else:
            choice = query_yes_no("Do you want to apply the suggestions?")

        if choice:
            _apply_diff(docstr_loc, args.no_generate_args_list, ipynb_files,
                        args.docstring_style)
        else:
            _remove_converted_python_files(ipynb_files)
            print_on_console("Nothing changed. Good bye!",
                             color="green",
                             emoji="thumbsup")
    else:
        print_on_console("\n\nNothing to be done. Good bye!",
                         color="green",
                         emoji="thumbsup")
Пример #2
0
def _apply_diff(docstr_loc, no_generate_args_list, ipynb_files,
                docstring_style):
    print_on_console("Applying diff", color="green")
    apply_diff(docstr_loc, no_generate_args_list, ipynb_files, docstring_style)
    print_on_console("Diff applied. Good bye!",
                     color="green",
                     emoji="thumbsup")
Пример #3
0
def inspect_and_download_latest_model(model_root: Path, download_url: str, is_old=False) -> bool:
    model_file = "pytorch_model_new.bin" if not is_old else "pytorch_model.bin"

    if not model_root.exists():
        os.makedirs(str(model_root))
        os.mkdir(str(model_root / "model"))
    elif model_root.exists() and not (model_root / "model").exists():
        os.mkdir(str(model_root / "model"))

    if Path(model_root/ "model" / model_file).exists() and Path(model_root/ "model" / model_file).is_file():
        return True
    
    print_on_console("There is no model. Downloading (maybe because you chose to use the newer model)", color="green")
    download_from_url(download_url, str(Path(model_root/ "model" / model_file)))
    print_on_console("Download complete", color="green", emoji="heavy_check_mark")
    return True
Пример #4
0
def inspect_and_download_latest_tslibs(tslibs_root: Path, download_url: str) -> bool:
    os_name = platform.system().lower()
    if os_name not in SUPPORTED_PLATFORMS:
        return (False, None)
    
    file_name = "python_ts_darwin64.so" if os_name == "darwin" else "python_ts_nix64.so"
    download_url = f"{download_url}{file_name}"

    if (tslibs_root/ "tslibs" / file_name).exists() and (tslibs_root/ "tslibs" / file_name).is_file():
        return (True, file_name) 

    if not (tslibs_root / "tslibs").exists():
        os.mkdir(str(tslibs_root / "tslibs"))
    
    print_on_console("There is no tree-sitter lib. Downloading", color="green")
    download_from_url(download_url, str(Path(tslibs_root/ "tslibs" / file_name)))
    print_on_console("Download complete", color="green", emoji="heavy_check_mark")
    return (True, file_name)
    
Пример #5
0
def main():
    setup_cmdline_args_for_docly_restore(parser)
    args = parser.parse_args()
    all_cached_files = get_all_files_from_cache()
    if not args.force:
        choice = query_yes_no(
            "It will restore all MODIFIED files to the state of last run of `docly-gen`. Are you sure?"
        )
    else:
        # Forceful application of restore command
        choice = True
    if choice:
        print_on_console("Restoring files", color="green")
        try:
            for file in check_out_path(Path().cwd().absolute()):
                if not is_dir(file) and (is_python_file(file)
                                         or is_ipynb_notebook(file)):
                    full_path = str(Path(file).absolute())
                    comparison_key = full_path[1:].replace("/", "#")
                    if comparison_key in all_cached_files:
                        source_file = str(CACHE_DIR / comparison_key)
                        final_file = str(Path(file).absolute())
                        shutil.move(source_file, final_file)
            print_on_console("Restoring done", color="green", emoji="thumbsup")
        except KeyboardInterrupt:
            print_on_console("Restoration not finished",
                             color="red",
                             emoji="X")
Пример #6
0
def main():
    print_on_console("Cleaning old model", color="green")
    if Path((Path.home() / ".docly" / "model" / "pytorch_model.bin")).exists():
        Path((Path.home() / ".docly" / "model" / "pytorch_model.bin")).unlink()
    print_on_console("Cleaning done", color="green", emoji="heavy_check_mark")
Пример #7
0
def main():
    # if look_for_update():
    #     print_on_console("There is an update available. Please run `pip install --upgrade docly`", color="green", emoji="rotating_light")
    _print_welcome()

    setup_cmdline_args_for_docly_gen(parser)
    args = parser.parse_args()

    if args.run_on_notebooks and not _if_jupytext_is_installed():
        print_on_console(
            "You have mentioned `run_on_notebooks` but the needed dependecy is not present. Please run `pip install 'docly[jupyter]'` for that. This switch will be ignored",
            color="green")
        args.run_on_notebooks = False

    # if args.run_on_notebooks:
    #     print_on_console("You have mentioned the `run_on_notebooks` switch. It is experimental", color="red", emoji="rotating_light")
    #     choice = query_yes_no("Do you want to continue?")
    #     if not choice:
    #         args.run_on_notebooks = False

    config = DoclyConfig(args.config_file)

    try:
        mdp = NEW_MODEL_DOWNLOAD_ROOT if not args.use_old_model else MODEL_DOWNLOAD_ROOT
        inspect_and_download_latest_model(ROOT, mdp, args.use_old_model)
    except KeyboardInterrupt:
        print_on_console("You stopped the download. Docly won't work",
                         color="red",
                         emoji="X")
        shutil.rmtree(str(ROOT / "model"))
        sys.exit(1)

    try:
        ready, tslib_file = inspect_and_download_latest_tslibs(
            ROOT, TSLIBS_DOWNLOAD_ROOT)
        if not ready:
            print_on_console("===== OS version not supported =====",
                             color="red",
                             emoji="X")
            return
    except KeyboardInterrupt:
        print_on_console("You stopped the download. Docly won't work",
                         color="red",
                         emoji="X")
        sys.exit(1)

    print_on_console("Loading Engine. Please wait", color="green")
    model_name = "pytorch_model_new.bin" if not args.use_old_model else "pytorch_model.bin"
    model, tokenizer = load_model(str(ROOT / "model" / model_name),
                                  args.use_old_model)
    print_on_console("Engine Loaded.", color="green", emoji="heavy_check_mark")

    ts_lib_path = str(ROOT / "tslibs" / tslib_file)

    table_rows, docstr_loc, ipynb_files = _process(args, model, tokenizer,
                                                   ts_lib_path, config)

    _deal_with_result(args, table_rows, docstr_loc, ipynb_files)