def upload_compressed_packages(password: str, file_path: click.Path): """Uploads a tar.gz file full of packages to the C&C server.""" if not file_path.endswith(".tar.gz"): click.echo("Only .tar.gz extension allowed.") else: with open(file_path, "rb") as f: prepared = requests.Request("PATCH", f"{C2_URL}/test_sets", files={ 'packages': f }).prepare() digest = b64encode(sha256(prepared.body).digest()).decode() prepared.headers['Digest'] = f"sha-256={digest}" headers = ['Digest'] signature = signatures.new_signature( password.encode(), "PATCH", "/test_sets", signature_headers=headers, header_recoverer=lambda h: prepared.headers.get(h)) prepared.headers['Authorization'] = ( signatures.new_authorization_header("Client", signature, headers)) try: resp = requests.Session().send(prepared) except requests.exceptions.ConnectionError: click.echo("Connection refused.") else: if resp.status_code in {400, 401, 415}: click.echo(resp.json()['error']) elif resp.status_code != 204: click.echo( "Unexpected response from Command and Control Sever.")
def export( path: click.Path, config: click.File, output: click.Path, format: str, annotator: List, categories: List, include_unfinished: bool = False, export_images: bool = True, ): """ Export the COCO annotations for an annotation project. To export all annotations of a project of all annotators, use: `pawls export <labeling_folder> <labeling_config> <output_path>` To export only finished annotations of from specified annotators, e.g. markn and shannons, use: `pawls export <labeling_folder> <labeling_config> <output_path> -u markn -u shannons`. To export all annotations of from a given annotator, use: `pawls export <labeling_folder> <labeling_config> <output_path> -u markn --include-unfinished`. """ assert ( format in ALL_SUPPORTED_EXPORT_TYPE ), f"Invalid export format {format}. Should be one of {ALL_SUPPORTED_EXPORT_TYPE}." print(f"Export the annotations to the {format} format.") config = LabelingConfiguration(config) annotation_folder = AnnotationFolder(path) if len(annotator) == 0: all_annotators = annotation_folder.all_annotators print( f"Export annotations from all available annotators {all_annotators}" ) else: all_annotators = annotator if len(categories) == 0: categories = config.categories print(f"Export annotations from all available categories {categories}") if format == "coco": coco_builder = COCOBuilder(categories, output) print( f"Creating paper data for annotation folder {annotation_folder.path}" ) coco_builder.create_paper_data(annotation_folder, save_images=export_images) for annotator in all_annotators: print(f"Export annotations from annotators {annotator}") anno_files = AnnotationFiles(path, annotator, include_unfinished) coco_builder.build_annotations(anno_files) print( f"Successfully exported {len(anno_files)} annotations of annotator {annotator} to {output}." ) elif format == "token": if not output.endswith(".csv"): output = f"{output}.csv" token_builder = TokenTableBuilder(categories, output) print( f"Creating paper data for annotation folder {annotation_folder.path}" ) token_builder.create_paper_data(annotation_folder) for annotator in all_annotators: # print(f"Export annotations from annotators {annotator}") anno_files = AnnotationFiles(path, annotator, include_unfinished) token_builder.create_annotation_for_annotator(anno_files) df = token_builder.export() print( f"Successfully exported annotations for {len(df)} tokens from annotators {all_annotators} to {output}." )