-
Notifications
You must be signed in to change notification settings - Fork 0
/
Examples.py
91 lines (75 loc) · 3.68 KB
/
Examples.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
from bs.operations.base import OperationPlugin # import the base class to build your plugin
import os
import random
# some shared information about these exemples
meta = {'version': "1.0.1",
'author': "Yohan Jarosz",
'contact': "[email protected]"}
__install_requires__ = [] # beta
class Simple(OperationPlugin):
info = {
'title': 'WriteFile', # The title of your operation
'description': """As an exemple, this plugin write the input you give in a text file.
You can also put <a href='https://github.com/bbcf/bs.operations/blob/master/Examples.py'>
links</a> to some documentation.
""", # Describe the operation's goal
'path': ['Examples', 'Automatics forms', 'Write output in a file'], # Under which category the operation will be set
# First in the list mean higher category
'in': [{'id': 'input', 'type': 'text', 'required': True}], # All input parameters
'out': [{'id': 'output', 'type': 'file'}], # All output parameters
'meta': meta, # Meta information (authors, version, ...)
}
def __call__(self, *args, **kw):
text = kw.get('input', '') # get the parameter back
path = self.temporary_path() # get a temporary path
with open(path, 'w') as f: # open a file & write the input
f.write(text)
self.new_file(path, 'output') # add a file to the result
return 1
class ReadFile(OperationPlugin):
info = {
'title': 'ReadFile',
'description': "Read the file to find the number of characters and write the result in an output file. "
"If randomize is checked, the result is between 0 and the file's number of characters ",
'path': ['Examples', 'Automatics forms', 'Read a file'],
'in': [{'id': 'fname', 'label': 'File Name', 'type': 'text'},
{'id': 'input', 'label': 'File input', 'type': 'file', 'required': True},
{'id': 'randomize', 'label': 'Randomize', 'type': 'boolean'}
],
'out': [{'id': 'output', 'type': 'file'}],
'meta': meta,
}
def __call__(self, *args, **kw):
# get parameters
fin = kw.get('input')
fname = kw.get('fname', '')
if fname == '':
fname = os.path.split(fin)[1]
randomize = kw.get('randomize', False)
# read the input file
nchar = 0
with open(fin, 'r') as rin:
for line in rin:
nchar += len(line)
# randomize if checked
if randomize:
nchar = random.randint(0, nchar - 1)
# take a temporary path where to write the file
fout = self.temporary_path(fname)
# write the result
with open(fout, 'w') as wout:
wout.write(str(nchar))
# add the file to the result
self.new_file(fout, 'output')
class Error(OperationPlugin):
info = {
'title': 'Error plugin',
'description': 'This plugin ends up with an error.',
'path': ['Examples', 'Automatics forms', 'Error'],
'in': [{'id': 'a', 'label': 'parameter "a"', 'required': False, 'type': 'text'}],
'out': [{'id': 'output', 'type': 'text'}],
'meta': meta,
}
def __call__(self, *args, **kw):
1 / 0
return 1