<s> from __future__ import unicode_literals <EOL> import frappe , os , json <EOL> import frappe . model <EOL> from frappe . modules import scrub , get_module_path , lower_case_files_for , scrub_dt_dn <EOL> def export_doc ( doc ) : <EOL> export_to_files ( [ [ doc . doctype , doc . name ] ] ) <EOL> def export_to_files ( record_list = None , record_module = None , verbose = <NUM_LIT:0> , create_init = None ) : <EOL> """<STR_LIT>""" <EOL> if frappe . flags . in_import : <EOL> return <EOL> if record_list : <EOL> for record in record_list : <EOL> write_document_file ( frappe . get_doc ( record [ <NUM_LIT:0> ] , record [ <NUM_LIT:1> ] ) , record_module , create_init = create_init ) <EOL> def write_document_file ( doc , record_module = None , create_init = None ) : <EOL> newdoc = doc . as_dict ( no_nulls = True ) <EOL> for df in doc . meta . get_table_fields ( ) : <EOL> for d in newdoc . get ( df . fieldname ) : <EOL> for fieldname in frappe . model . default_fields : <EOL> if fieldname in d : <EOL> del d [ fieldname ] <EOL> module = record_module or get_module_name ( doc ) <EOL> if create_init is None : <EOL> create_init = doc . doctype in lower_case_files_for <EOL> folder = create_folder ( module , doc . doctype , doc . name , create_init ) <EOL> fname = ( doc . doctype in lower_case_files_for and scrub ( doc . name ) ) or doc . name <EOL> with open ( os . path . join ( folder , fname + "<STR_LIT>" ) , '<STR_LIT>' ) as txtfile : <EOL> txtfile . write ( frappe . as_json ( newdoc ) ) <EOL> def get_module_name ( doc ) : <EOL> if doc . doctype == '<STR_LIT>' : <EOL> module = doc . name <EOL> elif doc . doctype == "<STR_LIT>" : <EOL> module = frappe . db . get_value ( "<STR_LIT>" , doc . document_type , "<STR_LIT>" ) <EOL> elif hasattr ( doc , '<STR_LIT>' ) : <EOL> module = doc . module <EOL> else : <EOL> module = frappe . db . get_value ( "<STR_LIT>" , doc . doctype , "<STR_LIT>" ) <EOL> return module <EOL> def create_folder ( module , dt , dn , create_init ) : <EOL> module_path = get_module_path ( module ) <EOL> dt , dn = scrub_dt_dn ( dt , dn ) <EOL> folder = os . path . join ( module_path , dt , dn ) <EOL> frappe . create_folder ( folder ) <EOL> if create_init : <EOL> create_init_py ( module_path , dt , dn ) <EOL> return folder <EOL> def create_init_py ( module_path , dt , dn ) : <EOL> def create_if_not_exists ( path ) : <EOL> initpy = os . path . join ( path , '<STR_LIT>' ) <EOL> if not os . path . exists ( initpy ) : <EOL> open ( initpy , '<STR_LIT:w>' ) . close ( ) <EOL> create_if_not_exists ( os . path . join ( module_path ) ) <EOL> create_if_not_exists ( os . path . join ( module_path , dt ) ) <EOL> create_if_not_exists ( os . path . join ( module_path , dt , dn ) ) </s>
