This repository has been archived by the owner on Oct 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
example.py
executable file
·180 lines (143 loc) · 5.04 KB
/
example.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
import click
from clickclick import AliasedGroup, Action, choice, FloatRange, OutputFormat, __version__, get_now
from clickclick import ok, warning, error, fatal_error, action, info
from clickclick.console import print_table
import time
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
STYLES = {
'FINE': {'fg': 'green'},
'ERROR': {'fg': 'red'},
'WARNING': {'fg': 'yellow', 'bold': True},
}
TITLES = {
'state': 'Status',
'creation_time': 'Creation Date',
'id': 'Identifier',
'desc': 'Description',
'name': 'Name',
}
MAX_COLUMN_WIDTHS = {
'desc': 50,
'name': 20,
}
output_option = click.option('-o', '--output', type=click.Choice(['text', 'json', 'tsv', 'yaml']), default='text',
help='Use alternative output format')
json_output_option = click.option('-o', '--output', type=click.Choice(['json', 'yaml']), default='json',
help='Use alternative output format')
watch_option = click.option('-w', '--watch', type=click.IntRange(1, 300), metavar='SECS',
help='Auto update the screen every X seconds')
def watching(watch: int):
if watch:
click.clear()
yield 0
if watch:
while True:
time.sleep(watch)
click.clear()
yield 0
def print_version(ctx, param, value):
if not value or ctx.resilient_parsing:
return
click.echo('ClickClick Example {}'.format(__version__))
ctx.exit()
@click.group(cls=AliasedGroup, context_settings=CONTEXT_SETTINGS)
@click.option('-V', '--version', is_flag=True, callback=print_version, expose_value=False, is_eager=True,
help='Print the current version number and exit.')
def cli():
pass
@cli.command('list')
@output_option
@watch_option
def list_dummy_states(output, watch):
'''Example for Listings'''
states = ['ERROR', 'FINE', 'WARNING']
i = 0
for _ in watching(watch):
i += 1
rows = []
for y in (1, 2, 3):
id = i * y - i
rows.append({'id': id,
'name': 'Column #{}'.format(id),
'state': states[id % len(states)],
'creation_time': 1444911300,
'desc': 'this is a ve' + 'r' * 50 + 'y long description',
'without_title': 'column without title',
'missing_column': 'Column are not in output'})
with OutputFormat(output):
print_table('id name state creation_time desc without_title'.split(), rows,
styles=STYLES, titles=TITLES, max_column_widths=MAX_COLUMN_WIDTHS)
@cli.command()
@output_option
def output(output):
'''Example for all possible Echo Formats
You see the message only, if the Output TEXT
'''
with OutputFormat(output):
action('This is a ok:')
ok()
action('This is a ok with message:')
ok('all is fine')
action('This is a warning:')
warning('please check this')
with Action('Start with working..') as act:
# save_the_world()
act.progress()
act.progress()
act.progress()
act.progress()
print_table('id name'.split(), [{'id': 1, 'name': 'Test #1'}, {'id': 2, 'name': 'Test #2'}])
info('Only FYI')
action('This is a error:')
error('this is wrong, please fix')
action('This is a fatal error:')
fatal_error('this is a fuckup')
info('I\'am not printed, the process a dead')
@cli.command()
def localtime():
'''Print the localtime'''
print('Localtime: {}'.format(get_now()))
@cli.command('work-in-progress')
def work_in_progress():
'''Work untile working is done'''
with Action('do anything..'):
pass
try:
with Action('create an excption..'):
raise
except:
pass
with Action('Start with working..') as act:
# save_the_world()
act.progress()
act.progress()
act.progress()
act.progress()
with Action('Calc 1 + 1..') as act:
# save_the_world()
act.ok(1+1)
with Action('Oh, I make an error..') as act:
# clear_the_oceans()
act.error('work not complete done')
with Action('Oh, I make a warning..') as act:
# clear_the_air()
act.warning('work is complicated')
try:
with Action('Start an exception..') as act:
function_not_found() # noqa
act.progress()
except:
pass
with Action('Make a final error..') as act:
act.fatal_error('this is the end..')
with Action('This should not run..'):
pass
@cli.command()
@click.argument('percentage', type=FloatRange(0, 100, clamp=True), required=True)
def work_done(percentage):
'''Work done in ?? %'''
state = choice('Please select the state of your work', ['Done', 'In Progress', 'unknown', 'lost'], default='lost')
print('Your work is {}% {}'.format(percentage, state))
if __name__ == "__main__":
cli()