-
Notifications
You must be signed in to change notification settings - Fork 1
/
norris.py
executable file
·57 lines (51 loc) · 1.78 KB
/
norris.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env python
"""
This is a silly program that is used to print out Chuck Norris jokes. If
Chuck Norris jokes aren't your thing you could make a file with motivational
sayings or something and update the configuration and this should still work.
See the example_config.txt for details.
"""
import urllib2
from random import choice
import ConfigParser
import os
def load_config(defaults, location=None, verbose=False):
"""
Load user configuration from a file.
:param defaults: a dict with the program defaults
:param location: a file location to load the config from. This will defaul to ~/.norris
:param verbose: set this to true if you want to see errors around loading configuration
"""
user_config = {}
location = location or os.path.expanduser('~/.norris')
try:
config = ConfigParser.ConfigParser()
config.read(location)
user_config = dict(config.items('config'))
except Exception, e:
if verbose:
print 'Error when opening user config at {0}: {1}'.format(location, e)
return dict(defaults, **user_config)
def get_line(url, timeout=.5):
"""
Hit the passed in url and return a random line of the output.
"""
return choice(urllib2.urlopen(url, timeout=timeout).readlines()).strip()
def main():
"""
Run the program printing out the text that is found.
"""
defaults = {
'url' : 'http://charliek.github.com/norris/norris.txt',
'timeout' : .5,
'verbose' : True
}
config = load_config(defaults)
try:
line = get_line(config['url'], float(config['timeout']))
print "{0}".format(line)
except Exception, e:
if bool(config['verbose']):
print 'An error has occurred : {0}'.format(e)
if __name__ == "__main__":
main()