Skip to content

Commit d116078

Browse files
committed
feat: 🎸 add text style
Closes: #12
1 parent de4f859 commit d116078

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

pyencrypt/cli.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,31 @@
3131
"""
3232

3333
PYTHON_MAJOR, PYTHON_MINOR = sys.version_info[:2]
34-
LAODER_FILE_NAME = "loader.cpython-{major}{minor}{abi}-{platform}.so".format(
35-
major=PYTHON_MAJOR,
36-
minor=PYTHON_MINOR,
37-
abi=sys.abiflags,
38-
platform=sys.platform)
34+
LAODER_FILE_NAME = click.style(
35+
"encrypted/loader.cpython-{major}{minor}{abi}-{platform}.so".format(
36+
major=PYTHON_MAJOR,
37+
minor=PYTHON_MINOR,
38+
abi=sys.abiflags,
39+
platform=sys.platform),
40+
blink=True,
41+
fg='blue')
42+
43+
SUCCESS_ANSI = click.style('successfully', fg='green')
44+
INVALID_KEY_MSG = click.style('Your encryption key is invalid.', fg='red')
3945

4046
FINISH_ENCRYPT_MSG = f"""
41-
Encryption completed successfully.
42-
Please copy encrypted/{LAODER_FILE_NAME} into your encrypted directory.
47+
Encryption completed {SUCCESS_ANSI}.
48+
Please copy {LAODER_FILE_NAME} into your encrypted directory.
4349
And then remove `encrypted` directory.
4450
Finally, add `import loader` at the top of your entry file.\
4551
"""
4652

47-
FINISH_DECRYPT_MSG = """
48-
Decryption completed successfully. Your origin source code has be put: {path}\
53+
FINISH_DECRYPT_MSG = f"""
54+
Decryption completed {SUCCESS_ANSI}. Your origin source code has be put: %s
4955
"""
5056

5157
FINISH_GENERATE_MSG = f"""
52-
Generate loader file successfully. Your loader file is located in {LAODER_FILE_NAME}
58+
Generate loader file {SUCCESS_ANSI}. Your loader file is located in {LAODER_FILE_NAME}
5359
"""
5460

5561

@@ -85,10 +91,12 @@ def cli():
8591
def encrypt_command(ctx, pathname, delete, key):
8692
"""Encrypt your python code"""
8793
if key is not None and not _check_key(key):
88-
ctx.fail(f'Your encryption key is invalid.')
94+
ctx.fail(INVALID_KEY_MSG)
8995
if key is None:
9096
key = generate_aes_key().decode()
91-
click.echo(f'Your randomly encryption 🔑 is {key}')
97+
click.echo(
98+
f'Your randomly encryption 🔑 is {click.style(key,underline=True, fg="yellow")}'
99+
)
92100

93101
path = Path(pathname)
94102
work_dir = Path(os.getcwd()) / 'encrypted' / 'src'
@@ -124,9 +132,12 @@ def encrypt_command(ctx, pathname, delete, key):
124132
help='Your encryption key.',
125133
type=click.STRING)
126134
@click.help_option('-h', '--help')
127-
def decrypt_command(pathname, key):
135+
@click.pass_context
136+
def decrypt_command(ctx, pathname, key):
128137
"""Decrypt encrypted pye file"""
129138
path = Path(pathname)
139+
if not _check_key(key):
140+
ctx.fail(INVALID_KEY_MSG)
130141

131142
if path.is_file():
132143
work_dir = Path(os.getcwd())
@@ -145,7 +156,7 @@ def decrypt_command(pathname, key):
145156
else:
146157
raise Exception(f'{path} is not a valid path.')
147158

148-
click.echo(FINISH_DECRYPT_MSG.format(path=work_dir))
159+
click.echo(FINISH_DECRYPT_MSG % work_dir)
149160

150161

151162
@cli.command(name='generate')
@@ -159,7 +170,7 @@ def decrypt_command(pathname, key):
159170
def generate_loader(ctx, key):
160171
"""Generate loader file using specified key"""
161172
if not _check_key(key):
162-
ctx.fail(f'Your encryption key is invalid.')
173+
ctx.fail(INVALID_KEY_MSG)
163174
cipher_key, d, n = encrypt_key(key.encode())
164175
generate_so_file(cipher_key, d, n)
165176
click.echo(FINISH_GENERATE_MSG)

0 commit comments

Comments
 (0)