aboutsummaryrefslogtreecommitdiff
path: root/datamaps
diff options
context:
space:
mode:
authorMatthew Lemon <matt@matthewlemon.com>2022-06-21 22:56:25 +0100
committerMatthew Lemon <matt@matthewlemon.com>2022-06-21 22:56:25 +0100
commitb619d5be7de5e0c5a5e250fc8b7db6e7e4ccf05d (patch)
treecbdcdc4b33ae5ee6bb1826f108e6a88866db5cb1 /datamaps
parenthousekeeping - adding some pytest config and other stuff (diff)
implemented --template flag - for testing
Diffstat (limited to 'datamaps')
-rw-r--r--datamaps/main.py50
-rw-r--r--datamaps/tests/resources/datamap_short.csv3
-rw-r--r--datamaps/tests/test_cli.py17
3 files changed, 47 insertions, 23 deletions
diff --git a/datamaps/main.py b/datamaps/main.py
index 6029ae9..36e868a 100644
--- a/datamaps/main.py
+++ b/datamaps/main.py
@@ -17,27 +17,24 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE. """
-import click
import logging
import sys
-from click import version_option
from functools import partial
from pathlib import Path
-from datamaps import __version__
+import click
+from click import version_option
from engine.adapters import cli as engine_cli
from engine.config import Config as engine_config
-from engine.exceptions import (
- DatamapFileEncodingError,
- MalFormedCSVHeaderException,
- MissingCellKeyError,
- MissingLineError,
- MissingSheetFieldError,
- NoApplicableSheetsInTemplateFiles,
- RemoveFileWithNoSheetRequiredByDatamap,
- DatamapNotCSVException,
- NestedZipError
-)
+from engine.exceptions import (DatamapFileEncodingError,
+ DatamapNotCSVException,
+ MalFormedCSVHeaderException,
+ MissingCellKeyError, MissingLineError,
+ MissingSheetFieldError, NestedZipError,
+ NoApplicableSheetsInTemplateFiles,
+ RemoveFileWithNoSheetRequiredByDatamap)
+
+from datamaps import __version__
logging.basicConfig(
level=logging.INFO,
@@ -324,9 +321,12 @@ def templates(to_master, datamap, zipinput, rowlimit, inputdir, validationonly):
# @click.argument("blank")
@click.argument("master", metavar="MASTER_FILE_PATH")
@click.option(
- "--datamap", "-d", help="Path to datamap (CSV) file.", metavar="CSV_FILE_PATH"
+ "--datamap", "-d", help="Path to datamap (CSV) file.", type=Path, metavar="CSV_FILE_PATH"
+)
+@click.option(
+ "--template", "-t", help="Path to blank template (spreadsheet) file.", type=Path, metavar="TEMPLATE_FILE_PATH"
)
-def master(master, datamap):
+def master(master, datamap, template):
"""Export data from a Master file.
Export data from a master file (at MASTER_FILE_PATH). A new populated template
@@ -338,18 +338,26 @@ def master(master, datamap):
blank_fn = engine_config.config_parser["DEFAULT"]["blank file name"]
if datamap:
- datamap_fn = datamap
+ if not datamap.is_absolute():
+ datamap_pth = Path.cwd() / datamap
+ else:
+ datamap_pth = datamap
+ else:
+ datamap_pth = input_dir / engine_config.config_parser["DEFAULT"]["datamap file name"]
+ if template:
+ if not template.is_absolute():
+ blank = Path.cwd() / template
+ else:
+ blank = template
else:
- datamap_fn = engine_config.config_parser["DEFAULT"]["datamap file name"]
+ blank = input_dir / blank_fn
- blank = input_dir / blank_fn
- datamap = input_dir / datamap_fn
# click.secho(f"EXPORTING master {master} to templates based on {blank}...")
be_logger.info(f"Exporting master {master} to templates based on {blank}.")
try:
- engine_cli.write_master_to_templates(blank, datamap, master)
+ engine_cli.write_master_to_templates(blank, datamap_pth, master)
except (FileNotFoundError, RuntimeError) as e:
logger.critical(str(e))
sys.exit(1)
diff --git a/datamaps/tests/resources/datamap_short.csv b/datamaps/tests/resources/datamap_short.csv
new file mode 100644
index 0000000..8b17852
--- /dev/null
+++ b/datamaps/tests/resources/datamap_short.csv
@@ -0,0 +1,3 @@
+cell_key,template_sheet,cellreference,type
+Project/Programme Name,Introduction,C11,TEXT
+Department,INTRODUCTION,C9,TEXT
diff --git a/datamaps/tests/test_cli.py b/datamaps/tests/test_cli.py
index abad923..d7310d6 100644
--- a/datamaps/tests/test_cli.py
+++ b/datamaps/tests/test_cli.py
@@ -2,11 +2,10 @@ import logging
import os
import shutil
from pathlib import Path
+from sys import platform
import pytest
from click.testing import CliRunner
-from sys import platform
-
from datamaps.main import _import, export
@@ -139,7 +138,21 @@ def test_export_with_alternative_datamap(mock_config, resource_dir, caplog):
x[2] for x in caplog.record_tuples
]
+def test_export_with_flag_template(mock_config, resource_dir):
+ runner = CliRunner()
+ mock_config.initialise()
+ _copy_resources_to_input(mock_config, resource_dir)
+ _master_file = os.path.join(mock_config.PLATFORM_DOCS_DIR, "input", "master.xlsx")
+ _dm_file = os.path.join(mock_config.PLATFORM_DOCS_DIR, "input", "datamap_short.csv")
+ result = runner.invoke(
+ export, ["master", _master_file, "-d", _dm_file, "-t", "/tmp/Documents/datamaps/input/blank_template.xlsm"]
+ )
+ # assert result.exit_code == 0
+ output = mock_config.PLATFORM_DOCS_DIR / "output" / "Chutney Bridge.xlsm"
+ assert output.is_file()
+
+@pytest.mark.skip("TODO")
def test_export_with_alternative_datamap_not_csv(mock_config, resource_dir, caplog):
runner = CliRunner()
mock_config.initialise()