From 0b1a8f9617ed264ce4f23e09c21d2c2582ac043d Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 29 Jul 2019 15:33:05 -0500 Subject: [PATCH] Add basic `crepes import-csv` command, for CSV -> CORE import --- rattail_corepos/corepos/commands.py | 32 ++++++++++++++ rattail_corepos/corepos/importing/csv.py | 53 ++++++++++++++++++++++++ setup.py | 1 + 3 files changed, 86 insertions(+) create mode 100644 rattail_corepos/corepos/importing/csv.py diff --git a/rattail_corepos/corepos/commands.py b/rattail_corepos/corepos/commands.py index b38b905..5008df6 100644 --- a/rattail_corepos/corepos/commands.py +++ b/rattail_corepos/corepos/commands.py @@ -147,3 +147,35 @@ class ImportCore(ImportToCore): if 'args' in kwargs: kwargs['dbkey'] = kwargs['args'].dbkey return kwargs + + +class ImportCSV(commands.ImportFileSubcommand): + """ + Import data from CSV file(s) to CORE database + """ + name = 'import-csv' + description = __doc__.strip() + default_handler_spec = 'rattail_corepos.corepos.importing.csv:FromCSVToCore' + + def add_parser_args(self, parser): + super(ImportCSV, self).add_parser_args(parser) + parser.add_argument('--dbkey', metavar='KEY', default='default', + help="Config key for database engine to be used as the \"target\" " + "CORE DB, i.e. where data will be imported *to*. This key must be " + "defined in the [rattail_corepos.db] section of your config file.") + + def get_handler_kwargs(self, **kwargs): + kwargs = super(ImportCSV, self).get_handler_kwargs(**kwargs) + if 'args' in kwargs: + args = kwargs['args'] + kwargs['dbkey'] = args.dbkey + return kwargs + + def get_handler_factory(self, **kwargs): + if self.config: + spec = self.config.get('rattail_corepos.importing', 'csv.handler', + default=self.default_handler_spec) + else: + # just use default, for sake of cmd line help + spec = self.default_handler_spec + return load_object(spec) diff --git a/rattail_corepos/corepos/importing/csv.py b/rattail_corepos/corepos/importing/csv.py new file mode 100644 index 0000000..44703fe --- /dev/null +++ b/rattail_corepos/corepos/importing/csv.py @@ -0,0 +1,53 @@ +# -*- coding: utf-8; -*- +################################################################################ +# +# Rattail -- Retail Software Framework +# Copyright © 2010-2019 Lance Edgar +# +# This file is part of Rattail. +# +# Rattail is free software: you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation, either version 3 of the License, or (at your option) any later +# version. +# +# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# Rattail. If not, see . +# +################################################################################ +""" +CSV -> CORE data import +""" + +from __future__ import unicode_literals, absolute_import + +from corepos.db import model as corepos, Session as CoreSession + +from rattail.importing.handlers import FromFileHandler +from rattail.importing.csv import FromCSVToSQLAlchemyMixin +from rattail_corepos.corepos.importing.model import ToCore +from rattail_corepos.corepos.importing.corepos import ToCoreHandler + + +class FromCSVToCore(FromCSVToSQLAlchemyMixin, FromFileHandler, ToCoreHandler): + """ + Handler for CSV -> CORE data import + """ + host_title = "CSV" + local_title = "CORE" + ToParent = ToCore + + @property + def local_title(self): + return "CORE ({})".format(self.dbkey) + + def get_model(self): + return corepos + + def make_session(self): + return CoreSession(bind=self.config.corepos_engines[self.dbkey]) diff --git a/setup.py b/setup.py index 0cbd5a6..1d263a1 100644 --- a/setup.py +++ b/setup.py @@ -107,6 +107,7 @@ setup( 'export-core = rattail_corepos.corepos.commands:ExportCore', 'export-csv = rattail_corepos.corepos.commands:ExportCSV', 'import-core = rattail_corepos.corepos.commands:ImportCore', + 'import-csv = rattail_corepos.corepos.commands:ImportCSV', ], 'rattail.config.extensions': [