def make_error_snapshot(exception): if dataent.conf.disable_error_snapshot: return logger = dataent.logger(__name__, with_more_info=False) try: error_id = '{timestamp:s}-{ip:s}-{hash:s}'.format( timestamp=cstr(datetime.datetime.now()), ip=dataent.local.request_ip or '127.0.0.1', hash=dataent.generate_hash(length=3)) snapshot_folder = get_error_snapshot_path() dataent.create_folder(snapshot_folder) snapshot_file_path = os.path.join(snapshot_folder, "{0}.json".format(error_id)) snapshot = get_snapshot(exception) with open(encode(snapshot_file_path), 'wb') as error_file: error_file.write(encode(dataent.as_json(snapshot))) logger.error('New Exception collected with id: {}'.format(error_id)) except Exception as e: logger.error('Could not take error snapshot: {0}'.format(e), exc_info=True)
def create_new_connection(module, connection_name): if not dataent.conf.get('developer_mode'): dataent.msgprint(_('Please enable developer mode to create new connection')) return # create folder module_path = dataent.get_module_path(module) connectors_folder = os.path.join(module_path, 'connectors') dataent.create_folder(connectors_folder) # create init py create_init_py(module_path, 'connectors', '') connection_class = connection_name.replace(' ', '') file_name = dataent.scrub(connection_name) + '.py' file_path = os.path.join(module_path, 'connectors', file_name) # create boilerplate file with open(file_path, 'w') as f: f.write(connection_boilerplate.format(connection_class=connection_class)) # get python module string from file_path app_name = dataent.db.get_value('Module Def', module, 'app_name') python_module = os.path.relpath( file_path, '../apps/{0}'.format(app_name)).replace(os.path.sep, '.')[:-3] return python_module
def make_site_config(db_name=None, db_password=None, site_config=None): dataent.create_folder(os.path.join(dataent.local.site_path)) site_file = get_site_config_path() if not os.path.exists(site_file): if not (site_config and isinstance(site_config, dict)): site_config = get_conf_params(db_name, db_password) with open(site_file, "w") as f: f.write(json.dumps(site_config, indent=1, sort_keys=True))
def write_file(content, fname, is_private=0): """write file to disk with a random name (to compare)""" file_path = get_files_path(is_private=is_private) # create directory (if not exists) dataent.create_folder(file_path) # write the file if isinstance(content, text_type): content = content.encode() with open(os.path.join(file_path.encode('utf-8'), fname.encode('utf-8')), 'wb+') as f: f.write(content) return get_files_path(fname, is_private=is_private)
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) dataent.create_folder(folder) # create init_py_files if create_init: create_init_py(module_path, dt, dn) return folder
def write_translations_file(app, lang, full_dict=None, app_messages=None): """Write a translation file for a given language. :param app: `app` for which translations are to be written. :param lang: Language code. :param full_dict: Full translated language dict (optional). :param app_messages: Source strings (optional). """ if not app_messages: app_messages = get_messages_for_app(app) if not app_messages: return tpath = dataent.get_pymodule_path(app, "translations") dataent.create_folder(tpath) write_csv_file(os.path.join(tpath, lang + ".csv"), app_messages, full_dict or get_full_dict(lang))
def execute(): if not os.path.exists( os.path.join(dataent.local.site_path, 'private', 'files')): dataent.create_folder( os.path.join(dataent.local.site_path, 'private', 'files'))
def make_boilerplate(dest, app_name): if not os.path.exists(dest): print("Destination directory does not exist") return # app_name should be in snake_case app_name = dataent.scrub(app_name) hooks = dataent._dict() hooks.app_name = app_name app_title = hooks.app_name.replace("_", " ").title() for key in ("App Title (default: {0})".format(app_title), "App Description", "App Publisher", "App Email", "App Icon (default 'octicon octicon-file-directory')", "App Color (default 'grey')", "App License (default 'MIT')"): hook_key = key.split(" (")[0].lower().replace(" ", "_") hook_val = None while not hook_val: hook_val = cstr(input(key + ": ")) if not hook_val: defaults = { "app_title": app_title, "app_icon": "octicon octicon-file-directory", "app_color": "grey", "app_license": "MIT" } if hook_key in defaults: hook_val = defaults[hook_key] if hook_key == "app_name" and hook_val.lower().replace( " ", "_") != hook_val: print("App Name must be all lowercase and without spaces") hook_val = "" elif hook_key == "app_title" and not re.match( "^(?![\W])[^\d_\s][\w -]+$", hook_val, re.UNICODE): print( "App Title should start with a letter and it can only consist of letters, numbers, spaces and underscores" ) hook_val = "" hooks[hook_key] = hook_val dataent.create_folder(os.path.join(dest, hooks.app_name, hooks.app_name, dataent.scrub(hooks.app_title)), with_init=True) dataent.create_folder(os.path.join(dest, hooks.app_name, hooks.app_name, "templates"), with_init=True) dataent.create_folder( os.path.join(dest, hooks.app_name, hooks.app_name, "www")) dataent.create_folder(os.path.join(dest, hooks.app_name, hooks.app_name, "templates", "pages"), with_init=True) dataent.create_folder( os.path.join(dest, hooks.app_name, hooks.app_name, "templates", "includes")) dataent.create_folder(os.path.join(dest, hooks.app_name, hooks.app_name, "config"), with_init=True) dataent.create_folder( os.path.join(dest, hooks.app_name, hooks.app_name, "public", "css")) dataent.create_folder( os.path.join(dest, hooks.app_name, hooks.app_name, "public", "js")) with open( os.path.join(dest, hooks.app_name, hooks.app_name, "__init__.py"), "w") as f: f.write(dataent.as_unicode(init_template)) with open(os.path.join(dest, hooks.app_name, "MANIFEST.in"), "w") as f: f.write(dataent.as_unicode(manifest_template.format(**hooks))) with open(os.path.join(dest, hooks.app_name, ".gitignore"), "w") as f: f.write( dataent.as_unicode( gitignore_template.format(app_name=hooks.app_name))) with open(os.path.join(dest, hooks.app_name, "setup.py"), "w") as f: f.write(dataent.as_unicode(setup_template.format(**hooks))) with open(os.path.join(dest, hooks.app_name, "requirements.txt"), "w") as f: f.write("dataent") with open(os.path.join(dest, hooks.app_name, "README.md"), "w") as f: f.write( dataent.as_unicode("## {0}\n\n{1}\n\n#### License\n\n{2}".format( hooks.app_title, hooks.app_description, hooks.app_license))) with open(os.path.join(dest, hooks.app_name, "license.txt"), "w") as f: f.write(dataent.as_unicode("License: " + hooks.app_license)) with open( os.path.join(dest, hooks.app_name, hooks.app_name, "modules.txt"), "w") as f: f.write(dataent.as_unicode(hooks.app_title)) with open(os.path.join(dest, hooks.app_name, hooks.app_name, "hooks.py"), "w") as f: f.write(dataent.as_unicode(hooks_template.format(**hooks))) touch_file( os.path.join(dest, hooks.app_name, hooks.app_name, "patches.txt")) with open( os.path.join(dest, hooks.app_name, hooks.app_name, "config", "desktop.py"), "w") as f: f.write(dataent.as_unicode(desktop_template.format(**hooks))) with open( os.path.join(dest, hooks.app_name, hooks.app_name, "config", "docs.py"), "w") as f: f.write(dataent.as_unicode(docs_template.format(**hooks))) print("'{app}' created at {path}".format(app=app_name, path=os.path.join(dest, app_name)))