This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from storebrand/files
Sync Sharepoint Files
- Loading branch information
Showing
13 changed files
with
1,513 additions
and
886 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Handle CSV files.""" | ||
|
||
import csv | ||
import logging | ||
import re | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class CSVHandler: | ||
"""Handle CSV files.""" | ||
|
||
def __init__(self, textcontent, delimiter=","): | ||
"""Initialize ExcelHandler.""" | ||
self.textcontent = textcontent | ||
self.delimiter = delimiter | ||
|
||
@staticmethod | ||
def format_key(key): | ||
"""Format key.""" | ||
formatted_key = re.sub(r"[^\w\s]", "", key) | ||
formatted_key = re.sub(r"\s+", "_", formatted_key) | ||
return formatted_key.lower() | ||
|
||
def get_dictreader(self): | ||
"""Read CSV file and return csv DictReader object for the file.""" | ||
dr = csv.DictReader( | ||
self.textcontent.splitlines(), | ||
fieldnames=None, | ||
restkey="_sdc_extra", | ||
delimiter=self.delimiter, | ||
) | ||
|
||
dr.fieldnames = [self.format_key(key) for key in dr.fieldnames.copy()] | ||
|
||
return dr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Handle Excel files.""" | ||
|
||
import logging | ||
import re | ||
import tempfile | ||
|
||
import openpyxl | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class ExcelHandler: | ||
"""Handle Excel files.""" | ||
|
||
def __init__(self, textcontent): | ||
"""Initialize ExcelHandler.""" | ||
self.xlsheet = self._load_workbook(textcontent) | ||
|
||
def _load_workbook(self, textcontent): | ||
"""Load workbook from textcontent.""" | ||
with tempfile.NamedTemporaryFile(mode="wb", suffix=".xlsx") as temp: | ||
temp.write(textcontent) | ||
temp.flush() | ||
workbook = openpyxl.load_workbook(temp.name, read_only=True) | ||
worksheets = workbook.worksheets | ||
active_sheet = worksheets[0] | ||
return active_sheet | ||
# self.xlsheet = active_sheet | ||
|
||
def get_row_iterator(self): | ||
"""Return a generator of rows.""" | ||
yield from self.generator_wrapper(self.xlsheet) | ||
|
||
@property | ||
def fieldnames(self): | ||
"""Return fieldnames.""" | ||
return [c.value for c in self.xlsheet[1]] | ||
|
||
@staticmethod | ||
def generator_wrapper(reader): | ||
"""Wrap a reader in a generator.""" | ||
header_row = None | ||
for row in reader: | ||
to_return = {} | ||
if header_row is None: | ||
header_row = row | ||
continue | ||
|
||
for index, cell in enumerate(row): | ||
header_cell = header_row[index] | ||
|
||
formatted_key = header_cell.value | ||
if not formatted_key: | ||
formatted_key = "" # default to empty string for key | ||
|
||
# remove non-word, non-whitespace characters | ||
formatted_key = re.sub(r"[^\w\s]", "", formatted_key) | ||
|
||
# replace whitespace with underscores | ||
formatted_key = re.sub(r"\s+", "_", formatted_key) | ||
|
||
to_return[formatted_key.lower()] = ( | ||
str(cell.value) if cell.value is not None else "" | ||
) | ||
|
||
yield to_return |
Oops, something went wrong.