From 796d766516781fc112b19d9bacdb636b7aa1784d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BF=D0=B8=D1=80=D0=B8=D0=B4=D0=BE=D0=BD=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=94=D0=B5=D0=BD=D0=B8=D1=81?= Date: Thu, 21 Jun 2012 11:22:00 +0400 Subject: [PATCH] move functions to core module --- console/application/cert_func.py | 19 +- console/application/cl_client.py | 9 +- console/application/function.py | 24 +-- console/application/methods_func.py | 263 ++----------------------- console/application/pid_information.py | 2 +- console/application/sid_func.py | 4 +- 6 files changed, 25 insertions(+), 296 deletions(-) diff --git a/console/application/cert_func.py b/console/application/cert_func.py index 16d71fb..3b4839c 100644 --- a/console/application/cert_func.py +++ b/console/application/cert_func.py @@ -23,6 +23,7 @@ import OpenSSL, hashlib from client_class import HTTPSClientCertTransport from cert_verify import VerifyError from calculate.core.datavars import DataVarsCore +from calculate.core.server.methods_func import get_password from calculate.lib.cl_lang import setLocalTranslate from calculate.lib.utils.common import getpass from calculate.lib.utils.files import listDirectory @@ -114,24 +115,6 @@ def delete_old_cert(client): except OSError, e: _print (e.message) -def get_password(text1 = None, text2 = None): - if not text1: - text1 = _('Password: ') - if not text2: - text2 = _('Repeat: ') - try: - pass1 = 'password' - pass2 = 'repeat' - while pass1 != pass2: - pass1 = getpass.getpass(text1) - pass2 = getpass.getpass(text2) - if pass1 != pass2: - print _('Passwords do not match') - except KeyboardInterrupt: - return None - passwd = pass1 if (pass1 and pass1 == pass2) else None - return passwd - def client_post_request (cert_path, args): if os.path.exists(cert_path + 'req_id'): print _("You have sent a request to sign the certificate.") diff --git a/console/application/cl_client.py b/console/application/cl_client.py index fa14007..643a865 100644 --- a/console/application/cl_client.py +++ b/console/application/cl_client.py @@ -23,7 +23,7 @@ import traceback as tb import time, logging import os, sys import threading, urllib2 -from function import analysis, clear, get_entire_frame +from function import analysis, clear, get_entire_frame, get_view_params from pid_information import client_list_methods from cert_func import client_post_auth, client_post_request, client_get_cert,\ client_post_cert, get_password_from_daemon, clear_password @@ -34,7 +34,7 @@ import M2Crypto, OpenSSL from calculate.core.datavars import DataVarsCore from client_class import HTTPSClientCertTransport -from methods_func import call_method, get_method_argparser, parse +from methods_func import call_method, get_method_argparser, parse, get_view from calculate.lib.utils.files import makeDirectory, readLinesFile from calculate.lib.cl_lang import setLocalTranslate setLocalTranslate('calculate_console',sys.modules[__name__]) @@ -229,7 +229,10 @@ def https_server(client, args, unknown_args, url, clVarsCore, wait_thread): return 1 elif args.method and args.help: - method_parser, view = get_method_argparser(client, args) + view_params = get_view_params(client, args.method + '_view', + step = None, expert = True) + view = get_view(client, args.method, client.sid, view_params) + method_parser = get_method_argparser(view, args) wait_thread.stop() sys.stdout.write("\b") sys.stdout.flush() diff --git a/console/application/function.py b/console/application/function.py index 79db1f0..fb15a84 100644 --- a/console/application/function.py +++ b/console/application/function.py @@ -90,7 +90,7 @@ def print_brief_group(Fields, group_name): _print ('\b'+group_name) uncompatible_count = 0 green = '\033[32m * \033[0m' - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' for field in Fields: if field.uncompatible: uncompatible_count += 1 @@ -202,21 +202,6 @@ def print_brief(view, brief_label): continue print_brief_group(Group.fields.Field, Group.name) -def listToArray (client, _list, _type = 'string'): - Array = client.factory.create('%sArray' %_type) - for i in _list: - Array['%s' %_type].append(i) - return Array - -def listToArrayArray (client, _list, _type = 'string'): - ArrayArray = client.factory.create('%sArrayArray' %_type) - for i in _list: - Array = client.factory.create('%sArray' %_type) - for j in i: - Array[_type].append(j) - ArrayArray['%sArray' %_type].append(Array) - return ArrayArray - class switch(object): def __init__(self, value): self.value = value @@ -291,13 +276,13 @@ def show_table(table, item): def show_error(item): if item.message: - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' print red + _("Error") _print (red + item.message) def show_warning(item): if item.message: - yellow = '\033[93m * \033[0m' + yellow = '\033[33m * \033[0m' print yellow + _("Warning") _print (yellow + item.message) @@ -483,7 +468,6 @@ def get_Progress(client, sid, pid, id): percent = returnProgr.percent while percent <= 100 and percent >= 0: if temp_progress != percent: -# last_message = print_progress(returnProgr, last_msg = last_message) last_message = print_progressbar(returnProgr, pbar, last_msg = last_message) # pbar.update(returnProgr.percent) @@ -592,7 +576,7 @@ def _return_revoked_serials(self, crlfile): call = '/usr/bin/openssl crl -text -noout -in %s' % crlfile call = shlex.split(call) serials = [] - (res,err) = subprocess.Popen(call, stdout=subprocess.PIPE).communicate() + (res,err)=subprocess.Popen(call, stdout=subprocess.PIPE).communicate() for line in res.split('\n'): if line.find('Serial Number:') == -1: continue diff --git a/console/application/methods_func.py b/console/application/methods_func.py index 2c8f01e..0fbe1d5 100644 --- a/console/application/methods_func.py +++ b/console/application/methods_func.py @@ -15,12 +15,13 @@ # limitations under the License. import argparse, sys -from function import listToArray, listToArrayArray, _create_obj, \ - get_view_params, print_brief +from function import _create_obj, get_view_params, print_brief from calculate.lib.cl_lang import setLocalTranslate setLocalTranslate('calculate_console',sys.modules[__name__]) import urllib2 -from cert_func import get_password + +from calculate.core.server.methods_func import get_method_argparser, \ + collect_object def parse(): parser = argparse.ArgumentParser(add_help=False) @@ -28,8 +29,7 @@ def parse(): '-h', '--help', action='store_true', default=False, dest='help', help=_("show this help message and exit")) parser.add_argument( - '--method', type=str, dest='method', - help=_('call method')) + '--method', type=str, dest='method', help=_('call method')) parser.add_argument( '-l', '--lang', type=str, dest='lang', help=_('language for translate')) @@ -89,70 +89,13 @@ def get_view(client, method, sid, view_params): raise Exception(1) return view -def get_method_argparser(client, args): +def call_method(client, args, wait_thread): method = args.method view_params = get_view_params(client, method + '_view', step = None, \ expert = True) view = get_view(client, method, client.sid, view_params) - progr = 'cl-console --method '+method - parser = argparse.ArgumentParser(prog=progr, add_help=False) - - for Group in view.groups.GroupField: - if not Group.fields: - continue -# if Group.name: - group = parser.add_argument_group(Group.name) - 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: - group.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: - group.add_argument(opt.shortopt, - type = arg_type, dest=field.name, metavar = \ - metavalue, action = action, help = opt.help) - elif opt.longopt and not opt.shortopt: - group.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: - group.add_argument(opt.shortopt, opt.longopt, - action='store_true', dest=field.name, - default=None, help = opt.help) - elif opt.shortopt and not opt.longopt: - group.add_argument(opt.shortopt, - action='store_true', dest=field.name, - default=None, help = opt.help) - elif opt.longopt and not opt.shortopt: - group.add_argument(opt.longopt, - action='store_true', dest=field.name, - default=None, help = opt.help) - return parser, view - -def call_method(client, args, wait_thread): - method = args.method - method_parser, view = get_method_argparser(client, args) + method_parser = get_method_argparser(view, args) param_object = _create_obj(client, method) try: args, unknown_args = method_parser.parse_known_args() @@ -192,7 +135,7 @@ def call_method(client, args, wait_thread): [field.opt.shortopt, field.opt.longopt])) \ + '. ' - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' _print ('\r' + red + params_text + error.message) return None @@ -206,10 +149,10 @@ def call_method(client, args, wait_thread): while True: try: ask = raw_input('\n' + _('Run process? (yes/no): ')) - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' except KeyboardInterrupt: ask = 'no' - red = '\n'+'\033[91m * \033[0m' + red = '\n'+'\033[31m * \033[0m' if ask.lower() in ['n', 'no']: print red + _('Interrupted by user') return None @@ -232,192 +175,8 @@ def call_method(client, args, wait_thread): params_text += _('Error in parameter ') params_text += ', '.join(filter(None, [field.opt.shortopt, field.opt.longopt]))+'. ' - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' _print ('\r' + red + params_text + error.message) return None wait_thread.stop() 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, wait_thread): - steps = None - for Group in view.groups.GroupField: - if not Group.fields: - continue - for field in Group.fields.Field: - if field.uncompatible: - continue - 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) - if val in ['off', 'none']: - param_object[field.name] = listToArray(client, [None]) - else: - 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, - wait_thread) - - elif field.element == 'table' and field.type == 'steps': - steps = field - - return param_object, steps - -def collect_table(field, val_list, client, wait_thread): - if not val_list: - return None - val_table = map(lambda x: x.split(':'), val_list) - - obj_body = [] - key_list = [] - if hasattr (field.tablevalue.body, 'stringArray'): - for obj_row in field.tablevalue.body.stringArray: - if hasattr(obj_row, 'string'): - key_list.append(obj_row.string[0]) - obj_body.append(obj_row.string) - - obj_body = collect_obj_body(obj_body, field) - column = len(field.tablevalue.head.string) - - ChoiceValue = field.tablevalue.values.ChoiceValue - type_list = map(lambda x: x.typefield, ChoiceValue) - for i in range(len(val_table)): - if 'password' in type_list: - if len(val_table[i]) != 2 or val_table[i][1].lower() != '': - wait_thread.stop() - sys.stdout.write('\r') - sys.stdout.flush() - password=get_password(_('Password for %s: ')%val_table[i][0],\ - _('Repeat password for %s: ') %val_table[i][0]) - password = password if password else '' - temp_row = [] - empty_row_flag = True if len (val_table[i]) == 1 else False - for j in range(column): - # not adding if readonly - if j > (len(ChoiceValue) + 1): - continue - - choice_value = ChoiceValue[j] - typefield = choice_value.typefield - # if readonly, except first column - - if typefield == 'readonly' and j > 0: - continue - elif typefield in ['check', 'check_tristate']: - if len (val_table[i]) < j + 1: - temp_row.append('') - continue - 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]) - elif typefield in ['input', 'combo', 'comboEdit', 'openfile', \ - 'file', 'radio', 'text']: - if len (val_table[i]) < j + 1: - temp_row.append('') - else: - temp_row.append(val_table[i][j]) - elif typefield == 'password': - if 'password' in locals(): - temp_row.append(password) - del password - else: - 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]) - - if temp_row[0] in key_list: - ind = key_list.index(temp_row[0]) - - count_val = filter (None, temp_row) - if (len(count_val) == 1 and not empty_row_flag) or \ - (len(count_val) == 2 and temp_row[1].lower() == 'none'): - obj_body.pop(ind) - key_list.pop(ind) - else: - obj_body.pop(ind) - obj_body.insert(ind, temp_row) - else: - count_val = filter (None, temp_row) - if (len(count_val) == 1 and not empty_row_flag) or \ - (len(count_val) == 2 and temp_row[1].lower() == 'none'): - continue - obj_body.append(temp_row) - key_list.append(temp_row[0]) - if not obj_body: - obj_body = [[None]] - return listToArrayArray(client, obj_body) - -def collect_obj_body(body, field): - column = len(field.tablevalue.head.string) - ChoiceValue = field.tablevalue.values.ChoiceValue - result_table = [] - for i in range(len(body)): - 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 (body[i]) < j + 1: - temp_row.append('') - continue - if not body[i][j]: - temp_row.append('') - elif body[i][j].lower() in ['on', 'yes']: - temp_row.append('on') - elif body[i][j].lower() in ['off', 'no']: - temp_row.append('off') - else: - temp_row.append(body[i][j]) - - elif typefield in ['input', 'combo', 'comboEdit', 'openfile', \ - 'file', 'password', 'radio', 'text']: - if len (body[i]) < j + 1: - temp_row.append('') - elif not body[i][j]: - temp_row.append('') - else: - temp_row.append(body[i][j]) - elif typefield in ['multichoice', 'multichoice_add']: - if len (body[i]) < j + 1: - temp_row.append('') - elif not body[i][j]: - temp_row.append('') - else: - temp_row.append(body[i][j]) - - result_table.append(temp_row) - return result_table \ No newline at end of file diff --git a/console/application/pid_information.py b/console/application/pid_information.py index 7002b2b..88232cf 100644 --- a/console/application/pid_information.py +++ b/console/application/pid_information.py @@ -47,7 +47,7 @@ def client_list_pid(client): """ get all process id for this session """ sid = get_sid(client.SID_FILE) - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' green = '\033[32m * \033[0m' try: list_pid = client.service.list_pid(sid = sid) diff --git a/console/application/sid_func.py b/console/application/sid_func.py index 00f9096..9b17cb2 100644 --- a/console/application/sid_func.py +++ b/console/application/sid_func.py @@ -61,7 +61,7 @@ def client_del_sid(client): return 0 def sid_inf(client, sid): - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' green = '\033[32m * \033[0m' """ get information about selected session """ s = client.service.sid_info(sid) @@ -93,7 +93,7 @@ def client_session_info(client, sid = None): return 1 def client_session_list(client): - red = '\033[91m * \033[0m' + red = '\033[31m * \033[0m' green = '\033[32m * \033[0m' res = client.service.get_sessions(client.sid) if hasattr (res, 'string'):