# remove all comments removeAllComments(dialogTree) # find all node names names = findAllNodeNames(dialogTree) parent_map = dict((c, p) for p in dialogTree.getiterator() for c in p) generateNodes(root, None, DEFAULT_ABORT, DEFAULT_AGAIN, DEFAULT_BACK, DEFAULT_REPEAT, DEFAULT_GENERIC) if VERBOSE: eprintf('\n') # create dialog structure for JSON dialogNodes = [] # convert XML tree to JSON structure printNodes(root, None, dialogNodes) if hasattr(config, 'common_outputs_directory') and hasattr(config, 'common_outputs_dialogs'): if not os.path.exists(getattr(config, 'common_outputs_directory')): os.makedirs(getattr(config, 'common_outputs_directory')) print('Created new output directory ' + getattr(config, 'common_outputs_directory')) with open(os.path.join(getattr(config, 'common_outputs_directory'), getattr(config, 'common_outputs_dialogs')), 'w') as outputFile: outputFile.write(json.dumps(dialogNodes, indent=4)) printf("File %s created\n", os.path.join(getattr(config, 'common_outputs_directory'), getattr(config, 'common_outputs_dialogs'))) else: print json.dumps(dialogNodes, indent=4) if hasattr(config, 'common_output_config'): config.saveConfiguration(getattr(config, 'common_output_config')) printf('\nFINISHING: ' + os.path.basename(__file__) + '\n')
def main(argv): parser = argparse.ArgumentParser( description= 'Converts dialog nodes from .xml format to Bluemix conversation service workspace .json format', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument( '-dm', '--common_dialog_main', required=False, help='main dialog file with dialogue nodes in xml format') parser.add_argument('-c', '--common_configFilePaths', help='configuaration file', action='append') parser.add_argument('-oc', '--common_output_config', help='output configuration file') parser.add_argument('-s', '--common_schema', required=False, help='schema file') parser.add_argument('-sc', '--common_scope', required=False, help='scope of dialog, e.g. type-local') parser.add_argument( '-of', '--common_outputs_directory', required=False, help='directory where the outputs will be stored (outputs is default)') parser.add_argument( '-od', '--common_outputs_dialogs', required=False, help='name of generated file (dialog.json is the default)') parser.add_argument('-v', '--verbose', required=False, help='verbosity', action='store_true') parser.add_argument('--log', type=str.upper, default=None, choices=list(logging._levelToName.values())) args = parser.parse_args(argv) global config if __name__ == '__main__': setLoggerConfig(args.log, args.verbose) config = Cfg(args) logger.info('STARTING: ' + os.path.basename(__file__)) # XML namespaces global XSI_NAMESPACE global XSI global NSMAP XSI_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance" XSI = "{%s}" % XSI_NAMESPACE NSMAP = {"xsi": XSI_NAMESPACE} # load dialogue from XML if hasattr(config, 'common_dialog_main'): #TODO might need UTF-8 dialogTree = LET.parse(getattr(config, 'common_dialog_main')) else: dialogTree = LET.parse(sys.stdin) # load schema schemaParam = getOptionalParameter(config, 'common_schema') if schemaParam: schemaDirname = os.path.split(os.path.abspath(__file__))[0] schemaFile = os.path.join(schemaDirname, schemaParam) if not os.path.exists(schemaFile): logger.error('Schema file %s not found.', schemaFile) exit(1) #TODO might need UTF-8 schemaTree = LET.parse(schemaFile) global schema schema = LET.XMLSchema(schemaTree) validate(dialogTree) # process dialog tree root = dialogTree.getroot() global rootGlobal rootGlobal = root importNodes(root, config) # remove all comments removeAllComments(dialogTree) # remove nodes which are out of specified scope removeOutOfScopeNodes(dialogTree) # find all node names global names names = findAllNodeNames(dialogTree) parent_map = dict((c, p) for p in dialogTree.getiterator() for c in p) generateNodes(root, None, DEFAULT_ABORT, DEFAULT_AGAIN, DEFAULT_BACK, DEFAULT_REPEAT, DEFAULT_GENERIC) # create dialog structure for JSON dialogNodes = [] # convert XML tree to JSON structure printNodes(root, None, dialogNodes) if hasattr(config, 'common_outputs_directory') and hasattr( config, 'common_outputs_dialogs'): if not os.path.exists(getattr(config, 'common_outputs_directory')): os.makedirs(getattr(config, 'common_outputs_directory')) logger.info("Created new output directory %s", getattr(config, 'common_outputs_directory')) with io.open(os.path.join(getattr(config, 'common_outputs_directory'), getattr(config, 'common_outputs_dialogs')), 'w', encoding='utf-8') as outputFile: outputFile.write( json.dumps(dialogNodes, indent=4, ensure_ascii=False)) logger.info( "File %s created", os.path.join(getattr(config, 'common_outputs_directory'), getattr(config, 'common_outputs_dialogs'))) else: print(json.dumps(dialogNodes, indent=4, ensure_ascii=False)) if hasattr(config, 'common_output_config'): config.saveConfiguration(getattr(config, 'common_output_config')) logger.info('FINISHING: ' + os.path.basename(__file__))
def main(argv): logger.info('STARTING: ' + os.path.basename(__file__)) parser = argparse.ArgumentParser( description= 'Deletes Bluemix conversation service workspace and deletes workspace id from config file.', formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-c', '--common_configFilePaths', help='configuaration file', action='append') parser.add_argument('-oc', '--common_output_config', help='output configuration file') parser.add_argument('-cu', '--conversation_url', required=False, help='url of the conversation service API') parser.add_argument('-cv', '--conversation_version', required=False, help='version of the conversation service API') parser.add_argument('-cn', '--conversation_username', required=False, help='username of the conversation service instance') parser.add_argument('-cp', '--conversation_password', required=False, help='password of the conversation service instance') parser.add_argument('-cid', '--conversation_workspace_id', required=False, help='workspace_id of the application.') parser.add_argument('-wn', '--conversation_workspace_name', required=False, help='name of the workspace') parser.add_argument( '-wnm', '--conversation_workspace_match_by_name', required=False, help= 'true if the workspace name should be matched by name (or pattern if defined)' ) parser.add_argument( '-wnp', '--conversation_workspace_name_pattern', required=False, help='regex pattern specifying a name of workspaces to be deleted') parser.add_argument('-v', '--verbose', required=False, help='verbosity', action='store_true') parser.add_argument('--log', type=str.upper, default=None, choices=list(logging._levelToName.values())) args = parser.parse_args(argv) if __name__ == '__main__': setLoggerConfig(args.log, args.verbose) config = Cfg(args) # load credentials version = getRequiredParameter(config, 'conversation_version') workspacesUrl = getRequiredParameter(config, 'conversation_url') username = getRequiredParameter(config, 'conversation_username') password = getRequiredParameter(config, 'conversation_password') try: workspaces = filterWorkspaces( config, getWorkspaces(workspacesUrl, version, username, password)) except SystemExit as e: logger.error("Failed to retrieve workspaces to delete.") sys.exit(1) nWorkspacesDeleted = 0 for workspace in workspaces: # delete workspace requestUrl = workspacesUrl + '/' + workspace[ 'workspace_id'] + '?version=' + version response = requests.delete(requestUrl, auth=(username, password), headers={'Accept': 'text/html'}) responseJson = response.json() # check errors during upload errorsInResponse(responseJson) if response.status_code == 200: nWorkspacesDeleted += 1 logger.info("Workspace '%s' was successfully deleted", workspace['name']) # delete workspaceId from config file if hasattr(config, 'conversation_workspace_id'): delattr(config, 'conversation_workspace_id') elif response.status_code == 400: logger.error( "Error while deleting workspace '%s', status code '%s' (invalid request)", workspace['name'], response.status_code) sys.exit(1) else: logger.error( "Error while deleting workspace '%s', status code '%s'", workspace['name'], response.status_code) sys.exit(1) if not nWorkspacesDeleted: logger.info("No workspace has been deleted") elif nWorkspacesDeleted == 1: logger.info("One workspace has been successfully deleted") else: logger.info( str(nWorkspacesDeleted) + " workspaces have been successfully deleted") outputConfigFile = getOptionalParameter(config, 'common_output_config') if outputConfigFile: config.saveConfiguration(outputConfigFile) logger.info("Configuration was saved to %s", outputConfigFile)
def main(argv): parser = argparse.ArgumentParser(description="Deploys a workspace in json format\ to the Watson Conversation Service. If there is no 'conversation_workspace_id' provided\ and the 'conversation_workspace_name_unique' is set to 'true', it uploads\ a workspace to the place specified by the 'conversation_workspace_name'" ,\ formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-of', '--common_outputs_directory', required=False, help='directory where the otputs are stored') parser.add_argument('-ow', '--common_outputs_workspace', required=False, help='name of the json file with workspace') parser.add_argument('-c', '--common_configFilePaths', help='configuaration file', action='append') parser.add_argument('-oc', '--common_output_config', help='output configuration file') parser.add_argument('-cu', '--conversation_url', required=False, help='url of the conversation service API') parser.add_argument('-cv', '--conversation_version', required=False, help='version of the conversation service API') parser.add_argument('-cn', '--conversation_username', required=False, help='username of the conversation service instance') parser.add_argument('-cp', '--conversation_password', required=False, help='password of the conversation service instance') parser.add_argument( '-cid', '--conversation_workspace_id', required=False, help= 'workspace_id of the application. If a workspace id is provided, previous workspace content is overwritten, otherwise a new workspace is created ' ) parser.add_argument('-wn', '--conversation_workspace_name', required=False, help='name of the workspace') parser.add_argument( '-wnu', '--conversation_workspace_name_unique', required=False, help= 'true if the workspace name should be unique across apecified assistant' ) parser.add_argument('-v', '--verbose', required=False, help='verbosity', action='store_true') parser.add_argument('--log', type=str.upper, default=None, choices=list(logging._levelToName.values())) args = parser.parse_args(argv) if __name__ == '__main__': setLoggerConfig(args.log, args.verbose) config = Cfg(args) logger.info('STARTING: ' + os.path.basename(__file__)) # workspace info try: workspaceFilePath = os.path.join( getRequiredParameter(config, 'common_outputs_directory'), getRequiredParameter(config, 'common_outputs_workspace')) with openFile(workspaceFilePath, 'r') as workspaceFile: workspace = json.load(workspaceFile) except IOError: logger.error('Cannot load workspace file %s', workspaceFilePath) sys.exit(1) # workspace name workspaceName = getOptionalParameter(config, 'conversation_workspace_name') if workspaceName: workspace['name'] = workspaceName # workspace language workspaceLanguage = getOptionalParameter(config, 'conversation_language') if workspaceLanguage: workspace['language'] = workspaceLanguage # credentials (required) username = getRequiredParameter(config, 'conversation_username') password = getRequiredParameter(config, 'conversation_password') # url (required) workspacesUrl = getRequiredParameter(config, 'conversation_url') # version (required) version = getRequiredParameter(config, 'conversation_version') # workspace id workspaces = filterWorkspaces( config, getWorkspaces(workspacesUrl, version, username, password)) if len(workspaces) > 1: # if there is more than one workspace with the same name -> error logger.error( 'There are more than one workspace with this name, do not know which one to update.' ) exit(1) elif len(workspaces) == 1: workspaceId = workspaces[0]['workspace_id'] logger.info("Updating existing workspace.") else: workspaceId = "" logger.info("Creating new workspace.") requestUrl = workspacesUrl + '/' + workspaceId + '?version=' + version # create/update workspace response = requests.post(requestUrl, auth=(username, password), headers={'Content-Type': 'application/json'}, data=json.dumps(workspace, indent=4)) responseJson = response.json() logger.verbose("response: %s", responseJson) if not errorsInResponse(responseJson): logger.info('Workspace successfully uploaded.') else: logger.error('Cannot upload workspace.') sys.exit(1) if not getOptionalParameter(config, 'conversation_workspace_id'): setattr(config, 'conversation_workspace_id', responseJson['workspace_id']) logger.info('WCS WORKSPACE_ID: %s', responseJson['workspace_id']) outputConfigFile = getOptionalParameter(config, 'common_output_config') if outputConfigFile: config.saveConfiguration(outputConfigFile) clientName = getOptionalParameter(config, 'context_client_name') if clientName: # Assembling uri of the client clientv2URL = 'https://clientv2-latest.mybluemix.net/#defaultMinMode=true' clientv2URL += '&prefered_workspace_id=' + getattr( config, 'conversation_workspace_id') clientv2URL += '&prefered_workspace_name=' + getattr( config, 'conversation_workspace_name') clientv2URL += '&shared_examples_service=&url=http://zito.mybluemix.net' clientv2URL += '&username='******'conversation_username') clientv2URL += '&custom_ui.title=' + getattr( config, 'conversation_workspace_name') clientv2URL += '&password='******'conversation_password') clientv2URL += '&custom_ui.machine_img=' clientv2URL += '&custom_ui.user_img=' clientv2URL += '&context.user_name=' + getattr(config, 'context_client_name') clientv2URL += '&context.link_build_date=' + unicode( datetime.datetime.now().strftime("%y-%m-%d-%H-%M")) clientv2URL += '&prefered_tts=none' clientv2URL += '&bluemix_tts.username=xx' clientv2URL += '&bluemix_tts.password=xx' clientv2URL += '&compact_mode=true' clientv2URL += '&compact_switch_enabled=true' clientv2URL += 'developer_switch_enabled=false' logger.info('clientv2URL=%s', clientv2URL) # create file with automatic redirect clientFileName = getOptionalParameter(config, 'common_outputs_client') if clientFileName: clientFilePath = os.path.join( getRequiredParameter(config, 'common_outputs_directory'), clientFileName) try: with openFile(clientFilePath, "w") as clientFile: clientFile.write( '<meta http-equiv="refresh" content=\"0; url=' + clientv2URL + '\" />') clientFile.write('<p><a href=\"' + clientv2URL + '\">Redirect</a></p>') clientFile.close() except IOError: logger.error('Cannot write to %s', clientFilePath) sys.exit(1) logger.info('FINISHING: ' + os.path.basename(__file__))