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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

atg.py 2.1KB

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