Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the config option projects is empty, do not omit any entries. #8

Merged
merged 5 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Change log

- Fetch all projects, not just the first 100.

- If the config option ``projects`` is empty, do not omit any entries.
icemac marked this conversation as resolved.
Show resolved Hide resolved

- Improve formatting to have always a precision of 2 for floats.

- No longer ignore entries with negative time.
8 changes: 4 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ should be okay.
`ticklog`: this file is used to write a line for each action which is done via
the Tick API. When using `--dry-run`, this file is also filled.

`projects` option should list all project prefixes. These prefixes will be used
to identify tick projects. If the script does not find anything that looks like
a tick project, it will skip that entry.

`projects` option should list all project prefixes for upload. These prefixes
will be used to identify tick projects. If the script for an entry does not
find a matching tick project, it will skip that entry. **Caution:** This option
can be empty or omitted to upload all projects.

TODO
====
Expand Down
19 changes: 7 additions & 12 deletions gtimelog2tick.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def read_config(config_file: pathlib.Path) -> dict:
email = config['gtimelog2tick'].get('email')
timelog = config['gtimelog2tick'].get('timelog')
ticklog = config['gtimelog2tick'].get('ticklog')
requested_projects = config['gtimelog2tick'].get('projects')
requested_projects = config['gtimelog2tick'].get('projects', '')
midnight = config['gtimelog'].get('virtual_midnight', '06:00')

if not subscription_id:
Expand All @@ -108,13 +108,6 @@ def read_config(config_file: pathlib.Path) -> dict:
"Your email address is not specified, set it via the"
" gtimelog2tick.email setting.")

if not requested_projects:
raise ConfigurationError(
"The list of projects is not specified, set them via the"
" gtimelog2tick.projects setting. Use the first letters of the"
" projects relevant for you from the projects page on"
" tickspot.com.")

requested_projects = set(requested_projects.split())

if not timelog:
Expand Down Expand Up @@ -266,10 +259,12 @@ def parse_timelog(
# Skip all non-work related entries.
if entry.message.endswith('**'):
continue
# Skip all lines which do not match the requested projects.
if not any(entry.message.startswith(x)
for x in config['requested_projects']):
continue
# Skip all lines which do not match the requested projects if requested
# projects are specified
if config['requested_projects']:
if not any(entry.message.startswith(x)
for x in config['requested_projects']):
continue

task, text, task_id = parse_entry_message(config, entry.message)
worklog = WorkLog(entry, text, task, task_id)
Expand Down
56 changes: 33 additions & 23 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import gtimelog2tick


def test_parse_timelog():
def test_gtimelog2tick__parse_timelog__1():
"""It omits entries which do not match the requested projects."""
entries = [
gtimelog2tick.Entry(datetime.datetime(2014, 3, 31, 14, 48),
datetime.datetime(2014, 3, 31, 17, 10),
Expand Down Expand Up @@ -46,6 +47,33 @@ def test_parse_timelog():
]


def test_gtimelog2tick__parse_timelog__2():
"""It omits no entries if requested projects is empty."""
entries = [
gtimelog2tick.Entry(datetime.datetime(2014, 3, 31, 14, 48),
datetime.datetime(2014, 3, 31, 17, 10),
'proj2: maint: work'),
gtimelog2tick.Entry(datetime.datetime(2014, 3, 31, 17, 48),
datetime.datetime(2014, 3, 31, 18, 10),
'proj3: dev: other'),
]
config = {
'requested_projects': [],
'tick_projects': [
gtimelog2tick.Project('proj2', 42, [
gtimelog2tick.Task('maintenance', 1),
]),
gtimelog2tick.Project('proj3', 43, [
gtimelog2tick.Task('development', 5),
])
]
}
assert list(gtimelog2tick.parse_timelog(config, entries)) == [
gtimelog2tick.WorkLog(entries[0], 'work', 'proj2: maintenance', 1),
gtimelog2tick.WorkLog(entries[-1], 'other', 'proj3: development', 5),
]


class Route:

def __init__(self, handler, params=None):
Expand Down Expand Up @@ -413,7 +441,7 @@ def test_no_args(env, mocker):
]


def test_gtimelog2tick__parse_timelog__1(env, mocker):
def test_gtimelog2tick__parse_timelog__3(env, mocker):
"""It raises a DataError for an entry with negative time."""
mocker.patch('gtimelog2tick.get_now',
return_value=datetime.datetime(2023, 12, 7).astimezone())
Expand All @@ -430,7 +458,7 @@ def test_gtimelog2tick__parse_timelog__1(env, mocker):
err.match(r'Negative hours: WorkLog\(.*')


def test_gtimelog2tick__parse_timelog__2(env, mocker):
def test_gtimelog2tick__parse_timelog__4(env, mocker):
"""It it ignores entries with zero minutes duration."""
mocker.patch('gtimelog2tick.get_now',
return_value=datetime.datetime(2023, 12, 7).astimezone())
Expand Down Expand Up @@ -747,22 +775,6 @@ def test_gtimelog2tick__read_config__7(tmpdir):
' gtimelog2tick.email setting.')


def test_gtimelog2tick__read_config__8(tmpdir):
"""It renders an exception if projects is missing."""
path = pathlib.Path(tmpdir) / 'config.ini'
path.write_text(textwrap.dedent("""\
[gtimelog]
[gtimelog2tick]
subscription_id = 123
token = <TOKEN>
user_id = 456
email = [email protected]"""))
with pytest.raises(gtimelog2tick.ConfigurationError) as err:
gtimelog2tick.read_config(path)
assert err.match('The list of projects is not specified, set them via the'
' gtimelog2tick.projects setting.')


def test_gtimelog2tick__read_config__9(tmpdir):
"""It renders an exception if timelog file does not exist."""
path = pathlib.Path(tmpdir) / 'config.ini'
Expand All @@ -772,8 +784,7 @@ def test_gtimelog2tick__read_config__9(tmpdir):
subscription_id = 123
token = <TOKEN>
user_id = 456
email = [email protected]
projects = FOO BAR"""))
email = [email protected]"""))
with pytest.raises(gtimelog2tick.ConfigurationError) as err:
gtimelog2tick.read_config(path)
assert err.match('Timelog file .*/timelog.txt does not exist.')
Expand All @@ -788,8 +799,7 @@ def test_gtimelog2tick__read_config__10(tmpdir):
subscription_id = 123
token = <TOKEN>
user_id = 456
email = [email protected]
projects = FOO BAR"""))
email = [email protected]"""))
(pathlib.Path(tmpdir) / 'timelog.txt').touch()
ticklog = pathlib.Path(tmpdir) / 'ticklog.txt'
ticklog.touch()
Expand Down