From 8752ac03b4acc461bd9156c2b2107635591404e9 Mon Sep 17 00:00:00 2001 From: "tfr@openerp.com" <> Date: Thu, 12 May 2011 10:49:08 +0200 Subject: [ADD] import framework bzr revid: tfr@openerp.com-20110512084908-j3o9lsrja52ybflf --- addons/import_base/mapper.py | 167 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 addons/import_base/mapper.py (limited to 'addons/import_base/mapper.py') diff --git a/addons/import_base/mapper.py b/addons/import_base/mapper.py new file mode 100644 index 00000000000..582a45ee4fb --- /dev/null +++ b/addons/import_base/mapper.py @@ -0,0 +1,167 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2004-2010 Tiny SPRL (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program 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 Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## + + +class mapper(object): + """ + super class for all mapper class + They are call before import data + to transform the mapping into real value that we + will import + + the call function receive a dictionary with external data + 'external_field' : value + """ + def __call__(self, external_values): + raise NotImplementedError() + +class dbmapper(mapper): + """ + Super class for mapper that need to access to + data base or any function of the import_framework + + self.parent contains a reference to the instance of + the import framework + """ + def set_parent(self, parent): + self.parent = parent + + +class concat(mapper): + """ + Use : contact('field_name1', 'field_name2', delimiter='_') + concat value of fields using the delimiter, delimiter is optional + and by default is a space + + """ + def __init__(self, *arg, **delimiter): + self.arg = arg + self.delimiter = delimiter and delimiter.get('delimiter', ' ') or ' ' + + def __call__(self, external_values): + return self.delimiter.join(map(lambda x : external_values.get(x,''), self.arg)) + +class ppconcat(mapper): + """ + Use : contact('field_name1', 'field_name2', delimiter='_') + concat external field name and value of fields using the delimiter, + delimiter is optional and by default is a two line feeds + + """ + def __init__(self, *arg, **delimiter): + self.arg = arg + self.delimiter = delimiter and delimiter.get('delimiter', ' ') or '\n\n' + + def __call__(self, external_values): + return self.delimiter.join(map(lambda x : x + ": " + external_values.get(x,''), self.arg)) + +class const(mapper): + """ + Use : const(arg) + return always arg + """ + def __init__(self, val): + self.val = val + + def __call__(self, external_values): + return self.val + +class value(mapper): + """ + Use : value(external_field_name) + Return the value of the external field name + this is equivalent to the a single string + + usefull for call if you want your call get the value + and don't care about the name of the field + call(self.method, value('field1')) + """ + def __init__(self, val, default=''): + self.val = val + self.default = default + + def __call__(self, external_values): + return external_values.get(self.val, self.default) + +class map_val(mapper): + """ + Use : map_val(external_field, val_mapping) + where val_mapping is a dictionary + with external_val : openerp_val + + usefull for selection field like state + to map value + """ + def __init__(self, val, map, default='draft'): + self.val = value(val) + self.map = map + self.default = default + + def __call__(self, external_values): + return self.map.get(self.val(external_values), self.default) + +class ref(dbmapper): + """ + Use : ref(table_name, external_id) + return the xml_id of the ressource + + to associate an already imported object with the current object + """ + def __init__(self, table, field_name): + self.table = table + self.field_name = field_name + + def __call__(self, external_values): + return self.parent.xml_id_exist(self.table, external_values.get(self.field_name)) + +class refbyname(dbmapper): + """ + Use : refbyname(table_name, external_name, res.model) + same as ref but use the name of the ressource to find it + """ + def __init__(self, table, field_name, model): + self.table = table + self.field_name = field_name + self.model = model + + def __call__(self, external_values): + v = external_values.get(self.field_name, '') + return self.parent.name_exist(self.table, v , self.model) + +class call(mapper): + """ + Use : call(function, arg1, arg2) + to call the function with external val follow by the arg specified + """ + def __init__(self, fun, *arg): + self.fun = fun + self.arg = arg + + def __call__(self, external_values): + args = [] + for arg in self.arg: + if isinstance(arg, mapper): + args.append(arg(external_values)) + else: + args.append(arg) + return self.fun(external_values, *args) + + \ No newline at end of file -- cgit v1.2.3 From 26f64c51f3e5534a827b80c345ba702349c1aee3 Mon Sep 17 00:00:00 2001 From: "tfr@openerp.com" <> Date: Fri, 24 Jun 2011 18:11:17 +0200 Subject: [IMP] review of contact import bzr revid: tfr@openerp.com-20110624161117-6n2qzn3u2iix1pvb --- addons/import_base/mapper.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'addons/import_base/mapper.py') diff --git a/addons/import_base/mapper.py b/addons/import_base/mapper.py index 582a45ee4fb..7f97ed0b615 100644 --- a/addons/import_base/mapper.py +++ b/addons/import_base/mapper.py @@ -94,12 +94,18 @@ class value(mapper): and don't care about the name of the field call(self.method, value('field1')) """ - def __init__(self, val, default=''): + def __init__(self, val, default='', fallback=False): self.val = val self.default = default + self.fallback = fallback def __call__(self, external_values): - return external_values.get(self.val, self.default) + val = external_values.get(self.val, self.default) + if self.fallback and (not val or val == self.default): + val = external_values.get(self.fallback, self.default) + return val + + class map_val(mapper): """ -- cgit v1.2.3