Skip to content

Commit 86067f9

Browse files
Add check argument
1 parent e6bfed1 commit 86067f9

1 file changed

Lines changed: 28 additions & 8 deletions

File tree

main.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
from argparse import ArgumentParser
2+
from functools import partial
23
import PyPDF2 as pdf # 1.26.0
34
import operator
45
import time
56
import os
67

78
def encrypt_word(word, contents: list[str]):
8-
for page_idx, page_text in enumerate(contents):
9-
if word in page_text:
10-
for line_idx, line in enumerate(page_text.splitlines()):
11-
words = line.split()
12-
if word in words:
13-
return f"{page_idx}-{line_idx}-{words.index(word)}"
9+
if coords := check_word(word, contents):
10+
return '-'.join(coords)
1411
return '#'
1512

1613
def encrypt(text, contents: list[str], delimiter=','):
1714
return '\n'.join(
1815
[
1916
delimiter.join(
2017
map(
21-
lambda word: encrypt_word(word, contents),
18+
partial(encrypt_word, contents=contents),
2219
line
2320
)
2421
)
@@ -42,6 +39,26 @@ def decrypt(phrase, contents: list[str], delimiter=','):
4239
words.append('#')
4340
return ' '.join(words)
4441

42+
def check_word(word, contents):
43+
for page_idx, page_text in enumerate(contents):
44+
if word in page_text:
45+
for line_idx, line in enumerate(page_text.splitlines()):
46+
words = line.split()
47+
if word in words:
48+
return page_idx, line_idx, words.index(word)
49+
50+
def check(clause, contents):
51+
result = []
52+
for line in splitter(clause):
53+
_result = []
54+
for word in line:
55+
if check_word(word, contents):
56+
_result.append(word)
57+
else:
58+
_result.append('#')
59+
result.append(' '.join(_result))
60+
return '\n'.join(result)
61+
4562
def is_pdf_valid(pdf_obj):
4663
""" Has the program detected any text in pdf or not """
4764
for page_text in map(extractor, pdf_obj.pages):
@@ -87,6 +104,7 @@ def print_figlet():
87104
parser = ArgumentParser(prog='BCipher', description=DESCRIPTION)
88105
parser.add_argument('source', help='desired pdf book')
89106
parser.add_argument('data', help='either a text between " or path of a text file')
107+
parser.add_argument('-c', '--check', action='store_true', help='do checking operation | check the presence of <data> values & put # for unaccessables')
90108
parser.add_argument('-e', '--encrypt', action='store_true', help='do encrypt operation | convert each word into a 3-part encoded clause')
91109
parser.add_argument('-d', '--decrypt', action='store_true', help='do decrypt operation | retrieve each encoded clause into a word')
92110
parser.add_argument('-o', '--output', help='export the result in this path')
@@ -112,7 +130,9 @@ def print_figlet():
112130
if args.lower_data:
113131
args.data = args.data.lower()
114132

115-
if args.decrypt:
133+
if args.check:
134+
result = check(args.data, content)
135+
elif args.decrypt:
116136
result = decrypt(args.data, content, delimiter=args.delimiter)
117137
elif args.encrypt:
118138
result = encrypt(args.data, content, delimiter=args.delimiter)

0 commit comments

Comments
 (0)