forked from mccutchen/strftime.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·63 lines (50 loc) · 2.04 KB
/
build.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
57
58
59
60
61
62
63
#!/usr/bin/env python3
import datetime
import sys
import urllib.request, urllib.error, urllib.parse
from bs4 import BeautifulSoup
import pystache
def main():
url = 'http://docs.python.org/2/library/datetime.html'
body = urllib.request.urlopen(url).read()
soup = BeautifulSoup(body, features="html.parser")
table = soup.find(id='strftime-and-strptime-behavior').find('table')
example_date = datetime.datetime(2013, 9, 30, 7, 6, 5)
directives = []
for row in table.select('tbody > tr'):
tds = row.find_all('td')
directive = tds[0].find('span').string
# we use getText() here because some of the meanings have extra markup
meaning = tds[1].getText().replace('\n', ' ')
example = example_date.strftime(directive)
directives.append({
'directive': directive,
'meaning': meaning,
'example': example
})
# also add the non zero padded version for some of them
# http://stackoverflow.com/questions/28894172/why-does-d-or-e-remove-the-leading-space-or-zero
exclude = ['%f', '%y']
if 'zero-padded' in meaning and directive not in exclude:
non_zero_padding_decimal = directive.replace("%", "%-")
non_zero_padding_example = example_date.strftime(
non_zero_padding_decimal
)
non_zero_padding_meaning = (
meaning.replace("zero-padded", "") + " (Platform specific)")
directives.append({
'directive': non_zero_padding_decimal,
'meaning': non_zero_padding_meaning,
'example': non_zero_padding_example
})
template = open('templates/index.html.mustache').read()
context = {
'example_date': str(example_date),
'example_date_repr': repr(example_date),
'directives': directives,
'timestamp': datetime.datetime.utcnow().strftime('%Y-%m-%d'),
}
print(pystache.render(template, context))
return 0
if __name__ == '__main__':
sys.exit(main())