Skip to content

Commit

Permalink
Simplying sendcard's arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
0x9900 committed Feb 24, 2024
1 parent fb75963 commit 7be90b1
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions eqsl/sendcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import logging
import os
import shutil
import sys
from argparse import ArgumentParser
from importlib.metadata import version
from pathlib import Path
Expand Down Expand Up @@ -61,36 +60,45 @@ def __call__(self, change: Change, path: str) -> bool:
return super().__call__(change, path) and path == self.full_name


def main() -> None:
def sendcard() -> None:
parser = ArgumentParser(description="Watch for the creation on an adif file and call eqsl")
parser.add_argument("-s", "--show", action="store_true", default=False,
help="Show the card")
parser.add_argument("-k", "--keep", action="store_true", default=False,
help="Keep the cards after they have been sent (do not delete)")
parser.add_argument("-p", "--path", required=True,
help="Directory where the ADIF file will be stored")
parser.add_argument("-a", "--adif", required=True,
help="ADIF file name")
parser.add_argument("--version", action="version", version=f'eqsl.%(prog)s {__version__}')
opts = parser.parse_args()

full_name = Path(opts.path, opts.adif)
full_name = Path(opts.adif).expanduser().absolute()
if full_name.exists():
logging.info('The ADIF file already exists. Sending cards.')
send_cards(full_name, opts.show, opts.keep)

watch_filter = ADIFilter(opts.path, opts.adif)
logging.info('Sendcards watching %s for %s', opts.path, opts.adif)
for changes in watch(opts.path, watch_filter=watch_filter, debounce=3200, recursive=False):
path = full_name.parent
adif_file = full_name.parts[-1]

if not path.exists():
raise FileNotFoundError(f'The directory {path} does not exist')

watch_filter = ADIFilter(path, adif_file)
logging.info('Sendcards watching %s for %s', path, adif_file)
for changes in watch(path, watch_filter=watch_filter, debounce=3200, recursive=False):
for change, filename in changes:
if change == Change.added:
logging.info('Calling send_cards with %s', filename)
send_cards(filename, opts.show, opts.keep)


if __name__ == "__main__":
def main():
try:
main()
except (FileNotFoundError, KeyboardInterrupt) as err:
sendcard()
except FileNotFoundError as err:
logging.info(err)
sys.exit()
except KeyboardInterrupt:
logging.info('Exit "^C" pressed')


if __name__ == "__main__":
main()

0 comments on commit 7be90b1

Please sign in to comment.