Exemplo n.º 1
0
    def handle(self, project, query, output_format, output, limit, **options):
        # note: this is heavily copy/paste/modified from commcare_export.cli
        commcare_hq = 'local_commcare_export'
        try:
            # local development only
            sys.path.append(os.path.join(os.getcwd(), 'lib',
                                         'commcare-export'))
            import commcare_export  # noqa
        except ImportError:
            raise CommandError(
                'This command requires commcare-export to be installed! '
                'Please run: pip install commcare-export. You may also need to run: '
                'pip install openpyxl==2.6.0b1 '
                'afterwards to run CommCare due to version incompatibilities.')
        from commcare_export import misc
        from commcare_export.checkpoint import CheckpointManager
        from commcare_export.cli import _get_writer, _get_query_from_file
        from commcare_export.commcare_minilinq import CommCareHqEnv
        from commcare_export.env import BuiltInEnv, JsonPathEnv, EmitterEnv

        print('commcare-export is installed.')

        writer = _get_writer(output_format, output, strict_types=False)

        query_obj = _get_query_from_file(
            query,
            None,  # missing_value
            writer.supports_multi_table_write,
            writer.max_column_length,
            writer.required_columns)
        checkpoint_manager = None
        if writer.support_checkpoints:
            md5 = misc.digest_file(query)
            checkpoint_manager = CheckpointManager(
                output,
                query,
                md5,
                project,
                commcare_hq,
            )
            since = checkpoint_manager.get_time_of_last_checkpoint()

        else:
            since = None

        commcarehq_base_url = commcare_hq
        api_client = LocalCommCareHqClient(
            url=commcarehq_base_url,
            project=project,
            limit=limit,
            checkpoint_manager=checkpoint_manager)
        if since is not None:
            since = dateutil.parser.parse(since)

        env = (BuiltInEnv({'commcarehq_base_url': commcarehq_base_url})
               | CommCareHqEnv(api_client, since=since)
               | JsonPathEnv({})
               | EmitterEnv(writer))

        with env:
            try:
                lazy_result = query_obj.eval(env)
                if lazy_result is not None:
                    # evaluate lazy results
                    for r in lazy_result:
                        list(r) if r else r
            except KeyboardInterrupt:
                print('\nExport aborted')
                return

        if checkpoint_manager:
            checkpoint_manager.set_final_checkpoint()
Exemplo n.º 2
0
    def handle(self, project, query, output_format, output, limit, **options):
        # note: this is heavily copy/paste/modified from commcare_export.cli
        commcare_hq = 'local_commcare_export'
        try:
            # local development only
            sys.path.append(os.path.join(os.getcwd(), 'lib', 'commcare-export'))
            import commcare_export  # noqa
        except ImportError:
            raise CommandError(
                'This command requires commcare-export to be installed! '
                'Please run: pip install commcare-export. You may also need to run: '
                'pip install openpyxl==2.6.0b1 '
                'afterwards to run CommCare due to version incompatibilities.'
            )
        from commcare_export import misc
        from commcare_export.checkpoint import CheckpointManager
        from commcare_export.cli import _get_writer, _get_query_from_file
        from commcare_export.commcare_minilinq import CommCareHqEnv
        from commcare_export.env import BuiltInEnv, JsonPathEnv, EmitterEnv

        print('commcare-export is installed.')

        writer = _get_writer(output_format, output, strict_types=False)

        query_obj = _get_query_from_file(
            query,
            None,  # missing_value
            writer.supports_multi_table_write,
            writer.max_column_length,
            writer.required_columns
        )
        checkpoint_manager = None
        if writer.support_checkpoints:
            md5 = misc.digest_file(query)
            checkpoint_manager = CheckpointManager(
                output,
                query,
                md5,
                project,
                commcare_hq,
            )
            since = checkpoint_manager.get_time_of_last_checkpoint()

        else:
            since = None

        commcarehq_base_url = commcare_hq
        api_client = LocalCommCareHqClient(
            url=commcarehq_base_url,
            project=project,
            limit=limit,
            checkpoint_manager=checkpoint_manager
        )
        if since is not None:
            since = dateutil.parser.parse(since)

        env = (
            BuiltInEnv({'commcarehq_base_url': commcarehq_base_url})
            | CommCareHqEnv(api_client, since=since)
            | JsonPathEnv({})
            | EmitterEnv(writer)
        )

        with env:
            try:
                lazy_result = query_obj.eval(env)
                if lazy_result is not None:
                    # evaluate lazy results
                    for r in lazy_result:
                        list(r) if r else r
            except KeyboardInterrupt:
                print('\nExport aborted')
                return

        if checkpoint_manager:
            checkpoint_manager.set_final_checkpoint()