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

111 行
3.3 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. def get_code_and_execute(module, doctype, docname, plugin=None, namespace=None):
  6. code = get_code(module, doctype, docname, plugin)
  7. return exec_code(code, namespace)
  8. def exec_code(code, namespace=None):
  9. if namespace is None: namespace = {}
  10. if code:
  11. exec code in namespace
  12. return namespace
  13. def get_code(module, doctype, docname, plugin=None):
  14. import MySQLdb
  15. try:
  16. code = read_file(module, doctype, docname, plugin, cache=True)
  17. except MySQLdb.OperationalError:
  18. # this happens when syncing
  19. return None
  20. return code
  21. def get_cache_key(doctype, docname, extn="py"):
  22. from webnotes.modules import scrub
  23. return "plugin_file:{doctype}:{docname}:{extn}".format(doctype=scrub(doctype),
  24. docname=scrub(docname), extn=scrub(extn))
  25. def get_plugin_name(doctype=None, docname=None):
  26. import os
  27. from webnotes.utils import get_site_base_path
  28. plugin = None
  29. if doctype:
  30. meta = webnotes.get_doctype(doctype)
  31. if meta.get_field("plugin"):
  32. plugin = webnotes.conn.get_value(doctype, docname, "plugin")
  33. if not plugin:
  34. plugin = os.path.basename(get_site_base_path())
  35. return plugin
  36. def read_file(module, doctype, docname, plugin=None, extn="py", cache=False):
  37. import os
  38. content = None
  39. if cache:
  40. content = webnotes.cache().get_value(get_cache_key(doctype, docname, extn))
  41. if not content:
  42. path = get_path(module, doctype, docname, plugin, extn)
  43. if os.path.exists(path):
  44. with open(path, 'r') as f:
  45. content = f.read() or "Does Not Exist"
  46. if cache:
  47. webnotes.cache().set_value(get_cache_key(doctype, docname, extn), content)
  48. return None if (content == "Does Not Exist") else content
  49. def get_path(module, doctype, docname, plugin=None, extn="py"):
  50. from webnotes.modules import scrub
  51. import os
  52. if not module: module = webnotes.conn.get_value(doctype, docname, "module")
  53. if not plugin: plugin = get_plugin_name(doctype, docname)
  54. # site_abs_path/plugins/module/doctype/docname/docname.py
  55. return os.path.join(get_plugin_path(scrub(plugin)), scrub(module),
  56. scrub(doctype), scrub(docname), scrub(docname) + "." + extn)
  57. def get_plugin_path(plugin=None):
  58. from webnotes.modules import scrub
  59. from webnotes.utils import get_site_path
  60. if not plugin: plugin = get_plugin_name(None, None)
  61. return get_site_path(webnotes.conf.get("plugins_path"), scrub(plugin))
  62. def remove_init_files():
  63. import os
  64. from webnotes.utils import get_site_path, cstr
  65. for path, folders, files in os.walk(get_site_path(webnotes.conf.get("plugins_path"))):
  66. for f in files:
  67. # cstr(f) is required when filenames are non-ascii
  68. if cstr(f) in ("__init__.py", "__init__.pyc"):
  69. os.remove(os.path.join(path, f))
  70. def clear_cache(doctype=None, docname=None):
  71. import os
  72. from webnotes.utils import get_site_path
  73. def clear_single(dt, dn):
  74. webnotes.cache().delete_value(get_cache_key(dt, dn, "py"))
  75. webnotes.cache().delete_value(get_cache_key(dt, dn, "js"))
  76. if not (doctype and docname):
  77. for path, folders, files in os.walk(get_site_path(webnotes.conf.get("plugins_path"))):
  78. if files:
  79. dt = os.path.basename(os.path.dirname(path))
  80. dn = os.path.basename(path)
  81. clear_single(dt, dn)
  82. else:
  83. clear_single(doctype, docname)