-
Notifications
You must be signed in to change notification settings - Fork 33
/
low_scraper.py
executable file
·184 lines (161 loc) · 6.87 KB
/
low_scraper.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
#!/usr/bin/env python2
"""Module for LowScraper"""
from scrapers import *
class LowScraper:
"""LowParse class
Low-Level_Programming project scraper.
Args:
soup (obj): BeautifulSoup obj containing parsed link
Attributes:
header_check (int): if 0, there is header. if 1, there is no header
"""
header_check = 0
def __init__(self, soup):
"""Instantiation of LowScraper"""
self.soup = soup
self.putchar_check = self.find_putchar()
self.prototypes_list = self.find_prototypes()
self.header_name = self.find_header()
self.file_names = self.find_files()
def find_putchar(self):
"""Method to scrape holberton's `_putchar`
Used for creating holberton's `_putchar` file if required
"""
sys.stdout.write(" -> Checking for _putchar... ")
search_putchar = self.soup.find(string=re.compile("You are allowed to use"))
try:
if len(search_putchar) == 23:
return search_putchar.next_sibling.text
except TypeError:
return None
def write_putchar(self):
"""Method to write/create holberton's `_putchar` if required"""
if self.putchar_check == "_putchar":
w_putchar = open("_putchar.c", "w+")
w_putchar.write("#include <unistd.h>\n")
w_putchar.write("\n")
w_putchar.write("/**\n")
w_putchar.write(" * _putchar - writes the character c to stdout\n")
w_putchar.write(" * @c: The character to print\n")
w_putchar.write(" *\n")
w_putchar.write(" * Return: On success 1.\n")
w_putchar.write(" * On error, -1 is returned, and errno")
w_putchar.write(" is set appropriately.\n")
w_putchar.write(" */\n")
w_putchar.write("int _putchar(char c)\n")
w_putchar.write("{\n")
w_putchar.write(" return (write(1, &c, 1));\n")
w_putchar.write("}")
w_putchar.close()
print("created")
else:
print("not created")
def find_prototypes(self):
"""Method to scrape for C prototypes"""
temp = []
find_protos = self.soup.find_all(string=re.compile("Prototype: "))
for item in find_protos:
temp.append(item.next_sibling.text.replace(";", ""))
return temp
def find_header(self):
"""Method to scrape for C header file name"""
try:
finder = "forget to push your header file"
header_text = self.soup.find(string=re.compile(finder)).previous_element
return header_text.previous_element.previous_element
except AttributeError:
self.header_check = 1
return ""
def write_header(self):
"""Method to write/create C header file if required"""
if self.header_check == 0:
# Making header include guard string
include_guard = self.header_name
include_guard = include_guard.replace('.', '_', 1)
include_guard = include_guard.upper()
sys.stdout.write(" -> Creating header file... ")
try:
w_header = open(self.header_name, "w+")
w_header.write('#ifndef %s\n' % include_guard)
w_header.write('#define %s\n' % include_guard)
w_header.write("\n")
w_header.write("#include <stdio.h>\n")
w_header.write("#include <stdlib.h>\n")
w_header.write("\n")
try:
if self.putchar_check == "_putchar":
w_header.write("int _putchar(char c);\n")
except TypeError:
pass
n = 0
for item in self.prototypes_list:
if n == len(self.prototypes_list):
break
w_header.write(self.prototypes_list[n] + ";\n")
n += 1
w_header.write("\n")
w_header.write('#endif /* %s */' % include_guard)
w_header.close()
print("done")
except AttributeError:
print("[ERROR] Failed to create header file")
else:
pass
def find_files(self):
"""Method to scrape for C file names"""
return self.soup.find_all(string=re.compile("File: "))
def write_files(self):
"""Method to write/create C files
Handles multiple file names by searching for ','.
"""
i = 0
sys.stdout.write(" -> Creating task files... ")
for item in self.file_names:
file_text = item.next_sibling.text
# Breaks incase more function names over file names
if self.prototypes_list != 0:
if (i == len(self.prototypes_list)):
break
try:
# Pulling out name of function for documentation
if self.prototypes_list != 0:
func_name = self.prototypes_list[i]
func_name = func_name.split("(", 1)[0]
tmp_split = func_name.split(" ")
func_name = tmp_split[len(tmp_split) - 1]
tmp_split = func_name.split("*")
func_name = tmp_split[len(tmp_split) - 1]
# Removing string after first comma (multiple file names)
find_comma = file_text.find(",")
if find_comma != -1:
w_file_name = open(file_text[:find_comma], "w+")
else:
w_file_name = open(file_text, "w+")
if self.header_check != 1:
w_file_name.write('#include "%s"\n\n' % self.header_name)
w_file_name.write("/**\n")
w_file_name.write(" * %s -\n" % func_name)
w_file_name.write(" *\n")
w_file_name.write(" * Return: \n")
w_file_name.write(" */\n")
w_file_name.write("%s\n" % self.prototypes_list[i])
w_file_name.write("{\n")
w_file_name.write("\n")
w_file_name.write("}")
w_file_name.close()
i += 1
except (AttributeError, IndexError):
sys.stdout.write("[ERROR] Failed to create ")
sys.stdout.write("task file %s\n" % file_text)
sys.stdout.write(" ... ")
continue
print("done")
def write_checker(self):
with open("check.sh", "w") as f:
f.write("#!/usr/bin/env bash\n")
f.write("betty ")
if self.header_name:
f.write('"%s" ' % self.header_name)
if self.file_names:
for i in self.file_names:
f.write('"%s" ' % i.next_sibling.text)