-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
411 lines (350 loc) · 14.7 KB
/
main.py
File metadata and controls
411 lines (350 loc) · 14.7 KB
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
import random
import flask
import io
import os
import requests
import urllib.request
import zipfile
# Set this flag to True to use WeasyPrint (emoji-friendly) or False for ReportLab.
USE_WEASYPRINT = True
if USE_WEASYPRINT:
from weasyprint import HTML
else:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
app = flask.Flask(__name__)
# --- Pattern Generation Functions ---
def generate_pattern(difficulty, char_set):
"""
Generate a pattern by repeating a random sequence of characters.
Args:
difficulty (int): The length of the pattern to generate
char_set (str): The set of characters to use for generation
Returns:
list: A list containing the generated pattern
"""
char_sequence = ''.join(random.choice(char_set) for _ in range(difficulty))
pattern = []
for i in range(difficulty):
pattern.append(char_sequence)
return pattern
def generate_pattern_num(difficulty, char_set="0123456789"):
"""
Generate a numeric pattern using one of three algorithms.
Args:
difficulty (int): The complexity level of the pattern
char_set (str, optional): The set of digits to use. Defaults to "0123456789".
Returns:
str: The generated numeric pattern
"""
# Select a random algorithm
algorithm = random.randint(0, 2)
if algorithm == 0:
# Algorithm 0: Simple repetition of a random sequence
sequence = ''.join(random.choice(char_set) for _ in range(difficulty))
pattern = []
for _ in range(difficulty):
pattern.append(sequence)
return ''.join(pattern)
elif algorithm == 1:
# Algorithm 1: Evolving sequence with selective digit modification
sequence = [random.choice(char_set) for _ in range(difficulty)]
operation = random.choice(["*", "/", "+", "-"])
factor = random.randint(2, 9) if operation in ["*", "/"] else random.randint(1, 9)
target_position = random.randint(0, len(sequence) - 1)
result = ""
initial_sequence = ''.join(sequence)
for iteration in range(1, difficulty + 1):
original_value = int(sequence[target_position])
# Apply the selected operation to the target digit
if operation == "*":
new_value = original_value * factor
elif operation == "/":
new_value = original_value // factor if factor != 0 else original_value
elif operation == "+":
new_value = original_value + factor
elif operation == "-":
new_value = (original_value - factor) % 10
# Update the sequence with the new value (last digit only)
sequence[target_position] = str(new_value)[-1]
current_sequence = ''.join(sequence)
result += current_sequence
return result
else: # algorithm == 2
# Algorithm 2: Apply operation to every digit in each iteration
sequence = list(''.join(random.choice(char_set) for _ in range(difficulty)))
operation = random.choice(["*", "/", "+", "-"])
factor = random.randint(2, 9) if operation in ["*", "/"] else random.randint(1, 9)
result = ""
for _ in range(1, difficulty + 1):
# Apply operation to each digit
for j in range(len(sequence)):
original_value = int(sequence[j])
if operation == "*":
new_value = original_value * factor
elif operation == "/":
new_value = original_value // factor if factor != 0 else original_value
elif operation == "+":
new_value = original_value + factor
elif operation == "-":
new_value = (original_value - factor) % 10
sequence[j] = str(new_value)[-1]
current_sequence = ''.join(sequence)
result += current_sequence
return result
# --- PDF Generation Functions ---
def generate_pdf_weasyprint(final_pattern_lines):
"""
Generate a PDF document with the provided pattern lines using WeasyPrint.
Args:
final_pattern_lines (list): List of pattern strings to include in the PDF
Returns:
BytesIO: A buffer containing the generated PDF
"""
# Page settings (letter size)
PAGE_WIDTH, PAGE_HEIGHT = 612, 792
margin = 40
line_height = 20
placed_boxes = [] # Track absolute positions to avoid overlapping
# Start building the HTML string with styles
html_lines = [f"""
<html>
<head>
<meta charset="utf-8">
<style>
body {{
position: relative;
width: {PAGE_WIDTH}px;
height: {PAGE_HEIGHT}px;
margin: 0;
padding: 0;
background: white;
font: 14px Helvetica, Arial, sans-serif;
}}
.title {{
position: absolute;
top: 20px;
width: 100%;
text-align: center;
font-weight: bold;
font-size: 24px;
color: black;
}}
.link {{
position: absolute;
bottom: 60px;
width: 100%;
text-align: center;
font-style: italic;
font-size: 12px;
color: blue;
}}
.signature {{
position: absolute;
bottom: 30px;
right: {margin}px;
color: black;
}}
.line {{
position: absolute;
white-space: nowrap;
}}
</style>
</head>
<body>
<div class="title">Pattern Generator</div>
<div class="link"><a href="https://github.com/thegoodduck/Pattern_Generator">https://github.com/thegoodduck/Pattern_Generator</a></div>
<div class="signature">Made with love by @thegoodduck</div>
"""]
# Rainbow colors (in hex)
rainbow_colors = ["#FF0000", "#FF7F00", "#FFFF00", "#00FF00", "#0000FF", "#4B0082", "#9400D3"]
# Place each pattern line on the page
for idx, line in enumerate(final_pattern_lines):
# Approximate text width
text_width = len(line) * 8
placed = False
attempts = 0
max_attempts = 100
# Try to place text without overlapping other elements
while not placed and attempts < max_attempts:
# Generate random position
random_x = random.uniform(margin, PAGE_WIDTH - margin - text_width)
random_y = random.uniform(margin + 50, PAGE_HEIGHT - margin - line_height - 60)
new_box = (random_x, random_y, random_x + text_width, random_y + line_height)
# Check for overlap with existing elements
overlap = any(
not (new_box[2] < box[0] or new_box[0] > box[2] or
new_box[3] < box[1] or new_box[1] > box[3])
for box in placed_boxes
)
if not overlap:
placed_boxes.append(new_box)
color = random.choice(rainbow_colors)
html_lines.append(f"""<div class="line" style="left:{random_x}px; top:{random_y}px; color:{color};">{line}</div>""")
placed = True
attempts += 1
# Fallback positioning if random placement fails
if not placed:
fallback_y = PAGE_HEIGHT - margin - idx * line_height - 60
color = random.choice(rainbow_colors)
html_lines.append(f"""<div class="line" style="left:{margin}px; top:{fallback_y}px; color:{color};">{line}</div>""")
# Finalize HTML and generate PDF
html_lines.append("</body></html>")
html_str = "\n".join(html_lines)
pdf_bytes = HTML(string=html_str).write_pdf()
return io.BytesIO(pdf_bytes)
def generate_pdf_reportlab(final_pattern_lines):
"""
Generate a PDF document with the provided pattern lines using ReportLab.
Args:
final_pattern_lines (list): List of pattern strings to include in the PDF
Returns:
BytesIO: A buffer containing the generated PDF
"""
pdf_buffer = io.BytesIO()
c = canvas.Canvas(pdf_buffer, pagesize=letter)
width, height = letter
margin = 40
line_height = 20
# Document metadata
title = "Pattern Generator"
link_text = "https://github.com/thegoodduck/Pattern_Generator"
signature = "Made with love by @thegoodduck"
# Add document title
c.setFillColorRGB(0, 0, 0)
c.setFont("Helvetica-Bold", 24)
c.drawCentredString(width / 2, height - 50, title)
# Rainbow colors (in RGB format)
rainbow_colors = [
(1, 0, 0), # Red
(1, 0.5, 0), # Orange
(1, 1, 0), # Yellow
(0, 1, 0), # Green
(0, 0, 1), # Blue
(0.29, 0, 0.51), # Indigo
(0.93, 0.51, 0.93) # Violet
]
# Place pattern lines on the page
placed_boxes = []
c.setFont("Helvetica", 14)
for idx, line in enumerate(final_pattern_lines):
text_width = c.stringWidth(line, "Helvetica", 14)
color = random.choice(rainbow_colors)
placed = False
attempts = 0
max_attempts = 100
# Try to place text without overlapping other elements
while not placed and attempts < max_attempts:
# Generate random position
random_x = random.uniform(margin, width - margin - text_width)
random_y = random.uniform(margin + 50, height - margin - line_height - 60)
new_box = (random_x, random_y, random_x + text_width, random_y + line_height)
# Check for overlap with existing elements
overlap = any(
not (new_box[2] < box[0] or new_box[0] > box[2] or
new_box[3] < box[1] or new_box[1] > box[3])
for box in placed_boxes
)
if not overlap:
placed_boxes.append(new_box)
c.setFillColorRGB(*color)
c.drawString(random_x, random_y, line)
placed = True
attempts += 1
# Fallback positioning if random placement fails
if not placed:
fallback_y = height - margin - idx * line_height - 60
c.setFillColorRGB(*color)
c.drawString(margin, fallback_y, line)
# Add link and signature
c.setFont("Helvetica-Oblique", 12)
c.setFillColorRGB(0, 0, 1)
c.drawCentredString(width / 2, 50, link_text)
link_width = 200
c.linkURL(link_text,
(width / 2 - link_width / 2, 40, width / 2 + link_width / 2, 60),
relative=0)
c.setFillColorRGB(0, 0, 0)
c.drawRightString(width - margin, 30, signature)
# Finalize the PDF
c.showPage()
c.save()
pdf_buffer.seek(0)
return pdf_buffer
# --- Flask Routes ---
@app.route('/', methods=['GET', 'POST'])
def index():
"""
Main route handler for the Pattern Generator web application.
GET: Displays the pattern generation form
POST: Processes form submission and returns a generated PDF
Returns:
Response: HTML template or PDF file download
"""
if flask.request.method == 'POST':
try:
# Process batch or single pattern generation
if flask.request.form.getlist('difficulty') and len(flask.request.form.getlist('difficulty')) > 1:
# Batch pattern generation
difficulties = flask.request.form.getlist('difficulty')
char_sets = flask.request.form.getlist('char_set')
pattern_list = []
# Generate each requested pattern
for diff, char_set in zip(difficulties, char_sets):
difficulty = int(diff)
# Choose appropriate generator based on character set
if char_set.isdigit():
pattern = generate_pattern_num(difficulty, char_set)
else:
pattern = generate_pattern(difficulty, char_set)
# Convert list patterns to strings if needed
if isinstance(pattern, list):
pattern = "".join(pattern)
pattern_list.append(pattern)
final_pattern_lines = pattern_list
else:
# Single pattern generation
try:
difficulty = int(flask.request.form['difficulty'])
char_set = flask.request.form['char_set']
# Choose appropriate generator based on character set
if char_set.isdigit():
pattern = generate_pattern_num(difficulty, char_set)
else:
pattern = generate_pattern(difficulty, char_set)
# Convert list patterns to strings if needed
if isinstance(pattern, list):
pattern = "".join(pattern)
final_pattern_lines = [pattern]
except ValueError:
# Handle invalid difficulty value
return flask.render_template('index.html',
error="Please enter a valid number for difficulty.")
except KeyError:
# Handle missing form fields
return flask.render_template('index.html',
error="Please fill in all required fields.")
# Generate PDF using selected backend
if USE_WEASYPRINT:
pdf_buffer = generate_pdf_weasyprint(final_pattern_lines)
else:
pdf_buffer = generate_pdf_reportlab(final_pattern_lines)
# Return PDF file for download
return flask.send_file(
pdf_buffer,
as_attachment=True,
download_name='pattern.pdf',
mimetype='application/pdf'
)
except Exception as e:
# Log exception and return error page
app.logger.error(f"Error generating pattern: {str(e)}")
return flask.render_template('index.html',
error="An error occurred while generating the pattern.")
# Display pattern generation form
return flask.render_template('index.html')
if __name__ == '__main__':
# Run the Flask application in debug mode (disable in production)
app.run(debug=True)