Last active
August 29, 2015 13:58
-
-
Save thedod/10307356 to your computer and use it in GitHub Desktop.
Example of how a twister/swizzler/etc. installer could look with npyscreen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import npyscreen | |
class TestInstaller(npyscreen.NPSApp): | |
def main(self): | |
# These lines create the form and populate it with widgets. | |
# A fairly complex screen in only 8 or so lines of code - a line for each control. | |
f = npyscreen.Form(name="Example installer: edit an RPC url",) | |
fields = { | |
'username':f.add(npyscreen.TitleText, name="Username:"), | |
'password':f.add(npyscreen.TitlePassword, name="Password:"), | |
'protocol':f.add(npyscreen.TitleSelectOne, name="RPC protocol", | |
values = ["http","https"], max_height=2, value=[0], scroll_exit=True), | |
'host':f.add(npyscreen.TitleText, value='127.0.0.1', name="RPC host:"), | |
'port':f.add(npyscreen.TitleText, value='28332', name="RPC port:"), | |
} | |
messages = ['This is an example installer.','It constructs an RPC url.','Enjoy.'] | |
while messages: | |
npyscreen.notify_confirm('\n'.join(messages),'Example installer') | |
messages = [] | |
f.edit() | |
if len(fields['username'].value.strip())<3: #LOL, I just wrote <3 | |
messages.append('Username missing or too short') # just an example bs validation method | |
if len(fields['password'].value.strip())<8: | |
messages.append('Password missing or too short') # just an example bs validation method | |
result = {} | |
for k in fields: | |
v = fields[k].value | |
result[k] = type(v)==type([]) and fields[k].values[v[0]].strip() or v.strip() | |
npyscreen.notify_confirm( | |
"{protocol}://{username}:{password}@{host}:{port}".format(**result), | |
"This is the RPC url (just sayin')") | |
if __name__ == "__main__": | |
App = TestInstaller() | |
App.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment