Welcome    Usage    Browse    Find CID    Search     Log in

cM API Documentation

module.py

Go to the documentation of this file.
00001 #
00002 # Collective Mind
00003 #
00004 # See cM LICENSE.txt for licensing details.
00005 # See cM Copyright.txt for copyright details.
00006 #
00007 # Developer(s): (C) Grigori Fursin, started on 2011.09
00008 #
00009 
00010 # Should always be here
00011 ini={}
00012 cm_kernel=None
00013 
00014 # Local settings
00015 import string
00016 import os
00017 import shutil
00018 import json
00019 
00020 # ============================================================================
00021 def init(i):
00022     return {'cm_return':0}
00023 
00024 # ============================================================================
00025 def add_data(i):
00026 
00027     """
00028     Add module
00029 
00030     Input:  {
00031             }
00032 
00033     Output: {
00034               cm_return  - return code >0 if error
00035               cm_path    - path to created data entry
00036             }
00037     """
00038 
00039     if 'cm_file_in' in i:
00040        p1=os.path.join(ini['path'], i['cm_file_in'])
00041        if not os.path.isfile(p1):
00042           return {'cm_return':1, 'cm_error':'can\'t find file '+p1}
00043 
00044     # If web access, move to 'core' module
00045     if i.get('cm_console','')=='web':
00046        i['cm_run_module_uoa']=ini['cfg']['cm_modules']['cm-core']
00047        i['cm_action']='add'
00048        return cm_kernel.access(i)
00049 
00050     # Go to common core function
00051     i['cm_run_module_uoa']=ini['cfg']['cm_modules']['cm-core']
00052     if cm_kernel.ini.get('cm_console','')!='web':  i['cm_console']=i.get('cm_console','')
00053     else:                                          i['cm_console']=''
00054     if cm_kernel.ini.get('web','')=='yes': i['cm_report_in_html']='yes'
00055     r=cm_kernel.access(i)
00056     if r['cm_return']>0: return r
00057 
00058     if 'cm_file_out' in i:
00059        p2=os.path.join(r['cm_path'], i['cm_file_out'])
00060        if os.path.isfile(p2):
00061           return {'cm_return':1, 'cm_error':'file already exists '+p2}
00062        else:
00063           shutil.copyfile(p1,p2)
00064 
00065     # We need it here since this function overloads original 'core' add_data function
00066     r['cm_skip_report']='yes'
00067 
00068     return r
00069 
00070 # ============================================================================
00071 def visualize_connections(i):
00072     """
00073     Detect parameters in web forms 
00074 
00075     Input:  {
00076               cm_show_only_graph - if 'yes', show only graph and skip all the rest
00077               cm_width           - graph width
00078               cm_height          - graph height
00079             }
00080 
00081     Output: {
00082               cm_return  - return code >0 if error
00083               cm_array   - parsed array with keys
00084               cm_actions - parsed array with commands
00085             }
00086     """
00087 
00088     # Web style
00089     if 'cfg' in cm_kernel.ini['web_style']: web=cm_kernel.ini['web_style']['cfg']
00090     else:
00091        return {'cm_return':1, 'cm_error':'web style is not defined'}
00092 
00093     # Detecting/restoring data from forms
00094     a1={}
00095 
00096     r=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00097                         'cm_action':'detect_form_params',
00098                         'cm_array':i, 
00099                         'cm_prefix':'#form1'})
00100     if r['cm_return']>0: return r
00101     cm_form_array1=r['cm_array']
00102     cm_form_commands1=r['cm_commands']
00103 
00104     # Get data description for this action
00105     r=cm_kernel.get_data_description({'cm_module_uoa':ini['cm_module_uoa'], 
00106                                       'cm_which_action':i['cm_action']})
00107     if r['cm_return']>0: return r
00108     cm_data_desc=r['cm_data_desc']
00109     cm_params_default=r['cm_params_default']
00110 
00111     # Check default
00112     if len(cm_form_array1)==0:
00113        a1=cm_params_default
00114     else:
00115        r=cm_kernel.restore_flattened_array({'cm_array':cm_form_array1, 
00116                                             'cm_replace_in_keys':{'^35^':'#', '^64^':'@'}})
00117        if r['cm_return']>0: return r
00118        a1=r['cm_array']
00119 
00120     # Set default
00121     width="900"
00122     if 'cm_width' in a1: width=a1['cm_width']
00123     elif 'cm_width' in i: width=i['cm_width']
00124 #    a1['cm_width']=width
00125 
00126     height="600"
00127     if 'cm_height' in a1: height=a1['cm_height']
00128     elif 'cm_height' in i: height=i['cm_height']
00129 #    a1['cm_height']=height
00130 
00131     # If there is data in form, means that refresh
00132     forms_exists='yes'
00133     if len(cm_form_array1)==0:forms_exists='no'
00134 
00135     # Get list of modules
00136     ls=[]
00137     ls1=[]
00138     pm=a1.get('prune_module_uoa','')
00139 
00140     r=cm_kernel.access({'cm_run_module_uoa':ini['cm_module_uid'],
00141                         'cm_action':'list'})
00142     if r['cm_return']>0: return r
00143     ls=r['cm_mixed']
00144 
00145     if pm=='':
00146        ls1=ls
00147     else:
00148        r=cm_kernel.access({'cm_run_module_uoa':ini['cm_module_uid'],
00149                            'cm_action':'load',
00150                            'cm_data_uoa':pm})
00151        if r['cm_return']>0: return r
00152        ls1.append(r)
00153 
00154     # Nodes
00155     a={'nodes':[], 'links':[]}
00156 
00157     nodes={}
00158     inodes=0
00159 
00160     # Links
00161     for l in ls1:
00162         x=l['cm_uoa'];  xu=l['cm_uid'];  xa=l['cm_alias'];  xd=l['cm_display_html']
00163    
00164         # Load module
00165         rx=cm_kernel.access({'cm_run_module_uoa':ini['cm_module_uid'],
00166                             'cm_action':'load',
00167                             'cm_data_uoa':xu})
00168         if rx['cm_return']>0 and ry['cm_return']!=16 and ry['cm_return']!=32: return rx
00169         if rx['cm_return']==0:
00170            d=rx['cm_data_obj']
00171 
00172            m=[]
00173 
00174            if 'cm_modules' in d['cfg']: 
00175               for q in d['cfg']['cm_modules']:
00176                   m.append(d['cfg']['cm_modules'][q])
00177 
00178            if 'cm_data_description' in d['cfg']:
00179               for q in d['cfg']['cm_data_description']:
00180                   dx=d['cfg']['cm_data_description'][q]
00181                   if dx.get('type','')=='uoa':
00182                      if dx.get('cm_module_uoa','')!='': 
00183                         m.append(dx['cm_module_uoa'])
00184 
00185            for q in m:
00186                # Load sub-module
00187                ry=cm_kernel.access({'cm_run_module_uoa':ini['cm_module_uid'],
00188                                    'cm_action':'load',
00189                                    'cm_data_uoa':q})
00190                if ry['cm_return']>0 and ry['cm_return']!=16 and ry['cm_return']!=32: return ry
00191                if ry['cm_return']==0:
00192                   y=ry['cm_uoa'];  yu=ry['cm_uid'];  ya=ry['cm_alias'];  yd=ry['cm_display_html']
00193 
00194                   if xu!=yu:
00195                      if xu not in nodes:
00196                         a['nodes'].append({"name":xd, "group":xu, "uid":xu})
00197                         nodes[xu]=inodes
00198                         inodes+=1 
00199                      nxu=nodes[xu]
00200 
00201                      if yu not in nodes:
00202                         a['nodes'].append({"name":yd, "group":yu, "uid":yu})
00203                         nodes[yu]=inodes
00204                         inodes+=1 
00205                      nyu=nodes[yu]
00206 
00207                      a['links'].append({"source":nxu, "target": nyu, "type":"arrow"})
00208 
00209     # Load D3 data
00210     rx=cm_kernel.access({'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-code'],
00211                          'cm_action':'load',
00212                          'cm_data_uoa':ini['cfg']['cm_web_d3_uoa']})
00213     if rx['cm_return']>0: return rx
00214 
00215     web_d3=rx['cm_data_obj']['cfg']
00216 
00217     link_d3=web['http_prefix']+'cm_web_action=show&cid='+ini['cfg']['cm_modules']['cm-code']+':'+ini['cfg']['cm_web_d3_uoa']+'&cm_web_filename=d3.v2.js'
00218 
00219     # Prepare visualization
00220     if i.get('cm_show_only_graph','')!='yes':
00221        cm_kernel.print_for_con('<span class="cm-title"><center>Visualize connections between modules</center></span>')
00222 
00223     cm_kernel.print_for_con('<script type="text/javascript" src="'+link_d3+'"></script>')
00224 
00225     cm_kernel.print_for_web(web['table_init']+'<tr><td>')
00226     cm_kernel.print_for_con('<div id="chart"></div>')
00227     if i.get('cm_show_only_graph','')!='yes':
00228        cm_kernel.print_for_con('<center><a href="#" id="download"><br>Download svg (experimental)</a></center>')
00229     cm_kernel.print_for_web('</td></tr></table>')
00230 
00231     cm_kernel.print_for_con(web_d3['d3_force_css'])
00232 
00233     cm_kernel.print_for_con('<script type="text/javascript">')
00234     cm_kernel.print_for_con('var nodes='+json.dumps(a['nodes'])+';')
00235     cm_kernel.print_for_con('var links='+json.dumps(a['links'])+';')
00236 
00237 #    sou=web['http_prefix']+'cm_menu='+web['cm_menu_browse']+'&cm_subaction_view&browse_cid='+ini['cm_module_uoa']+':'
00238     sou=web['http_prefix']+'view_cid='+ini['cm_module_uoa']+':'
00239 
00240     h=web_d3['d3_force_html'].replace(cm_kernel.convert_str_to_special('url'), sou)
00241     h=h.replace(cm_kernel.convert_str_to_special('cm_width'), width)
00242     h=h.replace(cm_kernel.convert_str_to_special('cm_height'), height)
00243     cm_kernel.print_for_con(h)
00244 
00245     cm_kernel.print_for_con('</script>')
00246  
00247     # Start form
00248     if i.get('cm_show_only_graph','')!='yes':
00249        # Customize form
00250        cm_kernel.print_for_con('<br><small><b>Customize</b></small>')
00251 
00252        cm_kernel.print_for_web('<FORM ACTION="" name="add_edit" METHOD="POST" enctype="multipart/form-data" accept-charset="utf-8">' )
00253 
00254        ii={'cm_run_module_uoa':ini['cfg']['cm_modules']['cm-web'],
00255            'cm_action':'visualize_data',
00256            'cm_array':a1,
00257            'cm_data_desc':cm_data_desc,
00258            'cm_form_commands':cm_form_commands1,
00259            'cm_separator':'#',
00260            'cm_separator_form':'#form1#',
00261            'cm_forms_exists':forms_exists,
00262            'cm_support_raw_edit':'yes',
00263            'cm_mode':'add'}
00264        if 'cm_raw_edit' in i: ii['cm_raw_edit']=i['cm_raw_edit']
00265        if 'cm_back_json' in i: ii['cm_back_json']=i['cm_back_json']
00266        r=cm_kernel.access(ii)
00267        if r['cm_return']>0: return r
00268        cm_kernel.print_for_web(r['cm_string'])
00269 
00270        cm_kernel.print_for_con('<input type="submit" class="cm-button" value="Refresh">')
00271 
00272        cm_kernel.print_for_con('</FORM>')
00273 
00274        cm_kernel.print_for_con('<HR class="cm-hr">')
00275 
00276     return {'cm_return':0}

Generated on Wed May 28 02:49:02 2014 for Collective Mind Framework by DoxyGen 1.6.1
Concept, design and coordination: Grigori Fursin (C) 1993-2013