Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (61 loc) · 1.47 KB
/
Copy pathmain.py
File metadata and controls
77 lines (61 loc) · 1.47 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
import pyperclip
import click
import translate
from datetime import datetime
@click.group()
def cli():
pass
@cli.command(help="Convert text to morse code")
@click.argument("text", type=str)
@click.option(
"--audio",
"-a",
is_flag=True,
default=False,
help="Generate a .wav audio file of the morse code",
)
@click.option(
"--copy",
"-c",
is_flag=True,
default=False,
help="Copy the result to the clipboard",
)
@click.option(
"--output",
"-o",
is_flag=False,
type=click.Path(writable=True),
help="Output file name (Example: signal.wav)",
)
def encode(text: str, audio: bool, copy: bool, output: str | None):
code = translate.to_morse(text)
click.echo(code)
if copy:
pyperclip.copy(code)
if audio:
if output is None:
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S-%f")
output = f"{timestamp}.wav"
translate.to_audio(code, output)
@cli.command(help="Convert morse code to text")
@click.argument("code", type=str)
@click.option(
"--lower", "-l", is_flag=True, default=False, help="Convert text to lower case"
)
@click.option(
"--copy",
"-c",
is_flag=True,
default=False,
help="Copy the result to the clipboard",
)
def decode(code: str, lower: bool, copy: bool):
text = translate.to_text(code)
if lower:
text = text.lower()
if copy:
pyperclip.copy(text)
click.echo(text)
if __name__ == "__main__":
cli()