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

Add date type processing #2

Merged
merged 5 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,48 @@ Process several sheets and worksheets in that sheets at a time:
}
```

By default date field define as date with "%Y-%m-%d %H:%M:%S" format. If there is necessary to be changed need to set specific_date_format:

```hocon
{ # config.conf
sheets = [
{
name = "Investor Loans",
worksheets = [
PageA,
PageB,
PageC
],
specific_date_format = "%Y-%m-%d"
},
{
# ...
}
]
}
```

By default date field define as date. If there is necessary to be changed need to set date_processing to False:

```hocon
{ # config.conf
sheets = [
{
name = "Investor Loans",
worksheets = [
PageA,
PageB,
PageC
],
date_processing = False
},
{
# ...
}
]
}
```

Specify the row number (1-based) to start processing from, in case you want to skip some unnecessary rows. The default number is 1.

```hocon
Expand Down
34 changes: 30 additions & 4 deletions tap_gsheets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pyhocon import ConfigFactory
from inflection import parameterize, tableize, underscore
import argparse
from dateutil import parser

LOGGER = singer.get_logger()

Expand All @@ -18,6 +19,8 @@ def sync(config):

# read config
sheets = []
date_processing = True

if 'sheet_name' in config:
# one-sheet single page config
sheet = {
Expand All @@ -42,18 +45,24 @@ def sync(config):
else:
worksheets = []

if "specific_date_format" in sheet:
gsheet_loader.specific_date_format = sheet["specific_date_format"]

if 'date_processing' in sheet and not sheet['date_processing']:
date_processing = False

# noinspection PyBroadException
try:
if len(worksheets) > 0:
for worksheet in worksheets:
process_worksheet(gsheets_loader, sheet_name, worksheet, start_from_row, config)
process_worksheet(gsheets_loader, sheet_name, worksheet, start_from_row, config, date_processing)
else:
process_worksheet(gsheets_loader, sheet_name, None, start_from_row, config)
process_worksheet(gsheets_loader, sheet_name, None, start_from_row, config, None)
except Exception as e:
LOGGER.error(f"Can't process a worksheet {sheet_name} because of:\n{e}", )


def process_worksheet(gsheets_loader, sheet_name, worksheet, start_from_row, config):
def process_worksheet(gsheets_loader, sheet_name, worksheet, start_from_row, config, date_processing):
if worksheet is None:
name_with_worksheet = sheet_name
else:
Expand All @@ -64,9 +73,13 @@ def process_worksheet(gsheets_loader, sheet_name, worksheet, start_from_row, con
else:
stream_name = tableize(parameterize(name_with_worksheet))

records = gsheets_loader.get_records_as_json(sheet_name, worksheet, start_from_row)

if date_processing:
run_date_processing(records)

schema = gsheets_loader.get_schema(sheet_name, worksheet, start_from_row)

records = gsheets_loader.get_records_as_json(sheet_name, worksheet, start_from_row)

# additional data transformations
column_mapping = None
Expand Down Expand Up @@ -100,6 +113,19 @@ def process_worksheet(gsheets_loader, sheet_name, worksheet, start_from_row, con
singer.write_record(stream_name, record_transformed)


def run_date_processing(records):
for record in records:
counter = 0
for field in record:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, basically this is looping through every record and every field trying to parse the date, right?
How many records we have in there?

I am just a bit concerned, because this could be quite impactful in the performance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative, it should be possible to look for a date column using only the first record. Would that work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll think about it. i believe that's this way incorrect. All date fields will be transformed to date_format. need to change whole logic

try:
d = parser.parse(record[field])
record[field] = (d.replace(tzinfo=None) + d.utcoffset()).strftime(gsheet_loader.specific_date_format)
except (TypeError, ValueError) as exception:
counter += 1
pass
if len(record) == counter:
break

def main():

# parse arguments. get config file path.
Expand Down
44 changes: 41 additions & 3 deletions tap_gsheets/gsheet_loader.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import gspread
from gspread.utils import numericise_all
from genson import SchemaBuilder
from genson import SchemaBuilder, TypedSchemaStrategy
from singer.schema import Schema
from oauth2client.service_account import ServiceAccountCredentials
import logging
import datetime

logging.getLogger('oauth2client').setLevel(logging.ERROR)

specific_date_format = "%Y-%m-%d %H:%M:%S"

class GSheetsLoader:
"""Wrapper for authenticating and retrieving data from Google Sheets"""
Expand Down Expand Up @@ -52,7 +53,7 @@ def get_schema(self, sheet_name, worksheet_name, start_from_row):
self.get_data(sheet_name, worksheet_name, start_from_row)

# add object to schema builder so he can infer schema
builder = SchemaBuilder()
builder = CustomSchemaBuilder()
if len(self.data[worksheet_name]) == 0:
# build sample record to be used for schema inference if the
# spreadsheet is empty
Expand All @@ -67,3 +68,40 @@ def get_schema(self, sheet_name, worksheet_name, start_from_row):
self.schema[worksheet_name] = singer_schema.to_dict()

return self.schema[worksheet_name]


class CustomDateTime(TypedSchemaStrategy):
"""
strategy for date-time formatted strings
"""
JS_TYPE = 'string'
PYTHON_TYPE = (str, type(u''))

# create a new instance variable
def __init__(self, node_class):
super().__init__(node_class)
self.format = "date-time"
self.timestamp = None

@classmethod
def match_object(self, obj):
super().match_object(obj)
try:
date_time_obj = datetime.datetime.strptime(obj, "{}".format(specific_date_format))
if isinstance(date_time_obj, datetime.datetime):
return True
else:
return False
except (TypeError, ValueError) as exception:
#print(exception)
return False

def to_schema(self):
schema = super().to_schema()
schema['type'] = self.JS_TYPE
schema['format'] = self.format
return schema

class CustomSchemaBuilder(SchemaBuilder):
""" detects & labels date-time formatted strings """
EXTRA_STRATEGIES = (CustomDateTime,)