最近一次偶然的机会,需要实现cognos mdl权限自动化,创建customer view ,赋权限,生成权限mdl,花了一点时间整理了一下,记录一下代码:主要参考 IBM Cognos Transfomer Developer Guide.
主要用到的以下方法:
SecurityNameSpaceMake
ViewMake
UserClassMake
SecurityObjectMake
实现思路:从配制文件里读取mdlpath, db信息,实现可配制化,上代码:
import pymysql #my sql 里存在所有的视图角色信息,权限维度等信息 import configparser #读取文件内容 config = configparser.ConfigParser() # 创建对象 config.read("mdlbatchfile", encoding='UTF-8') # 读取db信息 host = config.get('dbconfig', 'host') user = config.get('dbconfig', 'user') pwd = config.get('dbconfig', 'pwd') dbname = config.get('dbconfig', 'dbname') port = int(config.get('dbconfig', 'port')) # 读取 mdl 信息 mdlpath = config.get('mdlconfig', 'mdlpath') mdlSavePath = config.get('mdlconfig', 'mdlSavePath') mdl_batch_list = config.get('mdlconfig', 'mdl_batch_list') SecurityNamespace = config.get('mdlconfig', 'SecurityNamespace') # 取权限列表 agency_sql = config.get('sqlconfig', 'agency_sql') #读取角色信息 role_sql = config.get('sqlconfig', 'role_sql') #print(mdl_batch_list) #读取transformer 所在路径 cogtrPath = config.get('cogtrPath', 'cogtrPath') #连接数据库取相应的权限角色信息 db = pymysql.connect(host = host,user=user,password=pwd,port = port,database=dbname) cursor = db.cursor() cursor.execute(agency_sql) results = cursor.fetchall() cursor.execute(role_sql) result_role = cursor.fetchall() print(mdlSavePath+'/batchfile.bat') fo_batch = open(mdlSavePath+'/batchfile.bat','w', encoding='UTF-8') print('cd \"'+cogtrPath +'>\"') fo_batch.writelines('cd \"'+cogtrPath +'>\"\n' ) for mdlname in mdl_batch_list.split(','): mdlfullpath_var=mdlSavePath+'/'+mdlname+'_s.mdl' mdlpath_var = mdlpath+'/'+mdlname+'.mdl' #print(mdlname) #print('mdlpath:',mdlpath) # 生成单独的文件 print(mdlpath_var.replace('/','\\')) fo_batch.writelines('cogtr.exe -c -n2 -s -m \"' + mdlfullpath_var + '\"\n') #生成刷新文件 fo = open(mdlfullpath_var, 'w', encoding='UTF-8') fo.writelines("OpenMDL "+'"' +mdlpath_var.replace('/','\\') +'"' +'\n') fo.writelines('SecurityNameSpaceMake '+ '\"'+SecurityNamespace+ '\"'+' SecurityNamespaceCAMID '+ '\'CAMID (\"'+SecurityNamespace+'\")\''+'\n') fo.write('\n') #fo.writelines(mdlpath+'/'+mdlname+'.mdl'+'\n') concatstr = '' for row in results: row_str = 'ViewMake \"ComcodeViewA' + str(row[0]) + '\" Dimension "org" ViewUserClass ' + "\"" + str(row[0]) + "\" Apex \""+str(row[0])+'\"' row_str1 = 'UserClassMake \"' + str(row[0]) + '\" DimensionView "org" ' + "\"" + 'ComcodeViewA' + str( row[0]) + "\"" fo.write(row_str + '\n') fo.write(row_str1 + '\n') fo.write('\n') concatstr = concatstr + "\""+str(row[0])+"\""+' ' for row in result_role: row_str ='SecurityObjectMake \'CAMID(\"'+SecurityNamespace+':r:'+str(row[0])+'\")\''\ +' SecurityNamespace \"' +SecurityNamespace+'\" SecurityObjectDisplayName \"'+str(row[1])\ +'\" SecurityObjectType' endstr ='SecurityType_Role CustomViewList \"'+str(row[1])+'\" EndList' fo.write(row_str+ '\n') fo.write(endstr+ '\n') fo.write('\n') #print(row_str) #print(endstr) fo.write('PowerCubeUserListUpdate Cube ' + "\"" + mdlname + "\"" + ' StartList ' + concatstr + 'EndList \n') fo.write('SaveMDL' + '"' + mdlfullpath_var.replace('.mdl','_s.mdl') + '"') fo_batch.writelines('echo ok&pause') fo_batch.close() fo.close() db.close() print('success')
生成的结果图
第一次写代码,考虑不全,欢迎大家提意见。