-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
142 lines (110 loc) · 4.51 KB
/
reader.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
# Calculate the read speed of Red Hat Training adoc files.
import os
import math
import sys
import glob
#The average reading speed in words per minute for adults
averageSpeed = 200
#The time to "read" each image in seconds
imageReadTime = 12
#The time it takes to read a code block in seconds
timeToReadCodeBlock = 30
#Path for the file types to check
filetypes = "/*-lecture-content.adoc"
# Function to count number of characters, words, spaces and lines in a file
def counter(fname):
inCodeBlock = False
# variable to store total word count
num_words = 0
# variable to store total line count
num_lines = 0
# variable to store total character count
num_charc = 0
# variable to store total space count
num_spaces = 0
# variable to store total images
num_images = 0
#variable to store total code blocks
num_codeblocks = 0
with open(fname, 'r') as f:
# iterate by line
for line in f:
# separating a line from \n character and storing again in line
line = line.strip(os.linesep)
# split line into word array
wordslist = line.split()
# count the lines
num_lines = num_lines + 1
#Count number of words
if (len(wordslist)):
if (wordslist[0].startswith("image::")):
# count number of images
num_images = num_images + 1
elif(wordslist[0].startswith("----")):
if (inCodeBlock):
#End the codeblock and resume counting words
num_codeblocks = num_codeblocks + 1
inCodeBlock = False
else:
#First line in code block
inCodeBlock = True
elif ((not wordslist[0].startswith("//")) and (not inCodeBlock)):
# add words in this line to total words
num_words = num_words + len(wordslist)
num_charc = num_charc + sum(1 for c in line
if c not in (os.linesep, ' '))
num_spaces = num_spaces + sum(1 for s in line
if s in (os.linesep, ' '))
#print file name
print("--------------------------")
print("Analysis of " + fname)
print("--------------------------")
# printing total word count
print("Number of words in text file: ", num_words)
# printing total line count
print("Number of lines in text file: ", num_lines)
# printing total images
print("Number of images in text file: ", num_images)
# printing total codeblocks
print("Number of code blocks in text file: ", num_codeblocks)
readLength(num_words, num_images, num_codeblocks)
def readLength(wordCount, imageCount, codeBlockCount):
# Start with base level word count and average speed
readingTime = wordCount/averageSpeed
#Add time for images, convert seconds to minutes
imageTime = (imageCount * imageReadTime) / 60
#add time for code blocks, convert time to minutes
codeBlockTime = (codeBlockCount * timeToReadCodeBlock) / 60
readingTime = readingTime+imageTime + codeBlockTime
print("raw reading time: " + repr(readingTime))
print("--------------------------")
print("Add the following beneath the section title:")
print ("[role='rolehtml']")
print (repr(math.ceil(readingTime)) + " min read")
def directoryProcess(path):
print("in directory")
#verify that there are lecture adoc files present
fileList = glob.glob(path + filetypes)
if (len(fileList) <= 0):
print("No relevant files found in directory")
else:
print("Relevant files found: ", len(fileList))
#iterate through list of adoc files calling counter
for filename in fileList:
counter(filename)
if __name__ == '__main__':
if (len(sys.argv) < 2):
print("You must include the target path and filename")
print("Ex. `python3 reader.py /User/zpgutterman/lecture.adoc")
sys.exit()
input = sys.argv[1]
if not os.path.isfile(input) and not os.path.isdir(input):
print('The file or directory specified does not exist')
sys.exit()
try:
if (os.path.isfile(input)):
counter(input)
elif (os.path.isdir(input)):
directoryProcess(input)
except Exception as e:
print(e)