def get_from_files(self, doc): """ Loads page info from files in module """ from webnotes.modules import get_module_path, scrub import os path = os.path.join(get_module_path(doc.module), "page", scrub(doc.name)) # script fpath = os.path.join(path, scrub(doc.name) + ".js") if os.path.exists(fpath): with open(fpath, "r") as f: doc.fields["__script"] = f.read() # css fpath = os.path.join(path, scrub(doc.name) + ".css") if os.path.exists(fpath): with open(fpath, "r") as f: doc.style = f.read() # html fpath = os.path.join(path, scrub(doc.name) + ".html") if os.path.exists(fpath): with open(fpath, "r") as f: doc.content = f.read()
def build_for_modules(): """doctype descriptions, module names, etc for each module""" from webnotes.modules import get_module_path, get_doc_path for m in webnotes.conn.sql("""select name from `tabModule Def`"""): module_path = get_module_path(m[0]) if os.path.exists(module_path): messages = [] messages += [ t[0] for t in webnotes.conn.sql( """select description from tabDocType where module=%s""", m[0]) ] for t in webnotes.conn.sql( """select if(ifnull(title,'')='',name,title) from tabPage where module=%s and ifnull(standard,'No')='Yes' """, m[0]): messages.append(t[0]) messages += [ t[0] for t in webnotes.conn.sql( """select t1.name from tabReport t1, tabDocType t2 where t1.ref_doctype = t2.name and t1.is_standard = "Yes" and t2.module = %s""", m[0]) ] doctype_path = get_doc_path(m[0], 'Module Def', m[0]) write_messages_file(doctype_path, messages, 'doc')
def on_update(self): """ Writes the .txt for this page and if write_content is checked, it will write out a .html file """ import conf if not webnotes.in_import and getattr(conf,'developer_mode', 0) and self.doc.standard=='Yes': from webnotes.modules.export_file import export_to_files from webnotes.modules import get_module_path, scrub import os export_to_files(record_list=[['Page', self.doc.name]]) # write files path = os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name), scrub(self.doc.name)) # js if not os.path.exists(path + '.js'): with open(path + '.js', 'w') as f: f.write("""wn.pages['%s'].onload = function(wrapper) { wn.ui.make_app_page({ parent: wrapper, title: '%s', single_column: true }); }""" % (self.doc.name, self.doc.title))
def on_update(self): """ Writes the .txt for this page and if write_content is checked, it will write out a .html file """ import conf from webnotes.modules.import_file import in_import if not in_import and getattr(conf,'developer_mode', 0) and self.doc.standard=='Yes': from webnotes.modules.export_file import export_to_files from webnotes.modules import get_module_path, scrub import os export_to_files(record_list=[['Page', self.doc.name]]) # write files path = os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name), scrub(self.doc.name)) # js if not os.path.exists(path + '.js'): with open(path + '.js', 'w') as f: f.write("""wn.pages['%s'].onload = function(wrapper) { wn.ui.make_app_page({ parent: wrapper, title: '%s', single_column: true }); }""" % (self.doc.name, self.doc.title))
def get_code(module, dt, dn, extn, fieldname=None): from webnotes.modules import scrub, get_module_path import os, webnotes # get module (if required) if not module: module = webnotes.conn.get_value(dt, dn, 'module') # no module, quit if not module: return '' # file names if dt in ('Page','Doctype'): dt, dn = scrub(dt), scrub(dn) # get file name fname = dn + '.' + extn # code code = '' try: file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r') code = file.read() file.close() except IOError, e: # no file, try from db if fieldname: code = webnotes.conn.get_value(dt, dn, fieldname)
def copy_file(self, path, old_name, extn): import os from webnotes.modules import get_module_path, scrub if os.path.exists(os.path.join(path, old_name + extn)): os.system('cp %s %s' % (os.path.join(path, old_name + extn), \ os.path.join(get_module_path(self.doc.module), 'search_criteria', scrub(self.doc.name), scrub(self.doc.name) + extn)))
def get_code(module, dt, dn, extn, is_static=None, fieldname=None): from webnotes.modules import scrub, get_module_path import os, webnotes # get module (if required) if not module: module = webnotes.conn.sql( "select module from `tab%s` where name=%s" % (dt, '%s'), dn)[0][0] # no module, quit if not module: return '' # file names if scrub(dt) in ('page', 'doctype', 'search_criteria'): dt, dn = scrub(dt), scrub(dn) # get file name fname = dn + '.' + extn if is_static: fname = dn + '_static.' + extn # code code = '' try: file = open( os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r') code = file.read() file.close() except IOError, e: # no file, try from db if fieldname: code = webnotes.conn.get_value(dt, dn, fieldname)
def get_page_js(page, module=None): """ Returns the js code of a page. Will replace $import (page) or $import(module.page) with the code from the file """ import webnotes, os from webnotes.modules import scrub, get_module_path if type(page)==str: page_name = page else: page_name, module = page.name, page.module code = get_js_code(os.path.join(get_module_path(module), 'page', scrub(page_name), scrub(page_name))) if not code and type(page)!=str: code = page.script # compile for import if code and code.strip(): import re p = re.compile('\$import\( (?P<name> [^)]*) \)', re.VERBOSE) code = p.sub(sub_get_page_js, code) return code
def get_code(module, dt, dn, extn, is_static=None, fieldname=None): from webnotes.modules import scrub, get_module_path import os, webnotes # get module (if required) if not module: module = webnotes.conn.sql("select module from `tab%s` where name=%s" % (dt,'%s'),dn)[0][0] # no module, quit if not module: return '' # file names if scrub(dt) in ('page','doctype','search_criteria'): dt, dn = scrub(dt), scrub(dn) # get file name fname = dn + '.' + extn if is_static: fname = dn + '_static.' + extn # code code = '' try: file = open(os.path.join(get_module_path(scrub(module)), dt, dn, fname), 'r') code = file.read() file.close() except IOError, e: # no file, try from db if fieldname: code = webnotes.conn.get_value(dt, dn, fieldname)
def import_file(module, dt, dn, force=False): """Sync a file from txt if modifed, return false if not updated""" dt, dn = scrub_dt_dn(dt, dn) path = os.path.join(get_module_path(module), os.path.join(dt, dn, dn + '.txt')) return import_file_by_path(path, force)
def get_from_files(self, doc): """ Loads page info from files in module """ from webnotes.modules import get_module_path, scrub import os path = os.path.join(get_module_path(doc.module), 'page', scrub(doc.name)) # script fpath = os.path.join(path, scrub(doc.name) + '.js') if os.path.exists(fpath): with open(fpath, 'r') as f: doc.fields['__script'] = f.read() # css fpath = os.path.join(path, scrub(doc.name) + '.css') if os.path.exists(fpath): with open(fpath, 'r') as f: doc.style = f.read() # html fpath = os.path.join(path, scrub(doc.name) + '.html') if os.path.exists(fpath): with open(fpath, 'r') as f: doc.content = f.read()
def get_modules(for_module=None): import importlib docs = {"_label": "Modules"} if for_module: modules = [for_module] else: modules = webnotes.conn.sql_list( "select name from `tabModule Def` order by name") docs["_toc"] = ["docs.dev.modules." + d for d in modules] for m in modules: prefix = "docs.dev.modules." + m mydocs = docs[m] = { "_icon": "th", "_label": m, "_toc": [prefix + ".doctype", prefix + ".page", prefix + ".py_modules"], "doctype": get_doctypes(m), "page": get_pages(m), #"report": {}, "py_modules": { "_label": "Independent Python Modules for " + m, "_toc": [] } } # add stand alone modules module_path = get_module_path(m) prefix = prefix + ".py_modules." for basepath, folders, files in os.walk(module_path): for f in files: f = cstr(f) if f.endswith(".py") and \ (not f.split(".")[0] in os.path.split(basepath)) and \ (not f.startswith("__")): module_name = ".".join( os.path.relpath(os.path.join(basepath, f), "../app").split(os.path.sep))[:-3] # import module try: module = importlib.import_module(module_name) # create a new namespace for the module module_docs = mydocs["py_modules"][f.split(".") [0]] = {} # add to toc mydocs["py_modules"]["_toc"].append(prefix + f.split(".")[0]) inspect_object_and_update_docs(module_docs, module) except TypeError, e: webnotes.errprint("TypeError in importing " + module_name) except IndentationError, e: continue module_docs["_label"] = module_name module_docs["_function_namespace"] = module_name
def get_modules(for_module=None): import importlib docs = { "_label": "Modules" } if for_module: modules = [for_module] else: modules = webnotes.conn.sql_list("select name from `tabModule Def` order by name") docs["_toc"] = ["docs.dev.modules." + d for d in modules] for m in modules: prefix = "docs.dev.modules." + m mydocs = docs[m] = { "_icon": "th", "_label": m, "_toc": [ prefix + ".doctype", prefix + ".page", prefix + ".py_modules" ], "doctype": get_doctypes(m), "page": get_pages(m), #"report": {}, "py_modules": { "_label": "Independent Python Modules for " + m, "_toc": [] } } # add stand alone modules module_path = get_module_path(m) prefix = prefix + ".py_modules." for basepath, folders, files in os.walk(module_path): for f in files: if f.endswith(".py") and \ (not f.split(".")[0] in os.path.split(basepath)) and \ (not f.startswith("__")): module_name = ".".join(os.path.relpath(os.path.join(basepath, f), "../app").split(os.path.sep))[:-3] # import module try: module = importlib.import_module(module_name) # create a new namespace for the module module_docs = mydocs["py_modules"][f.split(".")[0]] = {} # add to toc mydocs["py_modules"]["_toc"].append(prefix + f.split(".")[0]) inspect_object_and_update_docs(module_docs, module) except TypeError, e: webnotes.errprint("TypeError in importing " + module_name) module_docs["_label"] = module_name module_docs["_function_namespace"] = module_name update_readme(docs[m], m) docs[m]["_gh_source"] = get_gh_url(module_path)
def get_from_files(self): """ Loads page info from files in module """ from webnotes.modules import get_module_path, scrub import os path = os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name)) # script fpath = os.path.join(path, scrub(self.doc.name) + '.js') if os.path.exists(fpath): with open(fpath, 'r') as f: self.doc.script = f.read() # css fpath = os.path.join(path, scrub(self.doc.name) + '.css') if os.path.exists(fpath): with open(fpath, 'r') as f: self.doc.style = f.read() # html fpath = os.path.join(path, scrub(self.doc.name) + '.html') if os.path.exists(fpath): with open(fpath, 'r') as f: self.doc.content = f.read() if webnotes.lang != 'en': from webnotes.translate import update_lang_js self.doc.script = update_lang_js(self.doc.script, path)
def import_file(module, dt, dn, force=False): """Sync a file from txt if modifed, return false if not updated""" webnotes.flags.in_import = True dt, dn = scrub_dt_dn(dt, dn) path = os.path.join(get_module_path(module), os.path.join(dt, dn, dn + '.txt')) ret = import_file_by_path(path, force) webnotes.flags.in_import = False return ret
def make_controller_template(self): from webnotes.modules import get_doc_path, get_module_path, scrub pypath = os.path.join(get_doc_path(self.doc.module, self.doc.doctype, self.doc.name), scrub(self.doc.name) + '.py') if not os.path.exists(pypath): with open(pypath, 'w') as pyfile: with open(os.path.join(get_module_path("core"), "doctype", "doctype", "doctype_template.py"), 'r') as srcfile: pyfile.write(srcfile.read())
def get_script(report_name): report = webnotes.doc("Report", report_name) script_path = os.path.join(get_module_path(webnotes.conn.get_value("DocType", report.ref_doctype, "module")), "report", scrub(report.name), scrub(report.name) + ".js") if os.path.exists(script_path): with open(script_path, "r") as script: return script.read() else: return "wn.query_reports['%s']={}" % report_name
def switch_module(dt, dn, to, frm=None, export=None): """ Change the module of the given doctype, if export is true, then also export txt and copy code files from src """ webnotes.conn.sql("update `tab"+dt+"` set module=%s where name=%s", (to, dn)) if export: export_doc(dt, dn) # copy code files if dt in ('DocType', 'Page', 'Report'): from_path = os.path.join(get_module_path(frm), scrub(dt), scrub(dn), scrub(dn)) to_path = os.path.join(get_module_path(to), scrub(dt), scrub(dn), scrub(dn)) # make dire if exists os.system('mkdir -p %s' % os.path.join(get_module_path(to), scrub(dt), scrub(dn))) for ext in ('py','js','html','css'): os.system('cp %s %s')
def import_file(module, doctype, docname, path=None): "imports a given file into the database" if not path: from webnotes.modules import get_module_path path = get_module_path(module) doclist = get_doclist(path, doctype, docname) if doclist: from webnotes.utils.transfer import set_doc set_doc(doclist, 1, 1, 1)
def onload(self): import os from webnotes.modules import get_module_path, scrub # load content try: file = open(os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name) + '.html'), 'r') self.doc.content = file.read() or '' file.close() except IOError, e: # no file / permission if e.args[0]!=2: raise e
def update_readme(mydocs, module, doctype=None, name=None): if doctype: readme_path = os.path.join(get_doc_path(module, doctype, name), "README.md") else: readme_path = os.path.join(get_module_path(module), "README.md") mydocs["_intro"] = "" if os.path.exists(readme_path): with open(readme_path, "r") as readmefile: mydocs["_intro"] = readmefile.read() mydocs["_modified"] = get_timestamp(readme_path)
def on_update(self): import webnotes.defs if hasattr(webnotes.defs, 'developer_mode') and webnotes.defs.developer_mode: from webnotes.modules.export_module import export_to_files from webnotes.modules import get_module_path, scurb import os export_to_files(record_list=[['Stylesheet', self.doc.name]]) file = open(os.path.join(get_module_path(self.doc.module), 'Stylesheet', scrub(self.doc.name), scrub(self.doc.name) + '.html'), 'w') file.write(self.doc.content) file.close()
def on_update(self): from webnotes.modules.export_module import export_to_files from webnotes.modules import get_module_path, scrub import os from webnotes import defs if getattr(defs,'developer_mode', 0): export_to_files(record_list=[['Page', self.doc.name]]) if self.doc.write_content and self.doc.content: file = open(os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name), scrub(self.doc.name) + '.html'), 'w') file.write(self.doc.content) file.close()
def get_script(report_name): report = webnotes.doc("Report", report_name) script_path = os.path.join(get_module_path(webnotes.conn.get_value("DocType", report.ref_doctype, "module")), "report", scrub(report.name), scrub(report.name) + ".js") if os.path.exists(script_path): with open(script_path, "r") as script: return script.read() elif report.javascript: return report.javascript else: return "wn.query_reports['%s']={}" % report_name
def rename_export(self, old_name): # export the folders self.export_doc() import os, shutil from webnotes.modules import get_module_path, scrub path = os.path.join(get_module_path(self.doc.module), 'search_criteria', scrub(old_name)) # copy py/js files self.copy_file(path, scrub(old_name), '.py') self.copy_file(path, scrub(old_name), '.js') self.copy_file(path, scrub(old_name), '.sql')
def get_folder_paths(modules, record_list): import os import webnotes import fnmatch import webnotes.defs from webnotes.modules import transfer_types, get_module_path, scrub folder_list=[] # get the folder list if record_list: for record in record_list: if scrub(record[1]) in ('doctype', 'page', 'search_criteria'): record[1], record[2] = scrub(record[1]), scrub(record[2]) folder_list.append(os.path.join(get_module_path(scrub(record[0])), \ record[1], record[2].replace('/','_'))) if modules: # system modules will be transferred in a predefined order and before all other modules sys_mod_ordered_list = ['roles', 'core','application_internal', 'mapper', 'settings'] all_mod_ordered_list = [t for t in sys_mod_ordered_list if t in modules] + list(set(modules).difference(sys_mod_ordered_list)) for module in all_mod_ordered_list: mod_path = get_module_path(module) types_list = listfolders(mod_path, 1) # list of types types_list = list(set(types_list).difference(['control_panel'])) all_transfer_types =[t for t in transfer_types if t in types_list] + list(set(types_list).difference(transfer_types)) # build the folders for d in all_transfer_types: if d not in ('files', 'startup', 'patches'): # get all folders inside type folder_list+=listfolders(os.path.join(mod_path, d)) return folder_list
def create_folder(module, dt, dn, create_init): module_path = get_module_path(module) dt, dn = scrub_dt_dn(dt, dn) # create folder folder = os.path.join(module_path, dt, dn) webnotes.create_folder(folder) # create init_py_files if create_init: create_init_py(module_path, dt, dn) return folder
def switch_module(dt, dn, to, frm=None, export=None): """ Change the module of the given doctype, if export is true, then also export txt and copy code files from src """ webnotes.conn.sql("update `tab" + dt + "` set module=%s where name=%s", (to, dn)) if export: export_doc(dt, dn) # copy code files if dt in ('DocType', 'Page', 'Report'): from_path = os.path.join(get_module_path(frm), scrub(dt), scrub(dn), scrub(dn)) to_path = os.path.join(get_module_path(to), scrub(dt), scrub(dn), scrub(dn)) # make dire if exists os.system('mkdir -p %s' % os.path.join(get_module_path(to), scrub(dt), scrub(dn))) for ext in ('py', 'js', 'html', 'css'): os.system('cp %s %s')
def create_folder(module, dt, dn, code_type): # get module path by importing the module module_path = get_module_path(module) dt, dn = scrub_dt_dn(dt, dn) # create folder folder = os.path.join(module_path, dt, dn) webnotes.create_folder(folder) # create init_py_files if code_type: create_init_py(module_path, dt, dn) return folder
def on_update(self): import webnotes.defs from webnotes.modules.export_module import export_to_files from webnotes.modules import get_module_path, scrub import os if hasattr(webnotes.defs, 'developer_mode') and webnotes.defs.developer_mode: export_to_files(record_list=[['Page Template', self.doc.name]]) file = open( os.path.join(get_module_path(self.doc.module), 'Page Template', self.doc.name, self.doc.name + '.html'), 'w') file.write(self.doc.content) file.close()
def import_attachments(m): import os, webnotes.defs import webnotes.utils.file_manager from webnotes.modules import get_module_path out = [] # get list try: folder = os.path.join(get_module_path(m), 'files') fl = os.listdir(folder) except OSError, e: if e.args[0]==2: return else: raise e
def on_update(self): """ Writes the .txt for this page and if write_content is checked, it will write out a .html file """ from webnotes import defs from webnotes.utils.transfer import in_transfer if not in_transfer and getattr(defs,'developer_mode', 0): from webnotes.modules.export_module import export_to_files from webnotes.modules import get_module_path, scrub import os export_to_files(record_list=[['Page', self.doc.name]]) if self.doc.write_content and self.doc.content: file = open(os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name), scrub(self.doc.name) + '.html'), 'w') file.write(self.doc.content) file.close()
def get_page_js(page): import webnotes, os from webnotes.modules import scrub, get_module_path code = get_js_code(os.path.join(get_module_path(page.module), 'page', scrub(page.name), scrub(page.name))) if not code: code = page.script # compile for import if code and code.strip(): import re p = re.compile('\$import\( (?P<name> [^)]*) \)', re.VERBOSE) code = p.sub(sub_get_page_js, code) return code
def create_folder(module, dt, dn): import webnotes, os # get module path by importing the module modules_path = get_module_path(module) code_type = dt in ['DocType', 'Page', 'Search Criteria'] # create folder folder = os.path.join(modules_path, code_type and scrub(dt) or dt, code_type and scrub(dn) or dn) webnotes.create_folder(folder) # create init_py_files if code_type: create_init_py(modules_path, scrub(dt), scrub(dn)) return folder
def get_doctype_js(dt): import webnotes, os from webnotes.modules import scrub, get_module_path dt_details = webnotes.conn.sql('select module, client_script from tabDocType where name = %s', dt) module = scrub(dt_details[0][0]) code = get_js_code(os.path.join(get_module_path(scrub(module)), 'doctype', scrub(dt), scrub(dt))) \ + '\n' + (dt_details[0][1] or '') # compile for import if code.strip(): import re p = re.compile('\$import\( (?P<name> [^)]*) \)', re.VERBOSE) code = p.sub(sub_get_doctype_js, code) return code
def import_module(module, verbose=0): "imports the all the records and files from the given module" from webnotes.modules import get_module_path import os not_module = ('startup', 'event_handlers', 'files', 'patches') if module in not_module: if verbose: webnotes.msgprint('%s is not a module' % module) return path = get_module_path(module) for doctype in listfolders(path, 1): for docname in listfolders(os.path.join(path, doctype), 1): import_file(module, doctype, docname, path) if verbose: webnotes.msgprint('Imported %s/%s/%s' % (module, doctype, docname)) import_attachments(module)
def create_folder(module, dt, dn, code_type, plugin=None): if plugin: module_path = os.path.join(get_plugin_path(plugin), scrub(module)) else: module_path = get_module_path(module) dt, dn = scrub_dt_dn(dt, dn) # create folder folder = os.path.join(module_path, dt, dn) webnotes.create_folder(folder) # create init_py_files if code_type: create_init_py(module_path, dt, dn) return folder
def onload(self): """ loads html from file before passing """ import os from webnotes.modules import get_module_path, scrub # load content if not self.doc.module: return try: file = open(os.path.join(get_module_path(self.doc.module), 'page', scrub(self.doc.name) + '.html'), 'r') self.doc.content = file.read() or '' file.close() except IOError, e: # no file / permission if e.args[0]!=2: raise e
def add_code(doctype, doclist): import os, conf from webnotes.modules import scrub, get_module_path doc = doclist[0] path = os.path.join(get_module_path(doc.module), 'doctype', scrub(doc.name)) def _add_code(fname, fieldname): fpath = os.path.join(path, fname) if os.path.exists(fpath): with open(fpath, 'r') as f: doc.fields[fieldname] = f.read() _add_code(scrub(doc.name) + '.js', '__js') _add_code(scrub(doc.name) + '.css', '__css') _add_code('%s_list.js' % scrub(doc.name), '__listjs') add_embedded_js(doc)
def get_script(report_name): report = webnotes.doc("Report", report_name) module = webnotes.conn.get_value("DocType", report.ref_doctype, "module") module_path = get_module_path(module) report_folder = os.path.join(module_path, "report", scrub(report.name)) script_path = os.path.join(report_folder, scrub(report.name) + ".js") script = None if os.path.exists(script_path): with open(script_path, "r") as script: script = script.read() if not script and report.is_standard == "No": script = webnotes.plugins.read_file(module, "Report", report.name, extn="js", cache=True) if not script and report.javascript: script = report.javascript if not script: script = "wn.query_reports['%s']={}" % report_name # load translations if webnotes.lang != "en": from webnotes.translate import get_lang_data if os.path.exists(report_folder): messages = get_lang_data(report_folder, webnotes.lang, 'js') webnotes.response["__messages"] = messages else: # TODO check if language files get exported here plugins_report_folder = webnotes.plugins.get_path( module, "Report", report.name) if os.path.exists(plugins_report_folder): messages = get_lang_data(plugins_report_folder, webnotes.lang, 'js') webnotes.response["__messages"] = messages return script
def create_folder(module, dt, dn): """ Creates directories for module and their __init__.py """ import webnotes, os # get module path by importing the module modules_path = get_module_path(module) code_type = dt in ['DocType', 'Page', 'Search Criteria'] # create folder folder = os.path.join(modules_path, code_type and scrub(dt) or dt, code_type and scrub(dn) or dn) webnotes.create_folder(folder) # create init_py_files if code_type: create_init_py(modules_path, scrub(dt), scrub(dn)) return folder