forked from hackyourlife/lima-gold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_quote.py
46 lines (41 loc) · 1.07 KB
/
random_quote.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
#!/bin/python
# -*- coding: utf-8 -*-
# vim: set ts=8 sts=8 sw=8 tw=80 noet cc=80 :
import os.path
import json
import random
QUOTES = {}
def init(config):
if not config.has_section("random_quote"):
return None
for option in config.options("random_quote"):
try:
config_path = json.loads(config.get("random_quote",
option))
except:
config_path = os.path.expanduser(
config.get("random_quote", option))
if(not os.path.isfile(config_path)):
print("Invalid json in config: " +
config.get("random_quote", option))
continue
if not isinstance(config_path, list):
config_path = [config_path]
qs = []
for p in config_path:
p = os.path.expanduser(p)
if isinstance(p, str) and os.path.isfile(p):
qs.extend([l.strip() for l in
open(p).readlines()])
else:
print(">> 404 No such file: " + p)
if len(qs) > 0:
QUOTES[option] = qs
def get_lists():
return list(QUOTES.keys())
def random_quote(lst=None):
if lst == None:
lst = random.choice(list(QUOTES.keys()))
elif lst not in QUOTES:
return None
return random.choice(QUOTES[lst])