示例#1
0
    option_parser.add_option("--competition-year", default=date.today().year,
                             type="int")
    option_parser.add_option("-n", "--nicknames")
    option_parser.add_option("-y", "--year", help="Graduation year", type="int")
    options, arguments = option_parser.parse_args(arguments[1:])
    index = count(0)
    try:
        connect(options.server)
        given_name = arguments[index.next()]
        surname = arguments[index.next()]
        school_id = Schools.get(arguments[index.next()])
        gender = parse_gender(arguments[index.next()])
    except ConnectionError, error:
        option_parser.error(error)
    except IndexError:
        option_parser.error("Exactly 4 positional arguments must be provided.")
    except InvalidGenderError, error:
        option_parser.error(error)
    except SQLObjectNotFound, error:
        option_parser.error(error)
    return options, (given_name, surname, school_id, gender)

@main_function(parse_arguments)
def main(options, (given_name, surname, school, gender)):
    """Add a new runner to the database from the command-line arguments."""
    Runners.create(given_name, surname, school, gender, options.year,
                   options.nicknames, options.competition_year)

if __name__ == "__main__":
    main()
示例#2
0
    """Read a YAML description of the runners from stdin, and save it to the
    database."""
    try:
        runners = load(stdin.read())
        verify(runners, options)
    except KeyboardInterrupt:
        return 1
    except ScannerError, error:
        print >> stderr, error
        return 1
    except (SQLObjectNotFound, VerificationError), error:
        print >> stderr, error
        return 1
    for runner in runners:
        Runners.create(runner["given_name"], runner["surname"],
                       runner["school"], runner["gender"], runner["year"],
                       runner["nicknames"], runner["competition_year"])

def verify(runners, options):
    """Check that all runners have the fields required by Runners.create().
    Raises a VerificationError if any problems are encountered."""
    for runner in runners:
        #Leaving out the names is not allowed
        for field in ("given_name", "surname"):
            if field not in runner:
                raise VerificationError("Required field \"%s\" left blank." %
                                        field)
        #School must be defined
        if "school" not in runner:
            if options.school is None:
                raise VerificationError("Required field \"school\" left blank.")