Skip to content

Commit

Permalink
Two bugfixes and some polish. (#3)
Browse files Browse the repository at this point in the history
* Copyright of contributed material belongs to contributor.

* The text in (e.g.) a SOAPBOX: line can contain another ":".

* The last character of a good ADIF log should be a newline.

* Merge multiple OPERATOR: lines.

* Between START-OF-LOG and END-OF-LOG, other tags can be in any order.

* Make things easier for contributors and others who want to run the tests.

* Bump version to 0.0.4.
  • Loading branch information
aknrdureegaesr authored and thxo committed Nov 23, 2019
1 parent c29acb1 commit 9151d55
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 18 deletions.
1 change: 0 additions & 1 deletion .build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ environment:
tasks:
- pytest: |
cd ${project}
export PYTHONPATH=$PYTHONPATH:$(pwd)
sudo pip3 install -r requirements_test.txt
py.test --cov-report term-missing --cov cabrillo -v
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ ipython_config.py

# pyenv
.python-version
# Virtual env as proposed in README
python-venv

# celery beat schedule file
celerybeat-schedule
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ python:
- "3.6"
cache: pip
install:
- "export PYTHONPATH=$PYTHONPATH:$(pwd)"
- pip install -r requirements_test.txt
script:
- py.test --cov-report term-missing --cov cabrillo -v
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2018 Howard Xiao
Copyright 2018-2019 Howard Xiao and contributors.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,25 @@ class Cabrillo(builtins.object)
| dx_exch: Received exchange incl. RST. List of each component.
| t: Transmitter ID for multi-transmitter categories in int. 0/1.
```

## Contributors

Pull requests are appreciated!

The following instructions show how to obtain the sourcecode and execute the tests.
They assume Python 3.3 or later.

For Posix plattforms (which includes Mac and Linux):

```
git clone https://github.com/thxo/cabrillo.git
cd cabrillo
python3 -m venv python-venv
source python-venv/bin/activate
pip install -r requirements_test.txt
py.test --cov-report term-missing --cov cabrillo -v
```

On a Windows machine, using `cmd.exe`, substitute
`python-venv/Scripts/activate` for
`source python-venv/bin/activate`.
15 changes: 9 additions & 6 deletions cabrillo/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from cabrillo.errors import InvalidQSOException, InvalidLogException
from cabrillo.data import KEYWORD_MAP

import re

def parse_qso(text):
"""Parse a single line of QSO into a QSO object.
Expand Down Expand Up @@ -74,12 +75,14 @@ def parse_log_text(text, ignore_unknown_key=False, check_categories=True):
results = dict()
results['x_anything'] = dict()

key_colon_value = re.compile(r'^\s*([^:]+?)\s*:\s*(.*?)\s*$')
for line in text.split('\n'):
try:
key, value = [x.replace('\r', '').strip() for x in line.split(':')]
except ValueError:
raise InvalidLogException('Line not delimited by `:`, '
'got {}.'.format(line))
match = key_colon_value.fullmatch(line)
if match:
key, value = match.group(1), match.group(2)
else:
raise InvalidLogException('Line does not start with `:`-delimited key, '
'got `{}`.'.format(line))

if key == 'END-OF-LOG':
break
Expand All @@ -98,7 +101,7 @@ def parse_log_text(text, ignore_unknown_key=False, check_categories=True):
results.setdefault(inverse_keywords[key], list()).append(
parse_qso(value))
elif key == 'OPERATORS':
results[inverse_keywords[key]] = value.replace(',', ' ').split()
results.setdefault(inverse_keywords[key], list()).extend(value.replace(',', ' ').split())
elif key in ['ADDRESS', 'SOAPBOX']:
results.setdefault(inverse_keywords[key], list()).append(value)
elif key in inverse_keywords.keys():
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="cabrillo",
version="0.0.3",
version="0.0.4",
author="Howard Xiao",
author_email="[email protected]",
description="A Python library to parse Cabrillo-format amateur radio "
Expand Down
3 changes: 2 additions & 1 deletion tests/CQWPX.log
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ADDRESS-COUNTRY: USA
OPERATORS: AA1XXX AA2XXX AA3XXX
SOAPBOX: Put your comments here.
SOAPBOX: Use multiple lines if needed.
SOAPBOX: Once you said "SOAPBOX:", the rest of the line is free-form.
QSO: 7005 CW 2009-05-30 0002 AA1ZZZ 599 1 S50A 599 4
QSO: 7006 CW 2009-05-30 0015 AA1ZZZ 599 2 EF8M 599 34
END-OF-LOG:
END-OF-LOG:
14 changes: 9 additions & 5 deletions tests/CQWPX_bad_style.log
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
START-OF-LOG: 3.0
QSO: 7005 CW 2009-05-30 0002 AA1ZZZ 599 1 S50A 599 4
CALLSIGN: AA1ZZZ
CONTEST:CQ-WPX-CW
CATEGORY-OPERATOR: SINGLE-OP
Expand All @@ -17,12 +18,15 @@ ADDRESS: 11 Hollis Street
ADDRESS-CITY: Uxbridge
ADDRESS-STATE-PROVINCE: MA
ADDRESS-POSTALCODE: 01569
ADDRESS-COUNTRY: USA
OPERATORS: AA1XXX, AA2XXX, AA3XXX,
SOAPBOX: Put your comments here.
SOAPBOX: Use multiple lines if needed.
QSO: 7005 CW 2009-05-30 0002 AA1ZZZ 599 1 S50A 599 4
QSO: 7006 CW 2009-05-30 0015 AA1ZZZ 599 2 EF8M 599 34
ADDRESS-COUNTRY: USA
OPERATORS: AA1XXX, AA2XXX,
OPERATORS:
OPERATORS:
OPERATORS: AA3XXX,
SOAPBOX: Put your comments here.
SOAPBOX: Use multiple lines if needed.
SOAPBOX: Once you said "SOAPBOX:", the rest of the line is free-form.
END-OF-LOG:


9 changes: 9 additions & 0 deletions tests/path_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import sys
import os.path

"""Make the cabrillo module importable from the tests' point of view."""

project_root_dir = os.path.dirname(os.path.dirname(__file__))

if not project_root_dir in sys.path:
sys.path.append(project_root_dir)
2 changes: 2 additions & 0 deletions tests/test_cabrillo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import pytest

import path_helper

from cabrillo import Cabrillo, QSO
from cabrillo.data import VALID_CATEGORIES_MAP
from cabrillo.errors import InvalidLogException
Expand Down
7 changes: 5 additions & 2 deletions tests/test_parser_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

import path_helper

from cabrillo import QSO
from cabrillo.errors import InvalidLogException
from cabrillo.parser import parse_log_file, parse_log_text
Expand Down Expand Up @@ -35,10 +37,11 @@ def test_parse_cqwpx():
assert cab.address_country == 'USA'
assert cab.operators == ['AA1XXX', 'AA2XXX', 'AA3XXX']
assert cab.soapbox == ['Put your comments here.',
'Use multiple lines if needed.']
'Use multiple lines if needed.',
'Once you said "SOAPBOX:", the rest of the line is free-form.']

out_lines = cab.write_text().split('\n')
correct_lines = open('tests/CQWPX.log').read().split('\n')
correct_lines = open('tests/CQWPX.log').read().strip().split('\n')
assert len(out_lines) == len(correct_lines) and sorted(
out_lines) == sorted(correct_lines)

Expand Down
1 change: 1 addition & 0 deletions tests/test_parser_qso.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime

import pytest
import path_helper

from cabrillo.errors import InvalidQSOException
from cabrillo.parser import parse_qso
Expand Down
2 changes: 2 additions & 0 deletions tests/test_qso.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

import path_helper

from cabrillo import QSO
from cabrillo.qso import frequency_to_band
from cabrillo.errors import InvalidQSOException
Expand Down

0 comments on commit 9151d55

Please sign in to comment.