Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. import re
  4. import os
  5. import collections
  6. import dicttoxml
  7. import xmltodict, json
  8. from collections import defaultdict
  9. from time import gmtime, strftime
  10. from reportlab.lib.units import inch
  11. from reportlab.lib.pagesizes import A4
  12. from reportlab.lib.styles import ParagraphStyle
  13. from reportlab.lib.styles import getSampleStyleSheet
  14. from reportlab.platypus import SimpleDocTemplate, Spacer, Paragraph, PageBreak
  15.  
  16. style = ParagraphStyle(
  17. name='Normal',
  18. fontName='Helvetica-Bold',
  19. fontSize=9,
  20. )
  21.  
  22. print strftime("%Y-%m-%d %H:%M:%S", gmtime())
  23. filesindir = os.listdir(os.getcwd())
  24.  
  25.  
  26. def returnasstring(arr):
  27. str = ''
  28. for s in arr:
  29. str += s + ' '
  30. return str.strip()
  31.  
  32.  
  33. def isEmpty(dictionary):
  34. for element in dictionary:
  35. if element:
  36. return True
  37. return False
  38.  
  39.  
  40. def rdicombine(filesindir):
  41. pages = ''
  42. rowcount = 0
  43. filsescount = 0
  44. for file in filesindir:
  45. if file.endswith('txt'):
  46. print u'loading...' + str(file)
  47. filsescount += 1
  48. with open(file) as f:
  49. page = f.readlines()
  50. for row in page:
  51. rowcount += 1
  52. row = returnasstring(re.sub('\s\s+', ' ', row.strip()).split(' '))
  53. pages += row + '\n'
  54.  
  55. if filsescount == 0:
  56. raise u'no datebase files found heare pal!'
  57. return pages.strip()
  58.  
  59.  
  60. def billparser(billslist):
  61. id = ''
  62. blokcs = []
  63. dicts = {}
  64.  
  65.  
  66. for row in billslist:
  67. variable = row.split()[0]
  68. data = returnasstring(row.split()[1:])
  69.  
  70. if variable == 'DT_PARTNER-PARTNER':
  71. id = data
  72.  
  73. if variable == 'DGV_END':
  74. dicts[id] = blokcs
  75. blokcs = []
  76.  
  77. else:
  78. blokcs.append(data)
  79.  
  80.  
  81. #print str(billcount) + ' bills found'
  82. return dicts
  83.  
  84.  
  85. def parstoxml(mainlist):
  86. print 'build xml loading...'
  87. xml = dicttoxml.dicttoxml(mainlist)
  88. xml = re.sub('type="str"', '', xml)
  89. xml = re.sub('type="list"', '', xml)
  90. xml = re.sub('type="dict"', '', xml)
  91. return xml
  92.  
  93.  
  94. def xmltojson(xml):
  95. j = xmltodict.parse(xml)
  96. json_data = json.dumps(j)
  97. return json_data
  98.  
  99.  
  100. def docbuilder(json_data, docname, l, r, t, b):
  101. frame = {'leftMargin': l, 'rightMargin': r, 'topMargin': t, 'bottomMargin': b}
  102. doc = SimpleDocTemplate(docname + '.pdf', pagesize=A4, **frame)
  103. elements = []
  104.  
  105. for each in json_data:
  106. client_id = each
  107. data = each
  108.  
  109.  
  110.  
  111. #elements.append(Spacer(1, 0.2 * inch))
  112. #ements.append(Paragraph("Paragraphs are a kind of Flowable. ", style))
  113. #elements.append(Spacer(1, 0.2 * inch))
  114. #elements.append(PageBreak())
  115. #doc.build(elements)
  116.  
  117.  
  118. baselists = rdicombine(filesindir).split('\n')
  119. mainlist = billparser(baselists)
  120. xml = parstoxml(mainlist)
  121. json_data = xmltojson(xml)
  122. docbuilder(mainlist, u'! Test', 0, 0, 0.3, 0.3)
  123.  
  124. with open('sw_database_xml.xml', 'w') as file:
  125. print 'save xml...'
  126. file.write(xml)
  127. # file.write(re.sub('type="str"', '', xml))
  128.  
  129.  
  130. print 'done :)'
  131. print strftime("%Y-%m-%d %H:%M:%S", gmtime())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement