Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
Ce dépôt est archivé. Vous pouvez voir les fichiers et le cloner, mais vous ne pouvez pas pousser ni ouvrir de ticket/demande d'ajout.

75 lignes
2.4KB

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Advanced Text Generator module for a KaiSD Text Tools.
  5. (c) 2013 Ivan "Kai SD" Korystin
  6. License: GPLv3
  7. '''
  8. from os.path import join, exists
  9. from os import makedirs
  10. class ATG(object):
  11. '''
  12. Advanced Text Generator is a class, created to generate multiple
  13. text files from table data.
  14. '''
  15. def __init__(self, data, template):
  16. '''
  17. Constructor.
  18. data - an instance of the data.Data class (i.e. CSVData)
  19. template - an instance of the template.Template class (i.e. TemplateV2)
  20. '''
  21. self.data = data
  22. self.template = template
  23. self.out = template.process(data)
  24. if type(self.out) == dict:
  25. self.multiple = True
  26. else:
  27. self.multiple = False
  28. def write_files(self, outputDir='.'):
  29. '''
  30. Write generated files to the given directory.
  31. '''
  32. encoding = self.template.encoding
  33. extension = self.template.extension
  34. out = self.out
  35. if self.multiple:
  36. for name in out.keys():
  37. namepath = name.replace('\\', '/').split('/')
  38. newpath = u''
  39. for i in namepath[:-1]:
  40. newpath = join(newpath, i)
  41. if not exists(join(unicode(outputDir),newpath)):
  42. makedirs(join(unicode(outputDir),newpath))
  43. fname = join(unicode(outputDir),name+'.'+extension)
  44. if fname.endswith('.'):
  45. fname = fname[:-1]
  46. f = open(fname, 'w')
  47. f.write(out[name].encode(encoding))
  48. self.log(' Saved %s' % (name+'.'+extension))
  49. f.close()
  50. else:
  51. name = self.template.bonusPrefix
  52. if name == '.':
  53. name = self.template.keyField
  54. namepath = name.replace('\\', '/').split('/')
  55. newpath = u''
  56. for i in namepath[:-1]:
  57. newpath = join(newpath, i)
  58. if not exists(join(unicode(outputDir),newpath)):
  59. makedirs(join(unicode(outputDir),newpath))
  60. f = open(join(unicode(outputDir),name+'.'+extension), 'w')
  61. f.write(out.encode(encoding))
  62. self.log(' Saved %s' % (name+'.'+extension))
  63. f.close()
  64. def log(self, text):
  65. '''
  66. Print information
  67. '''
  68. #print 'ATG:', text
  69. pass