You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
calculate-utils-3-console/console/application/methods_func.py

205 lines
8.2 KiB

#-*- coding: utf-8 -*-
# Copyright 2012 Calculate Ltd. http://www.calculate-linux.org
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse, sys
from suds import MethodNotFound
from function import create_obj, listToArray, listToArrayArray
from calculate.lib.cl_lang import setLocalTranslate
setLocalTranslate('calculate_console',sys.modules[__name__])
def _create_obj(client, method):
try:
view_params = create_obj(client, method)
except MethodNotFound:
print _('Method not found: ') + method
sys.exit(1)
return view_params
def get_view_params(client, method, step = None, expert = None, brief = None):
view_params = _create_obj(client, method)
view_params.step = step
view_params.expert = expert
view_params.brief = brief
return view_params
def get_view(client, method, sid, view_params):
view = client.service[0][method + '_view'](client.sid, view_params)
return view
def get_method_argparser(client, args):
method = args.method
view_params = get_view_params(client, method + '_view', step = None, \
expert = True)
progr = 'cl-console --method '+method
view = get_view(client, method, client.sid, view_params)
parser = argparse.ArgumentParser(prog=progr, add_help=False)
for Group in view.groups.GroupField:
if not Group.fields:
continue
for field in Group.fields.Field:
if field.opt:
action = None
arg_type = str
if field.element in ['check', 'check_tristate']:
arg_type = bool
elif field.element == 'radio' and field.type == 'bool':
arg_type = bool
elif field.element == 'table' and field.type != 'steps':
action = 'append'
opt = field.opt
metavalue = opt.metavalue if opt.metavalue \
else field.name.upper()
if ':' in metavalue:
metavalue = field.name.upper()
if arg_type == str:
if opt.shortopt and opt.longopt:
parser.add_argument(opt.shortopt, opt.longopt,
type = arg_type, dest=field.name, metavar = \
metavalue, action = action, help = opt.help)
elif opt.shortopt and not opt.longopt:
parser.add_argument(opt.shortopt,
type = arg_type, dest=field.name, metavar = \
metavalue, action = action, help = opt.help)
elif opt.longopt and not opt.shortopt:
parser.add_argument(opt.longopt,
type = arg_type, dest=field.name, metavar = \
metavalue, action = action, help = opt.help)
elif arg_type == bool:
if opt.shortopt and opt.longopt:
parser.add_argument(opt.shortopt, opt.longopt,
action='store_true', dest=field.name,
default=None, help = opt.help)
elif opt.shortopt and not opt.longopt:
parser.add_argument(opt.shortopt,
action='store_true', dest=field.name,
default=None, help = opt.help)
elif opt.longopt and not opt.shortopt:
parser.add_argument(opt.longopt,
action='store_true', dest=field.name,
default=None, help = opt.help)
return parser, view
def call_method(client, args):
print 'call_method'
method = args.method
# view_params = get_view_params(client, method + '_view', step = None, \
# expert = True)
#
# view = get_view(client, method, client.sid, view_params)
method_parser, view = get_method_argparser(client, args)
param_object = _create_obj(client, method)
args = method_parser.parse_known_args()[0]
param_object = collect_object(client, param_object, view, args)
# print param_object
method_result = client.service[0][method](client.sid, param_object)
return method_result
def _getattr(obj, attr):
return getattr(obj, attr) if hasattr(obj, attr) else None
def collect_object(client, param_object, view, args):
for Group in view.groups.GroupField:
if not Group.fields:
continue
for field in Group.fields.Field:
if field.element in ['check', 'check_tristate'] or \
field.element == 'radio' and field.type == 'bool':
param_object[field.name] = _getattr(args, field.name)
elif field.element == 'input' and \
field.name in ['cl_page_offset','cl_page_count']:
val = _getattr(args, field.name)
if not val:
val = 0
param_object[field.name] = val
elif field.element in ['input', 'combo', 'comboEdit', 'openfile', \
'file', 'password', 'radio']:
param_object[field.name] = _getattr(args, field.name)
elif field.element in ['multichoice', 'multichoice_add', \
'selecttable', 'selecttable_add']:
val = _getattr(args, field.name)
param_object[field.name] = listToArray(client, val.split(','))\
if val else None
elif field.element == 'table' and field.type != 'steps':
val = _getattr(args, field.name)
param_object[field.name] = collect_table(field, val, client)
return param_object
# print result_table
def collect_table(field, val_list, client):
if not val_list:
return None
val_table = map(lambda x: x.split(':'), val_list)
column = len(field.tablevalue.head.string)
ChoiceValue = field.tablevalue.values.ChoiceValue
result_table = []
for i in range(len(val_table)):
temp_row = []
for j in range(column):
# not adding if readonly
if j > (len(ChoiceValue) + 1):
continue
choice_value = ChoiceValue[j]
typefield = choice_value.typefield
if typefield == 'readonly':
continue
elif typefield in ['check', 'check_tristate']:
if len (val_table[i]) < j + 1:
temp_row.append('')
continue
try:
if val_table[i][j].lower() in ['on', 'yes']:
temp_row.append('on')
elif val_table[i][j].lower() in ['off', 'no']:
temp_row.append('off')
else:
temp_row.append(val_table[i][j])
except:
import ipdb
ipdb.set_trace()
elif typefield in ['input', 'combo', 'comboEdit', 'openfile', \
'file', 'password', 'radio']:
if len (val_table[i]) < j + 1:
temp_row.append('')
else:
temp_row.append(val_table[i][j])
elif typefield in ['multichoice', 'multichoice_add']:
if len (val_table[i]) < j + 1:
temp_row.append('')
else:
temp_row.append(val_table[i][j])
result_table.append(temp_row)
return listToArrayArray(client, result_table)