您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

__init__.py 3.7 KiB

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