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.7KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. '''
  4. Automatic Text Generator module for a Automatic 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. Automatic 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 join_filename(self, path, name, extension):
  29. '''
  30. Returns a file name for given path, name and extension.
  31. '''
  32. if extension:
  33. return join(unicode(path),name+'.'+extension)
  34. else:
  35. return join(unicode(path),name)
  36. def write_files(self, outputDir='.'):
  37. '''
  38. Write generated files to the given directory.
  39. '''
  40. encoding = self.template.encoding
  41. extension = self.template.extension
  42. out = self.out
  43. if self.multiple:
  44. for name in out.keys():
  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. fname = self.join_filename(outputDir, name, extension)
  52. if fname.endswith('.'):
  53. fname = fname[:-1]
  54. f = open(fname, 'w')
  55. f.write(out[name].encode(encoding))
  56. self.log(' Saved %s' % fname)
  57. f.close()
  58. else:
  59. name = self.template.bonusPrefix
  60. if name == '.':
  61. name = self.template.keyField
  62. namepath = name.replace('\\', '/').split('/')
  63. newpath = u''
  64. for i in namepath[:-1]:
  65. newpath = join(newpath, i)
  66. if not exists(join(unicode(outputDir),newpath)):
  67. makedirs(join(unicode(outputDir),newpath))
  68. fname = self.join_filename(outputDir, name, extension)
  69. f = open(fname, 'w')
  70. f.write(out.encode(encoding))
  71. self.log(' Saved %s' % fname)
  72. f.close()
  73. def log(self, text):
  74. '''
  75. Print information
  76. '''
  77. #print 'ATG:', text
  78. pass