-
Notifications
You must be signed in to change notification settings - Fork 0
/
strmanip.py
159 lines (132 loc) · 3.17 KB
/
strmanip.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
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.core.clipboard import CutBuffer, Clipboard
from kivy.config import Config
from ConfigParser import NoOptionError
try:
mouse = Config.get('input', 'mouse')
except NoOptionError:
pass
else:
Config.set('input', 'mouse', mouse + ',disable_multitouch')
root = Builder.load_string('''
<SLabel@Label>:
size_hint_y: None
height: self.texture_size[1] + dp(12)
<Buttons@BoxLayout>:
size_hint_y: None
height: dp(32)
BoxLayout:
orientation: 'vertical'
BoxLayout:
spacing: dp(4)
padding: dp(4)
BoxLayout:
orientation: 'vertical'
SLabel:
id: reference
text: 'Source'
TextInput:
id: source
multiline: True
on_text: app.apply_manip()
Buttons:
Button:
text: 'Clear'
on_release: app.clear_source()
Button:
text: 'Paste'
on_release: app.paste_clipboard()
Button:
id: cutbuffer
text: 'Buffer'
on_release: app.paste_cutbuffer()
BoxLayout:
orientation: 'vertical'
SLabel:
text: 'Output'
RelativeLayout:
TextInput:
id: output
readonly: True
multiline: True
valid: False
background_color: (1, 1, 1, 1) if self.valid else (1, 1, 1, 0.6)
SLabel:
id: error
pos_hint: {'y': 0}
text_size: self.width - dp(10), None
color: 1, 0.2, 0.3, 1
canvas.before:
Color:
rgba: 1, 1, 1, (0.8 if self.text else 0)
Rectangle:
pos: self.pos
size: self.size
Buttons:
Button:
text: 'Copy'
on_release: app.copy_output()
Button:
text: 'Copy and Exit'
on_release: app.copy_and_exit()
TextInput:
id: manip
size_hint_y: None
height: reference.height * 3.0
text: 's'
on_text: app.apply_manip()
valid: False
background_color: (1, 1, 1, 1) if self.valid else (1, 0.8, 0.8, 1)
''')
class StrManipApp(App):
def build(self):
return root
def on_start(self):
text = ''
if CutBuffer:
text = CutBuffer.get_cutbuffer()
else:
cbbtn = root.ids.cutbuffer
cbbtn.parent.remove_widget(cbbtn)
if not text:
text = Clipboard.copy()
root.ids.source.text = text or ''
root.ids.manip.focus = True
def apply_manip(self):
source = root.ids.source.text
manip = root.ids.manip.text
try:
output = eval(manip, {}, {'s': source})
if not isinstance(output, basestring):
try:
output = '\n'.join(output)
except Exception:
raise Exception('output is not a string or iterable')
except Exception as e:
root.ids.manip.valid = False
root.ids.output.valid = False
root.ids.error.text = str(e)
else:
root.ids.manip.valid = True
root.ids.output.text = ''
root.ids.output.text = output
root.ids.output.valid = True
root.ids.error.text = ''
def clear_source(self):
root.ids.source.text = ''
def paste_clipboard(self):
root.ids.source.text = Clipboard.paste()
def paste_cutbuffer(self):
root.ids.source.text = CutBuffer.get_cutbuffer()
def copy_output(self):
Clipboard.copy(root.ids.output.text)
if CutBuffer:
CutBuffer.set_cutbuffer(root.ids.output.text)
def copy_and_exit(self):
self.copy_output()
self.stop()
if __name__ == '__main__':
StrManipApp().run()