forked from mueslo/pythonBits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplating.py
More file actions
94 lines (70 loc) · 2.34 KB
/
Copy pathtemplating.py
File metadata and controls
94 lines (70 loc) · 2.34 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import * # noqa: F401, F403
from functools import partial
from math import floor
from . import _release, _github
# tag like [name=value]
def tag(tag_name):
def func(value=None):
if value:
return "[" + tag_name + "=" + str(value) + "]"
return "[" + tag_name + "]"
return func
# tag like [name=tv]ev[/name]
def tag_enc(tag_name):
return lambda ev, tv=None: (tag(tag_name)(tv) + str(ev) +
tag('/' + tag_name)())
img = tag('img')
b = tag_enc('b')
link = tag_enc('url')
size = tag_enc('size')
quote = tag_enc('quote')
spoiler = tag_enc('spoiler')
mi = tag_enc('mediainfo')
s1 = partial(size, tv=1)
s2 = partial(size, tv=2) # default
s3 = partial(size, tv=3)
s4 = partial(size, tv=4)
s7 = partial(size, tv=7)
align = tag_enc('align')
center = partial(align, tv='center')
color = tag_enc('color')
_list = tag_enc('list')
def list(x, style=None):
v = "".join("[*]"+x for x in x)
return _list(v, style)
# formats color tuple (255, 235, 85) to hexadecimal string "#ffeb55"
def fmt_col(c):
return "#" + "{:02x}{:02x}{:02x}".format(*c)
def h(x):
s = ""
for c in x:
if c.isupper():
s += s3(c)
else:
s += c.upper()
return b(s)
def section(name, content):
return center(h(name)) + quote(content)
release = align(link(color(s1("Generated by " + _release), '#999'),
_github), 'right')
def format_rating(rating, max, limit=10, s=None, fill=None, empty=None):
if rating is None:
return "No rating"
s = s or '★'
fill = fill or [0xff, 0xff, 0x00]
empty = empty or [0xa0, 0xa0, 0xa0]
limit = min(max, limit)
num_stars = rating * limit / max
black_stars = int(floor(num_stars))
partial_star = num_stars - black_stars
white_stars = limit - black_stars - 1
pf = [comp * partial_star for comp in fill]
pe = [comp * (1 - partial_star) for comp in empty]
partial_color = fmt_col(tuple(map(lambda x, y: int(x+y), pf, pe)))
stars = (color(s * black_stars, fmt_col(fill)) +
color(s, partial_color) +
color(s * white_stars, fmt_col(empty)))
return str(rating) + '/' + str(max) + ' ' + stars