예제 #1
0
파일: account.py 프로젝트: plazar/TOASTER
def is_curator(user_id, pulsar_id, existdb=None):
    """Return whether user has curator privileges for the given
        pulsar.

        Inputs:
            user_id: The ID of the user to check privileges for.
            pulsar_id: The ID of the pulsar in question.
            existdb: A (optional) existing database connection object.
                (Default: Establish a db connection)

        Output:
            curator: True if the user has curator privileges. 
                False otherwise.
    """
    # Check if user_id and pulsar_id are valid
    # Exceptions will be raise if no matches are found
    cache.get_userinfo(user_id)
    cache.get_pulsarname(pulsar_id)

    # Connect to the DB if necessary
    db = existdb or database.Database()
    db.connect()
    select = db.select([db.curators.c.user_id], \
                from_obj=[db.curators.\
                    outerjoin(db.users, \
                        onclause=db.curators.c.user_id == \
                                    db.users.c.user_id)]).\
                where((db.curators.c.pulsar_id == pulsar_id) & \
                        db.curators.c.user_id.in_((user_id,None)) & \
                        db.users.c.active)
    result = db.execute(select)
    rows = result.fetchall()
    result.close()

    curator = bool(rows)
    return curator
예제 #2
0
def write_timfile(toas, timfile, sortkeys=('freq', 'mjd'), flags=(),
                  outname="-", formatter=formatters.tempo2_formatter):
    """Write TOAs to a timfile.
        
        Inputs:
            toas: A list of TOAs.
            timfile: Information about the timfile from the DB.
            flags: A single string containing flags to add to each TOA.
            sortkeys: A list of keys to sort TOAs by.
            outname: The output file's name. (Default: stdout)
            formatter: A formatter function.

        Outputs:
            None
    """
    if outname != '-' and os.path.exists(outname):
        raise errors.FileError("The output timfile sepcified (%s) "
                               "already exists. Doing nothing..." % outname)
    if not timfile['comments']:
        raise errors.BadInputError("Timfile (ID: %d) has no comment!" %
                                   timfile['timfile_id'])

    # Sort TOAs
    utils.sort_by_keys(toas, sortkeys)
    if outname is '-':
        tim = sys.stdout
    else:
        tim = open(outname, 'w')

    wrapper = textwrap.TextWrapper(initial_indent="# ",
                                   subsequent_indent="# ")
    tim.write(wrapper.fill(timfile['comments'])+'\n')
    userinfo = cache.get_userinfo(timfile['user_id'])
    tim.write("# Created by: %s (%s)\n" %
              (userinfo['real_name'], userinfo['email_address']))
    tim.write("#         at: %s\n" % timfile['add_time'])
    tim.write("# Timfile ID: %d\n" % timfile['timfile_id'])
    tim.write("# (Automatically generated by TOASTER)\n")

    lines = formatter(toas, flags)
    tim.write("\n".join(lines)+"\n")
    if outname != '-':
        tim.close()
        notify.print_info("Successfully wrote %d TOAs to timfile (%s)" %
                          (len(toas), outname), 1)