def test_example(tmp_path: Path) -> None: """ It is possible to give a path and have it treated as a Pathlib path. """ @click.command('delete') @click.argument( 'existing_file', type=click_pathlib.Path(exists=True), ) def delete(existing_file: Path) -> None: assert isinstance(existing_file, Path) existing_file.unlink() tmp_file = tmp_path / 'new.txt' tmp_file.touch() assert tmp_file.exists() runner = CliRunner() result = runner.invoke( delete, [str(tmp_file)], catch_exceptions=False, ) assert result.exit_code == 0 assert not tmp_file.exists()
def target_image_option( command: Optional[Callable[..., None]] = None, required: bool = True, ) -> Callable[..., None]: """ An option decorator for choosing a target image. """ if not command: # Ignore type error as per https://github.com/python/mypy/issues/1484. return functools.partial( # type: ignore target_image_option, required=required, ) click_option_function = click.option( '--image', 'image_file_path', type=click_pathlib.Path( exists=True, file_okay=True, dir_okay=False, ), help='The path to an image to upload and set as the target image.', required=required, ) return click_option_function(command)
def sync_dir_run_option(command: Callable[..., None]) -> Callable[..., None]: """ A decorator for choosing a DC/OS checkout to sync before running commands. """ click_option_function = click.option( '--sync-dir', type=click_pathlib.Path( exists=True, dir_okay=True, file_okay=False, resolve_path=True, ), multiple=True, help=( 'The path to a DC/OS checkout. ' 'Part of this checkout will be synced to all master nodes before ' 'the command is run. ' 'The bootstrap directory is synced if the checkout directory ' 'variant matches the cluster variant.' 'Integration tests are also synced.' 'Use this option multiple times on a DC/OS Enterprise cluster to ' 'sync both DC/OS Enterprise and DC/OS Open Source tests.'), ) # type: Callable[[Callable[..., None]], Callable[..., None]] function = click_option_function(command) # type: Callable[..., None] return function
def dcos_checkout_dir_argument(command: Callable[..., None], ) -> Callable[..., None]: """ Decorate a function to allow choosing a DC/OS checkout directory. """ function = click.argument( 'dcos_checkout_dir', type=click_pathlib.Path(exists=True), envvar='DCOS_CHECKOUT_DIR', default='.', )(command) # type: Callable[..., None] return function
def installer_argument(command: Callable[..., None]) -> Callable[..., None]: """ Decorate a function to allow choosing a DC/OS installer. """ function = click.argument( 'installer', type=click_pathlib.Path( exists=True, dir_okay=False, file_okay=True, resolve_path=True, ), )(command) # type: Callable[..., None] return function
def image_argument(command: Callable[..., None]) -> Callable[..., None]: """ An argument decorator for choosing a query image. """ click_option_function = click.argument( 'image', type=click_pathlib.Path( exists=True, file_okay=True, dir_okay=False, ), ) return click_option_function(command)
def installer_path_argument( command: Callable[..., None], ) -> Callable[..., None]: """ Argument to choose a DC/OS installer URL. """ function = click.argument( 'installer', type=click_pathlib.Path( exists=True, dir_okay=False, file_okay=True, resolve_path=True, ), )(command) # type: Callable[..., None] return function
def extra_config_option(command: Callable[..., None]) -> Callable[..., None]: """ An option decorator for supplying extra DC/OS configuration options. """ function = click.option( '--extra-config', type=click_pathlib.Path( exists=True, file_okay=True, dir_okay=False, resolve_path=True, ), callback=_validate_dcos_configuration, help=('The path to a file including DC/OS configuration YAML. ' 'The contents of this file will be added to add to a default ' 'configuration.'), )(command) # type: Callable[..., None] return function
def license_key_option(command: Callable[..., None]) -> Callable[..., None]: """ An option decorator for passing a license key. """ click_option_function = click.option( '--license-key', type=click_pathlib.Path( exists=True, file_okay=True, dir_okay=False, resolve_path=True, ), envvar='DCOS_LICENSE_KEY_PATH', help=('This is ignored if using open source DC/OS. ' 'If using DC/OS Enterprise, this defaults to the value of the ' '`DCOS_LICENSE_KEY_PATH` environment variable.'), ) # type: Callable[[Callable[..., None]], Callable[..., None]] function = click_option_function(command) # type: Callable[..., None] return function
def genconf_dir_option(command: Callable[..., None]) -> Callable[..., None]: """ An option decorator for a custom "genconf" directory. """ click_option_function = click.option( '--genconf-dir', 'files_to_copy_to_genconf_dir', type=click_pathlib.Path( exists=True, dir_okay=True, file_okay=False, resolve_path=True, ), callback=get_files_to_copy_to_genconf_dir, help=( 'Path to a directory that contains additional files for the DC/OS ' 'installer. ' 'All files from this directory will be copied to the "genconf" ' 'directory before running the DC/OS installer.'), ) # type: Callable[[Callable[..., None]], Callable[..., None]] function = click_option_function(command) # type: Callable[..., None] return function
def workspace_dir_option(command: Callable[..., None]) -> Callable[..., None]: """ An option decorator for the workspace directory. """ help_text = ( 'Creating a cluster can use approximately 2 GB of temporary storage. ' 'Set this option to use a custom "workspace" for this temporary ' 'storage. ' 'See ' 'https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir ' 'for details on the temporary directory location if this option is ' 'not set.') function = click.option( '--workspace-dir', type=click_pathlib.Path( exists=True, dir_okay=True, file_okay=False, resolve_path=True, ), callback=get_workspace_dir, help=help_text, )(command) # type: Callable[..., None] return function
import glob from pathlib import Path from covid_graphs.country import CountryReport, GraphType from covid_graphs.heat_map import HeatMap CURRENT_DIR = Path(__file__).parent @click.command(help="COVID-19 web server") @click.option( "-d", "--data-dir", required=True, type=click_pathlib.Path(exists=True), help="Directory with country proto files", ) @click.option( "-p", "--simulated-polynomial", required=True, type=click_pathlib.Path(exists=True), help="Proto file with simulation results of polynomial growth", ) @click.option( "-e", "--simulated-exponential", required=True, type=click_pathlib.Path(exists=True), help="Proto file with simulation results of exponential growth",
def app() -> None: """Entry point for craftier CLI.""" @click.command( context_settings=_CONTEXT_SETTINGS, name="refactor", help="Refactor your code", ) @click.argument( "files", nargs=-1, type=click_pathlib.Path( exists=True, writable=True, readable=True, dir_okay=False, allow_dash=True, ), ) @click.option( "--config", "config_path", type=click_pathlib.Path(exists=True, readable=True, dir_okay=False), help="Path to the config file. Searches for .craftier.ini by default.", ) @click.option( "--debug/--no-debug", default=False, is_flag=True, help="Show extra debugging information.",
# -*- coding: utf-8 -*- import pathlib import sys import click import click_pathlib from isic_challenge_scoring import score_all from isic_challenge_scoring.types import ScoreException DirectoryPath = click_pathlib.Path(exists=True, file_okay=False, dir_okay=True, readable=True) @click.command(name='isic-challenge-scoring', help='ISIC Challenge submission scoring') @click.option( 'truth_input_path', '--groundtruth', required=True, type=DirectoryPath, help='path to the ground truth directory', ) @click.option( 'prediction_input_path', '--submission', required=True, type=DirectoryPath, help='path to the submission directory',
exit(-1) df = read_names(names_txt_path) if done: print("DONE:") print(get_done(df, done_path)) print() else: print("NOT DONE:") print(get_not_done(df, not_done_path)) print() @tools.command(name="to_xlsx") @click.argument("EXCELFILE", type=click_pathlib.Path(exists=True, dir_okay=False)) @click.argument("tutorname", envvar="TUTN", type=click.STRING, required=True) def to_xlsx(excelfile, tutorname): """ Exports results from names.txt to the given EXCELFILE with given TUTORNAME. You can also set the TUTN environment variable to pass this in. The original excel will not be edited, a new one with _done.xlsx will be created. """ if not names_txt_path.exists(): print( "[ERROR]: Please run the extract command first, names.txt missing") exit(-1) xl = pd.read_excel(excelfile, dtype={"ID-Nummer": "string"}) punkte_col = next(s for s in xl.columns if s.endswith("Punkte")) df = read_names(names_txt_path) df.set_index("MN", inplace=True)
import click_pathlib from tqdm import tqdm from .rhinobuilder import RhinoBuilder @click.command() @click.argument('keyword', type=str, default=str(os.path.basename(os.getcwd()))) @click.option('--directory', '-d', default='./', show_default=True, type=click_pathlib.Path(exists=True, file_okay=False, dir_okay=True, readable=True), help='Directory containing the pictures to rename.') @click.option('--backup', '-b', is_flag=True, show_default=True, help='Create copies instead of renaming the files.') @click.option('--lowercase', '-l', is_flag=True, default=True, show_default=True, help='Modify the extension to lowercase.') def main(keyword: str, directory: pathlib.PosixPath, backup: bool, lowercase: bool):
:param model: model :param samples: A list of samples dataframe :return: dictionary with metrics """ LOGGER.info('Evaluate model') metrics: Dict[str, Any] = {} for a_sample in samples: pass # TODO Code d'evaluation de chaque dataframe metrics['datetime'] = strftime("%Y-%m-%d %H:%M:%S", gmtime()) metrics['auc'] = 0.99 return metrics @click.command(short_help='Evaluate the model') @click.argument('model_filepath', metavar='<model>', type=click_pathlib.Path(exists=True)) @click.argument('sample_files', metavar='<selected files>', type=Glob(default_suffix="**/*.csv")) @click.argument('evaluate_filepath', metavar='<evaluate>', type=click_pathlib.Path()) def main(model_filepath: Path, sample_files: Sequence[Path], evaluate_filepath: Path) -> int: """ Evaluate the <model> with <selected files> and save result in <evaluate>. """ model = pickle.load(open(model_filepath, 'rb')) datasets = [pd.read_csv(f) for f in sample_files] metrics = evaluate_model(model, iter(datasets)) with open(evaluate_filepath, 'wt') as evaluate_file: json.dump(metrics, evaluate_file, indent=4) return 0
import pandas as pd import click import click_pathlib @click.command() @click.argument("results_dir", type=click_pathlib.Path(exists=True, dir_okay=True, resolve_path=True)) @click.argument("output_dir", type=click_pathlib.Path(exists=False, dir_okay=True, resolve_path=True)) def main(results_dir, output_dir): files = [x for x in results_dir.glob('**/*') if x.is_file()] lst_df = [] for csv_filename in files: df = pd.read_csv(csv_filename) lst_df.append(df) total_df = pd.concat(lst_df) output_dir.mkdir(parents=True, exist_ok=True) total_df.to_csv(output_dir / "raw_results.csv", index=False) if __name__ == "__main__": main()
""" with Halo(enabled=enable_spinner): client = docker_client() for name in (_PROXY_CONTAINER_NAME, _OPENVPN_CONTAINER_NAME): try: container = client.containers.get(container_id=name) except docker.errors.NotFound: pass else: container.remove(v=True, force=True) @click.command('setup-mac-network') @click.option( '--configuration-dst', type=click_pathlib.Path(exists=False), default='~/Documents/docker-for-mac.ovpn', callback=_get_ovpn_file_destination, show_default=True, help='The location to create an OpenVPN configuration file.', ) @click.option( '--force', is_flag=True, help=( 'Overwrite any files and destroy conflicting containers from previous ' 'uses of this command.'), ) @enable_spinner_option def setup_mac_network( configuration_dst: Path,
import click import click_pathlib import logging from heartdisease.models import models_utils logger = logging.getLogger(__name__) @click.group() def main(): logging.basicConfig(level=logging.INFO) pass @main.command() @click.option("--data-path", type=click_pathlib.Path(exists=True)) @click.option("--model-version", type=int) def train_model(data_path, model_version): models_utils.run(data_path, model_version) logger.info('Finished with training the final model.') @main.command() @click.option("--data-path", type=click_pathlib.Path(exists=True)) @click.option("--model-version", type=int) def retrain_model(data_path, model_version): models_utils.retrain(data_path, model_version) logger.info('Finished with retraining the final model.') @main.command()
) -> Optional[List[Tuple[str, List[str]]]]: if val is None: return None res: List[Tuple[str, List[str]]] = [] for v in val: name, values = v.split("=", maxsplit=1) res.append((name, values.split(","))) return res # TODO: add multitrials mode, options to report stats and random seed tuning (keeping the best out # of n models…) @click.command() @click.argument( "configs_dir", type=click_pathlib.Path(resolve_path=True, exists=True, file_okay=False), ) @click.argument( "treebanks_dir", type=click_pathlib.Path(resolve_path=True, exists=True, file_okay=False), ) @click.option( "--args", multiple=True, callback=parse_args_callback, help=( "An additional list of values for an argument, given as `name=value,value2,…`." " Leave a trailing comma to also run the default value of the argument" " Can be provided several times for different arguments." " Path options should have different file names." ),
) -> Iterable[Tuple[Path, np.ndarray]]: """ Extract and resize image from streams :param opened_files: List of tuple with path and IOBase :param dim: Input image width and height in pixels. (default 224,224) :return: Array of tuple with filename and dataframe """ for path, stream in opened_files: image = normalize_image(np.asarray(Image.open(stream).resize(dim))) yield (path, image) @click.command(short_help="Prepare dataset") @click.argument('input_raw_filepath', metavar='<tgz file>', type=click_pathlib.Path(exists=True, file_okay=True)) @click.argument('output_prepared_path', metavar='<target>', type=click_pathlib.Path(dir_okay=True)) @click.option("--image-width", type=click.INT, default=DEFAULT_IMAGE_SIZE, help="Input image width in pixels.") @click.option("--image-height", type=click.INT, default=DEFAULT_IMAGE_SIZE, help="Input image height in pixels.") def main(input_raw_filepath: Path, output_prepared_path: Path, image_width: int, image_height: int) -> int: """ This script extract and resize images from <tgz file> to <target>
#!/usr/bin/env python3 """Displays inline images in iTerm2.""" import click import click_pathlib from .payloads import ImageDim, ImageEsc, ImageLenUnit units = [x.name for x in ImageLenUnit] @click.command() @click.argument('image', type=click_pathlib.Path(readable=True)) @click.option('--x-val', type=float, default=0) @click.option('--x-unit', type=click.Choice(units, case_sensitive=False), default='AUTO') @click.option('--y-val', type=float, default=0) @click.option('--y-unit', type=click.Choice(units, case_sensitive=False), default='AUTO') @click.option('--preserve-aspect-ratio', type=bool, default=True) def main(image, x_val, x_unit, y_val, y_unit, preserve_aspect_ratio): """The main function.""" esc = ImageEsc.open(image) esc.width = ImageDim(x_val, ImageLenUnit[x_unit.upper()]) esc.height = ImageDim(y_val, ImageLenUnit[y_unit.upper()]) esc.preserve_aspect_ratio = preserve_aspect_ratio esc.write()
yield d_path else: d_path = pathlib.Path(path).resolve() d_path.mkdir(parents=True, exist_ok=True) yield d_path @click.group(help="A graph dependency parser") def cli(): pass @cli.command(help="Parse a raw or tokenized file") @click.argument( "model_path", type=click_pathlib.Path(resolve_path=True, exists=True), ) @click.argument( "input_path", type=click.Path(resolve_path=True, exists=True, dir_okay=False, allow_dash=True), ) @click.argument( "output_path", type=click.Path(resolve_path=True, dir_okay=False, writable=True, allow_dash=True), default="-",
return out_path / "hotspots.parquet" @click.group() def hotspots() -> None: pass @hotspots.command() @click.option( "--file-pattern", default=".*", help= "Only include files in the repository matching this regular expression", ) @click.argument("git_root", type=click_pathlib.Path()) @click.argument("data_dir", type=click_pathlib.Path()) def compute(git_root: Path, file_pattern: str, data_dir: Path) -> None: data_dir.mkdir(exist_ok=True) data = acquire_base_data(git_root, re.compile(file_pattern)) data.to_parquet(hotspot_data_file(data_dir)) @hotspots.command() @click.argument("data_dir", type=click_pathlib.Path(exists=True)) def visualize(data_dir: Path) -> None: data = pd.read_parquet(hotspot_data_file(data_dir)) create_app(data).run_server(debug=True)
This script performs the following steps: 1) Get a list of all pdf paths 2) split pdfs into single pages 3) If pdfs are greater than N pages, take only 3 pages """ from poctoolkit import pdf_utils, folder_utils import click import click_pathlib MAX_PAGES = 3 def split_pdfs(pdf_folder, output_folder): pdf_filepaths = folder_utils.files_from_directory(pdf_folder, "*.pdf") for pdf_filepath in pdf_filepaths: pdf_utils.split_pdf_pages(pdf_filepath, output_folder, max_pages=MAX_PAGES) @click.command() @click.argument("pdf_folder", type=click_pathlib.Path(exists=True)) @click.argument("output_folder") def split_pdf_driver(pdf_folder, output_folder): split_pdfs(pdf_folder, output_folder) if __name__ == "__main__": split_pdf_driver()
def find_matching_dir(output_directory: os.PathLike[str], title: str) -> Optional[Path]: output_directory = Path(output_directory) if not output_directory.is_dir(): # the output_directory doesn't exist, so it obviously has no subdirs return None for child in output_directory.iterdir(): if child.is_dir() and title.startswith(str(child.name)): return child return None @click.command() @click.option( "--config", "-c", type=click_pathlib.Path(dir_okay=False, resolve_path=True) ) @click.option( "--subscriptions", "-s", type=click_pathlib.Path(dir_okay=False, resolve_path=True) ) @click.option( "--output-directory", "-o", type=click_pathlib.Path(file_okay=False, resolve_path=True), ) @click.option("--advanced-sorting/--no-advanced-sorting", default=True) @click.version_option() @click.pass_context def main(ctx, config, subscriptions, output_directory, advanced_sorting): if not config: CONFIG_LOCATION.mkdir(parents=True, exist_ok=True)
CONTEXT_SETTINGS = dict(auto_envvar_prefix="DODOO") EPILOG = ( "All options and arguments can be specified via environment variables " "by prefixing their alphanumeric UPPERCASE equivalents with DODOO_.\n" "\nExample: DODOO_CODVERSION_FILE" ) @with_plugins(iter_entry_points("dodoo.cli_plugins")) @click.group(context_settings=CONTEXT_SETTINGS, help=_main.__doc__, epilog=EPILOG) @click.option( "-f", "--framework", "framework_dir", type=click_pathlib.Path(exists=True, file_okay=False, resolve_path=True), help="Path to Odoo framework, if odoo is not in your python path.", ) @click.option( "-c", "--confd", "config_dir", default="./config.d", show_default=True, type=click_pathlib.Path(exists=True, file_okay=False, resolve_path=True), help="Config directory containing json config files for develop, staging & " "production. A sane default config is shipped for each env.", ) @click.option( "--log-conf", "log_config",
import pandas as pd import click import click_pathlib @click.command() @click.argument("results_dir", type=click_pathlib.Path(exists=True, dir_okay=True, resolve_path=True)) @click.argument("output_dir", required=False, default=None, type=click_pathlib.Path(exists=False, dir_okay=True, resolve_path=True)) @click.option("-c", "--count", is_flag=True, help="Print the number of lines in the resulting csv.") @click.option("-d", "--delete", is_flag=True, help="Delete files after gathering.") def main(results_dir, output_dir, count, delete): if output_dir is None: output_dir = results_dir name_gathered_file = "gathered_results.csv" files = [x for x in results_dir.glob('**/*') if x.is_file()] lst_df = [] lst_other_files = [] real_count = 0 for csv_filename in files: if csv_filename.name == name_gathered_file: continue if csv_filename.suffix == ".csv": df = pd.read_csv(csv_filename) lst_df.append(df) real_count += 1 else: lst_other_files.append(csv_filename) if real_count < 2:
if current_sent: res.append(current_sent) current_sent = [] elif l.startswith("#"): continue else: current_sent.append(l.strip().split()[word_col]) if current_sent: res.append(current_sent) return res @click.command() @click.argument( "conll-file", type=click_pathlib.Path(resolve_path=True, exists=True, dir_okay=False), ) @click.argument( "out-file", type=click_pathlib.Path(resolve_path=True, dir_okay=False), ) @click.option( "--batch-size", type=int, default=1, help=("Number of sentences in a processing batch"), ) @click.option( "--device", type=str, default="cpu", help="A device to use (using pytorch syntax)" ) @click.option( "--model",