def rpc_handle_anwsers(self, answers=None): answers = Bag(answers or '') for page_id, page_answers in answers.items(): with self.clientPage(page_id) as clientPage: for value, changepath in page_answers.digest('#v,#a.client_path'): clientPage.set(changepath, value) print 'HEY SETTO IN CLIENTPAGE'
def readCode(self,modulepath=None): result=Bag() modulepath=modulepath or sys.modules[self.__module__].__file__ with open(modulepath, "r") as source_code: red = RedBaron(source_code.read()) result.fromJson(red.fst()) return result
def getTableContraints(self, table=None, schema=None): """Get a (list of) dict containing details about a column or all the columns of a table. Each dict has those info: name, position, default, dtype, length, notnull Every other info stored in information_schema.columns is available with the prefix '_pg_'""" sql = """SELECT CONSTRAINT_TYPE,COLUMN_NAME,TC.TABLE_NAME,TC.TABLE_SCHEMA,TC.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CU ON CU.CONSTRAINT_NAME=TC.CONSTRAINT_NAME WHERE CONSTRAINT_TYPE='UNIQUE' %s%s;""" filtertable = "" if table: filtertable = " AND TC.TABLE_NAME=:table" filterschema = "" if schema: filterschema = " AND TC.TABLE_SCHEMA=:schema" result = self.dbroot.execute(sql % (filtertable,filterschema), dict(schema=schema, table=table)).fetchall() res_bag = Bag() for row in result: row=dict(row) res_bag.setItem('%(TABLE_SCHEMA)s.%(TABLE_NAME)s.%(COLUMN_NAME)s'%row,row['CONSTRAINT_NAME']) return res_bag
def _buildSiteMenu_prepare(self, menubag, basepath=None): basepath = basepath or [] result = Bag() for node in menubag.nodes: value = node.getStaticValue() attributes = {} attributes.update(node.getAttr()) currbasepath = basepath if 'basepath' in attributes: newbasepath = attributes.pop('basepath') if newbasepath.startswith('/'): currbasepath = [self.site.home_uri + newbasepath[1:]] else: currbasepath = basepath + [newbasepath] if isinstance(value, Bag): value = self._buildSiteMenu_prepare(value, currbasepath) else: value = None filepath = attributes.get('file') if filepath: if not filepath.startswith('/'): attributes['file'] = os.path.join(*(currbasepath + [filepath])) else: attributes['file'] = self.site.home_uri + filepath.lstrip('/') result.setItem(node.label, value, attributes) return result
def te_renderTemplate(self, templateBuilder, record_id=None, extraData=None, locale=None,contentOnly=False,**kwargs): record = Bag() if record_id: record = templateBuilder.data_tblobj.record(pkey=record_id, virtual_columns=templateBuilder.virtual_columns).output('bag') else: record = templateBuilder.data_tblobj.record(pkey='*sample*',ignoreMissing=True, virtual_columns=templateBuilder.virtual_columns).output('sample') if extraData: record.update(extraData) locale = locale or templateBuilder.locale formats = templateBuilder.formats or dict() masks = templateBuilder.masks or dict() editcols = templateBuilder.editcols or dict() df_templates = templateBuilder.df_templates or dict() dtypes = templateBuilder.dtypes or dict() record.setItem('_env_', Bag(self.db.currentEnv)) #record.setItem('_template_', templateBuilder.doctemplate_info) htmlContent = templateReplace(templateBuilder.doctemplate,record, safeMode=True,noneIsBlank=False,locale=locale, formats=formats,masks=masks,editcols=editcols,df_templates=df_templates, dtypes=dtypes,localizer=self.localizer, urlformatter=self.externalUrl) if contentOnly: return htmlContent body = templateBuilder(htmlContent=htmlContent, record=record,page_debug='silver',**kwargs) return body
def onLoading(self, record, newrecord, loadingParameters, recInfo): tags = record['auth_tags'] b = Bag() if tags: for n, tag in enumerate(tags.split(',')): b.setItem('r%i.tagname' % n, tag) record.setItem('tag_bag', b, _bag_md5=tags)
class ViewRecordHistory(BaseComponent): def th_hiddencolumns(self): return '$event,$data' def th_struct(self,struct): r = struct.view().rows() r.fieldcell('version',name='V',width='4em') r.fieldcell('__ins_ts',name='TS',width='10em') r.fieldcell('username',name='User',width='10em') r.cell('changed_fields',name='Changes',width='15em',calculated=True) @public_method def th_applymethod(self,selection): selection.sort('version') self._curr_audit_record = Bag() def cb(row): b = Bag(row['data']) result = dict() if row['version'] >0: result['changed_fields'] = '<br/>'.join(b.keys()) b.pop('__ins_ts') b.pop('__version') b.pop('__mod_ts') self._curr_audit_record.update(b) result['__value__'] = self._curr_audit_record.deepcopy() return result selection.apply(cb) selection.sort('version:d') def th_order(self): return 'version:d' def th_query(self): return dict(column='tablename', op='contains', val='')
def menu_iframemenuPane(self, pane, **kwargs): b = Bag() b['root'] = MenuResolver(path=getattr(self,'menu_path',None), pagepath=self.pagepath,_page=self) b.getIndex() pane.data('gnr.appmenu', b) #leftPane = parentBC.contentPane(width='20%',_class='menupane',**kwargs) pane.tree(id="_gnr_main_menu_tree", storepath='gnr.appmenu.root', selected_file='gnr.filepath', labelAttribute='label', hideValues=True, _class='menutree', persist='site', inspect='shift', identifier='#p', getIconClass='return node.attr.iconClass || "treeNoIcon"', getLabelClass="return node.attr.labelClass;", openOnClick=True, connect_onClick="""var labelClass= $1.attr.labelClass; if(labelClass.indexOf('menu_existing_page')<0){ $1.setAttribute('labelClass',labelClass+' menu_existing_page'); } var inattr = $1.getInheritedAttributes(); this.publish("selected", objectUpdate({name:$1.label,pkg_menu:inattr.pkg_menu,"file":null,table:null,formResource:null,viewResource:null,fullpath:$1.getFullpath(null,true),modifiers:$2.__eventmodifier}, $1.attr)); """, autoCollapse=True, nodeId='_menutree_') pane.dataController("genro.wdgById('_gnrRoot').showHideRegion('left', false);", fired='^gnr.onStart', appmenu="=gnr.appmenu", _if="appmenu.len()==0")
def checkFullSyncTables_do(self,errorlog_folder=None,dbstores=None,packages=None,tbllist=None): errors = Bag() master_index = self.db.tablesMasterIndex()['_index_'] for tbl in master_index.digest('#a.tbl'): if tbllist and tbl not in tbllist: continue pkg,tblname = tbl.split('.') if packages and pkg not in packages: continue tbl = self.db.table(tbl) multidb = tbl.multidb if not multidb or multidb=='one' or multidb=='parent': continue main_f = tbl.query(addPkeyColumn=False,bagFields=True, excludeLogicalDeleted=False,ignorePartition=True, excludeDraft=False).fetch() if multidb=='*': tbl.checkSyncAll(dbstores=dbstores,main_fetch=main_f,errors=errors) else: tbl.checkSyncPartial(dbstores=dbstores,main_fetch=main_f,errors=errors) if errorlog_folder: for dbstore,v in errors.items(): p = '%s/%s.xml' %(errorlog_folder,dbstore) if v: v.toXml(p,autocreate=True) else: os.remove(p)
def _maintenance_get_items(self, items, child_name=None,exclude_guest=None, **kwargs): result = Bag() now = datetime.now() for item in items: item = dict(item) key = item['register_item_id'] item.pop('data',None) if exclude_guest and ( key.startswith('guest_') or item.get('user','').startswith('guest_')): continue _customClasses = [] item['_pkey'] = key item['alive'] = True item['age'] = (now - item.get('start_ts')).seconds item['last_refresh_age'] = (now - item.get('last_refresh_ts',item['start_ts'])).seconds item['last_event_age'] = (now - item.get('last_user_ts',item['start_ts'])).seconds item['last_rpc_age'] = (now - item.get('last_rpc_ts',item['start_ts'])).seconds if item['last_refresh_age'] > 60: item['alive'] = False _customClasses.append('disconnected') elif item['last_event_age'] > 60: _customClasses.append('inactive') #if child_name and not item[child_name]: # _customClasses.append('no_children') item.pop('datachanges', None) #if child_name is None: # self.maintenance_cellServerProfile(item) result.setItem(key, None, _customClasses=' '.join(_customClasses), **item) return result
def cleanUserBatches(self,user): if self.page.isGuest: return if not os.path.exists(self.page.userDocument('_batch_result')): return batch_results = [x for x in os.listdir(self.page.userDocument('_batch_result')) if not x.startswith('.')] batch_to_remove = [] with self.page.userStore(user) as store: already_registered_batch = [dc.path.split('.')[2] for dc in store.datachanges if dc.path.startswith('gnr.batch')] for res_doc_name in batch_results: result_doc = Bag(self.page.userDocument('_batch_result', res_doc_name)) resultNode = result_doc.getNode('result') batch_id = result_doc['batch_id'] if not resultNode or not 'url' in resultNode.attr: batch_to_remove.append(batch_id) continue if batch_id not in already_registered_batch: batch_path = 'gnr.batch.%s' % batch_id newbatch = Bag( dict(title=result_doc['title'], start_ts=result_doc['start_ts'], note=result_doc['note'], owner_page_id=result_doc['owner_page_id'])) store.set_datachange(batch_path, newbatch, reason='btc_create') reason = 'btc_result_doc' if result_doc['result'] else 'btc_error_doc' store.set_datachange(batch_path, result_doc, reason=reason) if batch_to_remove: self.remove_batch(batch_id=batch_to_remove)
def _relationPythonCode(self,relation): relpath = relation.pop('relation') r = relpath.split('.') if len(r)==3: pkg,table,id = r if pkg == 'main': relpath = '%s.%s' %(table,id) relation_name = relation.pop('relation_name') foreignkey = relation.pop('foreignkey') attributes = Bag() if relation_name: attributes['relation_name'] = relation_name if foreignkey: attributes['mode'] = 'foreignkey' for k, v in relation.items(): if v is not None: attributes[k] = v atlst = [] for k,v in attributes.items(): if v == 'True': v = True if isinstance(v,basestring): if "'" in v: v = '"%s"' %v else: v = "'%s'" %v atlst.append("%s=%s" %(k,v)) return """relation('%s',%s)""" %(relpath,', '.join(atlst))
def connected_users_bag(self, exclude=None, exclude_guest=True, max_age=600): users = self.page.site.register.users() result = Bag() exclude = exclude or [] now = datetime.now() if isinstance(exclude, basestring): exclude = exclude.split(',') for user, arguments in users.items(): if user in exclude: continue row = dict() if exclude_guest and user.startswith('guest_') or user == self.page.user: continue _customClasses = [] row['_pkey'] = user row['iconClass'] = 'greenLight' last_refresh_ts = arguments.get('last_refresh_ts') or arguments['start_ts'] last_user_ts = arguments.get('last_user_ts') or arguments['start_ts'] last_refresh_age = (now - last_refresh_ts).seconds last_event_age = (now - last_user_ts).seconds if last_refresh_age > 60: _customClasses.append('user_disconnected') row['iconClass'] = 'grayLight' elif last_event_age>300: _customClasses.append('user_away') row['iconClass'] = 'redLight' elif last_event_age > 60: _customClasses.append('user_idle') row['iconClass'] = 'yellowLight' row['_customClasses'] = _customClasses row['caption'] = arguments['user_name'] or user row.update(arguments) row.pop('datachanges', None) result.setItem(user, None, **row) return result
def load(self): page = self._page mailbox_tblobj = page.db.table('email.mailbox') where = '$parent_id IS NULL' if self.parent_id: where='$parent_id=:p_id' #sottopratiche elif self.account_id: where = '$account_id =:a_id AND $parent_id IS NULL' elif self.user_id: where='user_id=:u_id AND $parent_id IS NULL' else: where = '$parent_id IS NULL AND $user_id IS NULL AND $account_id IS NULL' q = mailbox_tblobj.query(where=where,p_id=self.parent_id,columns='*,$child_count', a_id=self.account_id,u_id=self.user_id,order_by='$system_code,$name') result = Bag() mailboxes = q.fetch() for mb in mailboxes: record = dict(mb) caption = mb['name'] pkey = record['id'] result.setItem(pkey,MailboxResolver(_page=page,parent_id=pkey), caption=caption, child_count=record['child_count'],pkey=pkey, account_id=record['account_id'],system_code=record['system_code'], parent_id=self.parent_id) return result
def fgr_slotbar_addrow( self, pane, _class="iconbox add_row", disabled="^.disabledButton", enable=None, delay=300, defaults=None, **kwargs ): kwargs.setdefault("visible", enable) menupath = None if defaults: menubag = Bag() for i, (caption, default_kw) in enumerate(defaults): menubag.setItem("r_%i" % i, None, caption=caption, default_kw=default_kw) pane.data(".addrow_menu_store", menubag) menupath = ".addrow_menu_store" return pane.slotButton( label="!!Add", publish="addrow", iconClass=_class, disabled=disabled, _delay=delay, menupath=menupath, **kwargs )
class GnrSqlDebugger(object): def __init__(self,parent): self.parent = parent self._debug_calls = Bag() def output(self, page, sql=None, sqlargs=None, dbtable=None, error=None,delta_time=None): dbtable = dbtable or '-no table-' kwargs=dict(sqlargs) kwargs.update(sqlargs) delta_time = int((delta_time or 0)*1000) if sqlargs and sql: formatted_sqlargs = dict([(k,'<span style="background-color:yellow;cursor:pointer;" title="%s" >%%(%s)s</span>' %(v,k)) for k,v in sqlargs.items()]) value = sql %(formatted_sqlargs) if error: kwargs['sqlerror'] = str(error) self._debug_calls.addItem('%03i Table %s' % (len(self._debug_calls), dbtable.replace('.', '_')), value,_execution_time=delta_time,_time_sql=delta_time,_description=dbtable,_type='sql', **kwargs) def onCollectDatachanges(self): page = self.parent.page if page.debug_sql and self._debug_calls: path = 'gnr.debugger.main.c_%s' % page.callcounter attributes=dict(server_time=int((time()-page._start_time)*1000)) call_kwargs = dict(page._call_kwargs) attributes['methodname'] = call_kwargs.pop('method') call_kwargs.pop('_lastUserEventTs',None) if not call_kwargs.get('_debug_info') and ('table' in call_kwargs or 'dbtable' in call_kwargs): call_kwargs['_debug_info'] = 'table: %s' %(call_kwargs.get('table') or call_kwargs.get('dbtable')) attributes['debug_info'] = call_kwargs.pop('_debug_info',None) #attributes['_method_parameters'] = call_kwargs attributes['sql_count'] = len(self._debug_calls) attributes['sql_total_time'] = self._debug_calls.sum('#a._time_sql') attributes['not_sql_time'] = attributes['server_time'] - attributes['sql_total_time'] attributes['r_count'] = page.callcounter page.setInClientData(path, self._debug_calls,attributes=attributes)
def get_datachanges(self, page_id, user=None, local_datachanges=None): """add??? :param page_id: add??? :param user: add???. Default value is ``None`` :param local_datachanges: add???. Default value is ``None`` :returns: add??? """ result = Bag() local_datachanges = local_datachanges or [] with self.register.pageStore(page_id) as pagestore: external_datachanges = list(pagestore.datachanges) or [] subscriptions = pagestore.getItem('_subscriptions') or Bag() pagestore.reset_datachanges() store_datachanges = [] for storename, storesubscriptions in subscriptions.items(): store = self.register.userStore(user) if storename == 'user' else self.register.stores(storename) with store: self._get_storechanges(store, storesubscriptions.items(), page_id, store_datachanges) for j, change in enumerate(external_datachanges + local_datachanges + store_datachanges): result.setItem('sc_%i' % j, change.value, change_path=change.path, change_reason=change.reason, change_fired=change.fired, change_attr=change.attributes, change_ts=change.change_ts, change_delete=change.delete) return result
def audit(self,tblobj,event,audit_mode=None,record=None,old_record=None,username=None,page=None): if event=='I' and audit_mode=='lazy': return if not username: username = self.db.currentEnv.get('user') version = record.get('__version',0) audit_record = dict(tablename=tblobj.fullname,event=event,username=username, record_pkey=record[tblobj.pkey],version=version,transaction_id=self.db.currentEnv.get('env_transaction_id')) if event == 'I' or event=='D': audit_record['data'] = self.getXmlRecord(record) if event == 'U': assert old_record, 'Missing old_record in an update that uses audit feature tbl: %s' %tblobj.fullname changes = Bag() for k in record.keys(): if k in ('__version','__mod_ts'): continue if record[k] != old_record.get(k): changes[k] = record[k] audit_record['data'] = changes.toXml() if audit_mode=='lazy' and version==1: first_audit = dict(audit_record) first_audit['version'] = 0 first_audit['event'] = 'I' first_audit['data'] = self.getXmlRecord(old_record) self.insert(first_audit) self.insert(audit_record)
def te_menuTemplates(self,table=None): result = Bag() from_userobject = self.db.table('adm.userobject').userObjectMenu(table,'template') #todo for n in from_userobject: result.setItem(n.label,None,tplmode='userobject',**n.attr) result.setItem('__newtpl__',None,caption='!!New Template') return result
def rpc_getItems(self, url=None, **kwargs): rss = Bag(url)['rss.channel'].digest('#v') rows = Bag() for i, item in enumerate(rss): if isinstance(item, Bag): rows.setItem('n_%i' % i, None, title=item['title'], link=item['link'], description=item['description']) return rows
def getInfoBag(self,pkg=None,tbl=None,user=None,user_group=None,_editMode=False): result = Bag() if tbl: pkg = tbl.split('.')[0] tblobj = self.db.table(tbl) cols_permission_base = Bag() allcols = tblobj.columns.keys() + tblobj.model.virtual_columns.keys() result['cols_permission'] = cols_permission_base if user and not user_group: user_group = self.db.table('adm.user').readColumns(where='$username=:u',u=user,columns='$group_code') f = self.query(where="""($pkgid IS NULL OR $pkgid=:pkg) AND ($tblid IS NULL OR $tblid=:tbl) AND ($user_group IS NULL OR $user_group=:user_group) AND ($username IS NULL OR $username=:user) """,pkg=pkg,tbl=tbl,user_group=user_group,user=user, order_by='$rank ASC',columns="""$data""").fetch() for r in f: data = Bag(r['data']) last_permissions = data.pop('cols_permission') if tbl: result['cols_permission'] = self._updateColsPermission(result['cols_permission'], last_permissions or Bag(), allcols,_editMode) for k,v in data.items(): if v is not None: result[k] = v return result
def configDefault_CORE(self, record=None): stressbag = record['stress_tracks'] stressbag['p'] = Bag(dict(track_name='Physical', n_boxes=2, code='p', skill='PHYSIQUE', extra_box_1=1, extra_box_2=3, extra_box_3=None)) stressbag['m'] = Bag(dict(track_name='Mental', n_boxes=2, code='m', skill='WILL', extra_box_1=1, extra_box_2=3, extra_box_3=None)) record.update(dict(pc_phases=3, game_creation=True, use_phases=True)) if record['use_approaches']: record.update(dict( approach_set='STD')) else: consequences_slots = Bag(record['consequences_slots']) consequences_slots.setItem('mild2', Bag(dict(code='m2', shifts=2, label='Mild opt.', skill='PHYSIQUE', lv=5)), _position=1) record.update(dict(stunt_sets='STD', skill_sets='STD', skill_cap=4, consequences_slots=consequences_slots))
def getTableContraints(self, table=None, schema=None): """Get a (list of) dict containing details about a column or all the columns of a table. Each dict has those info: name, position, default, dtype, length, notnull Every other info stored in information_schema.columns is available with the prefix '_mysql_'""" sql = """SELECT constraint_type,column_name,tc.table_name,tc.table_schema,tc.constraint_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS cu ON cu.constraint_name=tc.constraint_name WHERE constraint_type='UNIQUE' %s%s;""" filtertable = "" if table: filtertable = " AND tc.table_name=:table" filterschema = "" if schema: filterschema = " AND tc.table_schema=:schema" result = self.dbroot.execute(sql % (filtertable,filterschema), dict(schema=schema, table=table)).fetchall() res_bag = Bag() for row in result: row=dict(row) res_bag.setItem('%(table_schema)s.%(table_name)s.%(column_name)s'%row,row['constraint_name']) return res_bag
def ht_htableViewStore(self,pane,table=None,storepath='.store',caption_field=None,condition=None,caption=None, dbstore=None,root_id=None,columns=None,related_kwargs=None,resolved=False,**kwargs): b = Bag() tblobj = self.db.table(table) caption = caption or tblobj.name_plural if condition: d = pane.dataRpc(storepath,self.ht_remoteHtableViewStore, table=table, caption_field=caption_field, condition=condition, childname='store',caption=caption,dbstore=dbstore, columns=columns,related_kwargs=related_kwargs, **kwargs) return d v = TableHandlerTreeResolver(_page=self,table=table,caption_field=caption_field,dbstore=dbstore,related_kwargs=related_kwargs, root_id=root_id,columns=columns) b.setItem('root',v,caption=tblobj.name_long, child_count=1,pkey='',treeIdentifier='_root_') if resolved: def cb(self,*args,**kwargs): pass b.walk(cb) d = pane.data(storepath,b,childname='store',caption=caption,table=table) return d
class StaticHandlerManager(object): """ This class handles the StaticHandlers""" def __init__(self, site): self.site = site self.statics = Bag() def addAllStatics(self, module=None): """inspect self (or other modules) for StaticHandler subclasses and do addStatic for each""" module = module or sys.modules[self.__module__] def is_StaticHandler(cls): return inspect.isclass(cls) and issubclass(cls, StaticHandler) and cls is not StaticHandler statichandler_classes = inspect.getmembers(module, is_StaticHandler) for statichandler in statichandler_classes: self.add(statichandler[1]) def add(self, static_handler_factory, **kwargs): static_handler = static_handler_factory(self.site, **kwargs) self.statics.setItem(static_handler.prefix, static_handler, **kwargs) def get(self, static_name): return self.statics[static_name] def static_dispatcher(self, path_list, environ, start_response, download=False, **kwargs): handler = self.get(path_list[0][1:]) if handler: return handler.serve(path_list, environ, start_response, download=download, **kwargs) else: return self.site.not_found_exception(environ, start_response)
class DojoApiReader(object): discard = ['provides', 'resources', 'mixins'] discard = [] def __init__(self, apipath, resultpath=None): self.resultpath = resultpath or os.path.dirname(apipath) self.source = Bag(apipath)['javascript'] self.apibag = Bag() for node in self.source: attr = dict(node.attr) value = node.value location = attr.pop('location') if isinstance(value, Bag): value = self.convertObject(value) if location in self.apibag: print 'redefining object:', location else: self.write(location, value, attr) self.apibag.setItem(location, value, attr) def write(self, location, obj, attr): destpath = [self.resultpath, 'objects'] + location.split('.') + 'obj.xml' if attr: print attr destpath = os.path.join(**destpath) obj.toXml(destpath, autocreate=True) def convertObject(self, src): result = Bag() for node in src: label = node.label if label in self.discard: continue attr = dict(node.attr) value = node.value if label == 'mixins': label = 'mixins_%s' % attr.get('scope', 'undefined_scope') if isinstance(value, Bag): value = self.convertItems(value) if label in result: print 'redefining:', label result.setItem(label, value, attr) return result def convertItems(self, items): result = Bag() for k, node in enumerate(items): label = node.label value = node.value attr = dict(node.attr) if label == 'method': label = attr.get('name') if label == 'property': label = attr.get('name') else: label = 'r_%i' % k if label: result.setItem(label, value, attr) return result
def rpc_getLoadedFiles(self, uploadPath=None, **kwargs): path = self.site.getStaticPath(uploadPath) result = Bag() b = DirectoryResolver(path) for i, n in enumerate( [(t[1], t[2]) for t in b.digest('#a.file_ext,#a.file_name,#a.abs_path') if t[0] == 'xls']): result.setItem('r_%i' % i, None, filename=n[0], filepath=n[1]) return result
def buildViews(self): """TODO""" elements = self.dbroot.adapter.listElements('views', schema=self.schema) children = Bag(self.children) for element in elements: if not element in children: children.setItem(element, None, tag='view') return SqlTableList(parent=self.structparent, name=self.name, attrs=self.attrs, children=children)
def getSessionData(self, page_id): data = self.session.get(page_id) if data is None: data = Bag() self.session[page_id] = data else: data.restoreFromPicklable() return data
def newTblInfoItem(self,new_item=None,tblid=None,item_type=None,**kwargs): data = Bag() data.setItem('root', Bag(),caption=self.db.table(tblid).name_long) infoitem_tbl = self.db.table('adm.tblinfo_item') based_on = new_item.pop('based_on') if based_on: data = infoitem_tbl.readColumns(pkey=based_on,columns='$data') new_item['data'] = data new_item['tblid'] = tblid new_item['item_type'] = item_type infoitem_tbl.insert(new_item) self.db.commit() return new_item['info_key']
def rpc_getThermo(self, thermoId, flag=None): session = self.page.session if flag == 'stop': session.loadSessionData() thermoBag = session.pagedata.getItem( 'thermo_%s' % thermoId) or Bag() thermoBag['stop'] = True session.setInPageData('thermo_%s' % thermoId, thermoBag) session.saveSessionData() else: thermoBag = session.pagedata.getItem( 'thermo_%s' % thermoId) or Bag() return thermoBag
def statspane_availableFieldValues(self, code=None, dfname=None, fieldname=None): result = Bag() with self.sharedData('pandasdata') as pandasdata: gp = self.getGnrPandas(pandasdata, code) gnrdf = gp.dataframes[dfname] df = gnrdf.dataframe for i, v in enumerate(df[fieldname].unique()): if v is not None: result.setItem('r_%s' % i, None, fieldvalue=v) return result
def rpc_monitorUpdate(self): result = Bag() for pkg in self.db.packages.values(): cblist = sorted([ func_name for func_name in dir(self) if func_name.startswith('rpc_monitor_%s_' % pkg.name) ]) if len(cblist) > 0: result[pkg.name] = Bag() for cbname in cblist: data_name = cbname[len('rpc_monitor_%s_' % pkg.name):] result[pkg.name][data_name] = Bag(getattr(self, cbname)()) return result
def getProcessesBag(self, columns=None, treshold=None): columns = (columns or 'pid,name').split(',') result = Bag() for p in psutil.process_iter(): d = p.as_dict() if d['cpu_percent'] > (treshold or 0): d['create_time'] = datetime.datetime.fromtimestamp( d['create_time']) d['cpu_percent'] = d['cpu_percent'] or 0. d['memory_percent'] = d['memory_percent'] or 0. row = Bag([(k, d[k]) for k in columns if k in d]) result.setItem('p_%s' % p.pid, row) return result
def test_4_record(self, pane): frame = pane.framePane(height='400px') frame.top.slotToolbar('*,run,10').run.slotButton('Run',action='FIRE .run;') bc = frame.center.borderContainer() top = bc.contentPane(region='top',height='150px',splitter=True) data = Bag() data['table'] = 'fatt.cliente' data.setItem('pkey', '') top.data('.data',data) top.multiValueEditor(value='^.data') center = bc.contentPane(region='center') center.tree(storepath='.result') frame.dataRpc('.result',self.netBagTestCaller,data='=.data',methodname='record',_fired='^.run')
def _prepareFilterMenu(self, filterOn): colsMenu = Bag() fltList = splitAndStrip(filterOn, ',') for i, col in enumerate(fltList): caption = col if ':' in col: caption, col = col.split(':') colsMenu.setItem('r%i' % i, None, col=col, caption=caption, childcontent='') return colsMenu
def rpc_currentRegister(self, pageId=None): store = self.pageStore(pageId) result = Bag() register_item = store.register_item result['data'] = store.data result['info'] = Bag( dict(user=register_item['user'], pageId=register_item['register_item_id'], start_ts=register_item['start_ts'], user_ip=register_item['user_ip'], user_agent=register_item['user_agent'], pagename=register_item['pagename'])) return result
def getPrinters(self): """TODO""" printersBag = Bag() if self.hasCups: cups_connection = cups.Connection() for printer_name, printer in cups_connection.getPrinters().items(): printer.update(dict(name=printer_name)) printersBag.setItem( '%s.%s' % (printer['printer-location'], printer_name.replace(':', '_')), None, printer) else: print 'pyCups is not installed' return printersBag
def getPackagesBag(self): result = Bag() for pkgid, pkg in self.site.gnrapp.packages.items(): result['%s/%s' % (pkg.project, pkgid)] = Bag( dict(pkg=pkgid, project=pkg.project, tables=','.join(self.db.packages[pkgid].tables.keys()))) url = self.site.gnrapp.config['uke?url'] result = self.site.callGnrRpcUrl(url, 'commands', 'checkPackages', pkgbag=result) return result
def load_gnr_config(self): if hasattr(self.options, 'config_path') and self.options.config_path: config_path = self.options.config_path else: if sys.platform == 'win32': config_path = '~\gnr' else: config_path = '~/.gnr' config_path = self.config_path = expandpath(config_path) if os.path.isdir(config_path): self.gnr_config = Bag(config_path) else: self.gnr_config = Bag()
def syncOutTransaction(self, transaction): fname = '%s_%s_%s_%s.xml' % ( transaction['request'].strftime('%Y-%m-%d_%H%M%S'), transaction['request'].microsecond, transaction['maintable'], transaction['action']) fname = os.path.join(self.folderdialog4d, 'test', fname) trbag = Bag() trbag['command'] = 'sync_in' trbag['maintable'] = transaction['maintable'] trbag['action'] = transaction['action'] trbag['data'] = Bag(transaction['data']) trbag.toXml(fname) self.db.table('gnr.sync_out').delete(transaction) self.db.commit()
def test_0_firsttest(self, pane): """First test description""" b = Bag() b.setItem('foo', None, caption='Test 1') b.setItem('foo.bar', None, caption='Test 2', color='red') b.setItem('spam', None, caption='Test 3') pane.data('.menu', b) pane.div('^.value?caption', min_width='40px', min_height='16px', display='inline-block', padding_left='4px', border='1px solid silver', background='white', rounded=4).menu(storepath='.menu', selected_fullpath='.value', modifiers='*') pane.dataController(""" if($2.kw.updvalue){ var path = this.absDatapath('.value'); genro._data.setAttr(path,b.getNode(p).attr); } """, p="^.value", b='=.menu')
def _columnPythonCode(self, col, relation=None): attributes = Bag() col = col.deepcopy() name = col.pop('name') dtype = col.pop('dtype') size = col.pop('size') name_long = col.pop('name_long') name_short = col.pop('name_short') tag = col.pop('tag') #column,formulaColumn,aliasColumn,pyColumn if dtype and dtype not in ('A', 'T', 'C'): attributes['dtype'] = dtype if size: attributes['size'] = size attributes.setItem('name_long', name_long or name, localized=True) if name_short: attributes.setItem('name_short', name_short, localized=True) attributes.update(col) relationCode = '' if relation and relation['relation']: relationCode = '.%s' % self._relationPythonCode(relation) coltype = 'column' if attributes['sql_formula'] or attributes['select'] or attributes[ 'exists']: coltype = 'formulaColumn' elif attributes['relation_path']: coltype = 'aliasColumn' elif attributes['pymethod']: coltype = 'pyColumn' return "tbl.%s('%s'%s)%s" % ( coltype, name, self.bagToArgString(attributes), relationCode)
def getRepositoryBag(self): repositories = Bag() for path in uniquify(self.site.gnrapp.config['packages'].digest('#a.path')): has_git = False while path and not has_git: path = os.path.split(path)[0] has_git = os.path.exists(os.path.join(path,'.git')) if has_git and not path in repositories: changes = self.getGitRepositoriesChanges(path) repositories[path] = Bag(path=path,name=os.path.split(path)[1], to_pull="""<a href='#' onclick="genro.publish('git_pull',{repository:'%s'})">Pull %i</a>""" %(path,changes['to_pull']) if changes['to_pull'] else ' ', to_push = """<a href='#' onclick="genro.publish('git_push',{repository:'%s'})">Push %i</a>""" %(path,changes['to_push']) if changes['to_push'] else ' ', ) return repositories
def getDuplicates(self, table=None, field=None, code=None): result = Bag() f = self.db.table(table).query( columns='$%s AS fldval, count($%s) AS cnt' % (field, field), where="$%s LIKE :c AND $%s IS NOT NULL" % (field, field), c='%%%s%%' % code, group_by='$%s' % field, having="count($%s)>1" % field, addPkeyColumn=False, excludeDraft=False).fetch() for fldval, cnt in f: result[fldval.replace('.', '_')] = Bag(dict(fldval=fldval, cnt=cnt)) return result
class StaticHandlerManager(object): """ This class handles the StaticHandlers""" def __init__(self, site): self.site = site self.statics = Bag() def addAllStatics(self, module=None): """inspect self (or other modules) for StaticHandler subclasses and do addStatic for each""" module = module or sys.modules[self.__module__] def is_StaticHandler(cls): return inspect.isclass(cls) and issubclass( cls, StaticHandler) and cls is not StaticHandler statichandler_classes = inspect.getmembers(module, is_StaticHandler) for statichandler in statichandler_classes: self.add(statichandler[1]) def add(self, static_handler_factory, **kwargs): static_handler = static_handler_factory(self.site, **kwargs) self.statics.setItem(static_handler.prefix, static_handler, **kwargs) def get(self, static_name): return self.statics[static_name] def fileserve(self, f, environ, start_response, download=False, **kwargs): return StaticHandler(self.site).serve(f, environ, start_response, download=download, **kwargs) def static_dispatcher(self, path_list, environ, start_response, download=False, **kwargs): handler = self.get(path_list[0][1:]) if handler: result = handler.serve(path_list, environ, start_response, download=download, **kwargs) return result else: return self.site.not_found_exception(environ, start_response)
def rpc_getMessages(self): msgbag = Bag() msgfolder = self.pageLocalDocument(os.path.join('_messages')) if os.path.exists(msgfolder): files = [(os.stat(os.path.join(msgfolder, f)).st_mtime, f) for f in os.listdir(msgfolder)] files.sort() for f in files: if not f.startswith('.'): fname = os.path.join(msgfolder, f) b = Bag(fname) msgbag.update(b) os.remove(fname) return msgbag
def ivInline(self, parent, **kwargs): """An includedview on a datastore's path.""" bc = parent.borderContainer(**kwargs) b = Bag() b.addItem('r0', Bag(dict(name="name", description="Full name", size=40, type="U"))) b.addItem('r1', Bag(dict(name="birth date", description="Birth date", size=10, type="D"))) bc.data('inline_data', b) def structInlineData(struct): r = struct.view().rows() r.cell('name', name='!!Name', width='20ex') r.cell('description', name='!!Description', width='40ex') r.cell('size', name='!!Size', width='5ex') r.cell('type', name='!!Type', width='20ex') iv = self.includedViewBox(bc, label='!!Inline IncludedView', locked=False, datamode="bag", storepath='inline_data', struct=structInlineData, autoWidth=True, add_action=True, del_action=True) ge = iv.gridEditor() ge.textbox(gridcell="name") ge.textbox(gridcell="description") ge.numberspinner(gridcell="size", min=1, max=100) ge.filteringselect(gridcell="type", values="I:Int,R:Real,S:String,U:Unicode,D:Date,H:Time,DH:Date and Time,X:Bag")
def get_items(self, items, child_name=None, **kwargs): result = Bag() for key, item in items.items(): _customClasses = [] item['_pkey'] = key if item['last_refresh_age'] > 60: _customClasses.append('disconnected') elif item['last_event_age'] > 60: _customClasses.append('inactive') if child_name and not item[child_name]: _customClasses.append('no_children') item.pop('datachanges', None) result.setItem(key, None, _customClasses=' '.join(_customClasses), **item) return result
def compileSiteMenu(self, menubag, basepath=None, level=None): basepath = basepath or [] level = level or 0 result = Bag() level = level or 0 for node in menubag.nodes: attributes = {} attributes.update(node.getAttr()) currbasepath = basepath if 'pkg' in attributes: if 'dir' in attributes: url_info = self.site.getUrlInfo( [attributes['pkg'], attributes['dir']]) dirpath = os.path.join(url_info.basepath, *url_info.request_args) value = DirectoryResolver(dirpath, cacheTime=10, include='*.py', exclude='__*,.*', dropext=True, readOnly=False) currbasepath = [attributes['pkg'], attributes['dir']] else: value = self.packages[attributes['pkg']].pkgMenu['#0'] else: value = node.getStaticValue() if 'basepath' in attributes: newbasepath = attributes.pop('basepath') if newbasepath.startswith('/'): currbasepath = [self.site.home_uri + newbasepath[1:]] else: currbasepath = basepath + [newbasepath] if isinstance(value, Bag): value = self.compileSiteMenu(value, currbasepath, level=level + 1) elif not isinstance(value, DirectoryResolver): value = None filepath = attributes.get('file') if filepath: if not filepath.startswith('/'): attributes['file'] = os.path.join(*(currbasepath + [filepath])) else: attributes[ 'file'] = self.site.home_uri + filepath.lstrip('/') result.setItem(node.label, value, attributes) if len(result) == 1 and not level: result = result['#0'] return result
def packageMenu(self): result = Bag() for pkg in self.application.packages: attr = pkg.attr if attr.get('_syspackage'): continue lookup_tables = self.getLookupTable(pkg.label) if lookup_tables: result.setItem( pkg.label, Bag(code=pkg.label, description=pkg.value.attributes.get('name_long') or pkg)) return result
def de_loadStoreFromFile(self, path, default_title=None, **kwargs): html = '' title = '' path = self.site.getStaticPath(path) if os.path.exists(path): with open(path, 'r') as f: result = f.read() m = re.search("<body>(.*)</body>", result, re.I | re.S) if m: html = m.group(1) m = re.search("<title>(.*)</title>", result, re.I | re.S) if m: title = m.group(1) return Bag(content=Bag(body=html, title=title or default_title))
def columnsPane(self, where, **kwargs): pane = where.accordionPane(**kwargs) struct = Bag() r = struct.child('view').child('rows') r.child('cell', field='col', width='60%', name='Column') r.child('cell', field='as', width='30%', name='As') r.child('cell', field='width', width='10%', name='Width') pane.data('.colgrid.structure', struct) pane.data('.colgrid.data', Bag()) pane.staticGrid(storepath='.colgrid.data', structpath='.colgrid.structure', identifier='col')
def tracebackBag(limit=None): import linecache from gnr.core.gnrstructures import GnrStructData from gnr.core.gnrbag import Bag result = Bag() if limit is None: if hasattr(sys, 'tracebacklimit'): limit = sys.tracebacklimit n = 0 tb = sys.exc_info()[2] def cb(n): if n.resolver: n.resolver = None n.value = '*RESOLVER* %s' % n.label if isinstance(n.getValue('static'), GnrStructData): n.value = '*STRUCTURE*' while tb is not None and (limit is None or n < limit): tb_bag = Bag() f = tb.tb_frame lineno = tb.tb_lineno co = f.f_code filename = co.co_filename name = co.co_name linecache.checkcache(filename) line = linecache.getline(filename, lineno) if line: line = line.strip() else: line = None tb_bag['module'] = os.path.basename(os.path.splitext(filename)[0]) tb_bag['filename'] = filename tb_bag['lineno'] = lineno tb_bag['name'] = name tb_bag['line'] = line loc = Bag() for k, v in f.f_locals.items(): try: loc[k] = v except Exception: loc[k] = '*UNSERIALIZABLE* %s' % v.__class__ tb_bag['locals'] = loc tb_bag['locals'].walk(cb) tb = tb.tb_next n = n + 1 result['%s method %s line %s' % (tb_bag['module'], name, lineno)] = tb_bag return Bag(root=result)
def login_checkAvatar(self, password=None, user=None, serverTimeDelta=None, **kwargs): result = Bag() try: avatar = self.application.getAvatar(user, password=password, authenticate=True) if not avatar: return result except GnrRestrictedAccessException, e: return Bag(login_error_msg=e.description)
def cb(row): b = Bag(row['data']) result = dict() if row['version'] >0: result['changed_fields'] = '<br/>'.join(b.keys()) b.pop('__ins_ts') b.pop('__version') b.pop('__mod_ts') self._curr_audit_record.update(b) result['__value__'] = self._curr_audit_record.deepcopy() return result
def test_setItemPos(self): b = Bag({'a': 1, 'b': 2, 'c': 3, 'd': 4}) b.setItem('e', 5, _position='<') assert b['#0'] == 5 b.setItem('f', 6, _position='<c') assert b['#2'] == 6 b.setItem('g', 7, _position='<#3') assert b['#3'] == 7
def buildViews(self): """add??? :returns: add??? """ elements = self.dbroot.adapter.listElements('views', schema=self.schema) children = Bag(self.children) for element in elements: if not element in children: children.setItem(element, None, tag='view') return SqlTableList(parent=self.structparent, name=self.name, attrs=self.attrs, children=children)
def importFile(self, fullname): try: b = Bag(fullname) except: time.sleep( 10 ) # 4D may be still writing the file, wait some seconds and try again b = Bag(fullname) if 'transaction' in b: for tr, attr in b.digest('#v,#a'): n = tr.getNode('trigger') attr.update(n.getAttr()) self.writeTransaction(n.value, attr, file_name=fullname) else: self.writeImport(b, file_name=fullname)