You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import tools
  22. class mapper(object):
  23. """
  24. super class for all mapper class
  25. They are call before import data
  26. to transform the mapping into real value that we
  27. will import
  28. the call function receive a dictionary with external data
  29. 'external_field' : value
  30. """
  31. def __call__(self, external_values):
  32. raise NotImplementedError()
  33. class dbmapper(mapper):
  34. """
  35. Super class for mapper that need to access to
  36. data base or any function of the import_framework
  37. self.parent contains a reference to the instance of
  38. the import framework
  39. """
  40. def set_parent(self, parent):
  41. self.parent = parent
  42. class concat(mapper):
  43. """
  44. Use : contact('field_name1', 'field_name2', delimiter='_')
  45. concat value of fields using the delimiter, delimiter is optional
  46. and by default is a space
  47. """
  48. def __init__(self, *arg, **delimiter):
  49. self.arg = arg
  50. self.delimiter = delimiter and delimiter.get('delimiter', ' ') or ' '
  51. def __call__(self, external_values):
  52. return self.delimiter.join(map(lambda x : tools.ustr(external_values.get(x,'')), self.arg))
  53. class ppconcat(mapper):
  54. """
  55. Use : contact('field_name1', 'field_name2', delimiter='_')
  56. concat external field name and value of fields using the delimiter,
  57. delimiter is optional and by default is a two line feeds
  58. """
  59. def __init__(self, *arg, **delimiter):
  60. self.arg = arg
  61. self.delimiter = delimiter and delimiter.get('delimiter', ' ') or '\n\n'
  62. def __call__(self, external_values):
  63. return self.delimiter.join(map(lambda x : x + ": " + tools.ustr(external_values.get(x,'')), self.arg))
  64. class const(mapper):
  65. """
  66. Use : const(arg)
  67. return always arg
  68. """
  69. def __init__(self, val):
  70. self.val = val
  71. def __call__(self, external_values):
  72. return self.val
  73. class value(mapper):
  74. """
  75. Use : value(external_field_name)
  76. Return the value of the external field name
  77. this is equivalent to the a single string
  78. usefull for call if you want your call get the value
  79. and don't care about the name of the field
  80. call(self.method, value('field1'))
  81. """
  82. def __init__(self, val, default='', fallback=False):
  83. self.val = val
  84. self.default = default
  85. self.fallback = fallback
  86. def __call__(self, external_values):
  87. val = external_values.get(self.val, self.default)
  88. if self.fallback and (not val or val == self.default):
  89. val = external_values.get(self.fallback, self.default)
  90. return val
  91. class map_val(mapper):
  92. """
  93. Use : map_val(external_field, val_mapping)
  94. where val_mapping is a dictionary
  95. with external_val : openerp_val
  96. usefull for selection field like state
  97. to map value
  98. """
  99. def __init__(self, val, map, default='draft'):
  100. self.val = value(val)
  101. self.map = map
  102. self.default = default
  103. def __call__(self, external_values):
  104. return self.map.get(self.val(external_values), self.default)
  105. class ref(dbmapper):
  106. """
  107. Use : ref(table_name, external_id)
  108. return the xml_id of the ressource
  109. to associate an already imported object with the current object
  110. """
  111. def __init__(self, table, field_name):
  112. self.table = table
  113. self.field_name = field_name
  114. def __call__(self, external_values):
  115. return self.parent.xml_id_exist(self.table, external_values.get(self.field_name))
  116. class refbyname(dbmapper):
  117. """
  118. Use : refbyname(table_name, external_name, res.model)
  119. same as ref but use the name of the ressource to find it
  120. """
  121. def __init__(self, table, field_name, model):
  122. self.table = table
  123. self.field_name = field_name
  124. self.model = model
  125. def __call__(self, external_values):
  126. v = external_values.get(self.field_name, '')
  127. return self.parent.name_exist(self.table, v , self.model)
  128. class call(mapper):
  129. """
  130. Use : call(function, arg1, arg2)
  131. to call the function with external val follow by the arg specified
  132. """
  133. def __init__(self, fun, *arg):
  134. self.fun = fun
  135. self.arg = arg
  136. def __call__(self, external_values):
  137. args = []
  138. for arg in self.arg:
  139. if isinstance(arg, mapper):
  140. args.append(arg(external_values))
  141. else:
  142. args.append(arg)
  143. return self.fun(external_values, *args)
  144. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
  145. def val(field_name):
  146. def val_fun(data_line):
  147. return data_line[field_name]
  148. return val_fun
  149. def const(const):
  150. def val_fun(data_line):
  151. return const