You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

108 lines
3.2 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
  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. try:
  15. code = read_file(module, doctype, docname, plugin, cache=True)
  16. except webnotes.SQLError:
  17. return None
  18. return code
  19. def get_cache_key(doctype, docname, extn="py"):
  20. from webnotes.modules import scrub
  21. return "plugin_file:{doctype}:{docname}:{extn}".format(doctype=scrub(doctype),
  22. docname=scrub(docname), extn=scrub(extn))
  23. def get_plugin_name(doctype=None, docname=None):
  24. import os
  25. from webnotes.utils import get_site_base_path
  26. plugin = None
  27. if doctype:
  28. meta = webnotes.get_doctype(doctype)
  29. if meta.get_field("plugin"):
  30. plugin = webnotes.conn.get_value(doctype, docname, "plugin")
  31. if not plugin:
  32. plugin = os.path.basename(get_site_base_path())
  33. return plugin
  34. def read_file(module, doctype, docname, plugin=None, extn="py", cache=False):
  35. import os
  36. content = None
  37. if cache:
  38. content = webnotes.cache().get_value(get_cache_key(doctype, docname, extn))
  39. if not content:
  40. path = get_path(module, doctype, docname, plugin, extn)
  41. if os.path.exists(path):
  42. with open(path, 'r') as f:
  43. content = f.read() or "Does Not Exist"
  44. if cache:
  45. webnotes.cache().set_value(get_cache_key(doctype, docname, extn), content)
  46. return None if (content == "Does Not Exist") else content
  47. def get_path(module, doctype, docname, plugin=None, extn="py"):
  48. from webnotes.modules import scrub
  49. import os
  50. if not module: module = webnotes.conn.get_value(doctype, docname, "module")
  51. if not plugin: plugin = get_plugin_name(doctype, docname)
  52. # site_abs_path/plugins/module/doctype/docname/docname.py
  53. return os.path.join(get_plugin_path(scrub(plugin)), scrub(module),
  54. scrub(doctype), scrub(docname), scrub(docname) + "." + extn)
  55. def get_plugin_path(plugin=None):
  56. from webnotes.modules import scrub
  57. from webnotes.utils import get_site_path
  58. if not plugin: plugin = get_plugin_name(None, None)
  59. return get_site_path(webnotes.conf.get("plugins_path"), scrub(plugin))
  60. def remove_init_files():
  61. import os
  62. from webnotes.utils import get_site_path, cstr
  63. for path, folders, files in os.walk(get_site_path(webnotes.conf.get("plugins_path"))):
  64. for f in files:
  65. # cstr(f) is required when filenames are non-ascii
  66. if cstr(f) in ("__init__.py", "__init__.pyc"):
  67. os.remove(os.path.join(path, f))
  68. def clear_cache(doctype=None, docname=None):
  69. import os
  70. from webnotes.utils import get_site_path
  71. def clear_single(dt, dn):
  72. webnotes.cache().delete_value(get_cache_key(dt, dn, "py"))
  73. webnotes.cache().delete_value(get_cache_key(dt, dn, "js"))
  74. if not (doctype and docname):
  75. for path, folders, files in os.walk(get_site_path(webnotes.conf.get("plugins_path"))):
  76. if files:
  77. dt = os.path.basename(os.path.dirname(path))
  78. dn = os.path.basename(path)
  79. clear_single(dt, dn)
  80. else:
  81. clear_single(doctype, docname)