コード例 #1
0
ファイル: __init__.py プロジェクト: universeroc/fortune
def main():
    """
    Main program.
    """
    usage = 'Usage: %s [OPTIONS] fortune_file' % os.path.basename(sys.argv[0])
    arg_parser = CommandLineParser(usage=usage)
    arg_parser.add_option('-q', '--quiet', action='store_true', dest='quiet',
                          help="When updating the index file, don't emit " \
                               "messages.")
    arg_parser.add_option('-u', '--update', action='store_true', dest='update',
                          help='Update the index file, instead of printing a '
                               'fortune.')
    arg_parser.add_option('-V', '--version', action='store_true',
                          dest='show_version', help='Show version and exit.')

    arg_parser.epilogue = 'If <fortune_file> is omitted, fortune looks at the ' \
                          'FORTUNE_FILE environment variable for the path.'

    options, args = arg_parser.parse_args(sys.argv)
    if len(args) == 2:
        fortune_file = args[1]

    else:
        try:
            fortune_file = os.environ['FORTUNE_FILE']
        except KeyError:
            arg_parser.show_usage('Missing fortune file.')

    try:
        if options.show_version:
            print 'fortune, version %s' % __version__
        elif options.update:
            make_fortune_data_file(fortune_file)
        else:
            sys.stdout.write(get_random_fortune(fortune_file))
    except ValueError, msg:
        print >> sys.stderr, msg
        sys.exit(1)
コード例 #2
0
ファイル: read_fortunes.py プロジェクト: stargriv/fortune3
_PICKLE_PROTOCOL = -1

usage = 'Usage: %s [OPTIONS] fortune_file' % os.path.basename(sys.argv[0])

arg_parser = CommandLineParser(usage=usage)

options, args = arg_parser.parse_args(sys.argv)

if len(args) == 2:
    fortune_file = args[1]

else:
    try:
        fortune_file = os.environ['FORTUNE_FILE']
    except KeyError:
        arg_parser.show_usage('Missing fortune file.')

# fortune_file = open(fortune_file, 'rt', newline=None)
def random_int(start, end):
    try:
        # Use SystemRandom, if it's available, since it's likely to have
        # more entropy.
        r = random.SystemRandom()
    except:
        r = random

    return r.randint(start, end)

def _read_fortunes(fortune_file):
    with open(fortune_file, 'rt', newline=None) as f:
        line = f.readline()