<commit_before>#!/usr/bin/env python
"""
 SlipStream Client
 =====
 Copyright (C) 2014 SixSq Sarl (sixsq.com)
 =====
 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.
"""
from __future__ import print_function

import os
import sys
import binascii

from slipstream.CommandBase import CommandBase
from slipstream.Client import Client
import slipstream.util as util
import slipstream.commands.NodeInstanceRuntimeParameter as NodeInstanceRuntimeParameter
from slipstream.ConfigHolder import ConfigHolder

class MainProgram(CommandBase):
    '''A command-line program to generate random string (e.g. for password) and optionally set it in a runtime parameter.'''

    def __init__(self, argv=None):
        self.key = None
        self.size = None
        self.username = None
        self.password = <PASSWORD>
        self.cookie = None
        self.endpoint = None
        super(MainProgram, self).__init__(argv)

    def parse(self):
        usage = '''usage: %prog [options] [<key>]

<key>   Key for which to set the random value. If not provided, the random value is only returned.'''

        self.parser.usage = usage
        self.parser.add_option('-s','--size',
                               dest='size',
                               metavar='NUMBER',
                               help='Number of characters for the random string (default: 12)',
                               default=12)
        
        self.options, self.args = self.parser.parse_args()
        
        self._check_args()

    def _check_args(self):
        if len(self.args) > 1:
            self.usageExitTooManyArguments()
        if len(self.args) == 1:
            self.key = self.args[0]
        try:
            self.size = int(self.options.size)
        except ValueError:
            self.usageExit("Invalid size value, must be an integer")

    def doWork(self):
        
        rvalue = binascii.b2a_hex(os.urandom(self.size))

        if self.key is not None:
            configHolder = ConfigHolder(self.options)
            client = Client(configHolder)
            client.setRuntimeParameter(self.key, rvalue)
        
        print(rvalue)

if __name__ == "__main__":
    try:
        MainProgram()
    except KeyboardInterrupt:
        print('\n\nExecution interrupted by the user... goodbye!')
        sys.exit(-1)<commit_msg>change implementation to allow wider set of characters; correct size of result (fix #62)<commit_after>#!/usr/bin/env python
"""
 SlipStream Client
 =====
 Copyright (C) 2014 SixSq Sarl (sixsq.com)
 =====
 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.
"""
from __future__ import print_function

import sys
import random
import string

from slipstream.CommandBase import CommandBase
from slipstream.Client import Client
import slipstream.util as util
import slipstream.commands.NodeInstanceRuntimeParameter as NodeInstanceRuntimeParameter
from slipstream.ConfigHolder import ConfigHolder

class MainProgram(CommandBase):
    '''A command-line program to generate random string (e.g. for password) and optionally set it in a runtime parameter.'''

    def __init__(self, argv=None):
        self.key = None
        self.size = None
        self.username = None
        self.password = <PASSWORD>
        self.cookie = None
        self.endpoint = None
        super(MainProgram, self).__init__(argv)

    def parse(self):
        usage = '''usage: %prog [options] [<key>]

<key>   Key for which to set the random value. If not provided, the random value is only returned.'''

        self.parser.usage = usage
        self.parser.add_option('-s','--size',
                               dest='size',
                               metavar='NUMBER',
                               help='Number of characters for the random string (default: 12)',
                               default=12)
        
        self.options, self.args = self.parser.parse_args()
        
        self._check_args()

    def _check_args(self):
        if len(self.args) > 1:
            self.usageExitTooManyArguments()
        if len(self.args) == 1:
            self.key = self.args[0]
        try:
            self.size = int(self.options.size)
            if self.size < 1:
                self.usageExit('Size value must be a positive integer')
        except ValueError:
            self.usageExit("Invalid size value, must be a positive integer")

    def doWork(self):
        
        rvalue = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(self.size)])

        if self.key is not None:
            configHolder = ConfigHolder(self.options)
            client = Client(configHolder)
            client.setRuntimeParameter(self.key, rvalue)
        
        print(rvalue)

if __name__ == "__main__":
    try:
        MainProgram()
    except KeyboardInterrupt:
        print('\n\nExecution interrupted by the user... goodbye!')
        sys.exit(-1)
{'fraction_non_alphanumeric': 0.08929486085026464, 'fraction_numerical': 0.006146491377838484, 'mean_word_length': 2.4257309941520466, 'pattern_counts': {'":': 2, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3812684', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1509, 'n_words': 532}