def diff_ref_db(): """get diff using database as reference""" from webnotes.modules import scrub for dt in dt_map: # get all main docs for doc in webnotes.conn.sql("""select * from `tab%s`""" % dt, as_dict=1): # get file for this doc doc['doctype'] = dt #print doc['name'], doc['doctype'], doc['module'] path = os.path.join(conf.modules_path, scrub(doc['module']), \ scrub(dt), scrub(doc['name']), scrub(doc['name']) + '.txt') path_core = os.path.join(os.getcwd(), 'lib', 'py', 'core', \ scrub(dt), scrub(doc['name']), scrub(doc['name']) + '.txt') if os.path.exists(path): with open(path, 'r') as txtfile: target = peval_doclist(txtfile.read()) elif os.path.exists(path_core): with open(path_core, 'r') as txtfile: target = peval_doclist(txtfile.read()) else: target = [None,] doc_diff(doc, target[0]) # do diff for child records if target[0] and dt_map[dt].keys(): for child_dt in dt_map[dt]: # for each child type, we need to create # a key (e.g. fieldname, label) based mapping of child records in # txt files child_key_map = {} keys = dt_map[dt][child_dt] for target_d in target: if target_d['doctype'] == child_dt: for key in keys: if target_d.get(key): child_key_map[target_d.get(key)] = target_d break for d in webnotes.conn.sql("""select * from `tab%s` where parent=%s and docstatus<2""" % (child_dt, '%s'), doc['name'], as_dict=1): source_key = None d['doctype'] = child_dt for key in keys: if d.get(key): source_key = d.get(key) break # only if a key is found if source_key: doc_diff(d, child_key_map.get(source_key), source_key) print_stats()
def get_diff(path): for wt in os.walk(path): for fname in wt[2]: if fname.endswith('.txt'): path = os.path.join(wt[0], fname) with open(path, 'r') as txtfile: doclist_diff(peval_doclist(txtfile.read()))
def sync(module_name, docname, force=0): """sync doctype from file if modified""" with open(get_file_path(module_name, docname), 'r') as f: from webnotes.model.utils import peval_doclist doclist = peval_doclist(f.read()) modified = doclist[0]['modified'] if not doclist: raise Exception('DocList could not be evaluated') if modified == str(webnotes.conn.get_value(doclist[0].get('doctype'), doclist[0].get('name'), 'modified')) and not force: return webnotes.conn.begin() delete_doctype_docfields(doclist) save_doctype_docfields(doclist) save_perms_if_none_exist(doclist) webnotes.conn.sql("""UPDATE `tab{doctype}` SET modified=%s WHERE name=%s""".format(doctype=doclist[0]['doctype']), (modified, doclist[0]['name'])) webnotes.conn.commit() print module_name, '|', docname #raise Exception return doclist[0].get('name')
def sync(self, force=1): """ import the doclist if new """ from webnotes.model.utils import peval_doclist doclist = peval_doclist(self.read()) if doclist: from webnotes.utils.transfer import set_doc set_doc(doclist, 1, 1, 1) # since there is a new timestamp on the file, update timestamp in # the record webnotes.conn.sql("update `tab%s` set modified=now() where name=%s" \ % (doclist[0]['doctype'], '%s'), doclist[0]['name'])
def get_doclist(path, doctype, docname): "returns a doclist (list of dictionaries) of multiple records for the given parameters" import os from webnotes.model.utils import peval_doclist do_not_import = ('control_panel') fname = os.path.join(path,doctype,docname,docname+'.txt') if os.path.exists(fname) and (doctype not in do_not_import): f = open(fname,'r') dl = peval_doclist(f.read()) f.close() return dl else: return None
def diff_ref_file(): """get diff using .txt files as reference""" global missing, property_diff, property_count missing = property_diff = 0 property_count = {} for wt in os.walk(webnotes.defs.modules_path): for fname in wt[2]: if fname.endswith('.txt'): path = os.path.join(wt[0], fname) with open(path, 'r') as txtfile: doclist_diff(peval_doclist(txtfile.read())) print_stats()
def _get_fields(self, file_name): """ Returns a dictionary of DocFields by fieldname or label """ try: txt = open(file_name, 'r').read() except: return from webnotes.model.utils import peval_doclist doclist = peval_doclist(txt) fields = {} for d in doclist: if d['doctype']=='DocField': if d.get('fieldname') or d.get('label'): fields[d.get('fieldname') or d.get('label')] = d return fields
def _get_fields(self, file_name): """ Returns a dictionary of DocFields by fieldname or label """ try: txt = open(file_name, 'r').read() except: return from webnotes.model.utils import peval_doclist doclist = peval_doclist(txt) fields = {} for d in doclist: if d['doctype'] == 'DocField': if d.get('fieldname') or d.get('label'): fields[d.get('fieldname') or d.get('label')] = d return fields
def prepare_dict_of_label_fieldname(module_path): from webnotes.model.utils import peval_doclist from webnotes.model.sync import get_file_path doctype = {} for path, folders, files in os.walk(module_path): if path == module_path: modules_list = folders for f in files: if f.endswith(".txt"): rel_path = os.path.relpath(path, conf.modules_path) path_tuple = rel_path.split(os.sep) if (len(path_tuple)==3 and path_tuple[0] in modules_list and path_tuple[1] == 'doctype'): file_name = f[:-4] with open(get_file_path(path_tuple[0], file_name), 'r') as fn: doclist = peval_doclist(fn.read()) doctype[file_name] = dict(([d.get('label'),d.get('fieldname')] \ for d in doclist if d.get('doctype')=='DocField')) return doctype
def reload_single_doc(module, dt, dn): """Sync a file from txt""" import os dt, dn = scrub_dt_dn(dt, dn) path = os.path.join(get_module_path(module), os.path.join(dt, dn, dn + '.txt')) if os.path.exists(path): from webnotes.model.utils import peval_doclist with open(path, 'r') as f: doclist = peval_doclist(f.read()) if doclist: from webnotes.utils.transfer import set_doc set_doc(doclist, 1, 1, 1) # since there is a new timestamp on the file, update timestamp in webnotes.conn.sql("update `tab%s` set modified=now() where name=%s" \ % (doclist[0]['doctype'], '%s'), doclist[0]['name']) else: raise Exception, '%s missing' % path