def unregisterRefObj(self, ob): ref = self.getRefObjPath(ob) standard.writeLog(self, '[unregisterRefObj]: ref=' + ref) ref_by = self.synchronizeRefByObjs() if ref in ref_by: ref_by = [x for x in ref_by if x != ref] self.ref_by = ref_by
def __bobo_traverse__(self, TraversalRequest, name): # If the name is in the list of attributes, call it. attr = getattr(self, name, None) if attr is not None: return attr # otherwise do some 'magic' else: standard.writeLog( self, "[ZMSMetamodelProvider.__bobo_traverse__]: otherwise do some 'magic'" ) ob = self.getHome().aq_parent while ob is not None: content = getattr(ob, 'content', None) if content is not None: metaobj_manager = getattr(content, self.id, None) if metaobj_manager is not None: # If the name is in the list of attributes, call it. attr = getattr(metaobj_manager, name, None) if attr is not None: return attr ob = getattr(ob, 'aq_parent', None) return None
def remoteFiles(self, basepath): standard.writeLog(self, "[remoteFiles]: basepath=%s" % basepath) r = {} if os.path.exists(basepath): def traverse(base, path): names = os.listdir(path) for name in names: filepath = os.path.join(path, name) if os.path.isdir(filepath): traverse(base, filepath) elif name.startswith('__') and name.endswith('__.py'): # Read python-representation of repository-object standard.writeLog(self, "[remoteFiles]: read %s" % filepath) f = open(filepath, "rb") # py = standard.pystr(f.read()) py = f.read() f.close() # Analyze python-representation of repository-object d = {} try: c = get_class(py) d = c.__dict__ except: d['revision'] = standard.writeError( self, "[traverse]: can't analyze filepath=%s" % filepath) id = d.get('id', name) rd = {} rd['id'] = id rd['filename'] = filepath[len(base) + 1:] rd['data'] = py rd['version'] = d.get( "revision", self.getLangFmtDate(os.path.getmtime(filepath), 'eng')) r[rd['filename']] = rd # Read artefacts and avoid processing of hidden files, e.g. .DS_Store on macOS for file in [ x for x in names if x != name and not x.startswith('.') ]: artefact = os.path.join(path, file) if os.path.isfile(artefact): standard.writeLog( self, "[remoteFiles]: read artefact %s" % artefact) f = open(artefact, "rb") data = f.read() f.close() rd = {} rd['id'] = id rd['filename'] = artefact[len(base) + 1:] rd['data'] = data rd['version'] = self.getLangFmtDate( os.path.getmtime(artefact), 'eng') r[rd['filename']] = rd traverse(basepath, basepath) return r
def registerRefObj(self, ob): ref = self.getRefObjPath(ob) standard.writeLog(self, '[registerRefObj]: ref=' + ref) ref_by = self.synchronizeRefByObjs() if ref not in ref_by: ref_by.append(ref) self.ref_by = ref_by
def toXhtml(self, REQUEST, deep=True): standard.writeLog(self, '[toXhtml]') level = 0 html = '' if 'ZMS_PAGE_HTML_HEADER' in REQUEST: html += getattr(self, REQUEST.get('ZMS_PAGE_HTML_HEADER'))(self, REQUEST) else: html += '<html>\n' html += '<head>\n' html += self.f_headMeta_Locale(self, REQUEST) html += '<title>%s</title>\n' % self.getTitle(REQUEST) html += '</head>\n' html += '<body>\n' print_html = self.printHtml(level, _globals.MySectionizer(), REQUEST, deep) try: html += print_html except: html += standard.writeError(self, "[toXhtml]: can't append printHtml") if 'ZMS_PAGE_HTML_FOOTER' in REQUEST: html += getattr(self, REQUEST.get('ZMS_PAGE_HTML_FOOTER'))(self, REQUEST) else: html += '</body>\n' html += '</html>\n' html = localHtml(self, html) html = localIndexHtml(self, self, level, html, xhtml=True) return html
def updateChanges(self, ids, override=False): standard.writeLog(self, "[updateChanges]: ids=%s" % str(ids)) standard.triggerEvent(self, 'beforeUpdateRepositoryEvt') success = [] failure = [] repositories = {} for i in ids: # Initialize. provider_id = i[:i.find(':')] id = i[i.find(':') + 1:] provider = getattr(self, provider_id) # Read repositories for provider. if provider_id not in repositories: repositories[provider_id] = self.readRepository(provider) repository = repositories[provider_id] # Update. try: r = repository[id] provider.updateRepository(r) success.append(id) except: standard.writeError(self, "[updateChanges]: can't %s" % id) failure.append(id) standard.triggerEvent(self, 'afterUpdateRepositoryEvt') return success, failure
def exportExternalResources(self, obj, html, path, REQUEST): domains = [] for domain in self.getConfProperty('ZMS.export.domains', '').split(','): domain = domain.strip() if len(domain) > 0: domains.append(domain) if len(domains) == 0: return html for http_prefix in ['http:']: i = html.find(http_prefix) while i > 0: d = rfindDelimiter(html[:i]) # search delimiter ' or " k = rfindDelimiter( html[:d], '=') # search equal-sign between attribute name t = rfindDelimiter(html[:k], '<') # search start of tag # <img src="url"> # <a href='url'"> if (html[ t + 1: t + 4].lower() == 'img' and html[ k - 3: k].lower() == 'src') \ or (html[ t + 1].lower() == 'a' and html[ k - 4: k].lower() == 'href'): l = findDelimiter(html[d + 1:]) url = html[d + 1:d + l + 1] for domain in domains: if domain in url: try: standard.writeLog( self, '[exportExternalResources]: url=%s' % url) s_new = s_old = url for repl in ':/%&?;=': s_new = s_new.replace(repl, '_') # test if extension is a real extension at the end ? # http://host:port/uri.gif?a=x&b=k => http___host_port_uri.gif_a_x_b_k.gif # http://host:port/uri.gif => http__host_port_uri.gif # http://host:port/draw/ID/png => http__host_port_draw_ID_png.png # http://host:port/draw/ID?fmt=pdf&scale=2 => http__host_port_draw_ID_fmt_pdf_scale_2.pdf for ext in [ 'gif', 'jpg', 'png', 'pdf', 'csv', 'xls', 'doc', 'ppt' ]: if ext in url: if s_new[-len(ext) - 1:] != '.%s' % ext: s_new = "%s.%s" % (s_new, ext) break ext_path = '%s/%s' % (path, s_new) if not os.path.exists(ext_path): data = self.http_import(url) f = open(ext_path, 'w') f.write(data) f.close() html = html.replace(s_old, s_new) except: standard.writeError( self, '[exportExternalResources]: url=%s' % url) break i = html.find(http_prefix, i + len(http_prefix)) return html
def search(self, q, fq='', order=None): rtn = [] # ZCatalog. request = self.REQUEST lang = request.get('lang', self.getPrimaryLanguage()) zcatalog = getZCatalog(self, lang) # Find search-results. items = [] prototype = {} for fqs in fq.split(','): attr_id = fqs[:fqs.find(':')] if attr_id.endswith('_s'): attr_id = attr_id[:-2] fqk = 'zcat_index_%s' % attr_id if fqk in zcatalog.indexes(): fqv = fqs[fqs.find(':') + 1:] fqv = umlaut_quote(self, fqv) prototype[fqk] = fqv for index in zcatalog.indexes(): if index.find('zcat_index_') == 0: query = copy.deepcopy(prototype) query[index] = umlaut_quote(self, q) qr = zcatalog(query) standard.writeLog(self, "[search]: %s=%i" % (str(query), len(qr))) for item in qr: if item not in items: items.append(item.aq_base) # Process search-results. results = [] for item in items: data_record_id = item.data_record_id_ path = zcatalog.getpath(data_record_id) # Append to valid results. if len([x for x in results if x[1]['path'] == path]) == 0: result = {} result['path'] = path result['score'] = intValue(item.data_record_score_) result['normscore'] = intValue( item.data_record_normalized_score_) for column in zcatalog.schema(): k = column if k.find('zcat_index_') == 0: k = k[len('zcat_index_'):] result[k] = getattr(item, column, None) results.append((item.data_record_score_, result)) # Sort search-results. results = sorted(results, key=lambda x: x[0]) results.reverse() # Append search-results. rtn.extend([x[1] for x in results]) # Return list of search-results in correct sort-order. return rtn
def traverse(base, path, level=0): names = os.listdir(path) for name in names: filepath = os.path.join(path, name) if os.path.isdir(filepath) and (deep or level == 0): traverse(base, filepath, level + 1) elif name.startswith('__') and name.endswith('__.py'): # Read python-representation of repository-object standard.writeLog(self, "[readRepository]: read %s" % filepath) f = open(filepath, "rb") py = standard.pystr(f.read()) f.close() # Analyze python-representation of repository-object d = {} try: c = get_class(py) d = c.__dict__ except: d['revision'] = standard.writeError( self, "[readRepository]: ") id = d.get('id', name) r[id] = {} for k in [x for x in d if not x.startswith('__')]: v = d[k] if inspect.isclass(v): dd = v.__dict__ v = [] for kk in [ x for x in dd if not x.startswith('__') ]: vv = dd[kk] # Try to read artefact. if 'id' in vv: fileprefix = vv['id'].split('/')[-1] for file in [ x for x in names if x == fileprefix or x.startswith('%s.' % fileprefix) ]: artefact = os.path.join(path, file) standard.writeLog( self, "[readRepository]: read artefact %s" % artefact) f = open(artefact, "rb") data = f.read() f.close() try: if isinstance(data, bytes): data = data.decode('utf-8') except: pass vv['data'] = data break v.append((py.find('\t\t%s =' % kk), vv)) v.sort() v = [x[1] for x in v] r[id][k] = v
def manage_copyObjects(self, ids=None, REQUEST=None, RESPONSE=None): """Put a reference to the objects named in ids in the clip board""" standard.writeLog(self, "[CopySupport.manage_copyObjects]") super(self.__class__, self).manage_copyObjects(ids, REQUEST, RESPONSE) # Return with message. if RESPONSE is not None: message = '' RESPONSE.redirect('manage_main?lang=%s&manage_tabs_message=%s' % (REQUEST['lang'], standard.url_quote(message)))
def initObjChildren(self, REQUEST): standard.writeLog(self, "[initObjChildren]") self.getObjProperty('initObjChildren', REQUEST) metaObj = self.getMetaobj(self.meta_id) metaObjIds = self.getMetaobjIds() + ['*'] for metaObjAttrId in self.getMetaobjAttrIds(self.meta_id): metaObjAttr = self.getMetaobjAttr(self.meta_id, metaObjAttrId) if metaObjAttr['type'] in metaObjIds: self._initObjChildren(metaObjAttr, REQUEST)
def _set_sort_ids(self, ids, op, REQUEST): standard.writeLog( self, "[CopySupport._set_sort_ids]: %s" % self.absolute_url()) copy_of_prefix = 'copy_of_' sort_id = REQUEST.get('_sort_id', 0) + 1 for ob in self.getChildNodes(): id = ob.getId() if (id in ids) or (op == OP_MOVE and copy_of_prefix + id in ids): ob.setSortId(sort_id) sort_id += 1
def processFile(self, processId, filename, trans=None): standard.writeLog( self, '[processFile]: processId=%s'%processId) folder = _fileutil.getFilePath(filename) processOb = self.getFilterManager().getProcess(processId) command = processOb.get('command') # Save transformation to file. if trans: transfilename = '%s/%s'%( folder, trans.getId()) command = command.replace( '{trans}', transfilename) # Execute command. filename = processCommand(self, filename, command) # Return filename. return filename
def processCommand(self, filename, command): standard.writeLog( self, '[processCommand]: infilename=%s'%filename) infilename = _fileutil.getOSPath( filename) outfilename = _fileutil.getOSPath( filename) mCurDir = '{cur_dir}' mIn = '{in}' mOut = '{out}' i = command.find(mOut[:-1]) if i >= 0: j = command.find('}', i) mExt = command[i+len(mOut[:-1]):j] mOut = command[i:j+1] if len(mExt) > 0: outfilename = outfilename[:outfilename.rfind('.')] + mExt else: outfilename += '.tmp' tmpoutfilename = outfilename + '~' instance_home = standard.getINSTANCE_HOME() package_home = standard.getPACKAGE_HOME() package_home = os.path.normpath(package_home) command = command.replace( '{package_home}', package_home) command = command.replace( '{instance_home}', instance_home) command = command.replace( mCurDir, _fileutil.getFilePath(infilename)) command = command.replace( mIn, infilename) command = command.replace( mOut, tmpoutfilename) # Change directory (deprecated!). if self.getConfProperty('ZMS.filtermanager.processCommand.chdir', 0): path = _fileutil.getFilePath(filename) standard.writeLog( self, '[processCommand]: path=%s'%path) os.chdir(path) # Execute command. standard.writeLog( self, '[processCommand]: command=%s'%command) os.system(command) # Check if output file exists. try: os.stat( _fileutil.getOSPath( tmpoutfilename)) standard.writeLog( self, '[processCommand]: rename %s to %s'%( tmpoutfilename, outfilename)) try: os.remove( outfilename) except OSError: pass os.rename( tmpoutfilename, outfilename) except OSError: outfilename = infilename # Remove input file if it is the result of a transformation of output file. if outfilename != infilename: os.remove( infilename) # Return filename. standard.writeLog( self, '[processCommand]: outfilename=%s'%( outfilename)) return outfilename
def processMethod(self, processId, filename, trans, REQUEST): standard.writeLog( self, '[processMethod]: processId=%s'%processId) infilename = filename outfilename = filename REQUEST.set( 'ZMS_FILTER_IN', infilename) REQUEST.set( 'ZMS_FILTER_OUT', outfilename) REQUEST.set( 'ZMS_FILTER_TRANS', trans) REQUEST.set( 'ZMS_FILTER_CUR_DIR', _fileutil.getFilePath(infilename)) try: value = getattr( self, processId)( self, REQUEST) except: value = standard.writeError( self, '[processMethod]: processId=%s'%processId) outfilename = REQUEST.get( 'ZMS_FILTER_OUT') # Return filename. return outfilename
def xmlOnStartElement(self, sTagName, dTagAttrs, oParentNode): standard.writeLog( self, "[xmlOnStartElement]: sTagName=%s"%sTagName) # remove all ZMS-objects. ids = self.objectIds(list(self.dGlobalAttrs)) if ids: self.manage_delObjects(ids=ids) # initialize stacks. self.dTagStack = collections.deque() self.dValueStack = collections.deque() # WORKAROUND! The member variable "aq_parent" does not contain the right # parent object at this stage of the creation process (it will later on!). # Therefore, we introduce a special attribute containing the parent # object, which will be used by xmlGetParent() (see below). self.oParent = None
def update_extract(action, data): solr_url = zmscontext.getConfProperty('solr.url', 'http://localhost:8983/solr') solr_core = zmscontext.getConfProperty('solr.core', home_id) url = '%s/%s/update/extract' % (solr_url, solr_core) standard.writeLog( zmscontext, "[manage_zcatalog_update_documents.update]: %s=%s" % (action, url)) response = standard.pystr( standard.http_request( url, method='POST', data=data, headers={'Content-Type': 'multipart/form-data'})) standard.writeLog( zmscontext, "[manage_zcatalog_update_documents.update]: %s=%s" % (action, response)) return '%s=%s' % (action, response)
def update(action, xml): solr_url = zmscontext.getConfProperty('solr.url', 'http://localhost:8983/solr') solr_core = zmscontext.getConfProperty('solr.core', home_id) url = '%s/%s/update' % (solr_url, solr_core) url = '%s?%s' % (url, xml) standard.writeLog( zmscontext, "[manage_zcatalog_update_documents.update]: %s=%s" % (action, url)) result = standard.pystr( standard.http_import( zmscontext, url, method='POST', headers={'Content-Type': 'text/xml;charset=UTF-8'})) standard.writeLog( zmscontext, "[manage_zcatalog_update_documents.update]: %s=%s" % (action, result)) return '%s=%s' % (action, result)
def _initObjChildren(self, obj_attr, REQUEST): id = obj_attr['id'] ids = [ x.getId() for x in self.getChildNodes() if x.getId().startswith(id) ] mandatory = obj_attr.get('mandatory', 0) if mandatory: if len(ids) == 0: default = obj_attr.get('custom') if default: _fileutil.import_zexp(self, default, obj_attr['id'], obj_attr['id']) else: if obj_attr['type'] == '*' and isinstance( obj_attr['keys'], list) and len(obj_attr['keys']) > 0: obj_attr['type'] = obj_attr['keys'][0] self.initObjChild(obj_attr['id'], 0, obj_attr['type'], REQUEST) repetitive = obj_attr.get('repetitive', 0) if repetitive: if id in ids: new_id = self.getNewId(id) standard.writeLog( self, "[_initObjChildren]: Rename %s to %s" % (id, new_id)) if new_id not in self.objectIds(): try: self.manage_renameObject(id=id, new_id=new_id) except: ob = getattr(self, id) ob._setId(new_id) else: if not id in ids and len(ids) > 0: old_id = ids[0] standard.writeLog( self, "[_initObjChildren]: Rename %s to %s" % (old_id, id)) if id not in self.objectIds(): try: self.manage_renameObject(id=old_id, new_id=id) except: ob = getattr(self, old_id) ob._setId(id)
def getDiffs(self, provider, ignore=True): standard.writeLog(self, "[getDiffs]: provider=%s" % str(provider)) diff = [] local = self.localFiles(provider) remote = self.remoteFiles(provider) filenames = sorted(set(list(local) + list(remote))) for filename in filenames: if ignore and filename not in local.keys(): # ignore orphaned files in filesystem # if there are no references in model continue l = local.get(filename, {}) r = remote.get(filename, {}) if isinstance(l.get('data', ''), bytes): try: l['data'] = l['data'].decode('utf-8') except: pass if isinstance(r.get('data', ''), bytes): try: r['data'] = r['data'].decode('utf-8') except: pass if l.get('data', '') != r.get('data', ''): data = l.get('data', r.get('data', '')) try: try: mt, enc = standard.guess_content_type( filename.split('/')[-1], data) except: mt, enc = standard.guess_content_type( filename.split('/')[-1], data.encode('utf-8')) diff.append((filename, mt, l.get('id', r.get('id', '?')), l, r)) except: standard.writeLog( self, "[getDiffs]: Error in appending filename = %s to variable data, Line 232" % str(filename)) pass return diff
def getDiffs(self, provider, ignore=True): standard.writeLog(self, "[getDiffs]: provider=%s" % str(provider)) diff = [] local = self.localFiles(provider) remote = self.remoteFiles(provider) filenames = sorted(set(list(local) + list(remote))) for filename in filenames: if ignore and filename not in local.keys(): # ignore orphaned files in filesystem # if there are no references in model continue l = local.get(filename, {}) l_data = l.get('data') r = remote.get(filename, {}) r_data = r.get('data') # Check whether any bytes data are decodeable as utf-8 text if isinstance(l_data, bytes): try: l['data'] = l_data.decode('utf-8') except: # data is no text, but image etc. pass if isinstance(r_data, bytes): try: r['data'] = r_data.decode('utf-8') except: pass # If text then normalize Windows CR+LF line break to Unix LF if isinstance(l.get('data'), str): l['data'] = l['data'].replace('\r', '') if isinstance(r.get('data'), str): r['data'] = r['data'].replace('\r', '') # Only if text is not equal add to diff list if l.get('data') != r.get('data'): data = l_data or r_data if isinstance(data, str): data = data.encode('utf-8') mt, enc = standard.guess_content_type( filename.split('/')[-1], data) diff.append((filename, mt, l.get('id', r.get('id', '?')), l, r)) return diff
def getDiffs(self, provider, ignore=True): standard.writeLog(self, "[getDiffs]: provider=%s" % str(provider)) diff = [] local = self.localFiles(provider) remote = self.remoteFiles(provider) filenames = sorted(set(list(local) + list(remote))) for filename in filenames: if ignore and filename not in local.keys(): # ignore orphaned files in filesystem # if there are no references in model continue l = local.get(filename, {}) l_data = l.get('data') r = remote.get(filename, {}) r_data = r.get('data') # Try normalizing text codes to cleaned utf-8 if isinstance(l.get('data'), (str, bytes)): try: l['data'] = standard.pystr2(l['data']).decode('utf-8') except: pass if isinstance(r.get('data'), (str, bytes)): try: r['data'] = standard.pystr2(r['data']).decode('utf-8') except: pass # Try normalizing line breaks try: l['data'] = l['data'].replace('\r', '') r['data'] = r['data'].replace('\r', '') except: pass # Compare the the two sides if l.get('data') != r.get('data'): data = l.get('data') or r.get('data') mt, enc = standard.guess_content_type( filename.split('/')[-1], data) diff.append((filename, mt, l.get('id', r.get('id', '?')), l, r)) return diff
def OnEndElement(self, name): """ Builder.OnEndElement """ if True: standard.writeLog(self, "[Builder.OnEndElement(" + str(name) + ")]") skip = self.oCurrNode is not None and len( [x for x in self.oCurrNode.dTagStack if x.get('skip')]) > 0 if not skip and name == self.oCurrNode.meta_id: standard.writeLog(self, "[Builder.OnEndElement]: object finished") ##### VersionManager #### self.oCurrNode.resetObjStates() # notify current node self.oCurrNode.xmlOnEndElement() parent = self.oCurrNode.xmlGetParent() # set parent node as current node self.oCurrNode = parent else: # tag name is unknown -> offer it to current object if not self.oCurrNode.xmlOnUnknownEndTag(name): standard.writeLog( self, "[Builder.OnEndElement]: Unknown end-tag (/" + name + ")") # current object did not accept tag! raise ParseError("Unknown end-tag (" + name + ")") # current object did not accept tag!
def handleUrl(node,v): standard.writeLog(node,'[ZMSIndex] handleUrl %s'%v) if v.startswith('{$') and v.endswith('}'): old = v if not (v.startswith('{$__') and v.endswith('__}')) \ and not (v.startswith('{$') and v.find('id:')>0 and v.endswith('}')): data_data = '' brain = find_brain(v) if brain is not None: if v.find(';') > 0: data_data = v[v.find(';'):-1] data_id = '{$%s%s}'%(brain['get_uid'],data_data) new = data_id if old != new: standard.writeBlock(node,'[ZMSIndex] handleUrl %s->%s'%(old,new)) v = new ref = getLinkObj('{$%s}'%brain['get_uid']) ref.registerRefObj(node) else: log.append('ERROR %s'%standard.writeBlock(node,'[ZMSIndex] handleUrl ### MISSING LINKTARGET %s'%(v))) v = '{$__%s__}'%v[2:-1] return v
def exportFilter(self, id, REQUEST): # Set local variables. ob_filter = self.getFilterManager().getFilter(id) tempfolder, outfilename = self.initExportFilter( id, REQUEST) # Process filter. outfilename = processFilter(self, ob_filter, tempfolder, outfilename, REQUEST) # Return values. content_type = ob_filter.get('content_type', 'content/unknown') filename = 'exportFilter.%s'%content_type[content_type.find('/')+1:] # Zip File. if content_type == 'application/zip': data = _fileutil.buildZipArchive( outfilename, get_data=True) # Read File. else: standard.writeLog( self, '[exportFilter]: Read %s'%outfilename) f = open(outfilename, 'rb') data = f.read() f.close() # Remove temporary folder. if not self.getConfProperty('ZMS.debug', 0): _fileutil.remove( tempfolder, deep=1) # Return. return filename, data, content_type
def manage_packMediaDb(self, REQUEST=None, RESPONSE=None): """ manage_packMediaDb """ message = '' mediadb = self.getMediaDb() path = mediadb.getLocation() # Get filenames. filenames = mediadb.valid_filenames() standard.writeLog(self, "[manage_packMediaDb]: filenames %s" % str(filenames)) tempfolder = tempfile.mktemp() os.makedirs(tempfolder) standard.writeLog(self, "[manage_packMediaDb]: tempfolder %s" % tempfolder) # Traverse existing structure. def traverse(path, p): for filename in os.listdir(path): filepath = os.path.join(path, filename) if os.path.isdir(filepath): traverse(filepath, p) elif os.path.isfile(filepath): if filename not in p['filenames']: standard.writeBlock( self, "[manage_packMediaDb]: filename %s" % str(filename)) shutil.move(filepath, p['tempfolder'] + os.sep + filename) p['c'] += 1 p['t'] += 1 p = {'t': 0, 'c': 0, 'filenames': filenames, 'tempfolder': tempfolder} traverse(path, p) # Return with message. message = "%i files (total %i) moved to %s." % (p['c'], p['t'], p['tempfolder']) standard.writeBlock(self, "[manage_packMediaDb]: " + message) return message
def refreshRefToObjs(self): standard.writeLog(self, '[refreshRefToObjs]') if 'ref_to' in self.__dict__: old_ref_to = self.ref_to standard.writeLog(self, '[refreshRefToObjs]: old=%s' % str(old_ref_to)) new_ref_to = self.getRefToObjs() standard.writeLog(self, '[refreshRefToObjs] new=%s' % str(old_ref_to)) delattr(self, 'ref_to') for ref in old_ref_to: ref_ob = self.getLinkObj(ref) if ref_ob is not None: self_ref = ref_ob.getRefObjPath(self) if ref not in new_ref_to and self_ref in ref_ob.synchronizeRefByObjs( ): ref_ob.unregisterRefObj(self) for ref in new_ref_to: ref_ob = self.getLinkObj(ref) if ref_ob is not None: self_ref = ref_ob.getRefObjPath(self) if ref not in old_ref_to or self_ref not in ref_ob.synchronizeRefByObjs( ): ref_ob.registerRefObj(self)
def commitChanges(self, ids): standard.writeLog(self, "[commitChanges]: ids=%s" % str(ids)) standard.triggerEvent(self, 'beforeCommitRepositoryEvt') success = [] failure = [] for provider_id in list(set([x.split(':')[0] for x in ids])): provider = getattr(self, provider_id) basepath = self.get_conf_basepath(provider.id) for id in list( set([ x.split(':')[1] for x in ids if x.split(':')[0] == provider_id ])): try: # Read local-files from provider. files = self.localFiles(provider, [id]) # Recreate folder. if os.path.exists(basepath): for name in os.listdir(basepath): filepath = os.path.join(basepath, name) if os.path.isdir(filepath) and name == id: standard.writeLog( self, "[commitChanges]: clear dir %s" % filepath) dir = [ os.path.join(filepath, x) for x in os.listdir(filepath) ] [ _fileutil.remove(x) for x in dir if os.path.isfile(x) ] elif os.path.isfile( filepath) and name == '%s.py' % id: standard.writeLog( self, "[commitChanges]: remove file %s" % filepath) _fileutil.remove(filepath) # Clear folders. dir = list( set([ os.path.join(basepath, x[:x.rfind(os.path.sep)]) for x in files if x.endswith('__init__.py') ])) dir = [ x for x in dir if x.split(os.path.sep)[-1] in [y.split(':')[-1] for y in ids] ] [[ os.remove(z) for z in [os.path.join(x, y) for y in os.listdir(x)] if os.path.isfile(z) ] for x in dir if os.path.isdir(x)] # Write files. for file in files: filepath = os.path.join(basepath, file) folder = filepath[:filepath.rfind(os.path.sep)] standard.writeLog( self, "[commitChanges]: exists folder %s %s" % (folder, str(os.path.exists(folder)))) if not os.path.exists(folder): standard.writeLog( self, "[commitChanges]: create folder %s" % folder) _fileutil.mkDir(folder) standard.writeLog( self, "[commitChanges]: write %s" % filepath) data = files[file]['data'] if data is not None: f = open(filepath, "wb") try: f.write(data.encode('utf-8')) except: f.write(data) f.close() else: failure.append('%s is None' % file) success.append(id) except: standard.writeError(self, "[commitChanges]: can't %s" % id) failure.append(id) standard.triggerEvent(self, 'afterCommitRepositoryEvt') return success, failure
def readRepository(self, provider): standard.writeLog(self, "[readRepository]: provider=%s" % str(provider)) basepath = self.get_conf_basepath(provider.id) return _repositoryutil.readRepository(self, basepath)
def localFiles(self, provider, ids=None): standard.writeLog(self, "[localFiles]: provider=%s" % str(provider)) l = {} local = provider.provideRepository(ids) for id in local: o = local[id] filename = o.get('__filename__', [id, '__init__.py']) # Write python-representation. py = [] py.append('class %s:' % id.replace('.', '_').replace('-', '_')) py.append('\t"""') py.append('\tpython-representation of %s' % o['id']) py.append('\t"""') py.append('') e = sorted([ x for x in o if not x.startswith('__') and x == x.capitalize() and isinstance(o[x], list) ]) keys = sorted( [x for x in o if not x.startswith('__') and x not in e]) for k in keys: v = o.get(k) py.append('\t# %s' % k.capitalize()) py.append('\t%s = %s' % (standard.id_quote(k), standard.str_json(v, encoding="utf-8", formatted=True, level=2, allow_booleans=False))) py.append('') for k in e: v = o.get(k) if v and isinstance(v, list): py.append('\t# %s' % k.capitalize()) py.append('\tclass %s:' % standard.id_quote(k).capitalize()) for i in v: if 'id' in i: ob = i.get('ob') if ob is not None: fileexts = { 'DTML Method': '.dtml', 'DTML Document': '.dtml', 'External Method': '.py', 'Page Template': '.zpt', 'Script (Python)': '.py', 'Z SQL Method': '.zsql' } fileprefix = i['id'].split('/')[-1] data = zopeutil.readData(ob) # if type(data) is unicode: # data = standard.pystr2(data) version = '' if hasattr(ob, '_p_mtime'): version = standard.getLangFmtDate( DateTime(ob._p_mtime).timeTime(), 'eng') d = {} d['id'] = id d['filename'] = os.path.sep.join( filename[:-1] + [ '%s%s' % (fileprefix, fileexts.get(ob.meta_type, '')) ]) d['data'] = data d['version'] = version d['meta_type'] = ob.meta_type l[d['filename']] = d if 'ob' in i: del i['ob'] try: py.append( '\t\t%s = %s' % (self.id_quote(i['id']), standard.str_json(i, encoding="utf-8", formatted=True, level=3, allow_booleans=False))) except: py.append( '\t\t# ERROR: ' + standard.writeError( self, 'can\'t localFiles \'%s\'' % i['id'])) py.append('') d = {} d['__icon__'] = o.get('__icon__') d['__description__'] = o.get('__description__') d['id'] = id d['filename'] = os.path.sep.join(filename) d['data'] = '\n'.join(py) try: d['version'] = [ int(x) for x in o.get('revision', '0.0.0').split('.') ] except: d['version'] = [0, 0, 0] d['meta_type'] = 'Script (Python)' l[d['filename']] = d return l