|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Example extractor based on the clowder code.""" |
| 4 | + |
| 5 | +import logging |
| 6 | +import subprocess |
| 7 | +import json |
| 8 | +from typing import Dict |
| 9 | + |
| 10 | +from pyclowder.extractors import Extractor |
| 11 | +import pyclowder.files |
| 12 | + |
| 13 | + |
| 14 | +class WordCount(Extractor): |
| 15 | + """Count the number of characters, words and lines in a text file.""" |
| 16 | + def __init__(self): |
| 17 | + Extractor.__init__(self) |
| 18 | + |
| 19 | + # add any additional arguments to parser |
| 20 | + # self.parser.add_argument('--max', '-m', type=int, nargs='?', default=-1, |
| 21 | + # help='maximum number (default=-1)') |
| 22 | + |
| 23 | + # parse command line and load default logging configuration |
| 24 | + self.setup() |
| 25 | + |
| 26 | + # setup logging for the exctractor |
| 27 | + logging.getLogger('pyclowder').setLevel(logging.DEBUG) |
| 28 | + logging.getLogger('__main__').setLevel(logging.DEBUG) |
| 29 | + |
| 30 | + def process_message(self, connector, host, secret_key, resource, parameters): |
| 31 | + # Process the file and upload the results |
| 32 | + |
| 33 | + logger = logging.getLogger(__name__) |
| 34 | + inputfile = resource["local_paths"][0] |
| 35 | + file_id = resource['id'] |
| 36 | + |
| 37 | + if 'parameters' in parameters: |
| 38 | + params = None |
| 39 | + logging.info("Received parameters") |
| 40 | + try: |
| 41 | + params = json.loads(parameters['parameters']) |
| 42 | + except TypeError as e: |
| 43 | + print(f"Failed to load parameters, it's not compatible with json.loads().\nError:{e}") |
| 44 | + if type(parameters == Dict): |
| 45 | + params = parameters['parameters'] |
| 46 | + if "IMAGE_ANNOTATIONS" in params: |
| 47 | + image_annotations = params["IMAGE_ANNOTATIONS"] |
| 48 | + logging.info(f"Image annotations: {image_annotations}") |
| 49 | + |
| 50 | + result = json.loads(image_annotations) |
| 51 | + |
| 52 | + metadata = self.get_metadata(result, 'file', file_id, host) |
| 53 | + |
| 54 | + # Normal logs will appear in the extractor log, but NOT in the Clowder UI. |
| 55 | + logger.debug(metadata) |
| 56 | + |
| 57 | + # Upload metadata to original file |
| 58 | + pyclowder.files.upload_metadata(connector, host, secret_key, file_id, metadata) |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + extractor = WordCount() |
| 62 | + extractor.start() |
0 commit comments