jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def createRequest(self, op, req, outgoing_data):
    """Return a new QNetworkReply object.

    Args:
         op: Operation op
         req: const QNetworkRequest & req
         outgoing_data: QIODevice * outgoingData

    Return:
        A QNetworkReply.
    """
    proxy_factory = objreg.get('proxy-factory', None)
    if proxy_factory is not None:
        proxy_error = proxy_factory.get_error()
        if proxy_error is not None:
            return networkreply.ErrorNetworkReply(
                req, proxy_error, QNetworkReply.UnknownProxyError,
                self)

    for header, value in shared.custom_headers(url=req.url()):
        req.setRawHeader(header, value)

    host_blocker = objreg.get('host-blocker')
    if host_blocker.is_blocked(req.url()):
        log.webview.info("Request to {} blocked by host blocker.".format(
            req.url().host()))
        return networkreply.ErrorNetworkReply(
            req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
            self)

    # There are some scenarios where we can't figure out current_url:
    # - There's a generic NetworkManager, e.g. for downloads
    # - The download was in a tab which is now closed.
    current_url = QUrl()

    if self._tab_id is not None:
        assert self._win_id is not None
        try:
            tab = objreg.get('tab', scope='tab', window=self._win_id,
                             tab=self._tab_id)
            current_url = tab.url()
        except (KeyError, RuntimeError):
            # https://github.com/qutebrowser/qutebrowser/issues/889
            # Catching RuntimeError because we could be in the middle of
            # the webpage shutdown here.
            current_url = QUrl()

    if 'log-requests' in self._args.debug_flags:
        operation = debug.qenum_key(QNetworkAccessManager, op)
        operation = operation.replace('Operation', '').upper()
        log.webview.debug("{} {}, first-party {}".format(
            operation,
            req.url().toDisplayString(),
            current_url.toDisplayString()))

    scheme = req.url().scheme()
    if scheme in self._scheme_handlers:
        result = self._scheme_handlers[scheme](req, op, current_url)
        if result is not None:
            result.setParent(self)
            return result

    self.set_referer(req, current_url)
    return super().createRequest(op, req, outgoing_data)
//  [python]
def createRequest(self, op, req, outgoing_data):
    """Return a new QNetworkReply object.

    Args:
         op: Operation op
         req: const QNetworkRequest & req
         outgoing_data: QIODevice * outgoingData

    Return:
        A QNetworkReply.
    """
    proxy_factory = objreg.get('proxy-factory', None)
    if proxy_factory is not None:
        proxy_error = proxy_factory.get_error()
        if proxy_error is not None:
            return networkreply.ErrorNetworkReply(
                req, proxy_error, QNetworkReply.UnknownProxyError,
                self)

    scheme = req.url().scheme()
    if scheme in self._scheme_handlers:
        result = self._scheme_handlers[scheme](req)
        if result is not None:
            result.setParent(self)
            return result

    for header, value in shared.custom_headers(url=req.url()):
        req.setRawHeader(header, value)

    host_blocker = objreg.get('host-blocker')
    if host_blocker.is_blocked(req.url()):
        log.webview.info("Request to {} blocked by host blocker.".format(
            req.url().host()))
        return networkreply.ErrorNetworkReply(
            req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,
            self)

    # There are some scenarios where we can't figure out current_url:
    # - There's a generic NetworkManager, e.g. for downloads
    # - The download was in a tab which is now closed.
    current_url = QUrl()

    if self._tab_id is not None:
        assert self._win_id is not None
        try:
            tab = objreg.get('tab', scope='tab', window=self._win_id,
                             tab=self._tab_id)
            current_url = tab.url()
        except (KeyError, RuntimeError):
            # https://github.com/qutebrowser/qutebrowser/issues/889
            # Catching RuntimeError because we could be in the middle of
            # the webpage shutdown here.
            current_url = QUrl()

    if 'log-requests' in self._args.debug_flags:
        operation = debug.qenum_key(QNetworkAccessManager, op)
        operation = operation.replace('Operation', '').upper()
        log.webview.debug("{} {}, first-party {}".format(
            operation,
            req.url().toDisplayString(),
            current_url.toDisplayString()))

    self.set_referer(req, current_url)
    return super().createRequest(op, req, outgoing_data)
//  [python]
def _checkPolkitPrivilege(self, sender, conn, privilege):
    # from jockey
    """
    Verify that sender has a given PolicyKit privilege.

    sender is the sender's (private) D-BUS name, such as ":1:42"
    (sender_keyword in @dbus.service.methods). conn is
    the dbus.Connection object (connection_keyword in
    @dbus.service.methods). privilege is the PolicyKit privilege string.

    This method returns if the caller is privileged, and otherwise throws a
    PermissionDeniedByPolicy exception.
    """
    if sender is None and conn is None:
        # called locally, not through D-BUS
        return
    if not self.enforce_polkit:
        # that happens for testing purposes when running on the session
        # bus, and it does not make sense to restrict operations here
        return

    # query PolicyKit
    self._initPolkit()
    try:
        # we don't need is_challenge return here, since we call with AllowUserInteraction
        (is_auth, _, details) = self.polkit.CheckAuthorization(
                ('system-bus-name', {'name': dbus.String(sender, variant_level=1)}),
                privilege, {'': ''}, dbus.UInt32(1), '', timeout=3000)
    except dbus.DBusException as e:
        if e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown':
            # polkitd timed out, connect again
            self.polkit = None
            return self._checkPolkitPrivilege(sender, conn, privilege)
        else:
            raise

    if not is_auth:
        raise PermissionDeniedByPolicy(privilege)
//  [python]
def _checkPolkitPrivilege(self, sender, conn, privilege):
    # from jockey
    """
    Verify that sender has a given PolicyKit privilege.

    sender is the sender's (private) D-BUS name, such as ":1:42"
    (sender_keyword in @dbus.service.methods). conn is
    the dbus.Connection object (connection_keyword in
    @dbus.service.methods). privilege is the PolicyKit privilege string.

    This method returns if the caller is privileged, and otherwise throws a
    PermissionDeniedByPolicy exception.
    """
    if sender is None and conn is None:
        # called locally, not through D-BUS
        return
    if not self.enforce_polkit:
        # that happens for testing purposes when running on the session
        # bus, and it does not make sense to restrict operations here
        return

    info = SenderInfo(sender, conn)

    # get peer PID
    pid = info.connectionPid()

    # query PolicyKit
    self._initPolkit()
    try:
        # we don't need is_challenge return here, since we call with AllowUserInteraction
        (is_auth, _, details) = self.polkit.CheckAuthorization(
                ('unix-process', {'pid': dbus.UInt32(pid, variant_level=1),
                'start-time': dbus.UInt64(0, variant_level=1)}),
                privilege, {'': ''}, dbus.UInt32(1), '', timeout=3000)
    except dbus.DBusException as e:
        if e._dbus_error_name == 'org.freedesktop.DBus.Error.ServiceUnknown':
            # polkitd timed out, connect again
            self.polkit = None
            return self._checkPolkitPrivilege(sender, conn, privilege)
        else:
            raise

    if not is_auth:
        raise PermissionDeniedByPolicy(privilege)
//  [python]
def run_query(self, query, user):
    path = ""
    ua = ""
    args = {}
    try:
        args = yaml.safe_load(query)
        path = args['url']
        args.pop('url', None)
        ua = args['user-agent']
        args.pop('user-agent', None)

    except:
        pass

    try:
        response = requests_or_advocate.get(url=path, headers={"User-agent": ua})
        workbook = pd.read_excel(response.content, **args)

        df = workbook.copy()
        data = {'columns': [], 'rows': []}
        conversions = [
            {'pandas_type': np.integer, 'redash_type': 'integer',},
            {'pandas_type': np.inexact, 'redash_type': 'float',},
            {'pandas_type': np.datetime64, 'redash_type': 'datetime', 'to_redash': lambda x: x.strftime('%Y-%m-%d %H:%M:%S')},
            {'pandas_type': np.bool_, 'redash_type': 'boolean'},
            {'pandas_type': np.object, 'redash_type': 'string'}
        ]
        labels = []
        for dtype, label in zip(df.dtypes, df.columns):
            for conversion in conversions:
                if issubclass(dtype.type, conversion['pandas_type']):
                    data['columns'].append({'name': label, 'friendly_name': label, 'type': conversion['redash_type']})
                    labels.append(label)
                    func = conversion.get('to_redash')
                    if func:
                        df[label] = df[label].apply(func)
                    break
        data['rows'] = df[labels].replace({np.nan: None}).to_dict(orient='records')

        json_data = json_dumps(data)
        error = None
    except KeyboardInterrupt:
        error = "Query cancelled by user."
        json_data = None
    except UnacceptableAddressException:
        error = "Can't query private addresses."
        json_data = None
    except Exception as e:
        error = "Error reading {0}. {1}".format(path, str(e))
        json_data = None

    return json_data, error
//  [python]
def run_query(self, query, user):
    path = ""
    ua = ""
    args = {}
    try:
        args = yaml.safe_load(query)
        path = args['url']
        args.pop('url', None)
        ua = args['user-agent']
        args.pop('user-agent', None)

        if is_private_address(path) and settings.ENFORCE_PRIVATE_ADDRESS_BLOCK:
            raise Exception("Can't query private addresses.")
    except:
        pass

    try:
        response = requests.get(url=path, headers={"User-agent": ua})
        workbook = pd.read_excel(response.content, **args)

        df = workbook.copy()
        data = {'columns': [], 'rows': []}
        conversions = [
            {'pandas_type': np.integer, 'redash_type': 'integer',},
            {'pandas_type': np.inexact, 'redash_type': 'float',},
            {'pandas_type': np.datetime64, 'redash_type': 'datetime', 'to_redash': lambda x: x.strftime('%Y-%m-%d %H:%M:%S')},
            {'pandas_type': np.bool_, 'redash_type': 'boolean'},
            {'pandas_type': np.object, 'redash_type': 'string'}
        ]
        labels = []
        for dtype, label in zip(df.dtypes, df.columns):
            for conversion in conversions:
                if issubclass(dtype.type, conversion['pandas_type']):
                    data['columns'].append({'name': label, 'friendly_name': label, 'type': conversion['redash_type']})
                    labels.append(label)
                    func = conversion.get('to_redash')
                    if func:
                        df[label] = df[label].apply(func)
                    break
        data['rows'] = df[labels].replace({np.nan: None}).to_dict(orient='records')

        json_data = json_dumps(data)
        error = None
    except KeyboardInterrupt:
        error = "Query cancelled by user."
        json_data = None
    except Exception as e:
        error = "Error reading {0}. {1}".format(path, str(e))
        json_data = None

    return json_data, error
//  [python]
def camera_img_return_path(camera_unique_id, img_type, filename):
    """Return an image from stills or time-lapses"""
    camera = Camera.query.filter(Camera.unique_id == camera_unique_id).first()
    camera_path = assure_path_exists(
        os.path.join(PATH_CAMERAS, '{uid}'.format(uid=camera.unique_id)))
    if img_type == 'still':
        if camera.path_still:
            path = camera.path_still
        else:
            path = os.path.join(camera_path, img_type)
    elif img_type == 'timelapse':
        if camera.path_timelapse:
            path = camera.path_timelapse
        else:
            path = os.path.join(camera_path, img_type)
    else:
        return "Unknown Image Type"

    if os.path.isdir(path):
        files = (files for files in os.listdir(path)
                 if os.path.isfile(os.path.join(path, files)))
    else:
        files = []
    if filename in files:
        path_file = os.path.join(path, filename)
        if os.path.abspath(path_file).startswith(path):
            return send_file(path_file, mimetype='image/jpeg')

    return "Image not found"
//  [python]
def camera_img_return_path(camera_unique_id, img_type, filename):
    """Return an image from stills or time-lapses"""
    camera = Camera.query.filter(Camera.unique_id == camera_unique_id).first()
    camera_path = assure_path_exists(
        os.path.join(PATH_CAMERAS, '{uid}'.format(uid=camera.unique_id)))
    if img_type == 'still':
        if camera.path_still:
            path = camera.path_still
        else:
            path = os.path.join(camera_path, img_type)
    elif img_type == 'timelapse':
        if camera.path_timelapse:
            path = camera.path_timelapse
        else:
            path = os.path.join(camera_path, img_type)
    else:
        return "Unknown Image Type"

    if os.path.isdir(path):
        files = (files for files in os.listdir(path)
                 if os.path.isfile(os.path.join(path, files)))
    else:
        files = []
    if filename in files:
        path_file = os.path.join(path, filename)
        return send_file(path_file, mimetype='image/jpeg')

    return "Image not found"
//  [python]
def test_render_idn(self):
    w = widgets.AdminURLFieldWidget()
    self.assertHTMLEqual(
        conditional_escape(w.render('test', 'http://example-äüö.com')),
        '<p class="url">Currently: <a href="http://xn--example--7za4pnc.com">http://example-äüö.com</a><br />Change:<input class="vURLField" name="test" type="url" value="http://example-äüö.com" /></p>'
    )
//  [python]
def test_render_idn(self):
    w = widgets.AdminURLFieldWidget()
    self.assertHTMLEqual(
        conditional_escape(w.render('test', 'http://example-äüö.com')),
        '<p class="url">Currently:<a href="http://xn--example--7za4pnc.com">http://example-äüö.com</a><br />Change:<input class="vURLField" name="test" type="url" value="http://example-äüö.com" /></p>'
    )
//  [python]
def validate(self, data):
    user = providers.get_provider('login').login_user(**data, context=self.context)
    data['user'] = user
    return data
//  [python]
def validate(self, data):
    user = providers.get_provider('login').login_user(**data, context=self.context)
    if user is not None:
        data['user'] = user
    return data
//  [python]
def test_render_quoting(self):
    # WARNING: Don't use assertHTMLEqual in that testcase!
    # assertHTMLEqual will get rid of some escapes which are tested here!
    w = widgets.AdminURLFieldWidget()
    self.assertEqual(
        w.render('test', 'http://example.com/<sometag>some text</sometag>'),
        '<p class="url">Currently: <a href="http://example.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example.com/&lt;sometag&gt;some text&lt;/sometag&gt;</a><br />Change: <input class="vURLField" name="test" type="url" value="http://example.com/&lt;sometag&gt;some text&lt;/sometag&gt;" /></p>'
    )
    self.assertEqual(
        w.render('test', 'http://example-äüö.com/<sometag>some text</sometag>'),
        '<p class="url">Currently: <a href="http://xn--example--7za4pnc.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example-äüö.com/&lt;sometag&gt;some text&lt;/sometag&gt;</a><br />Change: <input class="vURLField" name="test" type="url" value="http://example-äüö.com/&lt;sometag&gt;some text&lt;/sometag&gt;" /></p>'
    )
    self.assertEqual(
        w.render('test', 'http://www.example.com/%C3%A4"><script>alert("XSS!")</script>"'),
        '<p class="url">Currently: <a href="http://www.example.com/%C3%A4%22%3E%3Cscript%3Ealert(%22XSS!%22)%3C/script%3E%22">http://www.example.com/%C3%A4&quot;&gt;&lt;script&gt;alert(&quot;XSS!&quot;)&lt;/script&gt;&quot;</a><br />Change: <input class="vURLField" name="test" type="url" value="http://www.example.com/%C3%A4&quot;&gt;&lt;script&gt;alert(&quot;XSS!&quot;)&lt;/script&gt;&quot;" /></p>'
    )
//  [python]
def test_render_quoting(self):
    w = widgets.AdminURLFieldWidget()
    self.assertHTMLEqual(
        conditional_escape(w.render('test', 'http://example.com/<sometag>some text</sometag>')),
        '<p class="url">Currently:<a href="http://example.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example.com/&lt;sometag&gt;some text&lt;/sometag&gt;</a><br />Change:<input class="vURLField" name="test" type="url" value="http://example.com/<sometag>some text</sometag>" /></p>'
    )
    self.assertHTMLEqual(
        conditional_escape(w.render('test', 'http://example-äüö.com/<sometag>some text</sometag>')),
        '<p class="url">Currently:<a href="http://xn--example--7za4pnc.com/%3Csometag%3Esome%20text%3C/sometag%3E">http://example-äüö.com/&lt;sometag&gt;some text&lt;/sometag&gt;</a><br />Change:<input class="vURLField" name="test" type="url" value="http://example-äüö.com/<sometag>some text</sometag>" /></p>'
    )
//  [python]
def _expand(self, key_material):
    output = [b""]
    counter = 1

    while self._algorithm.digest_size * (len(output) - 1) < self._length:
        h = hmac.HMAC(key_material, self._algorithm, backend=self._backend)
        h.update(output[-1])
        h.update(self._info)
        h.update(six.int2byte(counter))
        output.append(h.finalize())
        counter += 1

    return b"".join(output)[:self._length]
//  [python]
def _expand(self, key_material):
    output = [b""]
    counter = 1

    while (self._algorithm.digest_size // 8) * len(output) < self._length:
        h = hmac.HMAC(key_material, self._algorithm, backend=self._backend)
        h.update(output[-1])
        h.update(self._info)
        h.update(six.int2byte(counter))
        output.append(h.finalize())
        counter += 1

    return b"".join(output)[:self._length]
//  [python]
def refresh(self) -> Response:
    """
        Security endpoint for the refresh token, so we can obtain a new
        token without forcing the user to login again
    ---
    post:
      description: >-
        Use the refresh token to get a new JWT access token
      responses:
        200:
          description: Refresh Successful
          content:
            application/json:
              schema:
                type: object
                properties:
                  access_token:
                    description: A new refreshed access token
                    type: string
        401:
          $ref: '#/components/responses/401'
        500:
          $ref: '#/components/responses/500'
      security:
        - jwt_refresh: []
    """
    resp = {
        API_SECURITY_ACCESS_TOKEN_KEY: create_access_token(
            identity=get_jwt_identity(), fresh=False
        )
    }
    return self.response(200, **resp)
//  [python]
def refresh(self):
    """
        Security endpoint for the refresh token, so we can obtain a new
        token without forcing the user to login again
    ---
    post:
      description: >-
        Use the refresh token to get a new JWT access token
      responses:
        200:
          description: Refresh Successful
          content:
            application/json:
              schema:
                type: object
                properties:
                  access_token:
                    description: A new refreshed access token
                    type: string
        401:
          $ref: '#/components/responses/401'
        500:
          $ref: '#/components/responses/500'
      security:
        - jwt_refresh: []
    """
    resp = {
        API_SECURITY_ACCESS_TOKEN_KEY: create_access_token(
            identity=get_jwt_identity(), fresh=False
        )
    }
    return self.response(200, **resp)
//  [python]
def login(self) -> Response:
    """Login endpoint for the API returns a JWT and optionally a refresh token
    ---
    post:
      description: >-
        Authenticate and get a JWT access and refresh token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                username:
                  description: The username for authentication
                  example: admin
                  type: string
                password:
                  description: The password for authentication
                  example: complex-password
                  type: string
                provider:
                  description: Choose an authentication provider
                  example: db
                  type: string
                  enum:
                  - db
                  - ldap
                refresh:
                  description: If true a refresh token is provided also
                  example: true
                  type: boolean
      responses:
        200:
          description: Authentication Successful
          content:
            application/json:
              schema:
                type: object
                properties:
                  access_token:
                    type: string
                  refresh_token:
                    type: string
        400:
          $ref: '#/components/responses/400'
        401:
          $ref: '#/components/responses/401'
        500:
          $ref: '#/components/responses/500'
    """
    if not request.is_json:
        return self.response_400(message="Request payload is not JSON")
    try:
        login_payload = login_post.load(request.json)
    except ValidationError as error:
        return self.response_400(message=error.messages)

    # AUTH
    user = None
    if login_payload["provider"] == API_SECURITY_PROVIDER_DB:
        user = self.appbuilder.sm.auth_user_db(
            login_payload["username"], login_payload["password"]
        )
    elif login_payload["provider"] == API_SECURITY_PROVIDER_LDAP:
        user = self.appbuilder.sm.auth_user_ldap(
            login_payload["username"], login_payload["password"]
        )
    if not user:
        return self.response_401()

    # Identity can be any data that is json serializable
    resp = dict()
    resp[API_SECURITY_ACCESS_TOKEN_KEY] = create_access_token(
        identity=user.id, fresh=True
    )
    if "refresh" in login_payload:
        login_payload["refresh"] = create_refresh_token(identity=user.id)
    return self.response(200, **resp)
//  [python]
def login(self):
    """Login endpoint for the API returns a JWT and optionally a refresh token
    ---
    post:
      description: >-
        Authenticate and get a JWT access and refresh token
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                username:
                  description: The username for authentication
                  example: admin
                  type: string
                password:
                  description: The password for authentication
                  example: complex-password
                  type: string
                provider:
                  description: Choose an authentication provider
                  example: db
                  type: string
                  enum:
                  - db
                  - ldap
                refresh:
                  description: If true a refresh token is provided also
                  example: true
                  type: boolean
      responses:
        200:
          description: Authentication Successful
          content:
            application/json:
              schema:
                type: object
                properties:
                  access_token:
                    type: string
                  refresh_token:
                    type: string
        400:
          $ref: '#/components/responses/400'
        401:
          $ref: '#/components/responses/401'
        500:
          $ref: '#/components/responses/500'
    """
    if not request.is_json:
        return self.response_400(message="Request payload is not JSON")
    username = request.json.get(API_SECURITY_USERNAME_KEY, None)
    password = request.json.get(API_SECURITY_PASSWORD_KEY, None)
    provider = request.json.get(API_SECURITY_PROVIDER_KEY, None)
    refresh = request.json.get(API_SECURITY_REFRESH_KEY, False)
    if not username or not password or not provider:
        return self.response_400(message="Missing required parameter")
    # AUTH
    if provider == API_SECURITY_PROVIDER_DB:
        user = self.appbuilder.sm.auth_user_db(username, password)
    elif provider == API_SECURITY_PROVIDER_LDAP:
        user = self.appbuilder.sm.auth_user_ldap(username, password)
    else:
        return self.response_400(
            message="Provider {} not supported".format(provider)
        )
    if not user:
        return self.response_401()

    # Identity can be any data that is json serializable
    resp = dict()
    resp[API_SECURITY_ACCESS_TOKEN_KEY] = create_access_token(
        identity=user.id, fresh=True
    )
    if refresh:
        resp[API_SECURITY_REFRESH_TOKEN_KEY] = create_refresh_token(
            identity=user.id
        )
    return self.response(200, **resp)
//  [python]
def test_underscore_traversal(self):
    t = self.folder.t

    t.write('<p tal:define="p context/__class__" />')
    with self.assertRaises(NotFound):
        t()

    t.write('<p tal:define="p nocall: random/_itertools/repeat"/>')
    with self.assertRaises((NotFound, LocationError)):
        t()

    t.write('<p tal:content="random/_itertools/repeat/foobar"/>')
    with self.assertRaises((NotFound, LocationError)):
        t()
//  [python]
def test_underscore_traversal(self):
    t = self.folder.t

    t.write('<p tal:define="p context/__class__" />')
    with self.assertRaises(NotFound):
        t()

    t.write('<p tal:define="p nocall: random/_itertools/repeat"/>')
    with self.assertRaises(NotFound):
        t()

    t.write('<p tal:content="random/_itertools/repeat/foobar"/>')
    with self.assertRaises(NotFound):
        t()
//  [python]
def rpn(self, irc, msg, args):
    """<rpn math expression>

    Returns the value of an RPN expression.
    """
    stack = []
    for arg in args:
        try:
            x = complex(arg)
            if x == abs(x):
                x = abs(x)
            stack.append(x)
        except ValueError: # Not a float.
            if arg in SAFE_ENV:
                f = SAFE_ENV[arg]
                if callable(f):
                    called = False
                    arguments = []
                    while not called and stack:
                        arguments.append(stack.pop())
                        try:
                            stack.append(f(*arguments))
                            called = True
                        except TypeError:
                            pass
                    if not called:
                        irc.error(_('Not enough arguments for %s') % arg)
                        return
                else:
                    stack.append(f)
            elif arg in self._rpnEnv:
                self._rpnEnv[arg](stack)
            else:
                arg2 = stack.pop()
                arg1 = stack.pop()
                s = '%s%s%s' % (arg1, arg, arg2)
                try:
                    stack.append(safe_eval(s, allow_ints=False))
                except SyntaxError:
                    irc.error(format(_('%q is not a defined function.'),
                                     arg))
                    return
    if len(stack) == 1:
        irc.reply(str(self._complexToString(complex(stack[0]))))
    else:
        s = ', '.join(map(self._complexToString, list(map(complex, stack))))
        irc.reply(_('Stack: [%s]') % s)
//  [python]
def rpn(self, irc, msg, args):
    """<rpn math expression>

    Returns the value of an RPN expression.
    """
    stack = []
    for arg in args:
        try:
            x = complex(arg)
            if x == abs(x):
                x = abs(x)
            stack.append(x)
        except ValueError: # Not a float.
            if arg in self._mathSafeEnv:
                f = self._mathSafeEnv[arg]
                if callable(f):
                    called = False
                    arguments = []
                    while not called and stack:
                        arguments.append(stack.pop())
                        try:
                            stack.append(f(*arguments))
                            called = True
                        except TypeError:
                            pass
                    if not called:
                        irc.error(_('Not enough arguments for %s') % arg)
                        return
                else:
                    stack.append(f)
            elif arg in self._rpnEnv:
                self._rpnEnv[arg](stack)
            else:
                arg2 = stack.pop()
                arg1 = stack.pop()
                s = '%s%s%s' % (arg1, arg, arg2)
                try:
                    stack.append(eval(s, self._mathSafeEnv, self._mathSafeEnv))
                except SyntaxError:
                    irc.error(format(_('%q is not a defined function.'),
                                     arg))
                    return
    if len(stack) == 1:
        irc.reply(str(self._complexToString(complex(stack[0]))))
    else:
        s = ', '.join(map(self._complexToString, list(map(complex, stack))))
        irc.reply(_('Stack: [%s]') % s)
//  [python]
def is_authenticated(self, user, password):
    # The content of the file is not cached because reading is generally a
    # very cheap operation, and it's useful to get live updates of the
    # htpasswd file.
    with open(self.filename) as fd:
        for line in fd:
            line = line.strip()
            if line:
                login, hash_value = line.split(":")
                if login == user and self.verify(hash_value, password):
                    return True
    # Random timer to avoid timing oracles and simple bruteforce attacks
    time.sleep(1 + random.random())
    return False
//  [python]
def is_authenticated(self, user, password):
    # The content of the file is not cached because reading is generally a
    # very cheap operation, and it's useful to get live updates of the
    # htpasswd file.
    with open(self.filename) as fd:
        for line in fd:
            line = line.strip()
            if line:
                login, hash_value = line.split(":")
                if login == user:
                    return self.verify(hash_value, password)
    return False
//  [python]
def deserialize(self, content, format='application/json'):
    """
    Given some data and a format, calls the correct method to deserialize
    the data and returns the result.
    """
    desired_format = None

    format = format.split(';')[0]

    for short_format, long_format in self.content_types.items():
        if format == long_format:
            if hasattr(self, "from_%s" % short_format):
                desired_format = short_format
                break

    if desired_format is None:
        raise UnsupportedFormat("The format indicated '%s' had no available deserialization method. Please check your ``formats`` and ``content_types`` on your Serializer." % format)

    deserialized = getattr(self, "from_%s" % desired_format)(content)
    return deserialized
//  [python]
def deserialize(self, content, format='application/json'):
    """
    Given some data and a format, calls the correct method to deserialize
    the data and returns the result.
    """
    desired_format = None

    format = format.split(';')[0]

    for short_format, long_format in self.content_types.items():
        if format == long_format:
            if hasattr(self, "from_%s" % short_format):
                desired_format = short_format
                break
    
    if desired_format is None:
        raise UnsupportedFormat("The format indicated '%s' had no available deserialization method. Please check your ``formats`` and ``content_types`` on your Serializer." % format)
    
    deserialized = getattr(self, "from_%s" % desired_format)(content)
    return deserialized
//  [python]
def __init__(self, formats=None, content_types=None, datetime_formatting=None):
    self.supported_formats = []
    self.datetime_formatting = getattr(settings, 'TASTYPIE_DATETIME_FORMATTING', 'iso-8601')

    if formats is not None:
        self.formats = formats

    if content_types is not None:
        self.content_types = content_types

    if datetime_formatting is not None:
        self.datetime_formatting = datetime_formatting

    for format in self.formats:
        try:
            self.supported_formats.append(self.content_types[format])
        except KeyError:
            raise ImproperlyConfigured("Content type for specified type '%s' not found. Please provide it at either the class level or via the arguments." % format)
//  [python]
def __init__(self, formats=None, content_types=None, datetime_formatting=None):
    self.supported_formats = []
    self.datetime_formatting = getattr(settings, 'TASTYPIE_DATETIME_FORMATTING', 'iso-8601')
    
    if formats is not None:
        self.formats = formats
    
    if content_types is not None:
        self.content_types = content_types
    
    if datetime_formatting is not None:
        self.datetime_formatting = datetime_formatting
    
    for format in self.formats:
        try:
            self.supported_formats.append(self.content_types[format])
        except KeyError:
            raise ImproperlyConfigured("Content type for specified type '%s' not found. Please provide it at either the class level or via the arguments." % format)
//  [python]
def send_note_attachment(filename):
    """Return a file from the note attachment directory"""
    file_path = os.path.join(PATH_NOTE_ATTACHMENTS, filename)
    if file_path is not None:
        try:
            if os.path.abspath(file_path).startswith(PATH_NOTE_ATTACHMENTS):
                return send_file(file_path, as_attachment=True)
        except Exception:
            logger.exception("Send note attachment")
//  [python]
def send_note_attachment(filename):
    """Return a file from the note attachment directory"""
    file_path = os.path.join(PATH_NOTE_ATTACHMENTS, filename)
    if file_path is not None:
        try:
            return send_file(file_path, as_attachment=True)
        except Exception:
            logger.exception("Send note attachment")
//  [python]
def get_ipcache_entry(self, client):
    """Build a cache of dns results."""
    if client in self.ipcache:
        if self.ipcache[client]:
            return self.ipcache[client]
        else:
            raise socket.gaierror
    else:
        # need to add entry
        try:
            ipaddr = socket.gethostbyname(client)
            self.ipcache[client] = (ipaddr, client)
            return (ipaddr, client)
        except socket.gaierror:
            ipaddr = Popen(["getent", "hosts", client],
                           stdout=PIPE).stdout.read().strip().split()
            if ipaddr:
                self.ipcache[client] = (ipaddr, client)
                return (ipaddr, client)
            self.ipcache[client] = False
            self.logger.error("Failed to find IP address for %s" % client)
            raise socket.gaierror
//  [python]
def get_ipcache_entry(self, client):
    """Build a cache of dns results."""
    if client in self.ipcache:
        if self.ipcache[client]:
            return self.ipcache[client]
        else:
            raise socket.gaierror
    else:
        # need to add entry
        try:
            ipaddr = socket.gethostbyname(client)
            self.ipcache[client] = (ipaddr, client)
            return (ipaddr, client)
        except socket.gaierror:
            cmd = "getent hosts %s" % client
            ipaddr = Popen(cmd, shell=True, \
                           stdout=PIPE).stdout.read().strip().split()
            if ipaddr:
                self.ipcache[client] = (ipaddr, client)
                return (ipaddr, client)
            self.ipcache[client] = False
            self.logger.error("Failed to find IP address for %s" % client)
            raise socket.gaierror
//  [python]
def login_user(self, username, password, context, **kwargs):
    user = authenticate(request=context.get('request'),
                        username=username, password=password)
    if not user:
        login_reject.send(sender=self.__class__, username=username, reason='creds')
        raise FormattedException(m='incorrect_username_or_password', d={'reason': 'incorrect_username_or_password'},
                                 status_code=HTTP_401_UNAUTHORIZED)

    if not user.email_verified and not user.is_superuser:
        login_reject.send(sender=self.__class__, username=username, reason='email')
        raise FormattedException(m='email_verification_required', d={'reason': 'email_verification_required'},
                                 status_code=HTTP_401_UNAUTHORIZED)

    if not user.can_login():
        login_reject.send(sender=self.__class__, username=username, reason='closed')
        raise FormattedException(m='login_not_open', d={'reason': 'login_not_open'},
                                 status_code=HTTP_401_UNAUTHORIZED)

    login.send(sender=self.__class__, user=user)
    return user
//  [python]
def login_user(self, username, password, otp, context, **kwargs):
    user = authenticate(request=context.get('request'),
                        username=username, password=password)
    if not user:
        login_reject.send(sender=self.__class__, username=username, reason='creds')
        raise FormattedException(m='incorrect_username_or_password', d={'reason': 'incorrect_username_or_password'},
                                 status_code=HTTP_401_UNAUTHORIZED)

    if not user.email_verified and not user.is_superuser:
        login_reject.send(sender=self.__class__, username=username, reason='email')
        raise FormattedException(m='email_verification_required', d={'reason': 'email_verification_required'},
                                 status_code=HTTP_401_UNAUTHORIZED)

    if not user.can_login():
        login_reject.send(sender=self.__class__, username=username, reason='closed')
        raise FormattedException(m='login_not_open', d={'reason': 'login_not_open'},
                                 status_code=HTTP_401_UNAUTHORIZED)

    if user.totp_status == TOTPStatus.ENABLED:
        if not otp or otp == '':
            login_reject.send(sender=self.__class__, username=username, reason='no_2fa')
            raise FormattedException(m='2fa_required', d={'reason': '2fa_required'},
                                     status_code=HTTP_401_UNAUTHORIZED)
        totp = pyotp.TOTP(user.totp_secret)
        if not totp.verify(otp, valid_window=1):
            login_reject.send(sender=self.__class__, username=username, reason='incorrect_2fa')
            raise FormattedException(m='incorrect_2fa', d={'reason': 'incorrect_2fa'},
                                     status_code=HTTP_401_UNAUTHORIZED)
    login.send(sender=self.__class__, user=user)
    return user
//  [python]
def format_datetime(self, data):
    """
    A hook to control how datetimes are formatted.

    Can be overridden at the ``Serializer`` level (``datetime_formatting``)
    or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).

    Default is ``iso-8601``, which looks like "2010-12-16T03:02:14".
    """
    if self.datetime_formatting == 'rfc-2822':
        return format_datetime(data)

    return data.isoformat()
//  [python]
def format_datetime(self, data):
    """
    A hook to control how datetimes are formatted.
    
    Can be overridden at the ``Serializer`` level (``datetime_formatting``)
    or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).
    
    Default is ``iso-8601``, which looks like "2010-12-16T03:02:14".
    """
    if self.datetime_formatting == 'rfc-2822':
        return format_datetime(data)
    
    return data.isoformat()
//  [python]
def format_time(self, data):
    """
    A hook to control how times are formatted.

    Can be overridden at the ``Serializer`` level (``datetime_formatting``)
    or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).

    Default is ``iso-8601``, which looks like "03:02:14".
    """
    if self.datetime_formatting == 'rfc-2822':
        return format_time(data)

    return data.isoformat()
//  [python]
def format_time(self, data):
    """
    A hook to control how times are formatted.
    
    Can be overridden at the ``Serializer`` level (``datetime_formatting``)
    or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).
    
    Default is ``iso-8601``, which looks like "03:02:14".
    """
    if self.datetime_formatting == 'rfc-2822':
        return format_time(data)
    
    return data.isoformat()
//  [python]
def format_date(self, data):
    """
    A hook to control how dates are formatted.

    Can be overridden at the ``Serializer`` level (``datetime_formatting``)
    or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).

    Default is ``iso-8601``, which looks like "2010-12-16".
    """
    if self.datetime_formatting == 'rfc-2822':
        return format_date(data)

    return data.isoformat()
//  [python]
def format_date(self, data):
    """
    A hook to control how dates are formatted.
    
    Can be overridden at the ``Serializer`` level (``datetime_formatting``)
    or globally (via ``settings.TASTYPIE_DATETIME_FORMATTING``).
    
    Default is ``iso-8601``, which looks like "2010-12-16".
    """
    if self.datetime_formatting == 'rfc-2822':
        return format_date(data)
    
    return data.isoformat()
//  [python]
def from_yaml(self, content):
    """
    Given some YAML data, returns a Python dictionary of the decoded data.
    """
    if yaml is None:
        raise ImproperlyConfigured("Usage of the YAML aspects requires yaml.")

    return yaml.safe_load(content)
//  [python]
def from_yaml(self, content):
    """
    Given some YAML data, returns a Python dictionary of the decoded data.
    """
    if yaml is None:
        raise ImproperlyConfigured("Usage of the YAML aspects requires yaml.")
    
    return yaml.load(content)
//  [python]
def from_plist(self, content):
    """
    Given some binary plist data, returns a Python dictionary of the decoded data.
    """
    if biplist is None:
        raise ImproperlyConfigured("Usage of the plist aspects requires biplist.")

    return biplist.readPlistFromString(content)
//  [python]
def from_plist(self, content):
    """
    Given some binary plist data, returns a Python dictionary of the decoded data.
    """
    if biplist is None:
        raise ImproperlyConfigured("Usage of the plist aspects requires biplist.")
    
    return biplist.readPlistFromString(content)
//  [python]
def get_mime_for_format(self, format):
    """
    Given a format, attempts to determine the correct MIME type.

    If not available on the current ``Serializer``, returns
    ``application/json`` by default.
    """
    try:
        return self.content_types[format]
    except KeyError:
        return 'application/json'
//  [python]
def get_mime_for_format(self, format):
    """
    Given a format, attempts to determine the correct MIME type.
    
    If not available on the current ``Serializer``, returns
    ``application/json`` by default.
    """
    try:
        return self.content_types[format]
    except KeyError:
        return 'application/json'
//  [python]
def from_html(self, content):
    """
    Reserved for future usage.

    The desire is to handle form-based (maybe Javascript?) input, making an
    API available to a browser. This is on the TODO list but not currently
    implemented.
    """
    pass
//  [python]
def from_html(self, content):
    """
    Reserved for future usage.
    
    The desire is to handle form-based (maybe Javascript?) input, making an
    API available to a browser. This is on the TODO list but not currently
    implemented.
    """
    pass
//  [python]
def serialize(self, bundle, format='application/json', options={}):
    """
    Given some data and a format, calls the correct method to serialize
    the data and returns the result.
    """
    desired_format = None

    for short_format, long_format in self.content_types.items():
        if format == long_format:
            if hasattr(self, "to_%s" % short_format):
                desired_format = short_format
                break

    if desired_format is None:
        raise UnsupportedFormat("The format indicated '%s' had no available serialization method. Please check your ``formats`` and ``content_types`` on your Serializer." % format)

    serialized = getattr(self, "to_%s" % desired_format)(bundle, options)
    return serialized
//  [python]
def serialize(self, bundle, format='application/json', options={}):
    """
    Given some data and a format, calls the correct method to serialize
    the data and returns the result.
    """
    desired_format = None
    
    for short_format, long_format in self.content_types.items():
        if format == long_format:
            if hasattr(self, "to_%s" % short_format):
                desired_format = short_format
                break
    
    if desired_format is None:
        raise UnsupportedFormat("The format indicated '%s' had no available serialization method. Please check your ``formats`` and ``content_types`` on your Serializer." % format)
    
    serialized = getattr(self, "to_%s" % desired_format)(bundle, options)
    return serialized
//  [python]
def from_xml(self, content):
    """
    Given some XML data, returns a Python dictionary of the decoded data.
    """
    if lxml is None:
        raise ImproperlyConfigured("Usage of the XML aspects requires lxml.")

    return self.from_etree(parse_xml(StringIO(content)).getroot())
//  [python]
def from_xml(self, content):
    """
    Given some XML data, returns a Python dictionary of the decoded data.
    """
    if lxml is None:
        raise ImproperlyConfigured("Usage of the XML aspects requires lxml.")
    
    return self.from_etree(parse_xml(StringIO(content)).getroot())
//  [python]
def to_html(self, data, options=None):
    """
    Reserved for future usage.

    The desire is to provide HTML output of a resource, making an API
    available to a browser. This is on the TODO list but not currently
    implemented.
    """
    options = options or {}
    return 'Sorry, not implemented yet. Please append "?format=json" to your URL.'
//  [python]
def to_html(self, data, options=None):
    """
    Reserved for future usage.
    
    The desire is to provide HTML output of a resource, making an API
    available to a browser. This is on the TODO list but not currently
    implemented.
    """
    options = options or {}
    return 'Sorry, not implemented yet. Please append "?format=json" to your URL.'
//  [python]
def to_plist(self, data, options=None):
    """
    Given some Python data, produces binary plist output.
    """
    options = options or {}

    if biplist is None:
        raise ImproperlyConfigured("Usage of the plist aspects requires biplist.")

    return biplist.writePlistToString(self.to_simple(data, options))
//  [python]
def to_plist(self, data, options=None):
    """
    Given some Python data, produces binary plist output.
    """
    options = options or {}
    
    if biplist is None:
        raise ImproperlyConfigured("Usage of the plist aspects requires biplist.")
    
    return biplist.writePlistToString(self.to_simple(data, options))
//  [python]
def get_type_string(data):
    """
    Translates a Python data type into a string format.
    """
    data_type = type(data)

    if data_type in (int, long):
        return 'integer'
    elif data_type == float:
        return 'float'
    elif data_type == bool:
        return 'boolean'
    elif data_type in (list, tuple):
        return 'list'
    elif data_type == dict:
        return 'hash'
    elif data is None:
        return 'null'
    elif isinstance(data, basestring):
        return 'string'