Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

133 řádky
4.0 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. """
  23. Export files to modules
  24. """
  25. from webnotes.modules import scrub, get_module_path
  26. def export_to_files(record_list=[], record_module=None, verbose=0):
  27. """
  28. Export record_list to files. record_list is a list of lists ([doctype],[docname] ) ,
  29. """
  30. import webnotes.model.doc
  31. module_doclist =[]
  32. if record_list:
  33. for record in record_list:
  34. doclist = [d.fields for d in webnotes.model.doc.get(record[0], record[1])]
  35. write_document_file(doclist, record_module)
  36. def create_init_py(modules_path, dt, dn):
  37. """
  38. Creates __init__.py in the module directory structure
  39. """
  40. import os
  41. def create_if_not_exists(path):
  42. initpy = os.path.join(path, '__init__.py')
  43. if not os.path.exists(initpy):
  44. open(initpy, 'w').close()
  45. create_if_not_exists(os.path.join(modules_path))
  46. create_if_not_exists(os.path.join(modules_path, dt))
  47. create_if_not_exists(os.path.join(modules_path, dt, dn))
  48. def create_folder(module, dt, dn):
  49. """
  50. Creates directories for module and their __init__.py
  51. """
  52. import webnotes, os
  53. # get module path by importing the module
  54. modules_path = get_module_path(module)
  55. code_type = dt in ['DocType', 'Page', 'Search Criteria']
  56. # create folder
  57. folder = os.path.join(modules_path, code_type and scrub(dt) or dt, code_type and scrub(dn) or dn)
  58. webnotes.create_folder(folder)
  59. # create init_py_files
  60. if code_type:
  61. create_init_py(modules_path, scrub(dt), scrub(dn))
  62. return folder
  63. def get_module_name(doclist, record_module=None):
  64. """
  65. Returns the module-name of a doclist
  66. """
  67. # module name
  68. if doclist[0]['doctype'] == 'Module Def':
  69. module = doclist[0]['name']
  70. elif doclist[0]['doctype']=='Control Panel':
  71. module = 'Core'
  72. elif record_module:
  73. module = record_module
  74. else:
  75. module = doclist[0]['module']
  76. return module
  77. def write_document_file(doclist, record_module=None):
  78. """
  79. Write a doclist to file, can optionally specify module name
  80. """
  81. import os
  82. from webnotes.model.utils import pprint_doclist
  83. module = get_module_name(doclist, record_module)
  84. # create the folder
  85. code_type = doclist[0]['doctype'] in ['DocType','Page','Search Criteria']
  86. # create folder
  87. folder = create_folder(module, doclist[0]['doctype'], doclist[0]['name'])
  88. # separate code files
  89. clear_code_fields(doclist, folder, code_type)
  90. # write the data file
  91. fname = (code_type and scrub(doclist[0]['name'])) or doclist[0]['name']
  92. txtfile = open(os.path.join(folder, fname +'.txt'),'w+')
  93. txtfile.write(pprint_doclist(doclist))
  94. #dict_list = [pprint_dict(d) for d in doclist]
  95. #txtfile.write('[\n' + ',\n'.join(dict_list) + '\n]')
  96. txtfile.close()
  97. def clear_code_fields(doclist, folder, code_type):
  98. """
  99. Removes code from the doc
  100. """
  101. import os
  102. import webnotes
  103. # code will be in the parent only
  104. code_fields = webnotes.code_fields_dict.get(doclist[0]['doctype'], [])
  105. for code_field in code_fields:
  106. if doclist[0].get(code_field[0]):
  107. doclist[0][code_field[0]] = None