From aa2129fb4ec2e9fd61dd9f06a2316cf339385730 Mon Sep 17 00:00:00 2001 From: John Miller Date: Wed, 17 Oct 2018 09:28:08 -0400 Subject: [PATCH 1/2] Adds should_sync_field to utils --- singer/__init__.py | 1 + singer/utils.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/singer/__init__.py b/singer/__init__.py index 874ac39..2ad7333 100644 --- a/singer/__init__.py +++ b/singer/__init__.py @@ -7,6 +7,7 @@ strftime, strptime, update_state, + should_sync_field, ) from singer.logger import ( diff --git a/singer/utils.py b/singer/utils.py index bc8d844..8c09a72 100644 --- a/singer/utils.py +++ b/singer/utils.py @@ -195,3 +195,77 @@ def wrapped(*args, **kwargs): raise return wrapped return decorator + + +def should_sync_field(inclusion, selected, default=False): + """ + Returns True if a field should be synced. + + inclusion: automatic|available|unsupported + selected: True|False|None + default: (default: False) True|False + + + "automatic" inclusion always returns True: + + >>> should_sync_field("automatic", None, False) + True + >>> should_sync_field("automatic", True, False) + True + >>> should_sync_field("automatic", False, False) + True + >>> should_sync_field("automatic", None, True) + True + >>> should_sync_field("automatic", True, True) + True + >>> should_sync_field("automatic", False, True) + True + + + "unsupported" inclusion always returns False + + >>> should_sync_field("unsupported", None, False) + False + >>> should_sync_field("unsupported", True, False) + False + >>> should_sync_field("unsupported", False, False) + False + >>> should_sync_field("unsupported", None, True) + False + >>> should_sync_field("unsupported", True, True) + False + >>> should_sync_field("unsupported", False, True) + False + + "available" inclusion uses the selected value when set + >>> should_sync_field("available", True, False) + True + >>> should_sync_field("available", False, False) + False + >>> should_sync_field("available", True, True) + True + >>> should_sync_field("available", False, True) + False + + "available" inclusion uses the default value when selected is None + + >>> should_sync_field("available", None, False) + False + >>> should_sync_field("available", None, True) + True + """ + # always select automatic fields + if inclusion == "automatic": + return True + + # never select unsupported fields + if inclusion == "unsupported": + return False + + # at this point inclusion == "available" + # selected could be None, otherwise use the value of selected + if selected is not None: + return selected + + # if there was no selected value, use the default + return default From 339d2aedca925984127861e474e623b5fa71880e Mon Sep 17 00:00:00 2001 From: John Miller Date: Wed, 17 Oct 2018 09:30:14 -0400 Subject: [PATCH 2/2] whitespace --- singer/utils.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/singer/utils.py b/singer/utils.py index 8c09a72..81944fe 100644 --- a/singer/utils.py +++ b/singer/utils.py @@ -205,9 +205,7 @@ def should_sync_field(inclusion, selected, default=False): selected: True|False|None default: (default: False) True|False - "automatic" inclusion always returns True: - >>> should_sync_field("automatic", None, False) True >>> should_sync_field("automatic", True, False) @@ -223,7 +221,6 @@ def should_sync_field(inclusion, selected, default=False): "unsupported" inclusion always returns False - >>> should_sync_field("unsupported", None, False) False >>> should_sync_field("unsupported", True, False) @@ -248,7 +245,6 @@ def should_sync_field(inclusion, selected, default=False): False "available" inclusion uses the default value when selected is None - >>> should_sync_field("available", None, False) False >>> should_sync_field("available", None, True)