-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
executable file
·1171 lines (882 loc) · 33.9 KB
/
view.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#Domenic Bianchi
#CIS 2750 Assignment 2
#February 19, 2017
#This program displays an interface to display posts from a signle or mutliple streams
import os
import sys
import tty
import termios
import operator
import math
#An object of this class represents a single post
class Post:
def __init__(self, stream, sender, date, intDate, text, lineCount, read, multiPage):
self.stream = stream
self.sender = sender
self.date = date
self.intDate = intDate
self.text = text
self.lineCount = lineCount
self.read = read
self.multiPage = multiPage
def __str__(self):
tempStreamName = ""
if (self.stream[len(self.stream)-6:] == "Stream"):
tempStreamName = self.stream[:-6]
else:
tempStreamName = self.stream
if (self.multiPage == True):
return tempStreamName + "\nSender: " + self.sender + "\n" + self.date + "\n" + str(self.text)
else:
return tempStreamName + "\nSender: " + self.sender + "\n" + self.date + "\n" + str(self.text) + "---------"
#Gets all data required to create an object post (sender, stream, date, text, etc)
def getStringsFromFile(tempArray, byteIndexs, index, postFile, streamName, flag):
postCountArray = []
streamNameToAdd = ""
newTextToAdd = ""
dateToAdd = ""
senderToAdd = ""
textToAdd = ""
lineCount = 0
splitPost = False
i = 0
#Using the bytes from the data file, retrieve the posts
postFile.seek(int(byteIndexs[index]))
line = postFile.read(int(byteIndexs[index+1]) - int(byteIndexs[index]))
streamNameToAdd = "Stream: " + streamName
senderToAdd = line.splitlines()[0]
dateToAdd = line.splitlines()[1]
#Get all lines of the text portion of the post
for textLine in line.splitlines()[2:]:
textToAdd = textToAdd + textLine + "\n"
lineCount = lineCount + 1
#Using the string date, generate a number representing the date (to be used to sort posts by date)
intDate = parseDate(dateToAdd)
numOfPagesRequired = math.ceil(len(textToAdd.splitlines()) + 3) // 23 + 1
#If the post needs to be divided over more than one page
if (numOfPagesRequired > 1):
maxIndex = 19
minIndex = -1
firstPartOfPost = True
while numOfPagesRequired > 0:
tempText = ""
splitPost = True
counter = 0
for i in textToAdd.splitlines():
if (counter > minIndex):
tempText = tempText + i + "\n"
counter = counter + 1
if (counter == maxIndex):
minIndex = maxIndex - 1
maxIndex = maxIndex + 19
break
if (firstPartOfPost == True):
firstPartOfPost = False
post = Post(streamNameToAdd, senderToAdd[8:], dateToAdd, intDate, tempText, len(tempText.splitlines()) + 4, flag, True)
tempArray.append(post)
else:
post = Post("", senderToAdd[8:], "", intDate, tempText, len(tempText.splitlines()) + 1, flag, False)
tempArray.append(post)
numOfPagesRequired = numOfPagesRequired - 1
if (splitPost == False):
#Create post object
post = Post(streamNameToAdd, senderToAdd[8:], dateToAdd, intDate, textToAdd, lineCount+4, flag, False)
tempArray.append(post)
return {"array": tempArray}
#Creates two arrays of posts (objects), one for read posts and one for unread posts.
def preloadPosts(byteIndexs, postFile, offsetIndex, streamName):
tempArray = []
tempArray2 = []
returnArray = []
returnArray2 = []
if (offsetIndex == len(byteIndexs)):
offsetIndex = 0
#Get all unread posts
for index in range(offsetIndex, len(byteIndexs)): #set to 0 to go back to viewing all posts
if (index+2 > len(byteIndexs)):
break
returnArray = getStringsFromFile(tempArray, byteIndexs, index, postFile, streamName, False)
tempArray = returnArray["array"]
#Get all read posts
if (offsetIndex != 0):
for index in range(0, offsetIndex):
if (index+2 > len(byteIndexs)):
break
returnArray2 = getStringsFromFile(tempArray2, byteIndexs, index, postFile, streamName, True)
tempArray2 = returnArray2["array"]
return {"unreadArray": tempArray, "readArray": tempArray2}
#Adds all the byte data from the stream data file into an array
def getBytesAndLoadPosts(lines, postFile, offsetIndex, streamName):
newByteIndexs = []
newByteIndexs.append(0)
#Add all lines of the data file to an array which will be used to retrieve the posts
for byteNum in lines:
byteNum = byteNum.strip('\n')
newByteIndexs.append(byteNum)
returnArray = preloadPosts(newByteIndexs, postFile, offsetIndex, streamName)
return returnArray
#Checks if new posts have been added to the specified stream
def checkForNew(streamName, originalDataLineCount):
#Use the data file to check for new posts
dataFileName = "./messages/" + streamName + "StreamData.txt"
dataFile = open(dataFileName, 'r')
lines = dataFile.readlines()
dataFile.close()
#If the number of lines in the old data file are equal to the current number of lines in the data file thar means no new posts were added (since posts cannot be deleted)
if (len(lines) == originalDataLineCount):
return False
else:
return True
#Checks if new posts have been added to any streams that the user has permissions to view
def checkForNewAllMode(streamNames, originalDataLineCount):
lineCount = 0
#Get and combine the number of lines from all data files (from streams the user has permissions to view)
for stream in streamNames:
dataFileName = "./messages/" + stream + "StreamData.txt"
dataFile = open(dataFileName, 'r')
lines = dataFile.readlines()
lineCount = lineCount + len(lines)
dataFile.close()
#If the number of lines in the old data file are equal to the current number of lines in the data file thar means no new posts were added (since posts cannot be deleted)
if (lineCount == originalDataLineCount):
return False
else:
return True
#This function takes in a date in the format of a string and converts that string into a number that represents the date.
#For example, Feb. 14, 2017 10:23 will be converted to 021420171023
def parseDate(date):
newDate = ""
date = date.strip("\n")
date = date.split(" ")
#Conver the month name into its equivalent integer value
if (date[1] == "Jan."):
newDate = "1"
elif (date[1] == "Feb."):
newDate = "2"
elif (date[1] == "Mar."):
newDate = "3"
elif (date[1] == "Apr."):
newDate = "4"
elif (date[1] == "May."):
newDate = "5"
elif (date[1] == "Jun."):
newDate = "6"
elif (date[1] == "Jul."):
newDate = "7"
elif (date[1] == "Aug."):
newDate = "8"
elif (date[1] == "Sep."):
newDate = "9"
elif (date[1] == "Oct."):
newDate = "10"
elif (date[1] == "Nov."):
newDate = "11"
elif (date[1] == "Dec."):
newDate = "12"
#date[2] contains the day of the month. Remove the comma.
#date[3] contains the year
#date[4] contains the time (24-hour clock). Remove the : from the time
newDate = newDate + date[2].strip(",") + date[3] + date[4].replace(":", "")
return newDate
#When the user selects to view all streams that they have permissions to view, this function will take the data from all relevant stream files (data, user, stream) to create post objects and organize the posts into a read array and unread array
def loadAllStreams(allFileNames, username):
unreadArray = []
readArray = []
dataArray = []
userArray = []
streamArray = []
postOffset = []
dataInfo = []
newByteIndexs = []
temp = ""
posts = []
parsedDate = ""
textToAdd = ""
lineCount = 0
originalDataLineCount = 0
counter = -1
i = 0
splitPost = False
#Add all files that need to be used to one of the arrays (data array, user array, and stream/post array)
for name in allFileNames:
dataArray.append("./messages/" + name + "StreamData.txt")
userArray.append("./messages/" + name + "StreamUsers.txt")
streamArray.append("./messages/" + name + "Stream.txt")
#Get byte data for each stream
for dataFile in dataArray:
file = open(dataFile, 'r')
lines = file.readlines()
newByteIndexs.append(0)
for byteNum in lines:
byteNum = byteNum.strip('\n')
newByteIndexs.append(byteNum)
dataInfo.append(newByteIndexs)
newByteIndexs = []
file.close()
originalDataLineCount = originalDataLineCount + len(lines)
#Get data that tells the program what the last post read was in each stream
for userFile in userArray:
#Open the user file
file = open(userFile, 'r')
lines = file.readlines()
for user in lines:
user = user.strip('\n')
#Get the username
name = user.rsplit(' ', 1)[0]
#The number that "count" holds represents the last post read. For example, "7" would mean the user has read the first 7 posts
count = user.rsplit(' ', 1)[1]
#If the name from the text file matches the active user in the program, then save the data to an array
if (name == username):
postOffset.append(count)
file.close()
#Get posts and order them by date
for streamFile in streamArray:
file = open(streamFile, 'r')
counter = counter + 1
#Get all unread posts
for index in range(int(postOffset[i]), len(dataInfo[counter])-1): #set to 0 to get all posts from all streams
#Get post text from stream file
file.seek(int(dataInfo[counter][index]))
lines = file.read((int(dataInfo[counter][index+1])) - int(dataInfo[counter][index]))
#Format the strings needed to create a post object
streamNameToAdd = "Stream: " + streamFile[11:-4]
senderToAdd = lines.splitlines()[0]
dateToAdd = lines.splitlines()[1]
for textLine in lines.splitlines()[2:]:
textToAdd = textToAdd + textLine + "\n"
lineCount = lineCount + 1
intDate = parseDate(dateToAdd)
numOfPagesRequired = math.ceil(len(textToAdd.splitlines()) + 3) // 23 + 1
#If the post needs to be divided over more than one page
if (numOfPagesRequired > 1):
maxIndex = 19
minIndex = -1
firstPartOfPost = True
while numOfPagesRequired > 0:
tempText = ""
splitPost = True
counter2 = 0
for y in textToAdd.splitlines():
if (counter2 > minIndex):
tempText = tempText + y + "\n"
counter2 = counter2 + 1
if (counter2 == maxIndex):
minIndex = maxIndex - 1
maxIndex = maxIndex + 19
break
if (firstPartOfPost == True):
firstPartOfPost = False
post = Post(streamNameToAdd, senderToAdd[8:], dateToAdd, intDate, tempText, len(tempText.splitlines()) + 4, False, True)
unreadArray.append(post)
else:
post = Post("", senderToAdd[8:], "", intDate, tempText, len(tempText.splitlines()) + 1, False, False)
unreadArray.append(post)
numOfPagesRequired = numOfPagesRequired - 1
if (splitPost == False):
#Create post object
post = Post(streamNameToAdd, senderToAdd[8:], dateToAdd, intDate, textToAdd, lineCount+4, False, False)
unreadArray.append(post)
textToAdd = ""
lineCount = 0
splitPost = False
#Get all read posts
if (int(postOffset[i]) != 0):
for index in range(0, int(postOffset[i])):
#Get post text from stream file
file.seek(int(dataInfo[counter][index]))
lines = file.read((int(dataInfo[counter][index+1])) - int(dataInfo[counter][index]))
#Format the strings needed to create a post object
streamNameToAdd = "Stream: " + streamFile[11:-4]
senderToAdd = lines.splitlines()[0]
dateToAdd = lines.splitlines()[1]
for textLine in lines.splitlines()[2:]:
textToAdd = textToAdd + textLine + "\n"
lineCount = lineCount + 1
intDate = parseDate(dateToAdd)
numOfPagesRequired = math.ceil(len(textToAdd.splitlines()) + 3) // 23 + 1
#If the post needs to be divided over more than one page
if (numOfPagesRequired > 1):
maxIndex = 19
minIndex = -1
firstPartOfPost = True
while numOfPagesRequired > 0:
tempText = ""
splitPost = True
counter2 = 0
for z in textToAdd.splitlines():
if (counter2 > minIndex):
tempText = tempText + z + "\n"
counter2 = counter2 + 1
if (counter2 == maxIndex):
minIndex = maxIndex - 1
maxIndex = maxIndex + 19
break
if (firstPartOfPost == True):
firstPartOfPost = False
post = Post(streamNameToAdd, senderToAdd[8:], dateToAdd, intDate, tempText, len(tempText.splitlines()) + 4, False, True)
readArray.append(post)
else:
post = Post("", senderToAdd[8:], "", intDate, tempText, len(tempText.splitlines()) + 1, False, False)
readArray.append(post)
numOfPagesRequired = numOfPagesRequired - 1
if (splitPost == False):
#Create post object
post = Post(streamNameToAdd, senderToAdd[8:], dateToAdd, intDate, textToAdd, lineCount+4, False, False)
readArray.append(post)
textToAdd = ""
lineCount = 0
splitPost = False
i = i + 1
#Sort based of the value at index 1 of each array element
#Unread and read posts are sorted seperatly
unreadArray = sorted(unreadArray, key=operator.attrgetter('intDate'))
readArray = sorted(readArray, key=operator.attrgetter('intDate'))
return {"array": unreadArray, "readArray": readArray, "dataLineCount": originalDataLineCount}
#When the user selects to view a single stream, this function will take the data from all relevant stream files (data, user, stream/post) to create post objects and organize the posts into a read array and unread array
def loadStream(userStreams, userReadPostIndex, fullStreamNames, selection):
i = 0
offsetIndex = 0
#Get names of all 3 files for the stream
for fileName in userStreams:
if (selection == fileName):
temp = (fullStreamNames[i])[:-15]
#offsetIndex tells the program what was the last post read in the stream
offsetIndex = userReadPostIndex[i]
postFileName = temp + "Stream.txt"
dataFileName = temp + "StreamData.txt"
usersFileName = temp + "StreamUsers.txt"
break
i = i + 1
postFile = open(postFileName, 'r')
dataFile = open(dataFileName, 'r')
lines = dataFile.readlines()
dataFile.close()
originalDataLineCount = len(lines)
#Call helper functions to create the post objects
returnArray = getBytesAndLoadPosts(lines, postFile, offsetIndex, selection)
postFile.close()
return {"array": returnArray["unreadArray"], "readArray": returnArray["readArray"], "dataLineCount": originalDataLineCount}
#When the user selects option "s", this function will prompt the user to select a new stream
def promptForStream(streamList, printErrorMessage):
postFileName = ""
dataFileName = ""
returnArray = []
i = 0
print("")
os.system('clear')
#Display an error message if the user lost permission to view the stream that was active
if (printErrorMessage == True):
print("User no longer has permission to view this stream. Please select a new stream.")
print("Select a stream below: ")
for fileName in streamList:
print(fileName),
print("all")
#Prompt until the user inputs a valid stream name or the word "all"
while 1:
streamName = raw_input()
if ((streamName not in streamList) and streamName != "all"):
print("Unable to access stream")
else:
break
return streamName
#When ever a stream is first loaded or the user pages down, update the user file to reflect which posts have been read
def updateReadPosts(posts, lastPostRead, user):
userFileName = "./messages/" + posts[0].stream[8:] + "StreamUsers.txt"
dataFileName = "./messages/" + posts[0].stream[8:] + "StreamData.txt"
oldUserFile = open(userFileName, 'r')
dataFile = open(dataFileName, 'r')
newUserFile = open("./messages/temp.txt", 'w')
lines = oldUserFile.readlines()
postCount = len(dataFile.readlines())
dataFile.close()
#Loop through all line of the data file looking for a matching username
for username in lines:
username = username.strip('\n')
count = len(username)
#Looping through each line backwards, when the first space is found, everything to the right is the post read count; everything to the left is the username
for character in reversed(username):
count = count - 1
if character == " ":
usernameShort = username[:count]
#If the usernames match and the number of posts read in the file is less than the "new" last post read, update the text file to reflect the change
if (user == usernameShort and int(username[count+1:]) < lastPostRead+1 and lastPostRead+1 < postCount):
newUserFile.write(usernameShort + " " + str(lastPostRead+1) + "\n")
elif(user == usernameShort and int(username[count+1:]) < lastPostRead+1):
newUserFile.write(usernameShort + " " + str(postCount) + "\n")
else:
newUserFile.write(username + "\n")
break
oldUserFile.close()
newUserFile.close()
os.rename("./messages/temp.txt", userFileName)
#When in "all" mode, if the user selects the 'm' option, loop through all relevant stream user files and set the read post count to the # of lines in the matching data file (total number of posts in the stream)
def markAllStreams(streamList, user):
for fileName in streamList:
userFileName = "./messages/" + fileName + "StreamUsers.txt"
dataFileName = "./messages/" + fileName + "StreamData.txt"
oldUserFile = open(userFileName, 'r')
dataFile = open(dataFileName, 'r')
newUserFile = open("./messages/temp.txt", 'w')
lines = oldUserFile.readlines()
dataLineCount = dataFile.readlines()
for username in lines:
username = username.strip('\n')
count = len(username)
for character in reversed(username):
count = count - 1
if character == " ":
usernameShort = username[:count]
#If the usernames match, upate the read count to the highest possible value (total number of posts in the stream)
if (user == usernameShort):
newUserFile.write(usernameShort + " " + str(len(dataLineCount)) + "\n")
else:
newUserFile.write(username + "\n")
break
oldUserFile.close()
newUserFile.close()
dataFile.close()
os.rename("./messages/temp.txt", userFileName)
#When in "all" mode, when ever a stream is first loaded or the user pages down, update the user file to reflect which posts have been read
def updateReadPostsAllMode(posts, postIndexs, user):
streamList = {}
#In the list, there is a key for each stream where the value is the number of posts read in that stream
for i in range(postIndexs[0], postIndexs[1]+1):
if (posts[i].stream[8:] in streamList):
streamList[posts[i].stream[8:]] = int(streamList[posts[i].stream[8:]]) + 1
else:
streamList[posts[i].stream[8:]] = 1
#Loop through all stream user files that the active user has permission to
for element in streamList:
userFileName = "./messages/" + element + "Users.txt"
if not os.path.exists(userFileName):
continue
oldUserFile = open(userFileName, 'r')
newUserFile = open("./messages/temp.txt", 'w')
lines = oldUserFile.readlines()
#Loop through all lines of the user file looking for a matching username. When a match is found, update the read post count
for username in lines:
username = username.strip('\n')
count = len(username)
for character in reversed(username):
count = count - 1
if character == " ":
usernameShort = username[:count]
if (user == usernameShort):
newUserFile.write(usernameShort + " " + str(int(username[count+1:]) + int(streamList[element])) + "\n")
else:
newUserFile.write(username + "\n")
break
oldUserFile.close()
newUserFile.close()
os.rename("./messages/temp.txt", userFileName)
#Print a page of posts. Note: Only full posts are printed. If a post fit on the page, it will be printed on the next page
def printPosts2(posts, pageRanges):
print("")
os.system('clear')
lineCount = 0
#If the page range is a range, than print out the posts that correspond to the indexes within the range
if (pageRanges[0] != "Blank"):
for i in range(pageRanges[0], pageRanges[1]+1):
print(posts[i])
lineCount = lineCount + posts[i].lineCount
#If the page range is not a range but there are posts in the stream, displat a message saying all messages have been read
elif posts:
print("No unread messages. Page up to view older messages.")
lineCount = lineCount + 1
#Otherwise there are no posts in the stream
else:
print("There are no messages in this stream.")
lineCount = lineCount + 1
#The menu bar should be the very last line of the page/screen so fill all the empty lines with newline characters
while (lineCount < 23):
print("\n"),
lineCount = lineCount + 1
print("Pg Up Pg Down O-order toggle M-mark all S-stream C-check for new q-quit"),
#Given an array for posts, index them into pages. In other words, group postings so that they can all be displayed on multiple pages
def divideIntoPages(posts):
startOfBlock = True
startingIndex = 0
lineCount = 0
blockArray = []
i = 0
j = 0
while i < len(posts):
j = 0
#If the post can fit on the current page
if (lineCount + len(posts[i].text.splitlines()) + 4 < 24):
if (startOfBlock == True):
startingIndex = i
startOfBlock = False
lineCount = lineCount + len(posts[i].text.splitlines()) + 4
i = i + 1
#Otherwise, start indexing the next page
else:
startOfBlock = True
lineCount = 0
blockArray.append([startingIndex, i-1])
if (startOfBlock == False):
blockArray.append([startingIndex, i-1])
return blockArray
#When the user selects the "o" option, sort the posts based on name or date
def changePostOrder(posts, sortOption):
newArray = []
#Sort by name
if (sortOption == 1):
newArray = sorted(posts, key=operator.attrgetter('sender'))
#Sort by date
else:
newArray = sorted(posts, key=operator.attrgetter('intDate'))
return newArray
#Every time a key is pressed, check to make sure the active user still has permission to view the stream. For example, if the user is removed from the stream as they are viewing it, the next time they press a key, an error message will print
def hasPermissionToView(stream, user):
userFileName = "./messages/" + stream + "StreamUsers.txt"
userFile = open(userFileName, 'r')
lines = userFile.readlines()
#Within the stream user file, look for a matching username. If a match is made, that the means the user still has permission to view the stream. Otherwise, they do not.
for username in lines:
username = username.strip('\n')
count = len(username)
for character in reversed(username):
count = count - 1
if character == " ":
usernameShort = username[:count]
if (user == usernameShort):
userFile.close()
return True
userFile.close()
return False
def getFileData(argUsername):
fullStreamNames = []
userReadPostIndex = []
userStreams = []
printErrorMessage = False
#Get all file names in the message directory
for fileName in os.listdir("./messages"):
#Search for all files containing user data
if "StreamUsers" in fileName:
fileName = "./messages/" + fileName
file = open(fileName, 'r')
lines = file.readlines()
#Search for a line that matches the username of the active user and save the index that tells the program how many posts in that strea have already been read
for username in lines:
username = username.strip('\n')
fullName = username
count = len(username)
for character in reversed(username):
count = count - 1
if character == " ":
username = username[:count]
break
if argUsername == username:
fullStreamNames.append(fileName)
fileName = fileName[11:-15]
userStreams.append(fileName)
userReadPostIndex.append(int(fullName[count+1:]))
file.close()
return {"fullStreamNames": fullStreamNames, "userReadPostIndex": userReadPostIndex, "userStreams": userStreams, "printErrorMessage": printErrorMessage}
def programLoop():
userStreams = []
userReadPostIndex = []
fullStreamNames = []
byteIndexs = []
postCountArray = []
pageRanges = []
pageRanges2 = []
fullName = ""
userFileName = ""
dataFileName = ""
postFileName = ""
argUsername = ""
lineCount = 0
originalDataLineCount = 0
i = 0
postArray = []
readPostArray = []
returnArray = []
currentOrder = 1
currentViewPage = 0
lastPageViewed = 0
startOfUnread = 0
markAllPressed = False
allStreamMode = False
readPosts = False
displayNoMessagePage = False
allRead = False
permission = True
for arg in sys.argv[1:]:
argUsername = argUsername + arg + " "
argUsername = argUsername[:-1]
#Find all files containing username
returnArray = getFileData(argUsername)
fullStreamNames = returnArray["fullStreamNames"]
userReadPostIndex = returnArray["userReadPostIndex"]
userStreams = returnArray["userStreams"]
printErrorMessage = returnArray["printErrorMessage"]
for fileName in userStreams:
print(fileName),
if len(userStreams) == 0:
print("No Streams")
exit()
else:
print("all")
while 1:
selection = raw_input()
if ((selection not in userStreams) and selection != "all"):
print("Unable to access stream")
else:
break
if (selection == "all"):
allStreamMode = True
returnArray = loadAllStreams(userStreams, argUsername)
postArray = returnArray["array"]
readPostArray = returnArray["readArray"]
originalDataLineCount = returnArray["dataLineCount"]
for i in readPostArray:
readPosts = True
pageRanges = divideIntoPages(postArray)
if (readPosts == True):
pageRanges2 = divideIntoPages(readPostArray)
tempR = pageRanges
for i in range(0, len(tempR)):
for j in range(0, 2):
tempR[i][j] = tempR[i][j] + len(readPostArray)
postArray = readPostArray + postArray;
pageRanges = pageRanges2 + pageRanges
currentViewPage = len(pageRanges2)
startOfUnread = currentViewPage
#If not all posts have been viewed
if (len(pageRanges) > currentViewPage):
printPosts2(postArray, pageRanges[currentViewPage])
updateReadPostsAllMode(postArray, pageRanges[currentViewPage], argUsername)
else:
pageRanges.append(["Blank"])
displayNoMessagePage = True
allRead = True
printPosts2(postArray, pageRanges[currentViewPage])
else:
allStreamMode = False
returnArray = loadStream(userStreams, userReadPostIndex, fullStreamNames, selection)
postArray = returnArray["array"]
readPostArray = returnArray["readArray"]
originalDataLineCount = returnArray["dataLineCount"]
for i in readPostArray:
readPosts = True
pageRanges = divideIntoPages(postArray)
if (readPosts == True):
pageRanges2 = divideIntoPages(readPostArray)
tempR = pageRanges
for i in range(0, len(tempR)):
for j in range(0, 2):
tempR[i][j] = tempR[i][j] + len(readPostArray)
postArray = readPostArray + postArray;
pageRanges = pageRanges2 + pageRanges
currentViewPage = len(pageRanges2)
startOfUnread = currentViewPage
#If not all posts have been viewed
if (len(pageRanges) > currentViewPage):
printPosts2(postArray, pageRanges[currentViewPage])
updateReadPosts(postArray, pageRanges[currentViewPage][1], argUsername)
else:
pageRanges.append(["Blank"])
displayNoMessagePage = True
printPosts2(postArray, pageRanges[currentViewPage])
while True:
savedSettings = termios.tcgetattr(sys.stdin)
tty.setraw(sys.stdin)
optionInput = sys.stdin.read(1)
if (optionInput == '\x1b'):
optionInput = optionInput + sys.stdin.read(2)
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, savedSettings)
if (selection != "all"):
permission = hasPermissionToView(selection, argUsername)
if (permission == False):
optionInput = 's'
#End program (quit)
if (optionInput == 'q'):
break
#Page down
elif optionInput == '\x1b[B' and currentViewPage < len(pageRanges)-1:
goUpdate = False
printPosts2(postArray, pageRanges[currentViewPage+1])
currentViewPage = currentViewPage + 1
if (currentViewPage > lastPageViewed):
lastPageViewed = currentViewPage
goUpdate = True
if (allStreamMode == False and currentOrder == 1):
updateReadPosts(postArray, pageRanges[currentViewPage][1], argUsername)
elif (allStreamMode == True and goUpdate == True and allRead == False and currentViewPage > startOfUnread and currentOrder == 1):
updateReadPostsAllMode(postArray, pageRanges[currentViewPage], argUsername)
#Page up
elif optionInput == '\x1b[A' and currentViewPage > 0:
printPosts2(postArray, pageRanges[currentViewPage-1])
currentViewPage = currentViewPage - 1
if (displayNoMessagePage == True):
displayNoMessagePage = False
#Remove the element containg "Blank" since the message does not need to be displayed again
pageRanges.pop()
elif optionInput == 's':
fullStreamNames = []
userReadPostIndex = []
userStreams = []
printErrorMessage = False
displayNoMessagePage = False
currentOrder = 1
#Find all files containing username
returnArray = getFileData(argUsername)
fullStreamNames = returnArray["fullStreamNames"]
userReadPostIndex = returnArray["userReadPostIndex"]
userStreams = returnArray["userStreams"]
printErrorMessage = returnArray["printErrorMessage"]
if (permission == False):
permission = True
printErrorMessage = True
selection = promptForStream(userStreams, printErrorMessage)
if (selection == "all"):
allStreamMode = True
else:
allStreamMode = False
if (allStreamMode == False):
returnArray = loadStream(userStreams, userReadPostIndex, fullStreamNames, selection)
postArray = returnArray["array"]
readPostArray = returnArray["readArray"]
originalDataLineCount = returnArray["dataLineCount"]
for i in readPostArray:
readPosts = True
pageRanges = divideIntoPages(postArray)
if (readPosts == True):
pageRanges2 = divideIntoPages(readPostArray)
tempR = pageRanges
for i in range(0, len(tempR)):