Exemple #1
0
    def upload(self, name, encoded_args, max_errors, contents):
        """Upload file, verify it and create the entry.
    """
        max_errors = int(max_errors)
        if (QueryExpansionBase.applying_changes
                or QueryExpansionBase.uploading_dict):
            logging.error(
                "Create/Upload query exp ignored (operation in progress).")
            return admin_handler.formatValidationErrors(
                validatorlib.VALID_SHORT_CIRCUIT)

        val = {}
        config_utils.SafeExec(string.strip(encoded_args), val)
        if not val.has_key('QEPARAMS'):
            logging.error("Query expansion parameters missing in %s" %
                          encoded_args)
            return admin_handler.formatValidationErrors(
                validatorlib.VALID_SHORT_CIRCUIT)

        params = val['QEPARAMS']
        errors = validatorlib.VALID_OK

        # Uploaded entries are deletable and downloadable
        params[C.DELETABLE] = 1
        params[C.DOWNLOADABLE] = 1

        # Create a collection object in memory and do some initial checks on it,
        # but do not yet actually create it in the configuration.
        # Some of the code here is copied from collectionbase_handler, as we need
        # finer control.
        if not entconfig.IsNameValid(name):
            logging.error("Invalid query exp name %s" % name)
            return self.formatError(C.QUERYEXP_ENTRY_BAD_NAME, "Invalid name")

        if not self.check_max_license():
            return self.formatError(
                C.QUERYEXP_LICENSE_LIMIT,
                "License limit for query expansion reached")

        coll_obj = self.qe_base.ConstructCollectionObject(name)
        if coll_obj.Exists():
            logging.error("Query exp entry %s aleady exists" % name)
            return self.formatError(C.QUERYEXP_ENTRY_EXISTS,
                                    "Query expansion entry already exists.")

        QueryExpansionBase.uploading_dict = 1
        errors = self.qe_base.Upload(coll_obj, 0, params, max_errors, contents)
        QueryExpansionBase.uploading_dict = 0

        if errors != validatorlib.VALID_OK:
            msg = M.MSG_LOG_QUERYEXP_VALIDATION_FAILED % name
            self.writeAdminRunnerOpMsg(msg)
            # Make sure we deliver the right number of errors (the validator
            # sometimes returns too many)
            return admin_handler.formatValidationErrors(errors[0:max_errors])

        msg = M.MSG_LOG_CREATE_COLLECTION % ('query expansion', name)
        self.writeAdminRunnerOpMsg(msg)
        return admin_handler.formatValidationErrors(validatorlib.VALID_OK)
  def upload(self, name, encoded_args, max_errors, contents):
    """Upload file, verify it and create the entry.
    """
    max_errors = int(max_errors)
    if (QueryExpansionBase.applying_changes or
        QueryExpansionBase.uploading_dict):
      logging.error("Create/Upload query exp ignored (operation in progress).")
      return admin_handler.formatValidationErrors(
        validatorlib.VALID_SHORT_CIRCUIT)

    val = {}
    config_utils.SafeExec(string.strip(encoded_args), val)
    if not val.has_key('QEPARAMS'):
      logging.error("Query expansion parameters missing in %s" % encoded_args)
      return admin_handler.formatValidationErrors(
        validatorlib.VALID_SHORT_CIRCUIT)

    params = val['QEPARAMS']
    errors = validatorlib.VALID_OK

    # Uploaded entries are deletable and downloadable
    params[C.DELETABLE] = 1
    params[C.DOWNLOADABLE] = 1

    # Create a collection object in memory and do some initial checks on it,
    # but do not yet actually create it in the configuration.
    # Some of the code here is copied from collectionbase_handler, as we need
    # finer control.
    if not entconfig.IsNameValid(name):
      logging.error("Invalid query exp name %s" % name)
      return self.formatError(C.QUERYEXP_ENTRY_BAD_NAME,
                              "Invalid name")

    if not self.check_max_license():
      return self.formatError(C.QUERYEXP_LICENSE_LIMIT,
                              "License limit for query expansion reached")

    coll_obj = self.qe_base.ConstructCollectionObject(name)
    if coll_obj.Exists():
      logging.error("Query exp entry %s aleady exists" % name)
      return self.formatError(C.QUERYEXP_ENTRY_EXISTS,
                              "Query expansion entry already exists.")

    QueryExpansionBase.uploading_dict = 1
    errors = self.qe_base.Upload(coll_obj, 0, params, max_errors, contents)
    QueryExpansionBase.uploading_dict = 0

    if errors != validatorlib.VALID_OK:
      msg = M.MSG_LOG_QUERYEXP_VALIDATION_FAILED % name
      self.writeAdminRunnerOpMsg(msg)
      # Make sure we deliver the right number of errors (the validator
      # sometimes returns too many)
      return admin_handler.formatValidationErrors(errors[0:max_errors])

    msg = M.MSG_LOG_CREATE_COLLECTION % ('query expansion', name)
    self.writeAdminRunnerOpMsg(msg)
    return admin_handler.formatValidationErrors(validatorlib.VALID_OK)
  def set(self, paramName, value):
    """ Sets the parameter to a specified value """
    val = {}
    config_utils.SafeExec(string.strip(value), val)
    if not val.has_key(paramName): return 1
    value = val[paramName]

    errors = self.cfg.globalParams.set_var(paramName, value, true)
    return admin_handler.formatValidationErrors(errors)
  def setparams(self, policy_name, encoded_args):
    """Set the parameters for groups of scoring adjustments, including
       validation. Creates the group if it does not exist.
       If no errors, returns an empty list.
       Otherwise, returns a list of tuples of error code and detail string."""

    settings = {}
    config_utils.SafeExec(string.strip(encoded_args), settings)
    errors = []

    # Validate settings for each group.
    for group in settings.keys():
      if group == "patterns":
        # Params should be a list containing the scoring weight, then
        # alternating patterns and adjust levels. We only validate the
        # patterns.
        errors = self.validate_patterns(settings["patterns"])
      elif group == "datebias":
        # the only param, weight [0..100] is already validated in the
        # handler code (ScoringAdjustHandler)
        pass
      elif group == "metadata":
        # Params should be a list containing the scoring weight, then
        # alternating name:value metadata information and adjust levels.
        # We only validate the name:value metadata information.
        errors = self.validate_metadata(settings["metadata"])
      else:
        logging.info("Ignoring unknown scoring group " + group)

    # If no errors yet, make sure policy is present.
    policy = None
    if not errors:
      if policy_name == ScoringAdjustHandler.DEFAULT_POLICY_NAME:
        policy = self.cfg.getGlobalParam(C.ENT_SCORING_ADJUST)
      else:
        policies = self.cfg.getGlobalParam(C.ENT_SCORING_ADDITIONAL_POLICIES)
        if policies and policy_name in policies:
          policy = policies[policy_name]
      if policy is None:
        errors.append(validatorlib.ValidationError(
            policy_name, C.SCORING_ADJUST_POLICY_MISSING))

    # If no errors, now save each group (even unknown ones)
    if not errors:
      for group in settings.keys():
        policy[group] = settings[group]
      if policy_name == ScoringAdjustHandler.DEFAULT_POLICY_NAME:
        self.cfg.setGlobalParam(C.ENT_SCORING_ADJUST, policy)
      else:
        policies = self.cfg.getGlobalParam(C.ENT_SCORING_ADDITIONAL_POLICIES)
        policies[policy_name] = policy
        self.cfg.setGlobalParam(C.ENT_SCORING_ADDITIONAL_POLICIES, policies)
      errors = validatorlib.VALID_OK

    return admin_handler.formatValidationErrors(errors)
    def setvar(self, userName, varName, varVal):
        user = ent_collection.EntUserParam(userName, self.cfg.globalParams)
        # if user has no params yet, create them
        if not user.Exists() and not user.Create():
            logging.error("Failed to create userparam %s" % userName)
            user.Delete()
            return admin_handler.formatValidationErrors(
                [validatorlib.ValidationError("Invalid User")])

        val = {}
        config_utils.SafeExec(string.strip(varVal), val)
        if not val.has_key(varName): return 1
        value = val[varName]

        try:
            errors = user.set_var(varName, value, validate=1)
        except KeyError:
            return 1

        return admin_handler.formatValidationErrors(errors)
  def setvar(self, userName, varName, varVal):
    user = ent_collection.EntUserParam(userName,
                                       self.cfg.globalParams)
    # if user has no params yet, create them
    if not user.Exists() and not user.Create():
      logging.error("Failed to create userparam %s" % userName)
      user.Delete()
      return admin_handler.formatValidationErrors([
        validatorlib.ValidationError("Invalid User")])

    val = {}
    config_utils.SafeExec(string.strip(varVal), val)
    if not val.has_key(varName): return 1
    value = val[varName]

    try:
      errors = user.set_var(varName, value, validate = 1)
    except KeyError:
      return 1

    return admin_handler.formatValidationErrors(errors)
  def setfile(self, name, varName, varBody):
    coll_obj = self.construct_collection_object(name)

    # There's no infrastructure for returning warnings, so we'll just
    # silently convert any non-UTF8 characters.
    varBody = entconfig.RepairUTF8(varBody)

    try:
      errors = coll_obj.set_file_var_content(varName, varBody, validate = 1)
    except KeyError:
      return 1

    return admin_handler.formatValidationErrors(errors)
    def setvar(self, name, varName, varVal):
        val = {}
        config_utils.SafeExec(string.strip(varVal), val)
        if not val.has_key(varName): return 1
        value = val[varName]

        coll_obj = self.construct_collection_object(name)

        try:
            errors = coll_obj.set_var(varName, value, validate=1)
        except KeyError:
            return 1

        return admin_handler.formatValidationErrors(errors)
  def setvar(self, name, varName, varVal):
    val = {}
    config_utils.SafeExec(string.strip(varVal), val)
    if not val.has_key(varName): return 1
    value = val[varName]

    coll_obj = self.construct_collection_object(name)

    try:
      errors = coll_obj.set_var(varName, value, validate = 1)
    except KeyError:
      return 1

    return admin_handler.formatValidationErrors(errors)
    def setfile(self, name, varName, varBody):
        coll_obj = self.construct_collection_object(name)

        # There's no infrastructure for returning warnings, so we'll just
        # silently convert any non-UTF8 characters.
        varBody = entconfig.RepairUTF8(varBody)

        try:
            errors = coll_obj.set_file_var_content(varName,
                                                   varBody,
                                                   validate=1)
        except KeyError:
            return 1

        return admin_handler.formatValidationErrors(errors)
 def validate(self, paramName):
   """ Validates a parameter in the local context """
   errors = self.cfg.globalParams.ValidateVar(paramName)
   return admin_handler.formatValidationErrors(errors)
 def formatError(self, code, msg):
   """Format a single error for return."""
   return admin_handler.formatValidationErrors([
     validatorlib.ValidationError(msg, code)
     ])
 def validate_var(self, name, varName):
     coll_obj = self.construct_collection_object(name)
     errors = coll_obj.validate_var(varName)
     return admin_handler.formatValidationErrors(errors)
 def canexport(self, name):
   errors = self.cfg.globalParams.ValidateVar(('ENT_COLLECTIONS', name))
   if errors != validatorlib.VALID_OK:
     return admin_handler.formatValidationErrors(errors)
   return "VALID\n0"
 def validate_var(self, name, varName):
   coll_obj = self.construct_collection_object(name)
   errors = coll_obj.validate_var(varName)
   return admin_handler.formatValidationErrors(errors)
Exemple #16
0
 def formatError(self, code, msg):
     """Format a single error for return."""
     return admin_handler.formatValidationErrors(
         [validatorlib.ValidationError(msg, code)])
Exemple #17
0
 def setfile(self, paramName, value):
     """ Sets the content of a file parameter """
     errors = self.cfg.globalParams.set_file_var_content(
         paramName, value, true)
     return admin_handler.formatValidationErrors(errors)
 def setfile(self, paramName, value):
   """ Sets the content of a file parameter """
   errors = self.cfg.globalParams.set_file_var_content(
     paramName, value, true)
   return admin_handler.formatValidationErrors(errors)