def clear_curators(pulsar_id, existdb=None): """Clear all curators for a particular pulsar. Inputs: pulsar_id: The ID of the pulsar to edit curators for. existdb: A (optional) existing database connection object. (Default: Establish a db connection) Outputs: None """ utils.print_info("Removing all curators for %s" % \ utils.get_pulsarname(psr_id), 2) # Connect to the database db = existdb or database.Database() db.connect() # Remove curators delete = db.curators.delete().\ where(db.curators.c.pulsar_id==pulsar_id) result = db.execute(delete) result.close() if not existdb: db.close()
def check_new_name(pulsar_id, newname): """Check if the new name is OK to use for the given pulsar. The new name is invalid if it is already in use with a different pulsar_id entry. An error is raised if the proposed name is invalid. Inputs: pulsar_id: The DB ID number of the pulsar to rename. newname: The proposed new name. Ouputs: None """ pulsarid_cache = utils.get_pulsarid_cache() if (newname in pulsarid_cache.keys()) and \ (pulsarid_cache[newname] != pulsar_id): used_id = pulsarid_cache[newname] raise errors.BadInputError("The proposed pulsar name, '%s', " \ "is already in use with a different " \ "pulsar (%s, ID: %d). Pulsar names and " \ "aliases must refer to a single " \ "pulsar only." % \ (newname, utils.get_pulsarname(used_id), \ used_id))
def update_curators(pulsar_id, to_add_ids=[], to_rm_ids=[], existdb=None): """Update the list of curators for the given pulsar. Note: if a user is specified to be added and removed, the removal will take precedence. Inputs: pulsar_id: The ID of the pulsar to edit curators for. to_add_ids: List of user IDs to add as curators. to_rm_ids: List of user IDs to remove as curators. existdb: A (optional) existing database connection object. (Default: Establish a db connection) Outputs: None """ to_add_ids = set(to_add_ids) to_rm_ids = set(to_rm_ids) to_add_ids.difference_update(to_rm_ids) # Remove user_ids that will # lose curator privileges if config.cfg.verbosity >= 2: msg = "Updating curator privileges for %s" % \ utils.get_pulsarname(pulsar_id) for uid in to_add_ids: if uid is None: msg += "\n + Wildcard" else: msg += "\n + %s" % utils.get_userinfo(uid)['real_name'] for uid in to_rm_ids: if uid is None: msg += "\n - Wildcard" else: msg += "\n - %s" % utils.get_userinfo(uid)['real_name'] utils.print_info(msg, 2) # Connect to the database db = existdb or database.Database() db.connect() trans = db.begin() try: # Fetch list of curators select = db.select([db.curators.c.user_id]).\ where(db.curators.c.pulsar_id==pulsar_id) result = db.execute(select) rows = result.fetchall() result.close() # Don't re-add existing curators curators = [row['user_id'] for row in rows] to_add_ids.difference_update(curators) # Add curators ins = db.curators.insert() values=list() for add_id in to_add_ids: values.append({'pulsar_id':pulsar_id, \ 'user_id':add_id}) result = db.execute(ins, values) result.close() # Only add curators that are present to_rm_ids.intersection_update(curators) # Remove curators delete = db.curators.delete().\ where( (db.curators.c.pulsar_id==pulsar_id) & \ (db.curators.c.user_id.in_(to_rm_ids)) ) result = db.execute(delete) result.close() except: trans.rollback() raise else: trans.commit() finally: if not existdb: db.close()
def verify_replacement(obsolete_id, replacement, db): """Verify that the replacement file is a suitable replacement for the obsolete file. The following conditions must be satisfied: - Both observations refer to the same pulsar (i.e. same pulsar_id) - Both observations come from the same observing system (i.e. same obssystem_id) - The observations overlap (this is checked by comparing the start/end MJDs) Inputs: obsolete_id: The rawfile_id value of the file being replaced. replacement: The name of the replacement file. db: An connected database object. Outputs: None """ # Get info about the replacement file from its header replaceparams = utils.prep_file(replacement) # Get info about the obsolete file from database select = db.select([db.rawfiles.c.pulsar_id, \ db.rawfiles.c.obssystem_id, \ db.rawfiles.c.mjd, \ db.rawfiles.c.length]).\ where(db.rawfiles.c.rawfile_id==obsolete_id) result = db.execute(select) rows = result.fetchall() result.close() if len(rows) > 1: raise errors.InconsistentDatabaseError("Multiple matches (%s) " \ "for rawfile_id (%d)! This should not happen..." % \ (len(rows), obsolete_id)) elif len(rows) < 1: raise errors.BadInputError("The rawfile_id provided (%d) does not " \ "exist!" % obsolete_id) else: obsoleteparams = rows[0] # Now check that the obsolete file and its replacement are compatible # Check the replacement is from the same obssystem if obsoleteparams['obssystem_id'] != replaceparams['obssystem_id']: raise errors.FileError("The observing system of the replacement " \ "(ID: %d) doesn't match the observing system of " \ "the file it's replacing (ID: %d)." % \ (obsoleteparams['obssystem_id'], \ replaceparams['obssystem_id'])) # Check the replacement is data on the same pulsar if obsoleteparams['pulsar_id'] != replaceparams['pulsar_id']: raise errors.FileError("The pulsar name in the replacement file " \ "(%s) doesn't match the pulsar name in the file it's " "replacing (%s)." % \ (utils.get_pulsarname(obsoleteparams['pulsar_id']), \ utils.get_pulsarname(replaceparams['pulsar_id']))) # Check the replacement overlaps with the obsolete file omjdstart = obsoleteparams['mjd'] omjdend = omjdstart + obsoleteparams['length']/86400.0 rmjdstart = replaceparams['mjd'] rmjdend = rmjdstart + replaceparams['length']/86400.0 if (omjdstart >= rmjdend or omjdend <= rmjdstart): raise errors.FileError("The replacement file (MJD: %f - %f) " \ "doesn't overlap with the file it is replacing " \ "(MJD: %f - %f)." % \ (rmjdstart, rmjdend, omjdstart, omjdend))