Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

157 рядки
4.0 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes, os
  5. import webnotes.modules
  6. from webnotes.utils import cstr
  7. from webnotes.modules import export_doc, get_module_path, scrub
  8. def listfolders(path, only_name=0):
  9. """
  10. Returns the list of folders (with paths) in the given path,
  11. If only_name is set, it returns only the folder names
  12. """
  13. out = []
  14. for each in os.listdir(path):
  15. each = cstr(each)
  16. dirname = each.split(os.path.sep)[-1]
  17. fullpath = os.path.join(path, dirname)
  18. if os.path.isdir(fullpath) and not dirname.startswith('.'):
  19. out.append(only_name and dirname or fullpath)
  20. return out
  21. def switch_module(dt, dn, to, frm=None, export=None):
  22. """
  23. Change the module of the given doctype, if export is true, then also export txt and copy
  24. code files from src
  25. """
  26. webnotes.conn.sql("update `tab"+dt+"` set module=%s where name=%s", (to, dn))
  27. if export:
  28. export_doc(dt, dn)
  29. # copy code files
  30. if dt in ('DocType', 'Page', 'Report'):
  31. from_path = os.path.join(get_module_path(frm), scrub(dt), scrub(dn), scrub(dn))
  32. to_path = os.path.join(get_module_path(to), scrub(dt), scrub(dn), scrub(dn))
  33. # make dire if exists
  34. os.system('mkdir -p %s' % os.path.join(get_module_path(to), scrub(dt), scrub(dn)))
  35. for ext in ('py','js','html','css'):
  36. os.system('cp %s %s')
  37. def commonify_doclist(doclist, with_comments=1):
  38. """
  39. Makes a doclist more readable by extracting common properties.
  40. This is used for printing Documents in files
  41. """
  42. from webnotes.utils import get_common_dict, get_diff_dict
  43. def make_common(doclist):
  44. c = {}
  45. if with_comments:
  46. c['##comment'] = 'These values are common in all dictionaries'
  47. for k in common_keys:
  48. c[k] = doclist[0][k]
  49. return c
  50. def strip_common_and_idx(d):
  51. for k in common_keys:
  52. if k in d: del d[k]
  53. if 'idx' in d: del d['idx']
  54. return d
  55. def make_common_dicts(doclist):
  56. common_dict = {} # one per doctype
  57. # make common dicts for all records
  58. for d in doclist:
  59. if not d['doctype'] in common_dict:
  60. d1 = d.copy()
  61. if d1.has_key("name"):
  62. del d1['name']
  63. common_dict[d['doctype']] = d1
  64. else:
  65. common_dict[d['doctype']] = get_common_dict(common_dict[d['doctype']], d)
  66. return common_dict
  67. common_keys = ['owner','docstatus','creation','modified','modified_by']
  68. common_dict = make_common_dicts(doclist)
  69. # make docs
  70. final = []
  71. for d in doclist:
  72. f = strip_common_and_idx(get_diff_dict(common_dict[d['doctype']], d))
  73. f['doctype'] = d['doctype'] # keep doctype!
  74. # strip name for child records (only an auto generated number!)
  75. if f['doctype'] != doclist[0]['doctype'] and f.has_key("name"):
  76. del f['name']
  77. if with_comments:
  78. f['##comment'] = d['doctype'] + ('name' in f and (', ' + f['name']) or '')
  79. final.append(f)
  80. # add commons
  81. commons = []
  82. for d in common_dict.values():
  83. d['name']='__common__'
  84. if with_comments:
  85. d['##comment'] = 'These values are common for all ' + d['doctype']
  86. commons.append(strip_common_and_idx(d))
  87. common_values = make_common(doclist)
  88. return [common_values]+commons+final
  89. def uncommonify_doclist(dl):
  90. """
  91. Expands an commonified doclist
  92. """
  93. # first one has common values
  94. common_values = dl[0]
  95. common_dict = webnotes._dict()
  96. final = []
  97. idx_dict = {}
  98. for d in dl[1:]:
  99. if 'name' in d and d['name']=='__common__':
  100. # common for a doctype -
  101. del d['name']
  102. common_dict[d['doctype']] = d
  103. else:
  104. dt = d['doctype']
  105. if not dt in idx_dict: idx_dict[dt] = 1;
  106. d1 = webnotes._dict(common_values.copy())
  107. # update from common and global
  108. d1.update(common_dict[dt])
  109. d1.update(d)
  110. # idx by sequence
  111. d1['idx'] = idx_dict[dt]
  112. # increment idx
  113. idx_dict[dt] += 1
  114. final.append(d1)
  115. return final
  116. def pprint_doclist(doclist, with_comments = 1):
  117. from json import dumps
  118. return dumps(commonify_doclist(doclist, False), indent=1, sort_keys=True)
  119. def peval_doclist(txt):
  120. from json import loads
  121. try:
  122. return uncommonify_doclist(loads(txt))
  123. except Exception, e:
  124. return uncommonify_doclist(eval(txt))