jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def set_session_tracks(display_obj):
    """Save igv tracks as a session object. This way it's easy to verify that a user is requesting one of these files from remote_static view endpoint

    Args:
        display_obj(dict): A display object containing case name, list of genes, lucus and tracks
    """
    session_tracks = list(display_obj.get("reference_track", {}).values())
    for key, track_items in display_obj.items():
        if key not in ["tracks", "custom_tracks", "sample_tracks"]:
            continue
        for track_item in track_items:
            session_tracks += list(track_item.values())

    session["igv_tracks"] = session_tracks
//  [python]
def get(self, path: str) -> None:
    parts = path.split("/")
    component_name = parts[0]
    component_root = self._registry.get_component_path(component_name)
    if component_root is None:
        self.write("not found")
        self.set_status(404)
        return

    # follow symlinks to get an accurate normalized path
    component_root = os.path.realpath(component_root)
    filename = "/".join(parts[1:])
    abspath = os.path.realpath(os.path.join(component_root, filename))

    # Do NOT expose anything outside of the component root.
    if os.path.commonprefix([component_root, abspath]) != component_root:
        self.write("forbidden")
        self.set_status(403)
        return

    LOGGER.debug("ComponentRequestHandler: GET: %s -> %s", path, abspath)

    try:
        with open(abspath, "rb") as file:
            contents = file.read()
    except (OSError) as e:
        LOGGER.error(f"ComponentRequestHandler: GET {path} read error", exc_info=e)
        self.write("read error")
        self.set_status(404)
        return

    self.write(contents)
    self.set_header("Content-Type", self.get_content_type(abspath))

    self.set_extra_headers(path)
//  [python]
def get(self, path: str) -> None:
    parts = path.split("/")
    component_name = parts[0]
    component_root = self._registry.get_component_path(component_name)
    if component_root is None:
        self.write("not found")
        self.set_status(404)
        return

    filename = "/".join(parts[1:])
    abspath = os.path.join(component_root, filename)

    LOGGER.debug("ComponentRequestHandler: GET: %s -> %s", path, abspath)

    try:
        with open(abspath, "rb") as file:
            contents = file.read()
    except (OSError) as e:
        LOGGER.error(f"ComponentRequestHandler: GET {path} read error", exc_info=e)
        self.write("read error")
        self.set_status(404)
        return

    self.write(contents)
    self.set_header("Content-Type", self.get_content_type(abspath))

    self.set_extra_headers(path)
//  [python]
def stream_exists_backend(request, user_profile, stream_id, autosubscribe):
    # type: (HttpRequest, UserProfile, int, bool) -> HttpResponse
    try:
        stream = get_and_validate_stream_by_id(stream_id, user_profile.realm)
    except JsonableError:
        stream = None
    result = {"exists": bool(stream)}
    if stream is not None:
        recipient = get_recipient(Recipient.STREAM, stream.id)
        if not stream.invite_only and autosubscribe:
            bulk_add_subscriptions([stream], [user_profile])
        result["subscribed"] = is_active_subscriber(
            user_profile=user_profile,
            recipient=recipient)

        return json_success(result) # results are ignored for HEAD requests
    return json_response(data=result, status=404)
//  [python]
def stream_exists_backend(request, user_profile, stream_id, autosubscribe):
    # type: (HttpRequest, UserProfile, int, bool) -> HttpResponse
    try:
        stream = get_and_validate_stream_by_id(stream_id, user_profile.realm)
    except JsonableError:
        stream = None
    result = {"exists": bool(stream)}
    if stream is not None:
        recipient = get_recipient(Recipient.STREAM, stream.id)
        if autosubscribe:
            bulk_add_subscriptions([stream], [user_profile])
        result["subscribed"] = is_active_subscriber(
            user_profile=user_profile,
            recipient=recipient)

        return json_success(result) # results are ignored for HEAD requests
    return json_response(data=result, status=404)
//  [python]
def history_data(start_time, offset=None):
    """Return history data.

    Arguments:
        start_time: select history starting from this timestamp.
        offset: number of items to skip
    """
    # history atimes are stored as ints, ensure start_time is not a float
    start_time = int(start_time)
    hist = objreg.get('web-history')
    if offset is not None:
        entries = hist.entries_before(start_time, limit=1000, offset=offset)
    else:
        # end is 24hrs earlier than start
        end_time = start_time - 24*60*60
        entries = hist.entries_between(end_time, start_time)

    return [{"url": html.escape(e.url),
             "title": html.escape(e.title) or html.escape(e.url),
             "time": e.atime} for e in entries]
//  [python]
def history_data(start_time, offset=None):
    """Return history data.

    Arguments:
        start_time: select history starting from this timestamp.
        offset: number of items to skip
    """
    # history atimes are stored as ints, ensure start_time is not a float
    start_time = int(start_time)
    hist = objreg.get('web-history')
    if offset is not None:
        entries = hist.entries_before(start_time, limit=1000, offset=offset)
    else:
        # end is 24hrs earlier than start
        end_time = start_time - 24*60*60
        entries = hist.entries_between(end_time, start_time)

    return [{"url": e.url, "title": e.title or e.url, "time": e.atime}
            for e in entries]
//  [python]
def boboAwareZopeTraverse(object, path_items, econtext):
    """Traverses a sequence of names, first trying attributes then items.

    This uses zope.traversing path traversal where possible and interacts
    correctly with objects providing OFS.interface.ITraversable when
    necessary (bobo-awareness).
    """
    request = getattr(econtext, 'request', None)
    validate = getSecurityManager().validate
    path_items = list(path_items)
    path_items.reverse()

    while path_items:
        name = path_items.pop()

        if OFS.interfaces.ITraversable.providedBy(object):
            object = object.restrictedTraverse(name)
        else:
            found = traversePathElement(object, name, path_items,
                                        request=request)

            # Special backwards compatibility exception for the name ``_``,
            # which was often used for translation message factories.
            # Allow and continue traversal.
            if name == '_':
                warnings.warn('Traversing to the name `_` is deprecated '
                              'and will be removed in Zope 6.',
                              DeprecationWarning)
                object = found
                continue

            # All other names starting with ``_`` are disallowed.
            # This emulates what restrictedTraverse does.
            if name.startswith('_'):
                raise NotFound(name)

            # traversePathElement doesn't apply any Zope security policy,
            # so we validate access explicitly here.
            try:
                validate(object, object, name, found)
                object = found
            except Unauthorized:
                # Convert Unauthorized to prevent information disclosures
                raise NotFound(name)

    return object
//  [python]
def boboAwareZopeTraverse(object, path_items, econtext):
    """Traverses a sequence of names, first trying attributes then items.

    This uses zope.traversing path traversal where possible and interacts
    correctly with objects providing OFS.interface.ITraversable when
    necessary (bobo-awareness).
    """
    request = getattr(econtext, 'request', None)
    path_items = list(path_items)
    path_items.reverse()

    while path_items:
        name = path_items.pop()

        if name == '_':
            warnings.warn('Traversing to the name `_` is deprecated '
                          'and will be removed in Zope 6.',
                          DeprecationWarning)
        elif name.startswith('_'):
            raise NotFound(name)

        if OFS.interfaces.ITraversable.providedBy(object):
            object = object.restrictedTraverse(name)
        else:
            object = traversePathElement(object, name, path_items,
                                         request=request)
    return object
//  [python]
def get_image(path):
    def get_absolute_path(path):
        import os
        script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in
        rel_path = path
        abs_file_path = safe_join(script_dir, rel_path)
        return abs_file_path

    return send_file(
        get_absolute_path(f"./images/{path}"),
        mimetype='image/png',
        attachment_filename='snapshot.png',
        cache_timeout=0
    )
//  [python]
def get_image(path):
    def get_absolute_path(path):
        import os
        script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in
        rel_path = path
        abs_file_path = os.path.join(script_dir, rel_path)
        return abs_file_path

    return send_file(
        get_absolute_path(f"./images/{path}"),
        mimetype='image/png',
        attachment_filename='snapshot.png',
        cache_timeout=0
    )
//  [python]
def accounts_home_from_multiuse_invite(request: HttpRequest, confirmation_key: str) -> HttpResponse:
    realm = get_realm_from_request(request)
    multiuse_object = None
    try:
        multiuse_object = get_object_from_key(confirmation_key, [Confirmation.MULTIUSE_INVITE])
        if realm != multiuse_object.realm:
            return render(request, "confirmation/link_does_not_exist.html", status=404)
        # Required for OAuth 2
    except ConfirmationKeyException as exception:
        if realm is None or realm.invite_required:
            return render_confirmation_key_error(request, exception)
    return accounts_home(
        request, multiuse_object_key=confirmation_key, multiuse_object=multiuse_object
    )
//  [python]
def accounts_home_from_multiuse_invite(request: HttpRequest, confirmation_key: str) -> HttpResponse:
    multiuse_object = None
    try:
        multiuse_object = get_object_from_key(confirmation_key, [Confirmation.MULTIUSE_INVITE])
        # Required for OAuth 2
    except ConfirmationKeyException as exception:
        realm = get_realm_from_request(request)
        if realm is None or realm.invite_required:
            return render_confirmation_key_error(request, exception)
    return accounts_home(
        request, multiuse_object_key=confirmation_key, multiuse_object=multiuse_object
    )
//  [python]
def get_absolute_path(path):
    import os
    script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in
    rel_path = path
    abs_file_path = safe_join(script_dir, rel_path)
    return abs_file_path
//  [python]
def get_absolute_path(path):
    import os
    script_dir = os.path.dirname(__file__)  # <-- absolute dir the script is in
    rel_path = path
    abs_file_path = os.path.join(script_dir, rel_path)
    return abs_file_path
//  [python]
def post(self, request):
    totp_device = TOTPDevice(user=request.user)
    totp_device.save()
    add_2fa.send(sender=self.__class__, user=request.user)
    return FormattedResponse({"totp_secret": totp_device.totp_secret})
//  [python]
def post(self, request):
    totp_secret = pyotp.random_base32()
    request.user.totp_secret = totp_secret
    request.user.totp_status = TOTPStatus.VERIFYING
    request.user.save()
    add_2fa.send(sender=self.__class__, user=request.user)
    return FormattedResponse({"totp_secret": totp_secret})
//  [python]
def read_config(self, config, **kwargs):
    consent_config = config.get("user_consent")
    self.terms_template = self.read_template("terms.html")

    if consent_config is None:
        return
    self.user_consent_version = str(consent_config["version"])
    self.user_consent_template_dir = self.abspath(consent_config["template_dir"])
    if not path.isdir(self.user_consent_template_dir):
        raise ConfigError(
            "Could not find template directory '%s'"
            % (self.user_consent_template_dir,)
        )
    self.user_consent_server_notice_content = consent_config.get(
        "server_notice_content"
    )
    self.block_events_without_consent_error = consent_config.get(
        "block_events_error"
    )
    self.user_consent_server_notice_to_guests = bool(
        consent_config.get("send_server_notice_to_guests", False)
    )
    self.user_consent_at_registration = bool(
        consent_config.get("require_at_registration", False)
    )
    self.user_consent_policy_name = consent_config.get(
        "policy_name", "Privacy Policy"
    )
//  [python]
def read_config(self, config, **kwargs):
    consent_config = config.get("user_consent")
    self.terms_template = self.read_templates(["terms.html"], autoescape=True)[0]

    if consent_config is None:
        return
    self.user_consent_version = str(consent_config["version"])
    self.user_consent_template_dir = self.abspath(consent_config["template_dir"])
    if not path.isdir(self.user_consent_template_dir):
        raise ConfigError(
            "Could not find template directory '%s'"
            % (self.user_consent_template_dir,)
        )
    self.user_consent_server_notice_content = consent_config.get(
        "server_notice_content"
    )
    self.block_events_without_consent_error = consent_config.get(
        "block_events_error"
    )
    self.user_consent_server_notice_to_guests = bool(
        consent_config.get("send_server_notice_to_guests", False)
    )
    self.user_consent_at_registration = bool(
        consent_config.get("require_at_registration", False)
    )
    self.user_consent_policy_name = consent_config.get(
        "policy_name", "Privacy Policy"
    )
//  [python]
def create_app():
    if sys.version_info < (3, 0):
        log.info(
            '*** Python2 is EOL since end of 2019, this version of Calibre-Web is no longer supporting Python2, please update your installation to Python3 ***')
        print(
            '*** Python2 is EOL since end of 2019, this version of Calibre-Web is no longer supporting Python2, please update your installation to Python3 ***')
        web_server.stop(True)
        sys.exit(5)
    if not lxml_present:
        log.info('*** "lxml" is needed for calibre-web to run. Please install it using pip: "pip install lxml" ***')
        print('*** "lxml" is needed for calibre-web to run. Please install it using pip: "pip install lxml" ***')
        web_server.stop(True)
        sys.exit(6)
    if not wtf_present:
        log.info('*** "flask-WTF" is needed for calibre-web to run. Please install it using pip: "pip install flask-WTF" ***')
        print('*** "flask-WTF" is needed for calibre-web to run. Please install it using pip: "pip install flask-WTF" ***')
        web_server.stop(True)
        sys.exit(7)
    for res in dependency_check() + dependency_check(True):
        log.info('*** "{}" version does not fit the requirements. Should: {}, Found: {}, please consider installing required version ***'
            .format(res['name'],
                 res['target'],
                 res['found']))
    app.wsgi_app = ReverseProxied(app.wsgi_app)

    if os.environ.get('FLASK_DEBUG'):
        cache_buster.init_cache_busting(app)
    log.info('Starting Calibre Web...')

    Principal(app)
    lm.init_app(app)
    app.secret_key = os.getenv('SECRET_KEY', config_sql.get_flask_session_key(ub.session))

    web_server.init_app(app, config)

    babel.init_app(app)
    _BABEL_TRANSLATIONS.update(str(item) for item in babel.list_translations())
    _BABEL_TRANSLATIONS.add('en')

    if services.ldap:
        services.ldap.init_app(app, config)
    if services.goodreads_support:
        services.goodreads_support.connect(config.config_goodreads_api_key,
                                           config.config_goodreads_api_secret,
                                           config.config_use_goodreads)
    config.store_calibre_uuid(calibre_db, db.LibraryId)
    return app
//  [python]
def create_app():
    if sys.version_info < (3, 0):
        log.info(
            '*** Python2 is EOL since end of 2019, this version of Calibre-Web is no longer supporting Python2, please update your installation to Python3 ***')
        print(
            '*** Python2 is EOL since end of 2019, this version of Calibre-Web is no longer supporting Python2, please update your installation to Python3 ***')
        web_server.stop(True)
        sys.exit(5)
    if not lxml_present:
        log.info('*** "lxml" is needed for calibre-web to run. Please install it using pip: "pip install lxml" ***')
        print('*** "lxml" is needed for calibre-web to run. Please install it using pip: "pip install lxml" ***')
        web_server.stop(True)
        sys.exit(6)
    if not wtf_present:
        log.info('*** "flask-WTF" is needed for calibre-web to run. Please install it using pip: "pip install flask-WTF" ***')
        print('*** "flask-WTF" is needed for calibre-web to run. Please install it using pip: "pip install flask-WTF" ***')
        web_server.stop(True)
        sys.exit(7)
    for res in dependency_check() + dependency_check(True):
        log.info('*** "{}" version does not fit the requirements. Should: {}, Found: {}, please consider installing required version ***'
            .format(res['name'],
                 res['target'],
                 res['found']))
    app.wsgi_app = ReverseProxied(app.wsgi_app)

    if os.environ.get('FLASK_DEBUG'):
        cache_buster.init_cache_busting(app)
    log.info('Starting Calibre Web...')

    Principal(app)
    lm.init_app(app)
    app.secret_key = os.getenv('SECRET_KEY', config_sql.get_flask_session_key(ub.session))

    web_server.init_app(app, config)

    babel.init_app(app)
    _BABEL_TRANSLATIONS.update(str(item) for item in babel.list_translations())
    _BABEL_TRANSLATIONS.add('en')

    if services.ldap:
        services.ldap.init_app(app, config)
    if services.goodreads_support:
        services.goodreads_support.connect(config.config_goodreads_api_key,
                                           config.config_goodreads_api_secret,
                                           config.config_use_goodreads)
    config.store_calibre_uuid(calibre_db, db.Library_Id)
    return app
//  [python]
def _get_element_ptr_array(parent, key, array_bounds_check):

    assert isinstance(parent.typ, ArrayLike)

    if not is_integer_type(key.typ):
        raise TypeCheckFailure(f"{key.typ} used as array index")

    subtype = parent.typ.subtype

    if parent.value == "~empty":
        if array_bounds_check:
            # this case was previously missing a bounds check. codegen
            # is a bit complicated when bounds check is required, so
            # block it. there is no reason to index into a literal empty
            # array anyways!
            raise TypeCheckFailure("indexing into zero array not allowed")
        return IRnode.from_list("~empty", subtype)

    if parent.value == "multi":
        assert isinstance(key.value, int)
        return parent.args[key.value]

    ix = unwrap_location(key)

    if array_bounds_check:
        # clamplt works, even for signed ints. since two's-complement
        # is used, if the index is negative, (unsigned) LT will interpret
        # it as a very large number, larger than any practical value for
        # an array index, and the clamp will throw an error.
        clamp_op = "uclamplt"
        is_darray = isinstance(parent.typ, DArrayType)
        bound = get_dyn_array_count(parent) if is_darray else parent.typ.count
        # NOTE: there are optimization rules for this when ix or bound is literal
        ix = IRnode.from_list([clamp_op, ix, bound], typ=ix.typ)

    if parent.encoding == Encoding.ABI:
        if parent.location == STORAGE:
            raise CompilerPanic("storage variables should not be abi encoded")  # pragma: notest

        member_abi_t = subtype.abi_type

        ofst = _mul(ix, member_abi_t.embedded_static_size())

        return _getelemptr_abi_helper(parent, subtype, ofst)

    if parent.location.word_addressable:
        element_size = subtype.storage_size_in_words
    elif parent.location.byte_addressable:
        element_size = subtype.memory_bytes_required
    else:
        raise CompilerPanic("unreachable")  # pragma: notest

    ofst = _mul(ix, element_size)

    if has_length_word(parent.typ):
        data_ptr = add_ofst(parent, parent.location.word_scale * DYNAMIC_ARRAY_OVERHEAD)
    else:
        data_ptr = parent

    return IRnode.from_list(add_ofst(data_ptr, ofst), typ=subtype, location=parent.location)
//  [python]
def _get_element_ptr_array(parent, key, array_bounds_check):

    assert isinstance(parent.typ, ArrayLike)

    if not is_integer_type(key.typ):
        raise TypeCheckFailure(f"{key.typ} used as array index")

    subtype = parent.typ.subtype

    if parent.value == "~empty":
        if array_bounds_check:
            # this case was previously missing a bounds check. codegen
            # is a bit complicated when bounds check is required, so
            # block it. there is no reason to index into a literal empty
            # array anyways!
            raise TypeCheckFailure("indexing into zero array not allowed")
        return IRnode.from_list("~empty", subtype)

    if parent.value == "multi":
        assert isinstance(key.value, int)
        return parent.args[key.value]

    ix = unwrap_location(key)

    if array_bounds_check:
        # clamplt works, even for signed ints. since two's-complement
        # is used, if the index is negative, (unsigned) LT will interpret
        # it as a very large number, larger than any practical value for
        # an array index, and the clamp will throw an error.
        clamp_op = "uclamplt"
        is_darray = isinstance(parent.typ, DArrayType)
        bound = get_dyn_array_count(parent) if is_darray else parent.typ.count
        # NOTE: there are optimization rules for this when ix or bound is literal
        ix = IRnode.from_list([clamp_op, ix, bound], typ=ix.typ)

    if parent.encoding in (Encoding.ABI, Encoding.JSON_ABI):
        if parent.location == STORAGE:
            raise CompilerPanic("storage variables should not be abi encoded")  # pragma: notest

        member_abi_t = subtype.abi_type

        ofst = _mul(ix, member_abi_t.embedded_static_size())

        return _getelemptr_abi_helper(parent, subtype, ofst)

    if parent.location.word_addressable:
        element_size = subtype.storage_size_in_words
    elif parent.location.byte_addressable:
        element_size = subtype.memory_bytes_required
    else:
        raise CompilerPanic("unreachable")  # pragma: notest

    ofst = _mul(ix, element_size)

    if has_length_word(parent.typ):
        data_ptr = add_ofst(parent, parent.location.word_scale * DYNAMIC_ARRAY_OVERHEAD)
    else:
        data_ptr = parent

    return IRnode.from_list(add_ofst(data_ptr, ofst), typ=subtype, location=parent.location)
//  [python]
def needs_clamp(t, encoding):
    if encoding == Encoding.VYPER:
        return False
    if encoding != Encoding.ABI:
        raise CompilerPanic("unreachable")  # pragma: notest
    if isinstance(t, (ByteArrayLike, DArrayType)):
        return True
    if isinstance(t, BaseType):
        return t.typ not in ("int256", "uint256", "bytes32")
    if isinstance(t, SArrayType):
        return needs_clamp(t.subtype, encoding)
    if isinstance(t, TupleLike):
        return any(needs_clamp(m, encoding) for m in t.tuple_members())

    raise CompilerPanic("unreachable")  # pragma: notest
//  [python]
def needs_clamp(t, encoding):
    if encoding not in (Encoding.ABI, Encoding.JSON_ABI):
        return False
    if isinstance(t, (ByteArrayLike, DArrayType)):
        if encoding == Encoding.JSON_ABI:
            # don't have bytestring size bound from json, don't clamp
            return False
        return True
    if isinstance(t, BaseType) and t.typ not in ("int256", "uint256", "bytes32"):
        return True
    if isinstance(t, SArrayType):
        return needs_clamp(t.subtype, encoding)
    if isinstance(t, TupleLike):
        return any(needs_clamp(m, encoding) for m in t.tuple_members())
    return False
//  [python]
def _get_element_ptr_tuplelike(parent, key):
    typ = parent.typ
    assert isinstance(typ, TupleLike)

    if isinstance(typ, StructType):
        assert isinstance(key, str)
        subtype = typ.members[key]
        attrs = list(typ.tuple_keys())
        index = attrs.index(key)
        annotation = key
    else:
        assert isinstance(key, int)
        subtype = typ.members[key]
        attrs = list(range(len(typ.members)))
        index = key
        annotation = None

    # generated by empty() + make_setter
    if parent.value == "~empty":
        return IRnode.from_list("~empty", typ=subtype)

    if parent.value == "multi":
        assert parent.encoding != Encoding.ABI, "no abi-encoded literals"
        return parent.args[index]

    ofst = 0  # offset from parent start

    if parent.encoding == Encoding.ABI:
        if parent.location == STORAGE:
            raise CompilerPanic("storage variables should not be abi encoded")  # pragma: notest

        member_t = typ.members[attrs[index]]

        for i in range(index):
            member_abi_t = typ.members[attrs[i]].abi_type
            ofst += member_abi_t.embedded_static_size()

        return _getelemptr_abi_helper(parent, member_t, ofst)

    if parent.location.word_addressable:
        for i in range(index):
            ofst += typ.members[attrs[i]].storage_size_in_words
    elif parent.location.byte_addressable:
        for i in range(index):
            ofst += typ.members[attrs[i]].memory_bytes_required
    else:
        raise CompilerPanic(f"bad location {parent.location}")  # pragma: notest

    return IRnode.from_list(
        add_ofst(parent, ofst),
        typ=subtype,
        location=parent.location,
        encoding=parent.encoding,
        annotation=annotation,
    )
//  [python]
def _get_element_ptr_tuplelike(parent, key):
    typ = parent.typ
    assert isinstance(typ, TupleLike)

    if isinstance(typ, StructType):
        assert isinstance(key, str)
        subtype = typ.members[key]
        attrs = list(typ.tuple_keys())
        index = attrs.index(key)
        annotation = key
    else:
        assert isinstance(key, int)
        subtype = typ.members[key]
        attrs = list(range(len(typ.members)))
        index = key
        annotation = None

    # generated by empty() + make_setter
    if parent.value == "~empty":
        return IRnode.from_list("~empty", typ=subtype)

    if parent.value == "multi":
        assert parent.encoding != Encoding.ABI, "no abi-encoded literals"
        return parent.args[index]

    ofst = 0  # offset from parent start

    if parent.encoding in (Encoding.ABI, Encoding.JSON_ABI):
        if parent.location == STORAGE:
            raise CompilerPanic("storage variables should not be abi encoded")  # pragma: notest

        member_t = typ.members[attrs[index]]

        for i in range(index):
            member_abi_t = typ.members[attrs[i]].abi_type
            ofst += member_abi_t.embedded_static_size()

        return _getelemptr_abi_helper(parent, member_t, ofst)

    if parent.location.word_addressable:
        for i in range(index):
            ofst += typ.members[attrs[i]].storage_size_in_words
    elif parent.location.byte_addressable:
        for i in range(index):
            ofst += typ.members[attrs[i]].memory_bytes_required
    else:
        raise CompilerPanic(f"bad location {parent.location}")  # pragma: notest

    return IRnode.from_list(
        add_ofst(parent, ofst),
        typ=subtype,
        location=parent.location,
        encoding=parent.encoding,
        annotation=annotation,
    )
//  [python]
def _dynarray_make_setter(dst, src):
    assert isinstance(src.typ, DArrayType)
    assert isinstance(dst.typ, DArrayType)

    if src.value == "~empty":
        return IRnode.from_list(STORE(dst, 0))

    if src.value == "multi":
        ret = ["seq"]
        # handle literals

        # write the length word
        store_length = STORE(dst, len(src.args))
        ann = None
        if src.annotation is not None:
            ann = f"len({src.annotation})"
        store_length = IRnode.from_list(store_length, annotation=ann)
        ret.append(store_length)

        n_items = len(src.args)
        for i in range(n_items):
            k = IRnode.from_list(i, typ="uint256")
            dst_i = get_element_ptr(dst, k, array_bounds_check=False)
            src_i = get_element_ptr(src, k, array_bounds_check=False)
            ret.append(make_setter(dst_i, src_i))

        return ret

    with src.cache_when_complex("darray_src") as (b1, src):

        # for ABI-encoded dynamic data, we must loop to unpack, since
        # the layout does not match our memory layout
        should_loop = src.encoding == Encoding.ABI and src.typ.subtype.abi_type.is_dynamic()

        # if the subtype is dynamic, there might be a lot of
        # unused space inside of each element. for instance
        # DynArray[DynArray[uint256, 100], 5] where all the child
        # arrays are empty - for this case, we recursively call
        # into make_setter instead of straight bytes copy
        # TODO we can make this heuristic more precise, e.g.
        # loop when subtype.is_dynamic AND location == storage
        # OR array_size <= /bound where loop is cheaper than memcpy/
        should_loop |= src.typ.subtype.abi_type.is_dynamic()
        should_loop |= needs_clamp(src.typ.subtype, src.encoding)

        with get_dyn_array_count(src).cache_when_complex("darray_count") as (b2, count):
            ret = ["seq"]

            ret.append(STORE(dst, count))

            if should_loop:
                i = IRnode.from_list(_freshname("copy_darray_ix"), typ="uint256")

                loop_body = make_setter(
                    get_element_ptr(dst, i, array_bounds_check=False),
                    get_element_ptr(src, i, array_bounds_check=False),
                )
                loop_body.annotation = f"{dst}[i] = {src}[i]"

                ret.append(["repeat", i, 0, count, src.typ.count, loop_body])

            else:
                element_size = src.typ.subtype.memory_bytes_required
                # number of elements * size of element in bytes
                n_bytes = _mul(count, element_size)
                max_bytes = src.typ.count * element_size

                src_ = dynarray_data_ptr(src)
                dst_ = dynarray_data_ptr(dst)
                ret.append(copy_bytes(dst_, src_, n_bytes, max_bytes))

            return b1.resolve(b2.resolve(ret))
//  [python]
def _dynarray_make_setter(dst, src):
    assert isinstance(src.typ, DArrayType)
    assert isinstance(dst.typ, DArrayType)

    if src.value == "~empty":
        return IRnode.from_list(STORE(dst, 0))

    if src.value == "multi":
        ret = ["seq"]
        # handle literals

        # write the length word
        store_length = STORE(dst, len(src.args))
        ann = None
        if src.annotation is not None:
            ann = f"len({src.annotation})"
        store_length = IRnode.from_list(store_length, annotation=ann)
        ret.append(store_length)

        n_items = len(src.args)
        for i in range(n_items):
            k = IRnode.from_list(i, typ="uint256")
            dst_i = get_element_ptr(dst, k, array_bounds_check=False)
            src_i = get_element_ptr(src, k, array_bounds_check=False)
            ret.append(make_setter(dst_i, src_i))

        return ret

    with src.cache_when_complex("darray_src") as (b1, src):

        # for ABI-encoded dynamic data, we must loop to unpack, since
        # the layout does not match our memory layout
        should_loop = (
            src.encoding in (Encoding.ABI, Encoding.JSON_ABI)
            and src.typ.subtype.abi_type.is_dynamic()
        )

        # if the subtype is dynamic, there might be a lot of
        # unused space inside of each element. for instance
        # DynArray[DynArray[uint256, 100], 5] where all the child
        # arrays are empty - for this case, we recursively call
        # into make_setter instead of straight bytes copy
        # TODO we can make this heuristic more precise, e.g.
        # loop when subtype.is_dynamic AND location == storage
        # OR array_size <= /bound where loop is cheaper than memcpy/
        should_loop |= src.typ.subtype.abi_type.is_dynamic()
        should_loop |= needs_clamp(src.typ.subtype, src.encoding)

        with get_dyn_array_count(src).cache_when_complex("darray_count") as (b2, count):
            ret = ["seq"]

            ret.append(STORE(dst, count))

            if should_loop:
                i = IRnode.from_list(_freshname("copy_darray_ix"), typ="uint256")

                loop_body = make_setter(
                    get_element_ptr(dst, i, array_bounds_check=False),
                    get_element_ptr(src, i, array_bounds_check=False),
                )
                loop_body.annotation = f"{dst}[i] = {src}[i]"

                ret.append(["repeat", i, 0, count, src.typ.count, loop_body])

            else:
                element_size = src.typ.subtype.memory_bytes_required
                # number of elements * size of element in bytes
                n_bytes = _mul(count, element_size)
                max_bytes = src.typ.count * element_size

                src_ = dynarray_data_ptr(src)
                dst_ = dynarray_data_ptr(dst)
                ret.append(copy_bytes(dst_, src_, n_bytes, max_bytes))

            return b1.resolve(b2.resolve(ret))
//  [python]
def get_response(self, url, auth=None, http_method="get", **kwargs):

    # Get authentication values if not given
    if auth is None:
        auth = self.get_auth()

    # Then call requests to get the response from the given endpoint
    # URL optionally, with the additional requests parameters.
    error = None
    response = None
    try:
        response = requests_session.request(http_method, url, auth=auth, **kwargs)
        # Raise a requests HTTP exception with the appropriate reason
        # for 4xx and 5xx response status codes which is later caught
        # and passed back.
        response.raise_for_status()

        # Any other responses (e.g. 2xx and 3xx):
        if response.status_code != 200:
            error = "{} ({}).".format(self.response_error, response.status_code)

    except requests_or_advocate.HTTPError as exc:
        logger.exception(exc)
        error = "Failed to execute query. " "Return Code: {} Reason: {}".format(
            response.status_code, response.text
        )
    except UnacceptableAddressException as exc:
        logger.exception(exc)
        error = "Can't query private addresses."
    except requests_or_advocate.RequestException as exc:
        # Catch all other requests exceptions and return the error.
        logger.exception(exc)
        error = str(exc)

    # Return response and error.
    return response, error
//  [python]
def get_response(self, url, auth=None, http_method="get", **kwargs):
    if is_private_address(url) and settings.ENFORCE_PRIVATE_ADDRESS_BLOCK:
        raise Exception("Can't query private addresses.")

    # Get authentication values if not given
    if auth is None:
        auth = self.get_auth()

    # Then call requests to get the response from the given endpoint
    # URL optionally, with the additional requests parameters.
    error = None
    response = None
    try:
        response = requests_session.request(http_method, url, auth=auth, **kwargs)
        # Raise a requests HTTP exception with the appropriate reason
        # for 4xx and 5xx response status codes which is later caught
        # and passed back.
        response.raise_for_status()

        # Any other responses (e.g. 2xx and 3xx):
        if response.status_code != 200:
            error = "{} ({}).".format(self.response_error, response.status_code)

    except requests.HTTPError as exc:
        logger.exception(exc)
        error = "Failed to execute query. " "Return Code: {} Reason: {}".format(
            response.status_code, response.text
        )
    except requests.RequestException as exc:
        # Catch all other requests exceptions and return the error.
        logger.exception(exc)
        error = str(exc)

    # Return response and error.
    return response, error
//  [python]
def test_visitor(self):
    class CustomVisitor(self.asdl.VisitorBase):
        def __init__(self):
            super().__init__()
            self.names_with_seq = []

        def visitModule(self, mod):
            for dfn in mod.dfns:
                self.visit(dfn)

        def visitType(self, type):
            self.visit(type.value)

        def visitSum(self, sum):
            for t in sum.types:
                self.visit(t)

        def visitConstructor(self, cons):
            for f in cons.fields:
                if f.seq:
                    self.names_with_seq.append(cons.name)

    v = CustomVisitor()
    v.visit(self.types['mod'])
    self.assertEqual(v.names_with_seq,
                     ['Module', 'Module', 'Interactive', 'FunctionType', 'Suite'])
//  [python]
def test_visitor(self):
    class CustomVisitor(self.asdl.VisitorBase):
        def __init__(self):
            super().__init__()
            self.names_with_seq = []

        def visitModule(self, mod):
            for dfn in mod.dfns:
                self.visit(dfn)

        def visitType(self, type):
            self.visit(type.value)

        def visitSum(self, sum):
            for t in sum.types:
                self.visit(t)

        def visitConstructor(self, cons):
            for f in cons.fields:
                if f.seq:
                    self.names_with_seq.append(cons.name)

    v = CustomVisitor()
    v.visit(self.types['mod'])
    self.assertEqual(v.names_with_seq, ['Module', 'Interactive', 'Suite'])
//  [python]
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
    result = super(TagFormWidget, self).create_option(
        name=name, value=value, label='{}'.format(conditional_escape(label)),
        selected=selected, index=index, subindex=subindex, attrs=attrs
    )

    result['attrs']['data-color'] = self.queryset.get(pk=value).color

    return result
//  [python]
def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
    result = super(TagFormWidget, self).create_option(
        name=name, value=value, label=label, selected=selected,
        index=index, subindex=subindex, attrs=attrs
    )

    result['attrs']['data-color'] = self.queryset.get(pk=value).color

    return result
//  [python]
def home_get_dat():
    d = db.sentences_stats('get_data')
    n = db.sentences_stats('all_networks')

    rows = db.sentences_stats('get_clicks')
    c = rows[0][0]
    rows = db.sentences_stats('get_sessions')
    s = rows[0][0]
    vId = ('online', )
    rows = db.sentences_stats('get_online', vId)
    o = rows[0][0]

    return json.dumps({'status' : 'OK', 'd' : d, 'n' : n, 'c' : c, 's' : s, 'o' : o});
//  [python]
def home_get_dat():
    d = db.sentences_stats('get_data')
    n = db.sentences_stats('all_networks')

    ('clean_online')
    rows = db.sentences_stats('get_clicks')
    c = rows[0][0]
    rows = db.sentences_stats('get_sessions')
    s = rows[0][0]
    rows = db.sentences_stats('get_online')
    o = rows[0][0]

    return json.dumps({'status' : 'OK', 'd' : d, 'n' : n, 'c' : c, 's' : s, 'o' : o});
//  [python]
def home_get_preview():
    vId = request.form['vId']
    t = (vId,)
    d = db.sentences_stats('get_preview', t)
    n = db.sentences_stats('id_networks', t)
    return json.dumps({'status' : 'OK', 'vId' : vId, 'd' : d, 'n' : n});
//  [python]
def home_get_preview():
    vId = request.form['vId']
    d = db.sentences_stats('get_preview', vId)
    n = db.sentences_stats('id_networks', vId)
    return json.dumps({'status' : 'OK', 'vId' : vId, 'd' : d, 'n' : n});
//  [python]
def checkin(pickledata):
    uncompressed = bz2.decompress(base64.urlsafe_b64decode(pickledata))
    try:
        config = json.loads(uncompressed)
    except ValueError:
        config = pickle.loads(uncompressed)
    r, message = read_host_config(SESSION, config)
    if r is not None:
        return message + 'checked in successful'
    else:
        return message + 'error checking in'
//  [python]
def checkin(pickledata):
    config = pickle.loads(bz2.decompress(base64.urlsafe_b64decode(pickledata)))
    r, message = read_host_config(SESSION, config)
    if r is not None:
        return message + 'checked in successful'
    else:
        return message + 'error checking in'
//  [python]
def del_version(request, client_id, project, version):
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        scrapyd = get_scrapyd(client)
        result = scrapyd.delete_version(project=project, version=version)
        return JsonResponse(result)
//  [python]
def del_version(request, client_id, project, version):
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        try:
            scrapyd = get_scrapyd(client)
            result = scrapyd.delete_version(project=project, version=version)
            return JsonResponse(result)
        except ConnectionError:
            return JsonResponse({'message': 'Connect Error'})
//  [python]
def __init__(self, sydent):
    self.sydent = sydent
    # The default endpoint factory in Twisted 14.0.0 (which we require) uses the
    # BrowserLikePolicyForHTTPS context factory which will do regular cert validation
    # 'like a browser'
    self.agent = Agent(
        BlacklistingReactorWrapper(
            reactor=self.sydent.reactor,
            ip_whitelist=sydent.ip_whitelist,
            ip_blacklist=sydent.ip_blacklist,
        ),
        connectTimeout=15,
    )
//  [python]
def __init__(self, sydent):
    self.sydent = sydent
    # The default endpoint factory in Twisted 14.0.0 (which we require) uses the
    # BrowserLikePolicyForHTTPS context factory which will do regular cert validation
    # 'like a browser'
    self.agent = Agent(
        self.sydent.reactor,
        connectTimeout=15,
    )
//  [python]
def analyze(self, avc):
    import subprocess
    if avc.has_any_access_in(['execmod']):
        # MATCH
        # from https://docs.python.org/2.7/library/subprocess.html#replacing-shell-pipeline
        p1 = subprocess.Popen(['eu-readelf', '-d', avc.tpath], stdout=subprocess.PIPE)
        p2 = subprocess.Popen(["fgrep", "-q", "TEXTREL"], stdin=p1.stdout, stdout=subprocess.PIPE)
        p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
        p1.wait()
        p2.wait()
        if p2.returncode == 1:
            return self.report(("unsafe"))

        mcon = selinux.matchpathcon(avc.tpath.strip('"'), S_IFREG)[1]
        if mcon.split(":")[2] == "lib_t":
            return self.report()
    return None
//  [python]
def analyze(self, avc):
    import commands
    if avc.has_any_access_in(['execmod']):
        # MATCH
        if (commands.getstatusoutput("eu-readelf -d %s | fgrep -q TEXTREL" % avc.tpath)[0] == 1):
            return self.report(("unsafe"))

        mcon = selinux.matchpathcon(avc.tpath.strip('"'), S_IFREG)[1]
        if mcon.split(":")[2] == "lib_t":
            return self.report()
    return None
//  [python]
def del_project(request, client_id, project):
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        scrapyd = get_scrapyd(client)
        result = scrapyd.delete_project(project=project)
        return JsonResponse(result)
//  [python]
def del_project(request, client_id, project):
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        try:
            scrapyd = get_scrapyd(client)
            result = scrapyd.delete_project(project=project)
            return JsonResponse(result)
        except ConnectionError:
            return JsonResponse({'message': 'Connect Error'})
//  [python]
def job_log(request, client_id, project_name, spider_name, job_id):
    """
    get log of jog
    :param request: request object
    :param client_id: client id
    :param project_name: project name
    :param spider_name: spider name
    :param job_id: job id
    :return: log of job
    """
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        # get log url
        url = log_url(client.ip, client.port, project_name, spider_name, job_id)
        # get last 1000 bytes of log
        response = requests.get(url, timeout=5, headers={
            'Range': 'bytes=-1000'
        }, auth=(client.username, client.password) if client.auth else None)
        # Get encoding
        encoding = response.apparent_encoding
        # log not found
        if response.status_code == 404:
            return JsonResponse({'message': 'Log Not Found'}, status=404)
        # bytes to string
        text = response.content.decode(encoding, errors='replace')
        return HttpResponse(text)
//  [python]
def job_log(request, client_id, project_name, spider_name, job_id):
    """
    get log of jog
    :param request: request object
    :param client_id: client id
    :param project_name: project name
    :param spider_name: spider name
    :param job_id: job id
    :return: log of job
    """
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        # get log url
        url = log_url(client.ip, client.port, project_name, spider_name, job_id)
        try:
            # get last 1000 bytes of log
            response = requests.get(url, timeout=5, headers={
                'Range': 'bytes=-1000'
            }, auth=(client.username, client.password) if client.auth else None)
            # Get encoding
            encoding = response.apparent_encoding
            # log not found
            if response.status_code == 404:
                return JsonResponse({'message': 'Log Not Found'}, status=404)
            # bytes to string
            text = response.content.decode(encoding, errors='replace')
            return HttpResponse(text)
        except requests.ConnectionError:
            return JsonResponse({'message': 'Load Log Error'}, status=500)
//  [python]
def job_cancel(request, client_id, project_name, job_id):
    """
    cancel a job
    :param request: request object
    :param client_id: client id
    :param project_name: project name
    :param job_id: job id
    :return: json of cancel
    """
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        scrapyd = get_scrapyd(client)
        result = scrapyd.cancel(project_name, job_id)
        return JsonResponse(result)
//  [python]
def job_cancel(request, client_id, project_name, job_id):
    """
    cancel a job
    :param request: request object
    :param client_id: client id
    :param project_name: project name
    :param job_id: job id
    :return: json of cancel
    """
    if request.method == 'GET':
        client = Client.objects.get(id=client_id)
        try:
            scrapyd = get_scrapyd(client)
            result = scrapyd.cancel(project_name, job_id)
            return JsonResponse(result)
        except ConnectionError:
            return JsonResponse({'message': 'Connect Error'})
//  [python]
def render_html(request):
    """
    render html with url
    :param request:
    :return:
    """
    if request.method == 'GET':
        url = request.GET.get('url')
        url = unquote(base64.b64decode(url).decode('utf-8'))
        js = request.GET.get('js', 0)
        script = request.GET.get('script')
        response = requests.get(url, timeout=5)
        response.encoding = response.apparent_encoding
        html = process_html(response.text)
        return HttpResponse(html)
//  [python]
def render_html(request):
    """
    render html with url
    :param request:
    :return:
    """
    if request.method == 'GET':
        url = request.GET.get('url')
        url = unquote(base64.b64decode(url).decode('utf-8'))
        js = request.GET.get('js', 0)
        script = request.GET.get('script')
        try:
            response = requests.get(url, timeout=5)
            response.encoding = response.apparent_encoding
            html = process_html(response.text)
            return HttpResponse(html)
        except Exception as e:
            return JsonResponse({'message': e.args}, status=500)