Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

72 lignes
2.5 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. from __future__ import unicode_literals
  23. import webnotes, os
  24. from webnotes.modules import scrub, get_module_path, scrub_dt_dn
  25. def import_files(module, dt=None, dn=None):
  26. if type(module) is list:
  27. for m in module:
  28. return import_file(m[0], m[1], m[2])
  29. else:
  30. return import_file(module, dt, dn)
  31. def import_file(module, dt, dn, force=False):
  32. """Sync a file from txt if modifed, return false if not updated"""
  33. if dt.lower() == 'doctype':
  34. return
  35. dt, dn = scrub_dt_dn(dt, dn)
  36. path = os.path.join(get_module_path(module),
  37. os.path.join(dt, dn, dn + '.txt'))
  38. return import_file_by_path(path, force)
  39. def import_file_by_path(path, force=False):
  40. if os.path.exists(path):
  41. from webnotes.modules.utils import peval_doclist
  42. with open(path, 'r') as f:
  43. doclist = peval_doclist(f.read())
  44. if doclist:
  45. doc = doclist[0]
  46. if not force:
  47. # check if timestamps match
  48. if doc['modified']== str(webnotes.conn.get_value(doc['doctype'], doc['name'], 'modified')):
  49. return False
  50. from webnotes.modules.import_merge import set_doc
  51. set_doc(doclist, 1, 1, 1)
  52. # since there is a new timestamp on the file, update timestamp in
  53. webnotes.conn.sql("update `tab%s` set modified=%s where name=%s" % \
  54. (doc['doctype'], '%s', '%s'),
  55. (doc['modified'],doc['name']))
  56. return True
  57. else:
  58. raise Exception, '%s missing' % path