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.

export_module.py 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """
  2. Export files to modules
  3. """
  4. from webnotes.modules import scrub, get_module_path
  5. def export_to_files(record_list=[], record_module=None, verbose=0):
  6. """
  7. Export record_list to files. record_list is a list of lists ([doctype],[docname] ) ,
  8. """
  9. import webnotes.model.doc
  10. module_doclist =[]
  11. if record_list:
  12. for record in record_list:
  13. doclist = [d.fields for d in webnotes.model.doc.get(record[0], record[1])]
  14. write_document_file(doclist, record_module)
  15. def create_init_py(modules_path, dt, dn):
  16. """
  17. Creates __init__.py in the module directory structure
  18. """
  19. import os
  20. def create_if_not_exists(path):
  21. initpy = os.path.join(path, '__init__.py')
  22. if not os.path.exists(initpy):
  23. open(initpy, 'w').close()
  24. create_if_not_exists(os.path.join(modules_path))
  25. create_if_not_exists(os.path.join(modules_path, dt))
  26. create_if_not_exists(os.path.join(modules_path, dt, dn))
  27. def create_folder(module, dt, dn):
  28. """
  29. Creates directories for module and their __init__.py
  30. """
  31. import webnotes, os
  32. # get module path by importing the module
  33. modules_path = get_module_path(module)
  34. code_type = dt in ['DocType', 'Page', 'Search Criteria']
  35. # create folder
  36. folder = os.path.join(modules_path, code_type and scrub(dt) or dt, code_type and scrub(dn) or dn)
  37. webnotes.create_folder(folder)
  38. # create init_py_files
  39. if code_type:
  40. create_init_py(modules_path, scrub(dt), scrub(dn))
  41. return folder
  42. def get_module_name(doclist, record_module=None):
  43. """
  44. Returns the module-name of a doclist
  45. """
  46. # module name
  47. if doclist[0]['doctype'] == 'Module Def':
  48. module = doclist[0]['name']
  49. elif doclist[0]['doctype']=='Control Panel':
  50. module = 'Core'
  51. elif record_module:
  52. module = record_module
  53. else:
  54. module = doclist[0]['module']
  55. return module
  56. def write_document_file(doclist, record_module=None):
  57. """
  58. Write a doclist to file, can optionally specify module name
  59. """
  60. import os
  61. from webnotes.model.utils import pprint_doclist
  62. module = get_module_name(doclist, record_module)
  63. # create the folder
  64. code_type = doclist[0]['doctype'] in ['DocType','Page','Search Criteria']
  65. # create folder
  66. folder = create_folder(module, doclist[0]['doctype'], doclist[0]['name'])
  67. # separate code files
  68. clear_code_fields(doclist, folder, code_type)
  69. # write the data file
  70. fname = (code_type and scrub(doclist[0]['name'])) or doclist[0]['name']
  71. txtfile = open(os.path.join(folder, fname +'.txt'),'w+')
  72. txtfile.write(pprint_doclist(doclist))
  73. #dict_list = [pprint_dict(d) for d in doclist]
  74. #txtfile.write('[\n' + ',\n'.join(dict_list) + '\n]')
  75. txtfile.close()
  76. def clear_code_fields(doclist, folder, code_type):
  77. """
  78. Removes code from the doc
  79. """
  80. import os
  81. import webnotes
  82. # code will be in the parent only
  83. code_fields = webnotes.code_fields_dict.get(doclist[0]['doctype'], [])
  84. for code_field in code_fields:
  85. if doclist[0].get(code_field[0]):
  86. doclist[0][code_field[0]] = None