Advertisement
Guest User

terminal_ui.py

a guest
Aug 27th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. from ui import *
  2. import argparse
  3.  
  4.  
  5. class Terminal_ui(Ui):
  6.  
  7. def __init__(self):
  8.  
  9. """
  10. This constructor read user input from the terminal.
  11.  
  12. """
  13. print"%*100"
  14. arg= " message -ch fb,twitter"
  15. parser = argparse.ArgumentParser(usage='%(prog)s <your message> -ch <channel_list>',description='A way for Broadcast your messages')
  16. self.message=None
  17. self.channels=[]
  18. parser.add_argument('message',type=str, help='Message to be sended')
  19. parser.add_argument( '-ch', '--channels',type= str,nargs='+', required=True,help='Channel list to send the message',)
  20. args= parser.parse_args(arg.split())
  21. self.message =args.message
  22. self.channels = args.channels
  23.  
  24. def empty_message (self,Message):
  25.  
  26. """
  27. This Function is used to check message is empty or not. Return 'True' when empty message comes
  28. """
  29. if not Message.strip():
  30. return True
  31. else:
  32. return False
  33.  
  34.  
  35. def empty_channel(self,Channel_List):
  36. """
  37. This Function check channel is empty or not
  38. """
  39. if not Channel_List[0].strip():
  40. return True
  41. else:
  42. return False
  43.  
  44.  
  45. def get_mesg_and_chanl(self):
  46.  
  47. """
  48. This Function separate message and channel list from user input. Return a tuple of message string and list of channels
  49. For example: heloo -ch fb,gmail ----->>>>>> ("heloo",['fb,gmail'])
  50. """
  51.  
  52. if len(self.channels) == 1: # channel_list contains channel names inpute
  53. channel_list =self.channels[0].split(',') # removes ',' and split into list
  54. else:
  55. channel_list=self.channels
  56.  
  57. for i,item in enumerate(channel_list): # removes space in channels list
  58. channel_list[i]=channel_list[i]
  59.  
  60. channel_list = list(set(channel_list)) # removes duplicate channel names
  61.  
  62.  
  63. message= self.message # message contains message to be sended
  64.  
  65.  
  66. if self.empty_message(message): # check the message is empty or not
  67. self.display_error( "\t\tEnter valid message\t\t")
  68.  
  69. elif self.empty_channel(channel_list):
  70. self.display_error("\t\tEnter any channel name\t\t")
  71.  
  72. else:
  73. return (message,channel_list)
  74.  
  75. def display_error(self,error):
  76.  
  77. """
  78. This Function displays the errors
  79. """
  80.  
  81. print"\n ----------ERROR--------- "
  82.  
  83. print"""\n\n\n%*100\n\n\n\n"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement