<commit_before># Copyright 2022 The MediaPipe Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Building MediaPipe Tasks AARs."""

load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_build_aar_with_jni", "mediapipe_java_proto_src_extractor", "mediapipe_java_proto_srcs")
load("@build_bazel_rules_android//android:rules.bzl", "android_library")

_CORE_TASKS_JAVA_PROTO_LITE_TARGETS = [
    "//mediapipe/tasks/cc/components/containers/proto:category_java_proto_lite",
    "//mediapipe/tasks/cc/components/containers/proto:classifications_java_proto_lite",
    "//mediapipe/tasks/cc/components/containers/proto:embeddings_java_proto_lite",
    "//mediapipe/tasks/cc/components/containers/proto:landmarks_detection_result_java_proto_lite",
    "//mediapipe/tasks/cc/components/proto:segmenter_options_java_proto_lite",
    "//mediapipe/tasks/cc/components/processors/proto:classifier_options_java_proto_lite",
    "//mediapipe/tasks/cc/components/processors/proto:embedder_options_java_proto_lite",
    "//mediapipe/tasks/cc/core/proto:acceleration_java_proto_lite",
    "//mediapipe/tasks/cc/core/proto:base_options_java_proto_lite",
    "//mediapipe/tasks/cc/core/proto:external_file_java_proto_lite",
]

_VISION_TASKS_JAVA_PROTO_LITE_TARGETS = [
    "//mediapipe/tasks/cc/vision/object_detector/proto:object_detector_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/image_classifier/proto:image_classifier_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:gesture_classifier_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:gesture_embedder_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:gesture_recognizer_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:hand_gesture_recognizer_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/image_segmenter/proto:image_segmenter_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/hand_detector/proto:hand_detector_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/hand_landmarker/proto:hand_landmarker_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/hand_landmarker/proto:hand_landmarks_detector_graph_options_java_proto_lite",
]

_TEXT_TASKS_JAVA_PROTO_LITE_TARGETS = [
    "//mediapipe/tasks/cc/text/text_classifier/proto:text_classifier_graph_options_java_proto_lite",
]

def mediapipe_tasks_core_aar(name, srcs, manifest):
    """Builds medaipipe tasks core AAR.

    Args:
      name: The bazel target name.
      srcs: MediaPipe Tasks' core layer source files.
      manifest: The Android manifest.
    """

    mediapipe_tasks_java_proto_srcs = []
    for target in _CORE_TASKS_JAVA_PROTO_LITE_TARGETS:
        mediapipe_tasks_java_proto_srcs.append(
            _mediapipe_tasks_java_proto_src_extractor(target = target),
        )

    for target in _VISION_TASKS_JAVA_PROTO_LITE_TARGETS:
        mediapipe_tasks_java_proto_srcs.append(
            _mediapipe_tasks_java_proto_src_extractor(target = target),
        )

    for target in _TEXT_TASKS_JAVA_PROTO_LITE_TARGETS:
        mediapipe_tasks_java_proto_srcs.append(
            _mediapipe_tasks_java_proto_src_extractor(target = target),
        )

    mediapipe_tasks_java_proto_srcs.append(mediapipe_java_proto_src_extractor(
        target = "//mediapipe/calculators/core:flow_limiter_calculator_java_proto_lite",
        src_out = "com/google/mediapipe/calculator/proto/FlowLimiterCalculatorProto.java",
    ))

    mediapipe_tasks_java_proto_srcs.append(mediapipe_java_proto_src_extractor(
        target = "//mediapipe/calculators/tensor:inference_calculator_java_proto_lite",
        src_out = "com/google/mediapipe/calculator/proto/InferenceCalculatorProto.java",
    ))

    android_library(
        name = name,
        srcs = srcs + [
                   "//mediapipe/java/com/google/mediapipe/framework:java_src",
               ] + mediapipe_java_proto_srcs() +
               mediapipe_tasks_java_proto_srcs,
        javacopts = [
            "-Xep:AndroidJdkLibsChecker:OFF",
        ],
        manifest = manifest,
        deps = [
                   "//mediapipe/calculators/core:flow_limiter_calculator_java_proto_lite",
                   "//mediapipe/calculators/tensor:inference_calculator_java_proto_lite",
                   "//mediapipe/framework:calculator_java_proto_lite",
                   "//mediapipe/framework:calculator_profile_java_proto_lite",
                   "//mediapipe/framework:calculator_options_java_proto_lite",
                   "//mediapipe/framework:mediapipe_options_java_proto_lite",
                   "//mediapipe/framework:packet_factory_java_proto_lite",
                   "//mediapipe/framework:packet_generator_java_proto_lite",
                   "//mediapipe/framework:status_handler_java_proto_lite",
                   "//mediapipe/framework:stream_handler_java_proto_lite",
                   "//mediapipe/framework/formats:classification_java_proto_lite",
                   "//mediapipe/framework/formats:detection_java_proto_lite",
                   "//mediapipe/framework/formats:landmark_java_proto_lite",
                   "//mediapipe/framework/formats:location_data_java_proto_lite",
                   "//mediapipe/framework/formats:rect_java_proto_lite",
                   "//mediapipe/java/com/google/mediapipe/framework:android_framework",
                   "//mediapipe/java/com/google/mediapipe/framework/image",
                   "//mediapipe/tasks/java/com/google/mediapipe/tasks/core/jni:model_resources_cache_jni",
                   "//third_party:androidx_annotation",
                   "//third_party:autovalue",
                   "@com_google_protobuf//:protobuf_javalite",
                   "@maven//:com_google_guava_guava",
                   "@maven//:com_google_flogger_flogger",
                   "@maven//:com_google_flogger_flogger_system_backend",
                   "@maven//:com_google_code_findbugs_jsr305",
               ] +
               _CORE_TASKS_JAVA_PROTO_LITE_TARGETS +
               _VISION_TASKS_JAVA_PROTO_LITE_TARGETS +
               _TEXT_TASKS_JAVA_PROTO_LITE_TARGETS,
    )

def mediapipe_tasks_vision_aar(name, srcs, native_library):
    """Builds medaipipe tasks vision AAR.

    Args:
      name: The bazel target name.
      srcs: MediaPipe Vision Tasks' source files.
      native_library: The native library that contains vision tasks' graph and calculators.
    """

    native.genrule(
        name = name + "tasks_manifest_generator",
        outs = ["AndroidManifest.xml"],
        cmd = """
cat > $(OUTS) <<EOF
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.mediapipe.tasks.vision">
    <uses-sdk
        android:minSdkVersion="24"
        android:targetSdkVersion="30" />
</manifest>
EOF
""",
    )

    _mediapipe_tasks_aar(
        name = name,
        srcs = srcs,
        manifest = "AndroidManifest.xml",
        java_proto_lite_targets = _CORE_TASKS_JAVA_PROTO_LITE_TARGETS + _VISION_TASKS_JAVA_PROTO_LITE_TARGETS,
        native_library = native_library,
    )

def mediapipe_tasks_text_aar(name, srcs, native_library):
    """Builds medaipipe tasks text AAR.

    Args:
      name: The bazel target name.
      srcs: MediaPipe Text Tasks' source files.
      native_library: The native library that contains text tasks' graph and calculators.
    """

    native.genrule(
        name = name + "tasks_manifest_generator",
        outs = ["AndroidManifest.xml"],
        cmd = """
cat > $(OUTS) <<EOF
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.mediapipe.tasks.text">
    <uses-sdk
        android:minSdkVersion="24"
        android:targetSdkVersion="30" />
</manifest>
EOF
""",
    )

    _mediapipe_tasks_aar(
        name = name,
        srcs = srcs,
        manifest = "AndroidManifest.xml",
        java_proto_lite_targets = _CORE_TASKS_JAVA_PROTO_LITE_TARGETS + _TEXT_TASKS_JAVA_PROTO_LITE_TARGETS,
        native_library = native_library,
    )

def _mediapipe_tasks_aar(name, srcs, manifest, java_proto_lite_targets, native_library):
    """Builds medaipipe tasks AAR."""

    # When "--define EXCLUDE_OPENCV_SO_LIB=1" is set in the build command,
    # the OpenCV so libraries will be excluded from the AAR package to
    # save the package size.
    native.config_setting(
        name = "exclude_opencv_so_lib",
        define_values = {
            "EXCLUDE_OPENCV_SO_LIB": "1",
        },
        visibility = ["//visibility:public"],
    )

    native.cc_library(
        name = name + "_jni_opencv_cc_lib",
        srcs = select({
            "//mediapipe:android_arm64": ["@android_opencv//:libopencv_java3_so_arm64-v8a"],
            "//mediapipe:android_armeabi": ["@android_opencv//:libopencv_java3_so_armeabi-v7a"],
            "//mediapipe:android_arm": ["@android_opencv//:libopencv_java3_so_armeabi-v7a"],
            "//mediapipe:android_x86": ["@android_opencv//:libopencv_java3_so_x86"],
            "//mediapipe:android_x86_64": ["@android_opencv//:libopencv_java3_so_x86_64"],
            "//conditions:default": [],
        }),
        alwayslink = 1,
    )

    android_library(
        name = name + "_android_lib",
        srcs = srcs,
        manifest = manifest,
        proguard_specs = ["//mediapipe/java/com/google/mediapipe/framework:proguard.pgcfg"],
        deps = java_proto_lite_targets + [native_library] + [
            "//mediapipe/java/com/google/mediapipe/framework:android_framework",
            "//mediapipe/java/com/google/mediapipe/framework/image",
            "//mediapipe/framework:calculator_options_java_proto_lite",
            "//mediapipe/framework:calculator_java_proto_lite",
            "//mediapipe/framework/formats:classification_java_proto_lite",
            "//mediapipe/framework/formats:detection_java_proto_lite",
            "//mediapipe/framework/formats:landmark_java_proto_lite",
            "//mediapipe/framework/formats:location_data_java_proto_lite",
            "//mediapipe/framework/formats:rect_java_proto_lite",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:detection",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:category",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:classification_entry",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:classifications",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:landmark",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/processors:classifieroptions",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/core",
            "//third_party:autovalue",
            "@maven//:com_google_guava_guava",
        ] + select({
            "//conditions:default": [":" + name + "_jni_opencv_cc_lib"],
            "//mediapipe/framework/port:disable_opencv": [],
            "exclude_opencv_so_lib": [],
        }),
    )

    mediapipe_build_aar_with_jni(name, name + "_android_lib")

def _mediapipe_tasks_java_proto_src_extractor(target):
    proto_path = "com/google/" + target.split(":")[0].replace("cc/", "").replace("//", "").replace("_", "") + "/"
    proto_name = target.split(":")[-1].replace("_java_proto_lite", "").replace("_", " ").title().replace(" ", "") + "Proto.java"
    return mediapipe_java_proto_src_extractor(
        target = target,
        src_out = proto_path + proto_name,
    )
<commit_msg>Internal change<commit_after># Copyright 2022 The MediaPipe Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Building MediaPipe Tasks AARs."""

load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_build_aar_with_jni", "mediapipe_java_proto_src_extractor", "mediapipe_java_proto_srcs")
load("@build_bazel_rules_android//android:rules.bzl", "android_library")

_CORE_TASKS_JAVA_PROTO_LITE_TARGETS = [
    "//mediapipe/tasks/cc/components/containers/proto:category_java_proto_lite",
    "//mediapipe/tasks/cc/components/containers/proto:classifications_java_proto_lite",
    "//mediapipe/tasks/cc/components/containers/proto:embeddings_java_proto_lite",
    "//mediapipe/tasks/cc/components/containers/proto:landmarks_detection_result_java_proto_lite",
    "//mediapipe/tasks/cc/components/proto:segmenter_options_java_proto_lite",
    "//mediapipe/tasks/cc/components/processors/proto:classifier_options_java_proto_lite",
    "//mediapipe/tasks/cc/components/processors/proto:embedder_options_java_proto_lite",
    "//mediapipe/tasks/cc/core/proto:acceleration_java_proto_lite",
    "//mediapipe/tasks/cc/core/proto:base_options_java_proto_lite",
    "//mediapipe/tasks/cc/core/proto:external_file_java_proto_lite",
]

_VISION_TASKS_JAVA_PROTO_LITE_TARGETS = [
    "//mediapipe/tasks/cc/vision/object_detector/proto:object_detector_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/image_classifier/proto:image_classifier_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:gesture_classifier_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:gesture_embedder_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:gesture_recognizer_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/gesture_recognizer/proto:hand_gesture_recognizer_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/image_segmenter/proto:image_segmenter_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/hand_detector/proto:hand_detector_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/hand_landmarker/proto:hand_landmarker_graph_options_java_proto_lite",
    "//mediapipe/tasks/cc/vision/hand_landmarker/proto:hand_landmarks_detector_graph_options_java_proto_lite",
]

_TEXT_TASKS_JAVA_PROTO_LITE_TARGETS = [
    "//mediapipe/tasks/cc/text/text_classifier/proto:text_classifier_graph_options_java_proto_lite",
]

def mediapipe_tasks_core_aar(name, srcs, manifest):
    """Builds medaipipe tasks core AAR.

    Args:
      name: The bazel target name.
      srcs: MediaPipe Tasks' core layer source files.
      manifest: The Android manifest.
    """

    mediapipe_tasks_java_proto_srcs = []
    for target in _CORE_TASKS_JAVA_PROTO_LITE_TARGETS:
        mediapipe_tasks_java_proto_srcs.append(
            _mediapipe_tasks_java_proto_src_extractor(target = target),
        )

    for target in _VISION_TASKS_JAVA_PROTO_LITE_TARGETS:
        mediapipe_tasks_java_proto_srcs.append(
            _mediapipe_tasks_java_proto_src_extractor(target = target),
        )

    for target in _TEXT_TASKS_JAVA_PROTO_LITE_TARGETS:
        mediapipe_tasks_java_proto_srcs.append(
            _mediapipe_tasks_java_proto_src_extractor(target = target),
        )

    mediapipe_tasks_java_proto_srcs.append(mediapipe_java_proto_src_extractor(
        target = "//mediapipe/calculators/core:flow_limiter_calculator_java_proto_lite",
        src_out = "com/google/mediapipe/calculator/proto/FlowLimiterCalculatorProto.java",
    ))

    mediapipe_tasks_java_proto_srcs.append(mediapipe_java_proto_src_extractor(
        target = "//mediapipe/calculators/tensor:inference_calculator_java_proto_lite",
        src_out = "com/google/mediapipe/calculator/proto/InferenceCalculatorProto.java",
    ))

    android_library(
        name = name,
        srcs = srcs + [
                   "//mediapipe/java/com/google/mediapipe/framework:java_src",
               ] + mediapipe_java_proto_srcs() +
               mediapipe_tasks_java_proto_srcs,
        javacopts = [
            "-Xep:AndroidJdkLibsChecker:OFF",
        ],
        manifest = manifest,
        deps = [
                   "//mediapipe/calculators/core:flow_limiter_calculator_java_proto_lite",
                   "//mediapipe/calculators/tensor:inference_calculator_java_proto_lite",
                   "//mediapipe/framework:calculator_java_proto_lite",
                   "//mediapipe/framework:calculator_profile_java_proto_lite",
                   "//mediapipe/framework:calculator_options_java_proto_lite",
                   "//mediapipe/framework:mediapipe_options_java_proto_lite",
                   "//mediapipe/framework:packet_factory_java_proto_lite",
                   "//mediapipe/framework:packet_generator_java_proto_lite",
                   "//mediapipe/framework:status_handler_java_proto_lite",
                   "//mediapipe/framework:stream_handler_java_proto_lite",
                   "//mediapipe/framework/formats:classification_java_proto_lite",
                   "//mediapipe/framework/formats:detection_java_proto_lite",
                   "//mediapipe/framework/formats:landmark_java_proto_lite",
                   "//mediapipe/framework/formats:location_data_java_proto_lite",
                   "//mediapipe/framework/formats:rect_java_proto_lite",
                   "//mediapipe/java/com/google/mediapipe/framework:android_framework",
                   "//mediapipe/java/com/google/mediapipe/framework/image",
                   "//mediapipe/tasks/java/com/google/mediapipe/tasks/core/jni:model_resources_cache_jni",
                   "//third_party:androidx_annotation",
                   "//third_party:autovalue",
                   "@com_google_protobuf//:protobuf_javalite",
                   "@maven//:com_google_guava_guava",
                   "@maven//:com_google_flogger_flogger",
                   "@maven//:com_google_flogger_flogger_system_backend",
                   "@maven//:com_google_code_findbugs_jsr305",
               ] +
               _CORE_TASKS_JAVA_PROTO_LITE_TARGETS +
               _VISION_TASKS_JAVA_PROTO_LITE_TARGETS +
               _TEXT_TASKS_JAVA_PROTO_LITE_TARGETS,
    )

def mediapipe_tasks_vision_aar(name, srcs, native_library):
    """Builds medaipipe tasks vision AAR.

    Args:
      name: The bazel target name.
      srcs: MediaPipe Vision Tasks' source files.
      native_library: The native library that contains vision tasks' graph and calculators.
    """

    native.genrule(
        name = name + "tasks_manifest_generator",
        outs = ["AndroidManifest.xml"],
        cmd = """
cat > $(OUTS) <<EOF
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.mediapipe.tasks.vision">
    <uses-sdk
        android:minSdkVersion="24"
        android:targetSdkVersion="30" />
</manifest>
EOF
""",
    )

    _mediapipe_tasks_aar(
        name = name,
        srcs = srcs,
        manifest = "AndroidManifest.xml",
        java_proto_lite_targets = _CORE_TASKS_JAVA_PROTO_LITE_TARGETS + _VISION_TASKS_JAVA_PROTO_LITE_TARGETS,
        native_library = native_library,
    )

def mediapipe_tasks_text_aar(name, srcs, native_library):
    """Builds medaipipe tasks text AAR.

    Args:
      name: The bazel target name.
      srcs: MediaPipe Text Tasks' source files.
      native_library: The native library that contains text tasks' graph and calculators.
    """

    native.genrule(
        name = name + "tasks_manifest_generator",
        outs = ["AndroidManifest.xml"],
        cmd = """
cat > $(OUTS) <<EOF
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.mediapipe.tasks.text">
    <uses-sdk
        android:minSdkVersion="24"
        android:targetSdkVersion="30" />
</manifest>
EOF
""",
    )

    _mediapipe_tasks_aar(
        name = name,
        srcs = srcs,
        manifest = "AndroidManifest.xml",
        java_proto_lite_targets = _CORE_TASKS_JAVA_PROTO_LITE_TARGETS + _TEXT_TASKS_JAVA_PROTO_LITE_TARGETS,
        native_library = native_library,
    )

def _mediapipe_tasks_aar(name, srcs, manifest, java_proto_lite_targets, native_library):
    """Builds medaipipe tasks AAR."""

    # When "--define EXCLUDE_OPENCV_SO_LIB=1" is set in the build command,
    # the OpenCV so libraries will be excluded from the AAR package to
    # save the package size.
    native.config_setting(
        name = "exclude_opencv_so_lib",
        define_values = {
            "EXCLUDE_OPENCV_SO_LIB": "1",
        },
        visibility = ["//visibility:public"],
    )

    native.cc_library(
        name = name + "_jni_opencv_cc_lib",
        srcs = select({
            "//mediapipe:android_arm64": ["@android_opencv//:libopencv_java3_so_arm64-v8a"],
            "//mediapipe:android_armeabi": ["@android_opencv//:libopencv_java3_so_armeabi-v7a"],
            "//mediapipe:android_arm": ["@android_opencv//:libopencv_java3_so_armeabi-v7a"],
            "//mediapipe:android_x86": ["@android_opencv//:libopencv_java3_so_x86"],
            "//mediapipe:android_x86_64": ["@android_opencv//:libopencv_java3_so_x86_64"],
            "//conditions:default": [],
        }),
        alwayslink = 1,
    )

    android_library(
        name = name + "_android_lib",
        srcs = srcs,
        manifest = manifest,
        proguard_specs = ["//mediapipe/java/com/google/mediapipe/framework:proguard.pgcfg"],
        deps = java_proto_lite_targets + [native_library] + [
            "//mediapipe/java/com/google/mediapipe/framework:android_framework",
            "//mediapipe/java/com/google/mediapipe/framework/image",
            "//mediapipe/framework:calculator_options_java_proto_lite",
            "//mediapipe/framework:calculator_java_proto_lite",
            "//mediapipe/framework/formats:classification_java_proto_lite",
            "//mediapipe/framework/formats:detection_java_proto_lite",
            "//mediapipe/framework/formats:landmark_java_proto_lite",
            "//mediapipe/framework/formats:location_data_java_proto_lite",
            "//mediapipe/framework/formats:rect_java_proto_lite",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:detection",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:category",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:classificationresult",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:classifications",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/containers:landmark",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/components/processors:classifieroptions",
            "//mediapipe/tasks/java/com/google/mediapipe/tasks/core",
            "//third_party:autovalue",
            "@maven//:com_google_guava_guava",
        ] + select({
            "//conditions:default": [":" + name + "_jni_opencv_cc_lib"],
            "//mediapipe/framework/port:disable_opencv": [],
            "exclude_opencv_so_lib": [],
        }),
    )

    mediapipe_build_aar_with_jni(name, name + "_android_lib")

def _mediapipe_tasks_java_proto_src_extractor(target):
    proto_path = "com/google/" + target.split(":")[0].replace("cc/", "").replace("//", "").replace("_", "") + "/"
    proto_name = target.split(":")[-1].replace("_java_proto_lite", "").replace("_", " ").title().replace(" ", "") + "Proto.java"
    return mediapipe_java_proto_src_extractor(
        target = target,
        src_out = proto_path + proto_name,
    )
<|endoftext|>


<commit_before>"""Add registration tables

Revision ID: f9bb2a03ceb
Revises: 3b6c768b8803
Create Date: 2015-09-04 15:15:03.682231
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql.ddl import CreateSchema, DropSchema

from indico.core.db.sqlalchemy import PyIntEnum, UTCDateTime
from indico.modules.events.registration.models.registration_forms import RegistrationFormModificationMode

# revision identifiers, used by Alembic.
revision = 'f9bb2a03ceb'
down_revision = '3b6c768b8803'

def upgrade():
    op.execute(CreateSchema('event_registration'))
    op.create_table(
        'registration_forms',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('event_id', sa.Integer(), nullable=False, index=True),
        sa.Column('title', sa.String(), nullable=False),
        sa.Column('introduction', sa.Text(), nullable=False),
        sa.Column('start_dt', UTCDateTime, nullable=True),
        sa.Column('end_dt', UTCDateTime, nullable=True),
        sa.Column('modification_mode', PyIntEnum(RegistrationFormModificationMode), nullable=False),
        sa.Column('modification_end_dt', UTCDateTime, nullable=True),
        sa.Column('is_deleted', sa.Boolean(), nullable=False),
        sa.Column('require_user', sa.Boolean(), nullable=False),
        sa.Column('registration_limit', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['event_id'], ['events.events.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_table(
        'registration_form_items',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('registration_form_id', sa.Integer(), nullable=False),
        sa.Column('type', sa.Integer(), nullable=False),
        sa.Column('parent_id', sa.Integer(), nullable=True),
        sa.Column('position', sa.Integer(), nullable=False),
        sa.Column('title', sa.String(), nullable=False),
        sa.Column('description', sa.String(), nullable=True),
        sa.Column('is_enabled', sa.Boolean(), nullable=True),
        sa.Column('is_deleted', sa.Boolean(), nullable=True),
        sa.Column('current_data_id', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['parent_id'], ['event_registration.registration_form_items.id']),
        sa.ForeignKeyConstraint(['registration_form_id'], ['event_registration.registration_forms.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_table(
        'registration_form_field_data',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('field_id', sa.Integer(), nullable=False),
        sa.Column('data', postgresql.JSON(), nullable=False),
        sa.ForeignKeyConstraint(['field_id'], ['event_registration.registration_form_items.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_foreign_key(None,
                          'registration_form_items', 'registration_form_field_data',
                          ['current_data_id'], ['id'],
                          source_schema='event_registration', referent_schema='event_registration')

    op.create_table(
        'registrations',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('registration_form_id', sa.Integer(), nullable=False, index=True),
        sa.Column('user_id', sa.Integer(), nullable=True, index=True),
        sa.Column('submitted_dt', UTCDateTime, nullable=False),
        sa.ForeignKeyConstraint(['registration_form_id'], ['event_registration.registration_forms.id']),
        sa.ForeignKeyConstraint(['user_id'], ['users.users.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_table(
        'registration_data',
        sa.Column('registration_id', sa.Integer(), nullable=False),
        sa.Column('field_data_id', sa.Integer(), nullable=False),
        sa.Column('data', postgresql.JSON(), nullable=False),
        sa.Column('file', sa.LargeBinary(), nullable=True),
        sa.Column('file_metadata', postgresql.JSON(), nullable=False),
        sa.CheckConstraint("(file IS NULL) = (file_metadata::text = 'null')", name='valid_file'),
        sa.ForeignKeyConstraint(['field_data_id'], ['event_registration.registration_form_field_data.id']),
        sa.ForeignKeyConstraint(['registration_id'], ['event_registration.registrations.id']),
        sa.PrimaryKeyConstraint('registration_id', 'field_data_id'),
        schema='event_registration'
    )

def downgrade():
    op.drop_column('registration_form_items', 'current_data_id', schema='event_registration')
    op.drop_table('registration_data', schema='event_registration')
    op.drop_table('registrations', schema='event_registration')
    op.drop_table('registration_form_field_data', schema='event_registration')
    op.drop_table('registration_form_items', schema='event_registration')
    op.drop_table('registration_forms', schema='event_registration')
    op.execute(DropSchema('event_registration'))
<commit_msg>Drop foreign key instead of dropping the column<commit_after>"""Add registration tables

Revision ID: f9bb2a03ceb
Revises: 3b6c768b8803
Create Date: 2015-09-04 15:15:03.682231
"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from sqlalchemy.sql.ddl import CreateSchema, DropSchema

from indico.core.db.sqlalchemy import PyIntEnum, UTCDateTime
from indico.modules.events.registration.models.registration_forms import RegistrationFormModificationMode

# revision identifiers, used by Alembic.
revision = 'f9bb2a03ceb'
down_revision = '3b6c768b8803'

def upgrade():
    op.execute(CreateSchema('event_registration'))
    op.create_table(
        'registration_forms',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('event_id', sa.Integer(), nullable=False, index=True),
        sa.Column('title', sa.String(), nullable=False),
        sa.Column('introduction', sa.Text(), nullable=False),
        sa.Column('start_dt', UTCDateTime, nullable=True),
        sa.Column('end_dt', UTCDateTime, nullable=True),
        sa.Column('modification_mode', PyIntEnum(RegistrationFormModificationMode), nullable=False),
        sa.Column('modification_end_dt', UTCDateTime, nullable=True),
        sa.Column('is_deleted', sa.Boolean(), nullable=False),
        sa.Column('require_user', sa.Boolean(), nullable=False),
        sa.Column('registration_limit', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['event_id'], ['events.events.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_table(
        'registration_form_items',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('registration_form_id', sa.Integer(), nullable=False),
        sa.Column('type', sa.Integer(), nullable=False),
        sa.Column('parent_id', sa.Integer(), nullable=True),
        sa.Column('position', sa.Integer(), nullable=False),
        sa.Column('title', sa.String(), nullable=False),
        sa.Column('description', sa.String(), nullable=True),
        sa.Column('is_enabled', sa.Boolean(), nullable=True),
        sa.Column('is_deleted', sa.Boolean(), nullable=True),
        sa.Column('current_data_id', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['parent_id'], ['event_registration.registration_form_items.id']),
        sa.ForeignKeyConstraint(['registration_form_id'], ['event_registration.registration_forms.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_table(
        'registration_form_field_data',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('field_id', sa.Integer(), nullable=False),
        sa.Column('data', postgresql.JSON(), nullable=False),
        sa.ForeignKeyConstraint(['field_id'], ['event_registration.registration_form_items.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_foreign_key(None,
                          'registration_form_items', 'registration_form_field_data',
                          ['current_data_id'], ['id'],
                          source_schema='event_registration', referent_schema='event_registration')

    op.create_table(
        'registrations',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('registration_form_id', sa.Integer(), nullable=False, index=True),
        sa.Column('user_id', sa.Integer(), nullable=True, index=True),
        sa.Column('submitted_dt', UTCDateTime, nullable=False),
        sa.ForeignKeyConstraint(['registration_form_id'], ['event_registration.registration_forms.id']),
        sa.ForeignKeyConstraint(['user_id'], ['users.users.id']),
        sa.PrimaryKeyConstraint('id'),
        schema='event_registration'
    )

    op.create_table(
        'registration_data',
        sa.Column('registration_id', sa.Integer(), nullable=False),
        sa.Column('field_data_id', sa.Integer(), nullable=False),
        sa.Column('data', postgresql.JSON(), nullable=False),
        sa.Column('file', sa.LargeBinary(), nullable=True),
        sa.Column('file_metadata', postgresql.JSON(), nullable=False),
        sa.CheckConstraint("(file IS NULL) = (file_metadata::text = 'null')", name='valid_file'),
        sa.ForeignKeyConstraint(['field_data_id'], ['event_registration.registration_form_field_data.id']),
        sa.ForeignKeyConstraint(['registration_id'], ['event_registration.registrations.id']),
        sa.PrimaryKeyConstraint('registration_id', 'field_data_id'),
        schema='event_registration'
    )

def downgrade():
    op.drop_constraint('fk_registration_form_items_current_data_id_registration_form_field_data',
                       'registration_form_items', schema='event_registration')
    op.drop_table('registration_data', schema='event_registration')
    op.drop_table('registrations', schema='event_registration')
    op.drop_table('registration_form_field_data', schema='event_registration')
    op.drop_table('registration_form_items', schema='event_registration')
    op.drop_table('registration_forms', schema='event_registration')
    op.execute(DropSchema('event_registration'))
<|endoftext|>


<commit_before>from django.urls import include, path
from django.views.generic import TemplateView
from rest_framework import routers
from crowdfunding.api.views import ProjectInvestmentViewSet

#import djstripe
#from crowdfunding.views3 import show_checkout, new_checkout, create_checkout
from crowdfunding.views import stripe_checkout, ProjectInvestmentView, InvestViewBase
from crowdfunding.views import charge
app_name = 'crowdfunding'

router = routers.DefaultRouter()
router.register(r'projectinvestment', ProjectInvestmentViewSet)

urlpatterns = [
    path('', InvestViewBase.as_view(), name='start'),
    path('api/', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('presentation/', TemplateView.as_view(template_name="crowdfunding/crowdfunding.html")),
    path('charge/', charge, name='stripe-charge'),
    #path('pay/', process_payment, name="pay"),
    path('pay/', stripe_checkout, name="stripe-checkout"),
    #path('paypal/', include('paypal.standard.ipn.urls'), name="paypal-ipn"),
    #path('payment-done/', payment_done, name='payment_done'),
    #path('payment-cancelled/', payment_canceled, name='payment_cancelled'),
    path('fund/', ProjectInvestmentView.as_view(), name="fund"),
    path('progress/', TemplateView.as_view(template_name='crowdfunding/progress_template.html'), name='progress'),
    #path('stripe/', include("djstripe.urls", namespace="djstripe")),
    #path('checkouts/new', new_checkout, name="new-checkout"),
    #path('checkouts/<transaction_id>', show_checkout, name="show-checkout"),
    #re_path('checkouts/', create_checkout, name="create-checkout"),
    #re_path('braintree/(?P<amount>\d+)', CheckoutView.as_view(), name="braintree"),
    #re_path(r'^braintree/(?:(?P<amount>\d+)/)?$', CheckoutView.as_view(), name="braintree"),
    #re_path('ok/(?P<amount>\d+)', TemplateView.as_view(template_name="crowdfunding/ok.html"), name='crowdfunding-ok'),
    #re_path('pay/(?P<amount>\d+)', views2.process_payment, name="pay"),
]<commit_msg>make investment view login required<commit_after>from django.urls import include, path
from django.views.generic import TemplateView
from rest_framework import routers
from crowdfunding.api.views import ProjectInvestmentViewSet
from django.contrib.auth.decorators import login_required
#import djstripe
#from crowdfunding.views3 import show_checkout, new_checkout, create_checkout
from crowdfunding.views import stripe_checkout, ProjectInvestmentView, InvestViewBase
from crowdfunding.views import charge
app_name = 'crowdfunding'

router = routers.DefaultRouter()
router.register(r'projectinvestment', ProjectInvestmentViewSet)

urlpatterns = [
    path('', login_required(InvestViewBase.as_view()), name='start'),
    path('api/', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('presentation/', TemplateView.as_view(template_name="crowdfunding/crowdfunding.html")),
    path('charge/', charge, name='stripe-charge'),
    #path('pay/', process_payment, name="pay"),
    path('pay/', stripe_checkout, name="stripe-checkout"),
    #path('paypal/', include('paypal.standard.ipn.urls'), name="paypal-ipn"),
    #path('payment-done/', payment_done, name='payment_done'),
    #path('payment-cancelled/', payment_canceled, name='payment_cancelled'),
    path('fund/', ProjectInvestmentView.as_view(), name="fund"),
    path('progress/', TemplateView.as_view(template_name='crowdfunding/progress_template.html'), name='progress'),
    #path('stripe/', include("djstripe.urls", namespace="djstripe")),
    #path('checkouts/new', new_checkout, name="new-checkout"),
    #path('checkouts/<transaction_id>', show_checkout, name="show-checkout"),
    #re_path('checkouts/', create_checkout, name="create-checkout"),
    #re_path('braintree/(?P<amount>\d+)', CheckoutView.as_view(), name="braintree"),
    #re_path(r'^braintree/(?:(?P<amount>\d+)/)?$', CheckoutView.as_view(), name="braintree"),
    #re_path('ok/(?P<amount>\d+)', TemplateView.as_view(template_name="crowdfunding/ok.html"), name='crowdfunding-ok'),
    #re_path('pay/(?P<amount>\d+)', views2.process_payment, name="pay"),
]<|endoftext|>


<commit_before>"""Select2 view implementation."""

import collections
# import json

from dal.views import BaseQuerySetView, ViewMixin

from django import http
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
from django.utils.translation import ugettext as _
from django.views.generic.list import View

class Select2ViewMixin(object):
    """View mixin to render a JSON response for Select2."""

    def get_results(self, context):
        """Return data for the 'results' key of the response."""
        return [
            {
                'id': self.get_result_value(result),
                'text': self.get_result_label(result),
                'selected_text': self.get_selected_result_label(result),
            } for result in context['object_list']
        ]

    def get_create_option(self, context, q):
        """Form the correct create_option to append to results."""
        create_option = []
        display_create_option = False
        if self.create_field and q:
            page_obj = context.get('page_obj', None)
            if page_obj is None or page_obj.number == 1:
                display_create_option = True

            # Don't offer to create a new option if a
            # case-insensitive) identical one already exists
            existing_options = (self.get_result_label(result).lower()
                                for result in context['object_list'])
            if q.lower() in existing_options:
                display_create_option = False

        if display_create_option and self.has_add_permission(self.request):
            create_option = [{
                'id': q,
                'text': _('Create "%(new_value)s"') % {'new_value': q},
                'create_id': True,
            }]
        return create_option

    def render_to_response(self, context):
        """Return a JSON response in Select2 format."""
        q = self.request.GET.get('q', None)

        create_option = self.get_create_option(context, q)

        return http.JsonResponse(
            {
                'results': self.get_results(context) + create_option,
                'pagination': {
                    'more': self.has_more(context)
                }
            })

class Select2QuerySetView(Select2ViewMixin, BaseQuerySetView):
    """List options for a Select2 widget."""

class Select2ListView(ViewMixin, View):
    """Autocomplete from a list of items rather than a QuerySet."""

    def get_list(self):
        """Return the list strings from which to autocomplete."""
        return []

    def get(self, request, *args, **kwargs):
        """Return option list json response."""
        results = self.get_list()
        create_option = []
        if self.q:
            results = self.autocomplete_results(results)
            if hasattr(self, 'create'):
                create_option = [{
                    'id': self.q,
                    'text': _('Create "%(new_value)s"') % {'new_value': q},
                    'create_id': True
                }]
        return http.JsonResponse({
            'results': self.results(results) + create_option
        }, content_type='application/json')

    def autocomplete_results(self, results):
        """Return list of strings that match the autocomplete query."""
        return [x for x in results if self.q.lower() in x.lower()]

    def results(self, results):
        """Return the result dictionary."""
        return [dict(id=x, text=x) for x in results]

    def post(self, request):
        """Add an option to the autocomplete list.

        If 'text' is not defined in POST or self.create(text) fails, raises
        bad request. Raises ImproperlyConfigured if self.create if not defined.
        """
        if not hasattr(self, 'create'):
            raise ImproperlyConfigured('Missing "create()"')

        text = request.POST.get('text', None)

        if text is None:
            return http.HttpResponseBadRequest()

        text = self.create(text)

        if text is None:
            return http.HttpResponseBadRequest()

        return http.JsonResponse({
            'id': text,
            'text': text,
        })

class Select2GroupListView(Select2ListView):
    """View mixin for grouped options."""

    def get_item_as_group(self, entry):
        """Return the item with its group."""
        group = None
        value = entry

        if isinstance(entry, collections.Sequence) and \
           not isinstance(entry, six.string_types):

            entry_length = len(entry)
            if(entry_length > 1):
                group, value = entry[0:2]
            elif(entry_length > 0):
                value = entry[0]

        if not isinstance(value, collections.Sequence) or \
           isinstance(value, six.string_types):
            value = (value,)

        return (group, value),

    def get(self, request, *args, **kwargs):
        """Return option list with children(s) json response."""
        results_dict = {}
        results = self.get_list()

        if results:
            flat_results = [(group, item) for entry in results
                            for group, items in self.get_item_as_group(entry)
                            for item in items]

            if self.q:
                q = self.q.lower()
                flat_results = [(g, x) for g, x in flat_results
                                if q in x.lower()]
            for group, value in flat_results:
                results_dict.setdefault(group, [])
                results_dict[group].append(value)

        return http.JsonResponse({
            "results":
                [{"id": x, "text": x} for x in results_dict.pop(None, [])] +
                [{"id": g, "text": g, "children": [{"id": x, "text": x}
                                                   for x in l]}
                 for g, l in six.iteritems(results_dict)]
        })
<commit_msg>Change q to self.q because q does not exist in that scope<commit_after>"""Select2 view implementation."""

import collections
# import json

from dal.views import BaseQuerySetView, ViewMixin

from django import http
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
from django.utils.translation import ugettext as _
from django.views.generic.list import View

class Select2ViewMixin(object):
    """View mixin to render a JSON response for Select2."""

    def get_results(self, context):
        """Return data for the 'results' key of the response."""
        return [
            {
                'id': self.get_result_value(result),
                'text': self.get_result_label(result),
                'selected_text': self.get_selected_result_label(result),
            } for result in context['object_list']
        ]

    def get_create_option(self, context, q):
        """Form the correct create_option to append to results."""
        create_option = []
        display_create_option = False
        if self.create_field and q:
            page_obj = context.get('page_obj', None)
            if page_obj is None or page_obj.number == 1:
                display_create_option = True

            # Don't offer to create a new option if a
            # case-insensitive) identical one already exists
            existing_options = (self.get_result_label(result).lower()
                                for result in context['object_list'])
            if q.lower() in existing_options:
                display_create_option = False

        if display_create_option and self.has_add_permission(self.request):
            create_option = [{
                'id': q,
                'text': _('Create "%(new_value)s"') % {'new_value': q},
                'create_id': True,
            }]
        return create_option

    def render_to_response(self, context):
        """Return a JSON response in Select2 format."""
        q = self.request.GET.get('q', None)

        create_option = self.get_create_option(context, q)

        return http.JsonResponse(
            {
                'results': self.get_results(context) + create_option,
                'pagination': {
                    'more': self.has_more(context)
                }
            })

class Select2QuerySetView(Select2ViewMixin, BaseQuerySetView):
    """List options for a Select2 widget."""

class Select2ListView(ViewMixin, View):
    """Autocomplete from a list of items rather than a QuerySet."""

    def get_list(self):
        """Return the list strings from which to autocomplete."""
        return []

    def get(self, request, *args, **kwargs):
        """Return option list json response."""
        results = self.get_list()
        create_option = []
        if self.q:
            results = self.autocomplete_results(results)
            if hasattr(self, 'create'):
                create_option = [{
                    'id': self.q,
                    'text': _('Create "%(new_value)s"') % {'new_value': self.q},
                    'create_id': True
                }]
        return http.JsonResponse({
            'results': self.results(results) + create_option
        }, content_type='application/json')

    def autocomplete_results(self, results):
        """Return list of strings that match the autocomplete query."""
        return [x for x in results if self.q.lower() in x.lower()]

    def results(self, results):
        """Return the result dictionary."""
        return [dict(id=x, text=x) for x in results]

    def post(self, request):
        """Add an option to the autocomplete list.

        If 'text' is not defined in POST or self.create(text) fails, raises
        bad request. Raises ImproperlyConfigured if self.create if not defined.
        """
        if not hasattr(self, 'create'):
            raise ImproperlyConfigured('Missing "create()"')

        text = request.POST.get('text', None)

        if text is None:
            return http.HttpResponseBadRequest()

        text = self.create(text)

        if text is None:
            return http.HttpResponseBadRequest()

        return http.JsonResponse({
            'id': text,
            'text': text,
        })

class Select2GroupListView(Select2ListView):
    """View mixin for grouped options."""

    def get_item_as_group(self, entry):
        """Return the item with its group."""
        group = None
        value = entry

        if isinstance(entry, collections.Sequence) and \
           not isinstance(entry, six.string_types):

            entry_length = len(entry)
            if(entry_length > 1):
                group, value = entry[0:2]
            elif(entry_length > 0):
                value = entry[0]

        if not isinstance(value, collections.Sequence) or \
           isinstance(value, six.string_types):
            value = (value,)

        return (group, value),

    def get(self, request, *args, **kwargs):
        """Return option list with children(s) json response."""
        results_dict = {}
        results = self.get_list()

        if results:
            flat_results = [(group, item) for entry in results
                            for group, items in self.get_item_as_group(entry)
                            for item in items]

            if self.q:
                q = self.q.lower()
                flat_results = [(g, x) for g, x in flat_results
                                if q in x.lower()]
            for group, value in flat_results:
                results_dict.setdefault(group, [])
                results_dict[group].append(value)

        return http.JsonResponse({
            "results":
                [{"id": x, "text": x} for x in results_dict.pop(None, [])] +
                [{"id": g, "text": g, "children": [{"id": x, "text": x}
                                                   for x in l]}
                 for g, l in six.iteritems(results_dict)]
        })
<|endoftext|>


<commit_before>import os
import logging
from netCDF4 import Dataset

from climate.pyom import PyOMLegacy, diagnostics, pyom_method

BASE_PATH = os.path.dirname(os.path.realpath(__file__))
DATA_FILES = dict(
    dz = "dz.npy",
    temperature = "lev_t.npy",
    salt = "lev_s.npy",
    sss = "lev_sss.npy",
    sst = "lev_sst.npy",
    tau_x = "trenberth_taux.npy",
    tau_y = "trenberth_tauy.npy",
    q_net = "ncep_qnet.npy",
    bathymetry = "bathymetry.npy",
    tidal_energy = "tidal_energy.npy",
    wind_energy = "wind_energy.npy",
    ecmwf = "ecmwf_4deg_monthly.cdf",
)
DATA_FILES = {key: os.path.join(BASE_PATH, val) for key, val in DATA_FILES.items()}

class GlobalFourDegree(PyOMLegacy):
    """
    global 4 deg model with 15 levels
    """
    @pyom_method
    def set_parameter(self):
        m=self.fortran.main_module

        (m.nx,m.ny,m.nz) = (90,40,15)
        m.dt_mom = 1800.0
        m.dt_tracer = 86400.0

        m.coord_degree = True
        m.enable_cyclic_x = True

        m.congr_epsilon = 1e-8
        m.congr_max_iterations = 20000
        m.enable_streamfunction = True

        m.enable_diag_ts_monitor = True
        m.ts_monint = 365*86400./24.
        m.enable_diag_snapshots  = True
        m.snapint  =  365*86400. /24.0
        m.enable_diag_overturning= True
        m.overint  =  365*86400./24.0
        m.overfreq = m.dt_tracer
        m.enable_diag_energy     = True;
        m.energint = 365*86400./24.
        m.energfreq = 86400
        m.enable_diag_averages   = True
        m.aveint  = 86400.*30
        m.avefreq = 86400

        I=self.fortran.isoneutral_module
        I.enable_neutral_diffusion = True
        I.K_iso_0 = 1000.0
        I.K_iso_steep = 1000.0
        I.iso_dslope=4./1000.0
        I.iso_slopec=1./1000.0
        I.enable_skew_diffusion = True

        m.enable_hor_friction  = True
        m.A_h = (4*m.degtom)**3*2e-11
        m.enable_hor_friction_cos_scaling = True
        m.hor_friction_cosPower = 1

        m.enable_implicit_vert_friction = True
        T=self.fortran.tke_module
        T.enable_tke = True
        T.c_k = 0.1
        T.c_eps = 0.7
        T.alpha_tke = 30.0
        T.mxl_min = 1e-8
        T.tke_mxl_choice = 2
        T.enable_tke_superbee_advection = True

        E=self.fortran.eke_module
        E.enable_eke = True
        E.eke_k_max  = 1e4
        E.eke_c_k    = 0.4
        E.eke_c_eps  = 0.5
        E.eke_cross  = 2.
        E.eke_crhin  = 1.0
        E.eke_lmin   = 100.0
        E.enable_eke_superbee_advection = True

        I=self.fortran.idemix_module
        I.enable_idemix = True
        I.enable_idemix_hor_diffusion = True
        I.enable_eke_diss_surfbot = True
        I.eke_diss_surfbot_frac = 0.2 # fraction which goes into bottom
        I.enable_idemix_superbee_advection = True

        m.eq_of_state_type = 5

    @pyom_method
    def _read_binary(self, var):
        return np.load(DATA_FILES[var])

    @pyom_method
    def set_grid(self):
        m=self.fortran.main_module
        ddz = np.array([50.,70.,100.,140.,190.,240.,290.,340.,390.,440.,490.,540.,590.,640.,690.])
        m.dzt[:] = ddz[::-1]
        m.dxt[:] = 4.0
        m.dyt[:] = 4.0
        m.y_origin = -76.0
        m.x_origin = 4.0

    @pyom_method
    def set_coriolis(self):
        m=self.fortran.main_module
        m.coriolis_t[...] = 2*m.omega*np.sin(m.yt[np.newaxis,:]/180.*m.pi)

    @pyom_method
    def set_topography(self):
        m = self.main_module
        bathymetry_data = self._read_binary("bathymetry")
        salt_data = self._read_binary("salt")[:,:,::-1]
        for k in xrange(m.nz-1, -1, -1):
            mask_salt = salt_data[:,:,k] != 0.
            m.kbot[2:-2, 2:-2][mask_salt] = k+1
        mask_bathy = bathymetry_data == 0
        m.kbot[2:-2, 2:-2][mask_bathy] = 0
        m.kbot[m.kbot == m.nz] = 0

    @pyom_method
    def set_initial_conditions(self):
        """ setup initial conditions
        """
        m = self.fortran.main_module
        self.taux, self.tauy, self.qnec, self.qnet, self.sss_clim, self.sst_clim = (np.zeros((m.nx+4,m.ny+4,12)) for _ in range(6))

        # initial conditions for T and S
        temp_data = self._read_binary("temperature")[:,:,::-1]
        print(temp_data.shape)
        m.temp[2:-2,2:-2,:,:2] = temp_data[...,np.newaxis] * m.maskT[2:-2,2:-2,:,np.newaxis]

        salt_data = self._read_binary("salt")[:,:,::-1]
        m.salt[2:-2,2:-2,:,:2] = salt_data[...,np.newaxis] * m.maskT[2:-2,2:-2,:,np.newaxis]

        # use Trenberth wind stress from MITgcm instead of ECMWF (also contained in ecmwf_4deg.cdf)
        self.taux[2:-2,2:-2,:] = self._read_binary("tau_x") / m.rho_0
        self.tauy[2:-2,2:-2,:] = self._read_binary("tau_y") / m.rho_0

        # heat flux
        with Dataset(DATA_FILES["ecmwf"],"r") as ecmwf_data:
            self.qnec[2:-2,2:-2,:] = np.array(ecmwf_data.variables["Q3"]).transpose()
            self.qnec[self.qnec <= -1e10] = 0.0

        q = self._read_binary("q_net")
        self.qnet[2:-2,2:-2,:] = -q
        self.qnet[self.qnet <= -1e10] = 0.0

        fxa = np.sum(self.qnet[2:-2, 2:-2, :] * m.area_t[2:-2, 2:-2, np.newaxis]) / 12 / np.sum(m.area_t[2:-2, 2:-2])
        logging.info(" removing an annual mean heat flux imbalance of %e W/m^2" % fxa)
        self.qnet[...] = (self.qnet - fxa) * m.maskT[: ,: , -1, np.newaxis]

        # SST and SSS
        self.sst_clim[2:-2,2:-2,:] = self._read_binary("sst")
        self.sss_clim[2:-2,2:-2,:] = self._read_binary("sss")

        idm = self.fortran.idemix_module
        if idm.enable_idemix:
            idm.forc_iw_bottom[2:-2,2:-2] = self._read_binary("tidal_energy") / m.rho_0
            idm.forc_iw_surface[2:-2,2:-2] = self._read_binary("wind_energy") / m.rho_0 * 0.2

    @pyom_method
    def _get_periodic_interval(self, currentTime, cycleLength, recSpacing, nbrec):
        # interpolation routine taken from mitgcm
        locTime = currentTime - recSpacing * 0.5 + cycleLength * (2 - round(currentTime / cycleLength))
        tmpTime = locTime % cycleLength
        tRec1 = int(tmpTime / recSpacing)
        tRec2 = int(tmpTime % nbrec)
        wght2 = (tmpTime - recSpacing * tRec1) / recSpacing
        wght1 = 1. - wght2
        return (tRec1, wght1), (tRec2, wght2)

    @pyom_method
    def set_forcing(self):
        m=self.fortran.main_module

        (n1,f1), (n2,f2) = self._get_periodic_interval((m.itt - 1) * m.dt_tracer, 365*86400.0, 365*86400./12., 12)

        # wind stress
        m.surface_taux[:]=(f1*self.taux[:,:,n1] + f2*self.taux[:,:,n2])
        m.surface_tauy[:]=(f1*self.tauy[:,:,n1] + f2*self.tauy[:,:,n2])

        # tke flux
        tkm=self.fortran.tke_module
        if tkm.enable_tke:
            tkm.forc_tke_surface[1:-1,1:-1] = np.sqrt((0.5*(m.surface_taux[1:-1,1:-1] + m.surface_taux[:-2,1:-1]))**2  \
                                            + (0.5 * (m.surface_tauy[1:-1,1:-1] + m.surface_tauy[1:-1,:-2]))**2)**(3./2.)
        # heat flux : W/m^2 K kg/J m^3/kg = K m/s
        cp_0 = 3991.86795711963
        sst  =  f1*self.sst_clim[:,:,n1] + f2*self.sst_clim[:,:,n2]
        qnec =  f1*self.qnec[:,:,n1] + f2*self.qnec[:,:,n2]
        qnet =  f1*self.qnet[:,:,n1] + f2*self.qnet[:,:,n2]
        m.forc_temp_surface[:] = (qnet + qnec * (sst - m.temp[:,:,-1,self.get_tau()])) * m.maskT[:,:,-1] / cp_0 / m.rho_0

        # salinity restoring
        t_rest= 30 * 86400.0
        sss = f1*self.sss_clim[:,:,n1] + f2*self.sss_clim[:,:,n2]
        m.forc_salt_surface[:] = 1. / t_rest * (sss - m.salt[:,:,-1,self.get_tau()]) * m.maskT[:,:,-1] * m.dzt[-1]

        # apply simple ice mask
        mask = (m.temp[:,:,-1,self.get_tau()] * m.maskT[:,:,-1] <= -1.8) & (m.forc_temp_surface <= 0.0)
        m.forc_temp_surface[mask] = 0.0
        m.forc_salt_surface[mask] = 0.0

        if m.enable_tempsalt_sources:
            m.temp_source[:] = m.maskT * self.rest_tscl * (f1*self.t_star[:,:,:,n1] + f2*self.t_star[:,:,:,n2] - m.temp[:,:,:,self.get_tau()])
            m.salt_source[:] = m.maskT * self.rest_tscl * (f1*self.s_star[:,:,:,n1] + f2*self.s_star[:,:,:,n2] - m.salt[:,:,:,self.get_tau()])

    @pyom_method
    def set_diagnostics(self):
        m = self.fortran.main_module
        for var in ("temp", "salt", "u", "v", "w", "surface_taux", "surface_tauy", "psi"):
            m.variables[var].average = True

if __name__ == "__main__":
    GlobalFourDegree().run(snapint= 86400.0*10, runlen = 86400.*365)
<commit_msg>:lipstick:<commit_after>import os
import logging
from netCDF4 import Dataset

from climate.pyom import PyOMLegacy, diagnostics, pyom_method

BASE_PATH = os.path.dirname(os.path.realpath(__file__))
DATA_FILES = dict(
    dz = "dz.npy",
    temperature = "lev_t.npy",
    salt = "lev_s.npy",
    sss = "lev_sss.npy",
    sst = "lev_sst.npy",
    tau_x = "trenberth_taux.npy",
    tau_y = "trenberth_tauy.npy",
    q_net = "ncep_qnet.npy",
    bathymetry = "bathymetry.npy",
    tidal_energy = "tidal_energy.npy",
    wind_energy = "wind_energy.npy",
    ecmwf = "ecmwf_4deg_monthly.cdf",
)
DATA_FILES = {key: os.path.join(BASE_PATH, val) for key, val in DATA_FILES.items()}

class GlobalFourDegree(PyOMLegacy):
    """
    global 4 deg model with 15 levels
    """
    @pyom_method
    def set_parameter(self):
        m=self.fortran.main_module

        (m.nx,m.ny,m.nz) = (90,40,15)
        m.dt_mom = 1800.0
        m.dt_tracer = 86400.0

        m.coord_degree = True
        m.enable_cyclic_x = True

        m.congr_epsilon = 1e-8
        m.congr_max_iterations = 20000
        m.enable_streamfunction = True

        m.enable_diag_ts_monitor = True
        m.ts_monint = 365*86400./24.
        m.enable_diag_snapshots  = True
        m.snapint  =  365*86400. /24.0
        m.enable_diag_overturning= True
        m.overint  =  365*86400./24.0
        m.overfreq = m.dt_tracer
        m.enable_diag_energy     = True;
        m.energint = 365*86400./24.
        m.energfreq = 86400
        m.enable_diag_averages   = True
        m.aveint  = 86400.*30
        m.avefreq = 86400

        I=self.fortran.isoneutral_module
        I.enable_neutral_diffusion = True
        I.K_iso_0 = 1000.0
        I.K_iso_steep = 1000.0
        I.iso_dslope=4./1000.0
        I.iso_slopec=1./1000.0
        I.enable_skew_diffusion = True

        m.enable_hor_friction  = True
        m.A_h = (4*m.degtom)**3*2e-11
        m.enable_hor_friction_cos_scaling = True
        m.hor_friction_cosPower = 1

        m.enable_implicit_vert_friction = True
        T=self.fortran.tke_module
        T.enable_tke = True
        T.c_k = 0.1
        T.c_eps = 0.7
        T.alpha_tke = 30.0
        T.mxl_min = 1e-8
        T.tke_mxl_choice = 2
        T.enable_tke_superbee_advection = True

        E=self.fortran.eke_module
        E.enable_eke = True
        E.eke_k_max  = 1e4
        E.eke_c_k    = 0.4
        E.eke_c_eps  = 0.5
        E.eke_cross  = 2.
        E.eke_crhin  = 1.0
        E.eke_lmin   = 100.0
        E.enable_eke_superbee_advection = True

        I=self.fortran.idemix_module
        I.enable_idemix = True
        I.enable_idemix_hor_diffusion = True
        I.enable_eke_diss_surfbot = True
        I.eke_diss_surfbot_frac = 0.2 # fraction which goes into bottom
        I.enable_idemix_superbee_advection = True

        m.eq_of_state_type = 5

    @pyom_method
    def _read_binary(self, var):
        return np.load(DATA_FILES[var])

    @pyom_method
    def set_grid(self):
        m=self.fortran.main_module
        ddz = np.array([50.,70.,100.,140.,190.,240.,290.,340.,390.,440.,490.,540.,590.,640.,690.])
        m.dzt[:] = ddz[::-1]
        m.dxt[:] = 4.0
        m.dyt[:] = 4.0
        m.y_origin = -76.0
        m.x_origin = 4.0

    @pyom_method
    def set_coriolis(self):
        m=self.fortran.main_module
        m.coriolis_t[...] = 2*m.omega*np.sin(m.yt[np.newaxis,:]/180.*m.pi)

    @pyom_method
    def set_topography(self):
        m = self.main_module
        bathymetry_data = self._read_binary("bathymetry")
        salt_data = self._read_binary("salt")[:,:,::-1]
        for k in xrange(m.nz-1, -1, -1):
            mask_salt = salt_data[:,:,k] != 0.
            m.kbot[2:-2, 2:-2][mask_salt] = k+1
        mask_bathy = bathymetry_data == 0
        m.kbot[2:-2, 2:-2][mask_bathy] = 0
        m.kbot[m.kbot == m.nz] = 0

    @pyom_method
    def set_initial_conditions(self):
        """ setup initial conditions
        """
        m = self.fortran.main_module
        self.taux, self.tauy, self.qnec, self.qnet, self.sss_clim, self.sst_clim = (np.zeros((m.nx+4,m.ny+4,12)) for _ in range(6))

        # initial conditions for T and S
        temp_data = self._read_binary("temperature")[:,:,::-1]
        m.temp[2:-2,2:-2,:,:2] = temp_data[:,:,,np.newaxis] * m.maskT[2:-2,2:-2,:,np.newaxis]

        salt_data = self._read_binary("salt")[:,:,::-1]
        m.salt[2:-2,2:-2,:,:2] = salt_data[...,np.newaxis] * m.maskT[2:-2,2:-2,:,np.newaxis]

        # use Trenberth wind stress from MITgcm instead of ECMWF (also contained in ecmwf_4deg.cdf)
        self.taux[2:-2,2:-2,:] = self._read_binary("tau_x") / m.rho_0
        self.tauy[2:-2,2:-2,:] = self._read_binary("tau_y") / m.rho_0

        # heat flux
        with Dataset(DATA_FILES["ecmwf"],"r") as ecmwf_data:
            self.qnec[2:-2,2:-2,:] = np.array(ecmwf_data.variables["Q3"]).transpose()
            self.qnec[self.qnec <= -1e10] = 0.0

        q = self._read_binary("q_net")
        self.qnet[2:-2,2:-2,:] = -q
        self.qnet[self.qnet <= -1e10] = 0.0

        fxa = np.sum(self.qnet[2:-2, 2:-2, :] * m.area_t[2:-2, 2:-2, np.newaxis]) / 12 / np.sum(m.area_t[2:-2, 2:-2])
        logging.info(" removing an annual mean heat flux imbalance of %e W/m^2" % fxa)
        self.qnet[...] = (self.qnet - fxa) * m.maskT[: ,: , -1, np.newaxis]

        # SST and SSS
        self.sst_clim[2:-2,2:-2,:] = self._read_binary("sst")
        self.sss_clim[2:-2,2:-2,:] = self._read_binary("sss")

        idm = self.fortran.idemix_module
        if idm.enable_idemix:
            idm.forc_iw_bottom[2:-2,2:-2] = self._read_binary("tidal_energy") / m.rho_0
            idm.forc_iw_surface[2:-2,2:-2] = self._read_binary("wind_energy") / m.rho_0 * 0.2

    @pyom_method
    def _get_periodic_interval(self, currentTime, cycleLength, recSpacing, nbrec):
        # interpolation routine taken from mitgcm
        locTime = currentTime - recSpacing * 0.5 + cycleLength * (2 - round(currentTime / cycleLength))
        tmpTime = locTime % cycleLength
        tRec1 = int(tmpTime / recSpacing)
        tRec2 = int(tmpTime % nbrec)
        wght2 = (tmpTime - recSpacing * tRec1) / recSpacing
        wght1 = 1. - wght2
        return (tRec1, wght1), (tRec2, wght2)

    @pyom_method
    def set_forcing(self):
        m=self.fortran.main_module

        (n1,f1), (n2,f2) = self._get_periodic_interval((m.itt - 1) * m.dt_tracer, 365*86400.0, 365*86400./12., 12)

        # wind stress
        m.surface_taux[:]=(f1*self.taux[:,:,n1] + f2*self.taux[:,:,n2])
        m.surface_tauy[:]=(f1*self.tauy[:,:,n1] + f2*self.tauy[:,:,n2])

        # tke flux
        tkm=self.fortran.tke_module
        if tkm.enable_tke:
            tkm.forc_tke_surface[1:-1,1:-1] = np.sqrt((0.5*(m.surface_taux[1:-1,1:-1] + m.surface_taux[:-2,1:-1]))**2  \
                                            + (0.5 * (m.surface_tauy[1:-1,1:-1] + m.surface_tauy[1:-1,:-2]))**2)**(3./2.)
        # heat flux : W/m^2 K kg/J m^3/kg = K m/s
        cp_0 = 3991.86795711963
        sst  =  f1*self.sst_clim[:,:,n1] + f2*self.sst_clim[:,:,n2]
        qnec =  f1*self.qnec[:,:,n1] + f2*self.qnec[:,:,n2]
        qnet =  f1*self.qnet[:,:,n1] + f2*self.qnet[:,:,n2]
        m.forc_temp_surface[:] = (qnet + qnec * (sst - m.temp[:,:,-1,self.get_tau()])) * m.maskT[:,:,-1] / cp_0 / m.rho_0

        # salinity restoring
        t_rest= 30 * 86400.0
        sss = f1*self.sss_clim[:,:,n1] + f2*self.sss_clim[:,:,n2]
        m.forc_salt_surface[:] = 1. / t_rest * (sss - m.salt[:,:,-1,self.get_tau()]) * m.maskT[:,:,-1] * m.dzt[-1]

        # apply simple ice mask
        mask = (m.temp[:,:,-1,self.get_tau()] * m.maskT[:,:,-1] <= -1.8) & (m.forc_temp_surface <= 0.0)
        m.forc_temp_surface[mask] = 0.0
        m.forc_salt_surface[mask] = 0.0

        if m.enable_tempsalt_sources:
            m.temp_source[:] = m.maskT * self.rest_tscl * (f1*self.t_star[:,:,:,n1] + f2*self.t_star[:,:,:,n2] - m.temp[:,:,:,self.get_tau()])
            m.salt_source[:] = m.maskT * self.rest_tscl * (f1*self.s_star[:,:,:,n1] + f2*self.s_star[:,:,:,n2] - m.salt[:,:,:,self.get_tau()])

    @pyom_method
    def set_diagnostics(self):
        m = self.fortran.main_module
        for var in ("temp", "salt", "u", "v", "w", "surface_taux", "surface_tauy", "psi"):
            m.variables[var].average = True

if __name__ == "__main__":
    GlobalFourDegree().run(snapint= 86400.0*10, runlen = 86400.*365)
<|endoftext|>


<commit_before><commit_msg>cosmetic<commit_after><|endoftext|>


<commit_before><commit_msg>Remove some redundant code from Canvas.tag_bind(), which added all bindings to a dictionary _tagcommands which was otherwise unused. (This was checked in accidentally with rev. 1.125 and not deleted with rev. 1.127 when the other half of this code was removed -- although even as originally checked in the _tagcommands variable was never used.)<commit_after><|endoftext|>


<commit_before>from datetime import timedelta

from faker import Faker
from twisted.internet import reactor

from logger.application import LoggerComponent
from logger.log_repository import LogRepository
from mdstudio.db.impl.mongo_client_wrapper import MongoClientWrapper
from mdstudio.deferred.chainable import test_chainable
from mdstudio.unittest.db import DBTestCase
from mdstudio.utc import now

class TestLogRepository(DBTestCase):
    faker = Faker()

    def setUp(self):
        self.service = LoggerComponent()
        self.db = MongoClientWrapper("localhost", 27127).get_database('users~db')
        self.rep = LogRepository(self.db)
        self.claims = {
            'logType': 'user',
            'username': self.faker.user_name(),
            'group': self.faker.user_name()
        }

        if not reactor.getThreadPool().started:
            reactor.getThreadPool().start()

    def test_construction(self):
        self.assertEqual(self.db, self.rep.db)

    def test_get_log_collection_name(self):
        self.assertEqual(LogRepository.get_log_collection_name({
            'logType': 'user',
            'username': 'test-user'
        }), 'users~test-user')

    def test_get_log_collection_name2(self):
        self.assertEqual(LogRepository.get_log_collection_name({
            'logType': 'group',
            'group': 'test-group'
        }), 'groups~test-group')

    def test_get_log_collection_name3(self):
        self.assertEqual(LogRepository.get_log_collection_name({
            'logType': 'groupRole',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        }), 'grouproles~test-group~test-group-role')

    def test_get_log_collection_name_not_exists(self):
        self.assertRaises(ValueError, LogRepository.get_log_collection_name, {
            'logType': 'random',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        })

    def test_logs(self):
        self.assertEqual(self.rep.logs({
            'logType': 'user',
            'username': 'test-user'
        }).collection, 'users~test-user')

    def test_logs2(self):
        self.assertEqual(self.rep.logs({
            'logType': 'group',
            'group': 'test-group'
        }).collection, 'groups~test-group')

    def test_logs3(self):
        self.assertEqual(self.rep.logs({
            'logType': 'groupRole',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        }).collection, 'grouproles~test-group~test-group-role')

    def test_logs_not_exists(self):
        self.assertRaises(ValueError, self.rep.logs, {
            'logType': 'random',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        })

    @test_chainable
    def test_insert(self):
        dic1 = self.faker.pydict(10, True, 'str', 'str', 'str', 'str', 'float', 'int', 'int', 'uri', 'email')
        dic2 = self.faker.pydict(10, True, 'str', 'str', 'str', 'str', 'float', 'int', 'int', 'uri', 'email')
        yield self.rep.insert(self.claims, [dic1, dic2])
        all = yield self.db.find_many('users~{}'.format(self.claims['username']), {}, projection={'_id': False})['results']

        dic1['tags'] = ['logs']
        dic2['tags'] = ['logs']

        self.assertLessEqual(now() - all[0]['createdAt'], timedelta(seconds=1))
        del all[0]['createdAt']
        dic1['createdBy'] = self.claims
        self.assertEqual(all[0], dic1)

        self.assertLessEqual(now() - all[1]['createdAt'], timedelta(seconds=1))
        del all[1]['createdAt']
        dic2['createdBy'] = self.claims
        self.assertEqual(all[1], dic2)
<commit_msg>Fix missing datetime field in test<commit_after>from datetime import timedelta

from faker import Faker
from twisted.internet import reactor

from logger.application import LoggerComponent
from logger.log_repository import LogRepository
from mdstudio.db.impl.mongo_client_wrapper import MongoClientWrapper
from mdstudio.deferred.chainable import test_chainable
from mdstudio.unittest.db import DBTestCase
from mdstudio.utc import now

class TestLogRepository(DBTestCase):
    faker = Faker()

    def setUp(self):
        self.service = LoggerComponent()
        self.db = MongoClientWrapper("localhost", 27127).get_database('users~db')
        self.rep = LogRepository(self.db)
        self.claims = {
            'logType': 'user',
            'username': self.faker.user_name(),
            'group': self.faker.user_name()
        }

        if not reactor.getThreadPool().started:
            reactor.getThreadPool().start()

    def test_construction(self):
        self.assertEqual(self.db, self.rep.db)

    def test_get_log_collection_name(self):
        self.assertEqual(LogRepository.get_log_collection_name({
            'logType': 'user',
            'username': 'test-user'
        }), 'users~test-user')

    def test_get_log_collection_name2(self):
        self.assertEqual(LogRepository.get_log_collection_name({
            'logType': 'group',
            'group': 'test-group'
        }), 'groups~test-group')

    def test_get_log_collection_name3(self):
        self.assertEqual(LogRepository.get_log_collection_name({
            'logType': 'groupRole',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        }), 'grouproles~test-group~test-group-role')

    def test_get_log_collection_name_not_exists(self):
        self.assertRaises(ValueError, LogRepository.get_log_collection_name, {
            'logType': 'random',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        })

    def test_logs(self):
        self.assertEqual(self.rep.logs({
            'logType': 'user',
            'username': 'test-user'
        }).collection, 'users~test-user')

    def test_logs2(self):
        self.assertEqual(self.rep.logs({
            'logType': 'group',
            'group': 'test-group'
        }).collection, 'groups~test-group')

    def test_logs3(self):
        self.assertEqual(self.rep.logs({
            'logType': 'groupRole',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        }).collection, 'grouproles~test-group~test-group-role')

    def test_logs_not_exists(self):
        self.assertRaises(ValueError, self.rep.logs, {
            'logType': 'random',
            'group': 'test-group',
            'groupRole': 'test-group-role'
        })

    @test_chainable
    def test_insert(self):
        dic1 = self.faker.pydict(10, True, 'str', 'str', 'str', 'str', 'float', 'int', 'int', 'uri', 'email')
        dic2 = self.faker.pydict(10, True, 'str', 'str', 'str', 'str', 'float', 'int', 'int', 'uri', 'email')
        dic1['time'] = self.faker.datetime()
        dic2['time'] = self.faker.datetime()
        yield self.rep.insert(self.claims, [dic1, dic2])
        all = yield self.db.find_many('users~{}'.format(self.claims['username']), {}, projection={'_id': False})['results']

        dic1['tags'] = ['logs']
        dic2['tags'] = ['logs']

        self.assertLessEqual(now() - all[0]['createdAt'], timedelta(seconds=1))
        del all[0]['createdAt']
        dic1['createdBy'] = self.claims
        self.assertEqual(all[0], dic1)

        self.assertLessEqual(now() - all[1]['createdAt'], timedelta(seconds=1))
        del all[1]['createdAt']
        dic2['createdBy'] = self.claims
        self.assertEqual(all[1], dic2)
<|endoftext|>


<commit_before>#! /usr/bin/env python

from .retrieval_info import BASE_DIR, CACHE
from .manualabor import work_laboriousy, chmod, PERM_755, PERM_872
from .find_new_cos_data import find_new_cos_data
from .request_data import run_all_retrievals

# Change permissions on base directory.
chmod(basepath=BASE_DIR, mode=PERM_755, filetype=None, recursive=True

# Run find_new_cos_data
find_new_cos_data(pkl_it=True, use_cs=True, run_labor=True, do_chmod=False)

# Run request_data
run_all_retrievals(prop_dict=None, pkl_file="filestoretrieve.p", prl=True, do_chmod=False)

# Run manualabor
work_laboriously(prl=True, do_chmod=False)

<commit_msg>Fixed 2 typos<commit_after>#! /usr/bin/env python

from .retrieval_info import BASE_DIR, CACHE
from .manualabor import work_laboriously, chmod, PERM_755, PERM_872
from .find_new_cos_data import find_new_cos_data
from .request_data import run_all_retrievals

# Change permissions on base directory.
chmod(basepath=BASE_DIR, mode=PERM_755, filetype=None, recursive=True)

# Run find_new_cos_data
find_new_cos_data(pkl_it=True, use_cs=True, run_labor=True, do_chmod=False)

# Run request_data
run_all_retrievals(prop_dict=None, pkl_file="filestoretrieve.p", prl=True, do_chmod=False)

# Run manualabor
work_laboriously(prl=True, do_chmod=False)

<|endoftext|>


<commit_before>from corehq.apps.locations.models import SQLLocation
from custom.icds_reports.utils import india_now, DATA_NOT_ENTERED
from custom.icds_reports.models.views import TakeHomeRationMonthly

class TakeHomeRationExport(object):
    title = 'Take Home Ration'

    def __init__(self, location, month, loc_level=0, beta=False):
        self.location = location
        self.loc_level = loc_level
        self.month = month
        self.beta = beta

    def get_excel_data(self):

        def _format_infrastructure_data(data):
            return data if data is not None else DATA_NOT_ENTERED

        filters = {"month": self.month, "aggregation_level": 5}

        if self.loc_level == 4:
            filters['supervisor_id'] = self.location
            order_by = ('awc_name',)
        elif self.loc_level == 3:
            filters['block_id'] = self.location
            order_by = ('supervisor_name', 'awc_name')
        elif self.loc_level == 2:
            filters['district_id'] = self.location
            order_by = ('block_name', 'supervisor_name', 'awc_name')
        elif self.loc_level == 1:
            filters['state_id'] = self.location
            order_by = ('district_name', 'block_name', 'supervisor_name', 'awc_name')
        else:
            order_by = ('state_name', 'district_name', 'block_name', 'supervisor_name', 'awc_name')

        query_set = TakeHomeRationMonthly.objects.filter(**filters).order_by(*order_by)

        filters = [['Generated at', india_now()]]

        data = query_set.values('state_name', 'district_name', 'block_name',
                                'supervisor_name', 'awc_name', 'aww_name', 'contact_phone_number',
                                'is_launched', 'total_thr_candidates', 'thr_given_21_days',
                                'thr_distribution_image_count')

        headers = ['State', 'District', 'Block', 'Sector', 'Awc Name', 'AWW Name', 'AWW Phone No.',
                   'Total No. of Beneficiaries eligible for THR',
                   'Total No. of Beneficiaries received THR>=21 days in given month',
                   'Total No of Pictures taken by AWW']

        excel_rows = [headers]

        for row in data:
            row_data = [
                row['state_name'],
                row['district_name'],
                row['block_name'],
                row['supervisor_name'],
                row['awc_name'],
            ]
            if row['is_launched'] != 'yes':
                AWC_NOT_LAUNCHED = 'AWC Not Launched'
                row_data.extend([
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                ])
            else:
                row_data.extend([
                    _format_infrastructure_data(row['aww_name']),
                    _format_infrastructure_data(row['contact_phone_number']),
                    _format_infrastructure_data(row['total_thr_candidates']),
                    _format_infrastructure_data(row['thr_given_21_days']),
                    _format_infrastructure_data(row['thr_distribution_image_count'])
                ])

            excel_rows.append(row_data)
        if self.location:
            locs = SQLLocation.objects.get(location_id=self.location).get_ancestors(include_self=True)
            for loc in locs:
                filters.append([loc.location_type.name.title(), loc.name])
        else:
            filters.append(['Location', 'National'])

        date = self.month
        filters.append(['Month', date.strftime("%B")])
        filters.append(['Year', date.year])

        return [
            [
                self.title,
                excel_rows
            ],
            [
                'Export Info',
                filters
            ]
        ]
<commit_msg>reverted unwanted change<commit_after>from corehq.apps.locations.models import SQLLocation
from custom.icds_reports.utils import india_now, DATA_NOT_ENTERED
from custom.icds_reports.models.views import TakeHomeRationMonthly

class TakeHomeRationExport(object):
    title = 'Take Home Ration'

    def __init__(self, location, month, loc_level=0, beta=False):
        self.location = location
        self.loc_level = loc_level
        self.month = month
        self.beta = beta

    def get_excel_data(self):

        def _format_infrastructure_data(data):
            return data if data is not None else DATA_NOT_ENTERED

        filters = {"month": self.month, "aggregation_level": 5}

        if self.loc_level == 4:
            filters['supervisor_id'] = self.location
            order_by = ('awc_name',)
        elif self.loc_level == 3:
            filters['block_id'] = self.location
            order_by = ('supervisor_name', 'awc_name')
        elif self.loc_level == 2:
            filters['district_id'] = self.location
            order_by = ('block_name', 'supervisor_name', 'awc_name')
        elif self.loc_level == 1:
            filters['state_id'] = self.location
            order_by = ('district_name', 'block_name', 'supervisor_name', 'awc_name')
        else:
            order_by = ('state_name', 'district_name', 'block_name', 'supervisor_name', 'awc_name')

        query_set = TakeHomeRationMonthly.objects.filter(**filters).order_by(*order_by)

        data = query_set.values('state_name', 'district_name', 'block_name',
                                'supervisor_name', 'awc_name', 'aww_name', 'contact_phone_number',
                                'is_launched', 'total_thr_candidates', 'thr_given_21_days',
                                'thr_distribution_image_count')

        headers = ['State', 'District', 'Block', 'Sector', 'Awc Name', 'AWW Name', 'AWW Phone No.',
                   'Total No. of Beneficiaries eligible for THR',
                   'Total No. of Beneficiaries received THR>=21 days in given month',
                   'Total No of Pictures taken by AWW']

        excel_rows = [headers]

        for row in data:
            row_data = [
                row['state_name'],
                row['district_name'],
                row['block_name'],
                row['supervisor_name'],
                row['awc_name'],
            ]
            if row['is_launched'] != 'yes':
                AWC_NOT_LAUNCHED = 'AWC Not Launched'
                row_data.extend([
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                    AWC_NOT_LAUNCHED,
                ])
            else:
                row_data.extend([
                    _format_infrastructure_data(row['aww_name']),
                    _format_infrastructure_data(row['contact_phone_number']),
                    _format_infrastructure_data(row['total_thr_candidates']),
                    _format_infrastructure_data(row['thr_given_21_days']),
                    _format_infrastructure_data(row['thr_distribution_image_count'])
                ])

            excel_rows.append(row_data)
        filters = [['Generated at', india_now()]]
        if self.location:
            locs = SQLLocation.objects.get(location_id=self.location).get_ancestors(include_self=True)
            for loc in locs:
                filters.append([loc.location_type.name.title(), loc.name])
        else:
            filters.append(['Location', 'National'])

        date = self.month
        filters.append(['Month', date.strftime("%B")])
        filters.append(['Year', date.year])

        return [
            [
                self.title,
                excel_rows
            ],
            [
                'Export Info',
                filters
            ]
        ]
<|endoftext|>