Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.12 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- mode: python; coding: utf-8 -*-
  3.  
  4. assert str is not bytes
  5.  
  6. import sys
  7. import xml.etree.ElementTree as ET
  8. import itertools
  9.  
  10. CATEGORY_COLUMN_FIELD = 1
  11. STORE_ID_FIELD = 0
  12. LAYOUT_ID_FIELD = 0
  13. LANGUAGE_ID_FIELD = 1
  14. PRODUCT_STOCK_STATUS_ID_FIELD = 7
  15.  
  16. class ConvertError(Exception):
  17. pass
  18.  
  19. def id_by_guid_func_create(id_iter, id_by_guid_map):
  20. def func(guid):
  21. try:
  22. id = id_by_guid_map[guid]
  23. except KeyError:
  24. pass
  25. else:
  26. return id
  27.  
  28. id = next(id_iter)
  29. id_by_guid_map[guid] = id
  30.  
  31. return id
  32.  
  33. return func
  34.  
  35. def sql_quote(s):
  36. assert not isinstance(s, bytes)
  37.  
  38. if str is None:
  39. s = ''
  40.  
  41. if not isinstance(s, str):
  42. s = str(s)
  43.  
  44. s = s.replace('\\', '\\\\')
  45. s = s.replace('\'', '\\\'')
  46. s = '\'{}\''.format(s)
  47.  
  48. return s
  49.  
  50. def convert(begin_id, xml_fd, sql_fd):
  51. tree = ET.parse(xml_fd)
  52. root_el = tree.getroot()
  53.  
  54. if root_el.tag != 'КоммерческаяИнформация':
  55. raise ConvertError
  56.  
  57. group_el_list = []
  58. product_el_list = []
  59.  
  60. group_meta_list = []
  61. group_meta_by_guid_map = {}
  62. product_meta_list = []
  63.  
  64. category_id_iter = itertools.count(start=begin_id)
  65. product_id_iter = itertools.count(start=begin_id)
  66. category_id_by_guid_map = {None: 0}
  67. product_id_by_guid_map = {None: 0}
  68. category_id_by_guid = id_by_guid_func_create(category_id_iter, category_id_by_guid_map)
  69. product_id_by_guid = id_by_guid_func_create(product_id_iter, product_id_by_guid_map)
  70.  
  71. for child1_el in root_el:
  72. for child2_el in child1_el:
  73. for child3_el in child2_el:
  74. if child1_el.tag == 'Классификатор' and \
  75. child2_el.tag == 'Группы' and \
  76. child3_el.tag == 'Группа':
  77. group_el_list.append((child3_el, None))
  78. if child1_el.tag == 'Каталог' and \
  79. child2_el.tag == 'Товары' and \
  80. child3_el.tag == 'Товар':
  81. product_el_list.append(child3_el)
  82.  
  83. scheduled_el_list = group_el_list
  84.  
  85. while scheduled_el_list:
  86. new_scheduled_el_list = []
  87.  
  88. for child1_el, parent_guid in scheduled_el_list:
  89. guid = None
  90. name = None
  91.  
  92. for child2_el in child1_el:
  93. if guid is None and child2_el.tag == 'Ид':
  94. guid = child2_el.text
  95. elif name is None and child2_el.tag == 'Наименование':
  96. name = child2_el.text
  97.  
  98. if guid is None or name is None:
  99. continue
  100.  
  101. group_meta = {
  102. 'guid': guid,
  103. 'parent_guid': parent_guid,
  104. 'category_id': category_id_by_guid(guid),
  105. 'parent_category_id': category_id_by_guid(parent_guid),
  106. 'name': name,
  107. }
  108. group_meta_list.append(group_meta)
  109. group_meta_by_guid_map[guid] = group_meta
  110.  
  111. for child2_el in child1_el:
  112. for child3_el in child2_el:
  113. if child2_el.tag == 'Группы' and \
  114. child3_el.tag == 'Группа':
  115. new_scheduled_el_list.append((child3_el, guid))
  116.  
  117. scheduled_el_list = new_scheduled_el_list
  118.  
  119. for child1_el in product_el_list:
  120. guid = None
  121. barcode = None
  122. name = None
  123. groups = []
  124.  
  125. for child2_el in child1_el:
  126. if guid is None and child2_el.tag == 'Ид':
  127. guid = child2_el.text
  128. elif barcode is None and child2_el.tag == 'Штрихкод':
  129. barcode = child2_el.text
  130. elif name is None and child2_el.tag == 'Наименование':
  131. name = child2_el.text
  132.  
  133. for child3_el in child2_el:
  134. if child2_el.tag == 'Группы' and \
  135. child3_el.tag == 'Ид':
  136. groups.append(child3_el.text)
  137.  
  138. if guid is None or name is None:
  139. continue
  140.  
  141. product_meta = {
  142. 'guid': guid,
  143. 'product_id': product_id_by_guid(guid),
  144. 'barcode': barcode,
  145. 'name': name,
  146. 'groups': groups,
  147. }
  148. product_meta_list.append(product_meta)
  149.  
  150. sql_fd.write('-- BEGIN of category list\n')
  151. for group_meta in group_meta_list:
  152. sql_line_list = []
  153.  
  154. sql_line_list.append('-- BEGIN of category')
  155.  
  156. sql_line_list.append(
  157. 'INSERT INTO `oc_category` (`category_id`, `parent_id`, `top`, `column`, `status`, `date_added`, `date_modified`) VALUES ({}, {}, {}, {}, {}, now(), now());'.format(
  158. sql_quote(group_meta['category_id']),
  159. sql_quote(group_meta['parent_category_id']),
  160. sql_quote(1),
  161. sql_quote(CATEGORY_COLUMN_FIELD),
  162. sql_quote(1),
  163. )
  164. )
  165.  
  166. sql_line_list.append(
  167. 'INSERT INTO `oc_category_description` (`category_id`, `language_id`, `name`, `meta_title`) VALUES ({}, {}, {}, {});'.format(
  168. sql_quote(group_meta['category_id']),
  169. sql_quote(LANGUAGE_ID_FIELD),
  170. sql_quote(group_meta['name']),
  171. sql_quote(group_meta['name']),
  172. )
  173. )
  174.  
  175. sql_line_list.append(
  176. 'INSERT INTO `oc_category_to_store` (`category_id`, `store_id`) VALUES ({}, {});'.format(
  177. sql_quote(group_meta['category_id']),
  178. sql_quote(STORE_ID_FIELD),
  179. )
  180. )
  181.  
  182. sql_line_list.append(
  183. 'INSERT INTO `oc_category_to_layout` (`category_id`, `store_id`, `layout_id`) VALUES ({}, {}, {});'.format(
  184. sql_quote(group_meta['category_id']),
  185. sql_quote(STORE_ID_FIELD),
  186. sql_quote(LAYOUT_ID_FIELD),
  187. )
  188. )
  189.  
  190. sql_line_list.append('-- END of category\n')
  191. sql_fd.write('{}\n'.format('\n'.join(sql_line_list)))
  192. sql_fd.write('-- END of category list\n\n')
  193.  
  194. sql_fd.write('-- BEGIN of product list\n')
  195. for product_meta in product_meta_list:
  196. sql_line_list = []
  197.  
  198. sql_line_list.append('-- BEGIN of product')
  199.  
  200. sql_line_list.append(
  201. 'INSERT INTO `oc_product` (`product_id`, `model`, `ean`, `quantity`, `stock_status_id`, `shipping`, `date_available`, `subtract`, `minimum`, `status`, `date_added`,`date_modified`) VALUES ('
  202. '{}, {}, {}, {}, {}, {}, now(), {}, {}, {}, now(), now());'.format(
  203. sql_quote(product_meta['product_id']),
  204. sql_quote(product_meta['name']),
  205. sql_quote(product_meta['barcode']),
  206. sql_quote(1000),
  207. sql_quote(PRODUCT_STOCK_STATUS_ID_FIELD),
  208. sql_quote(1),
  209. sql_quote(1),
  210. sql_quote(1),
  211. sql_quote(1),
  212. )
  213. )
  214.  
  215. sql_line_list.append(
  216. 'INSERT INTO `oc_product_description` (`product_id`, `language_id`, `name`, `meta_title`) VALUES ('
  217. '{}, {}, {}, {});'.format(
  218. sql_quote(product_meta['product_id']),
  219. sql_quote(LANGUAGE_ID_FIELD),
  220. sql_quote(product_meta['name']),
  221. sql_quote(product_meta['name']),
  222. )
  223. )
  224.  
  225. for group_guid in product_meta['groups']:
  226. category_meta = group_meta_by_guid_map.get(group_guid)
  227.  
  228. if category_meta is None:
  229. continue
  230.  
  231. sql_line_list.append(
  232. 'INSERT INTO `oc_product_to_category` (`product_id`, `category_id`) VALUES ('
  233. '{}, {});'.format(
  234. sql_quote(product_meta['product_id']),
  235. sql_quote(category_meta['category_id']),
  236. )
  237. )
  238.  
  239. sql_line_list.append(
  240. 'INSERT INTO `oc_product_to_store` (`product_id`, `store_id`) VALUES ('
  241. '{}, {});'.format(
  242. sql_quote(product_meta['product_id']),
  243. sql_quote(STORE_ID_FIELD),
  244. )
  245. )
  246.  
  247. sql_line_list.append(
  248. 'INSERT INTO `oc_product_to_layout` (`product_id`, `store_id`, `layout_id`) VALUES ('
  249. '{}, {}, {});'.format(
  250. sql_quote(product_meta['product_id']),
  251. sql_quote(STORE_ID_FIELD),
  252. sql_quote(LAYOUT_ID_FIELD),
  253. )
  254. )
  255.  
  256. sql_line_list.append('-- END of product\n')
  257. sql_fd.write('{}\n'.format('\n'.join(sql_line_list)))
  258. sql_fd.write('-- END of product list\n\n')
  259.  
  260. def main():
  261. begin_id = int(sys.argv[1])
  262. in_path = sys.argv[2]
  263. out_path = sys.argv[3]
  264.  
  265. with open(in_path, mode='rb') as xml_fd, \
  266. open(out_path, mode='wt', encoding='utf-8', newline='\n') as sql_fd:
  267. convert(begin_id, xml_fd, sql_fd)
  268.  
  269. if __name__ == '__main__':
  270. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement