Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 28 ■ PROJECT 9: FILE SHARING II—NOW WITH GUI! 523 class ListableNode(Node): """ An extended version of Node, which can list the files in its file directory. """ def list(self): return listdir(self.dirname) class Client(wx.App): """ The main client class, which takes care of setting up the GUI and starts a Node for serving files. """ def __init__(self, url, dirname, urlfile): """ Creates a random secret, instantiates a ListableNode with that secret, starts a Thread with the ListableNode's _start method (making sure the Thread is a daemon so it will quit when the application quits), reads all the URLs from the URL file and introduces the Node to them. Finally, sets up the GUI. """ # Give the server a head start: sleep(HEAD_START) self.server = ServerProxy(url) super(Client, self).__init__() self.secret = randomString(SECRET_LENGTH) n = ListableNode(url, dirname, self.secret) t = Thread(target=n._start) t.setDaemon(1) t.start() for line in open(urlfile): line = line.strip() self.server.hello(line) def updateList(self): """ Updates the list box with the names of the files available from the server Node. """ self.files.Set(self.server.list())
524 CHAPTER 28 ■ PROJECT 9: FILE SHARING II—NOW WITH GUI! def OnInit(self): """ Sets up the GUI. Creates a window, a text field, a button, and a list box, and lays them out. Binds the submit button to self.fetchHandler. """ win = wx.Frame(None, title="File Sharing Client", size=(400, 300)) bkg = wx.Panel(win) self.input = input = wx.TextCtrl(bkg); submit = wx.Button(bkg, label="Fetch", size=(80, 25)) submit.Bind(wx.EVT_BUTTON, self.fetchHandler) hbox = wx.BoxSizer() hbox.Add(input, proportion=1, flag=wx.ALL | wx.EXPAND, border=10) hbox.Add(submit, flag=wx.TOP | wx.BOTTOM | wx.RIGHT, border=10) self.files = files = wx.ListBox(bkg) self.updateList() vbox = wx.BoxSizer(wx.VERTICAL) vbox.Add(hbox, proportion=0, flag=wx.EXPAND) vbox.Add(files, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM, border=10) bkg.SetSizer(vbox) win.Show() return True def fetchHandler(self, event): """ Called when the user clicks the 'Fetch' button. Reads the query from the text field, and calls the fetch method of the server Node. After handling the query, updateList is called. If the query is not handled, an error message is printed. """ query = self.input.GetValue() try: self.server.fetch(query, self.secret) self.updateList()
- Page 504 and 505: CHAPTER 25 ■ ■ ■ Project 6: R
- Page 506 and 507: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 508 and 509: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 510 and 511: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 512: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 515 and 516: 484 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 517 and 518: 486 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 519 and 520: 488 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 521 and 522: 490 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 523 and 524: 492 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 525 and 526: 494 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 527 and 528: 496 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 529 and 530: 498 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 531 and 532: 500 CHAPTER 27 ■ PROJECT 8: FILE
- Page 533 and 534: 502 CHAPTER 27 ■ PROJECT 8: FILE
- Page 535 and 536: 504 CHAPTER 27 ■ PROJECT 8: FILE
- Page 537 and 538: 506 CHAPTER 27 ■ PROJECT 8: FILE
- Page 539 and 540: 508 CHAPTER 27 ■ PROJECT 8: FILE
- Page 541 and 542: 510 CHAPTER 27 ■ PROJECT 8: FILE
- Page 543 and 544: 512 CHAPTER 27 ■ PROJECT 8: FILE
- Page 545 and 546: 514 CHAPTER 27 ■ PROJECT 8: FILE
- Page 547 and 548: 516 CHAPTER 27 ■ PROJECT 8: FILE
- Page 549 and 550: 518 CHAPTER 28 ■ PROJECT 9: FILE
- Page 551 and 552: 520 CHAPTER 28 ■ PROJECT 9: FILE
- Page 553: 522 CHAPTER 28 ■ PROJECT 9: FILE
- Page 558 and 559: CHAPTER 29 ■ ■ ■ Project 10:
- Page 560 and 561: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 562 and 563: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 564 and 565: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 566 and 567: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 568 and 569: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 570 and 571: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 572 and 573: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 574 and 575: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 576 and 577: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 578 and 579: APPENDIX A ■ ■ ■ The Short Ve
- Page 580 and 581: APPENDIX A ■ THE SHORT VERSION 54
- Page 582 and 583: APPENDIX A ■ THE SHORT VERSION 55
- Page 584 and 585: APPENDIX A ■ THE SHORT VERSION 55
- Page 586: APPENDIX A ■ THE SHORT VERSION 55
- Page 589 and 590: 558 APPENDIX B ■ PYTHON REFERENCE
- Page 591 and 592: 560 APPENDIX B ■ PYTHON REFERENCE
- Page 593 and 594: 562 APPENDIX B ■ PYTHON REFERENCE
- Page 595 and 596: 564 APPENDIX B ■ PYTHON REFERENCE
- Page 597 and 598: 566 APPENDIX B ■ PYTHON REFERENCE
- Page 599 and 600: 568 APPENDIX B ■ PYTHON REFERENCE
- Page 601 and 602: 570 APPENDIX B ■ PYTHON REFERENCE
- Page 603 and 604: 572 APPENDIX C ■ ONLINE RESOURCES
CHAPTER 28 ■ PROJECT 9: FILE SHARING II—NOW WITH GUI! 523<br />
class ListableNode(Node):<br />
"""<br />
An extended version of Node, which can list the files<br />
in its file direc<strong>to</strong>ry.<br />
"""<br />
def list(self):<br />
return listdir(self.dirname)<br />
class Client(wx.App):<br />
"""<br />
The main client class, which takes care of setting up the GUI and<br />
starts a Node for serving files.<br />
"""<br />
def __init__(self, url, dirname, urlfile):<br />
"""<br />
Creates a random secret, instantiates a ListableNode with that secret,<br />
starts a Thread with the ListableNode's _start method (making sure the<br />
Thread is a daemon so it will quit when the application quits),<br />
reads all the URLs from the URL file and introduces the Node <strong>to</strong><br />
them. Finally, sets up the GUI.<br />
"""<br />
# Give the server a head start:<br />
sleep(HEAD_START)<br />
self.server = ServerProxy(url)<br />
super(Client, self).__init__()<br />
self.secret = randomString(SECRET_LENGTH)<br />
n = ListableNode(url, dirname, self.secret)<br />
t = Thread(target=n._start)<br />
t.setDaemon(1)<br />
t.start()<br />
for line in open(urlfile):<br />
line = line.strip()<br />
self.server.hello(line)<br />
def updateList(self):<br />
"""<br />
Updates the list box with the names of the files available<br />
from the server Node.<br />
"""<br />
self.files.Set(self.server.list())