25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

103 lines
3.3 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. Utilities for using modules
  24. """
  25. import webnotes
  26. transfer_types = ['Role', 'Print Format','DocType','Page','DocType Mapper','GL Mapper','Search Criteria', 'Patch']
  27. def scrub(txt):
  28. return txt.replace(' ','_').replace('-', '_').replace('/', '_').lower()
  29. def scrub_dt_dn(dt, dn):
  30. """Returns in lowercase and code friendly names of doctype and name for certain types"""
  31. ndt, ndn = dt, dn
  32. if dt.lower() in ('doctype', 'search criteria', 'page'):
  33. ndt, ndn = scrub(dt), scrub(dn)
  34. return ndt, ndn
  35. def get_module_path(module):
  36. """Returns path of the given module"""
  37. import os, conf
  38. m = scrub(module)
  39. if m in ('core'):
  40. path_to_lib = os.sep.join(conf.modules_path.split(os.path.sep)[:-1])
  41. return os.path.join(path_to_lib, 'lib', 'py', 'core')
  42. else:
  43. return os.path.join(conf.modules_path, m)
  44. def reload_doc(module, dt=None, dn=None):
  45. """reload single / list of records"""
  46. if type(module) is list:
  47. for m in module:
  48. reload_single_doc(m[0], m[1], m[2])
  49. else:
  50. reload_single_doc(module, dt, dn)
  51. def reload_single_doc(module, dt, dn):
  52. """Sync a file from txt"""
  53. import os
  54. dt, dn = scrub_dt_dn(dt, dn)
  55. path = os.path.join(get_module_path(module),
  56. os.path.join(dt, dn, dn + '.txt'))
  57. if os.path.exists(path):
  58. from webnotes.model.utils import peval_doclist
  59. with open(path, 'r') as f:
  60. doclist = peval_doclist(f.read())
  61. if doclist:
  62. from webnotes.utils.transfer import set_doc
  63. set_doc(doclist, 1, 1, 1)
  64. # since there is a new timestamp on the file, update timestamp in
  65. webnotes.conn.sql("update `tab%s` set modified=now() where name=%s" \
  66. % (doclist[0]['doctype'], '%s'), doclist[0]['name'])
  67. else:
  68. raise Exception, '%s missing' % path
  69. def export_doc(doctype, name):
  70. """write out a doc"""
  71. from webnotes.modules.export_module import write_document_file
  72. import webnotes.model.doc
  73. module = webnotes.conn.get_value(doctype, name, 'module')
  74. doclist = [d.fields for d in webnotes.model.doc.get(doctype, name)]
  75. write_document_file(doclist, module)
  76. def get_all_modules():
  77. """Return list of all modules"""
  78. import conf
  79. from webnotes.modules.utils import listfolders
  80. if hasattr(conf, 'modules_path'):
  81. return listfolders(conf.modules_path, 1)