def UserBuild(fCallback):
    'Parse args and try to build a jasmin.routing.jasminApi.User instance to pass it to fCallback'
    def parse_args_and_call_with_instance(self, *args, **kwargs):
        cmd = args[0]
        arg = args[1]

        # Empty line
        if cmd is None:
            return self.protocol.sendData()
        # Initiate jasmin.routing.jasminApi.User with sessBuffer content
        if cmd == 'ok':
            if ('uid' not in self.sessBuffer or
                    'group' not in self.sessBuffer or
                    'username' not in self.sessBuffer or
                    'password' not in self.sessBuffer):
                return self.protocol.sendData(
                    'You must set User id (uid), group (gid), username and password before saving !')

            # Set defaults when not defined
            if 'mt_credential' not in self.sessBuffer:
                self.sessBuffer[UserKeyMap['mt_messaging_cred']['keyMapValue']] = globals()[
                    UserKeyMap['mt_messaging_cred']['class']]()
            if 'smpps_credential' not in self.sessBuffer:
                self.sessBuffer[UserKeyMap['smpps_cred']['keyMapValue']] = globals()[
                    UserKeyMap['smpps_cred']['class']]()

            user = {}
            for key, value in self.sessBuffer.iteritems():
                user[key] = value
            try:
                UserInstance = User(**user)
                # Hand the instance to fCallback
                return fCallback(self, UserInstance)
            except Exception, e:
                return self.protocol.sendData('Error: %s' % str(e))
        else:
            # Unknown key
            if cmd not in UserKeyMap:
                return self.protocol.sendData('Unknown User key: %s' % cmd)

            if isinstance(UserKeyMap[cmd], dict):
                # Provisioning a sub-User instance (MtMessagingCredential ...)
                subKeyMap = UserKeyMap[cmd]

                # Syntax validation
                _r = re.match(r'^(\S+) (\S+) (\S+.*$)', arg)
                if not _r:
                    return self.protocol.sendData('Error: expected syntax: %s section key value' % cmd)

                section = _r.group(1).lower()
                key = _r.group(2).lower()
                value = _r.group(3)

                # Validate section
                possible_values = subKeyMap.keys()
                possible_values.remove('class')
                possible_values.remove('keyMapValue')
                valid_section = False
                for pv in possible_values:
                    if section == pv.lower():
                        section = pv
                        valid_section = True
                        break
                if not valid_section:
                    return self.protocol.sendData('Error: invalid section name: %s, possible values: %s' % (
                        section, ', '.join(possible_values)))

                # Validate key
                if key not in subKeyMap[section].keys():
                    return self.protocol.sendData('Error: invalid key: %s, possible keys: %s' % (
                        key, ', '.join(subKeyMap[section].keys())))
                SectionKey = subKeyMap[section][key]

                try:
                    # Input value are received in string type, castToBuiltCorrectCredType will fix the
                    # type depending on class, section and SectionKey
                    SectionValue = castToBuiltCorrectCredType(subKeyMap['class'], section, SectionKey, value)

                    # Instanciate a new sub-User object
                    if subKeyMap['keyMapValue'] not in self.sessBuffer:
                        self.sessBuffer[subKeyMap['keyMapValue']] = globals()[subKeyMap['class']]()

                    # Set sub-User object value
                    getattr(self.sessBuffer[subKeyMap['keyMapValue']], 'set%s' % section)(
                        SectionKey, SectionValue)
                except (jasminApiCredentialError, ValueError) as e:
                    return self.protocol.sendData('Error: %s' % str(e))
            else:
                # Provisioning User instance
                # IF we got the gid, instanciate a Group if gid exists or return an error
                if cmd == 'gid':
                    group = self.pb['router'].getGroup(arg)
                    if group is None:
                        return self.protocol.sendData(
                            'Unknown Group gid:%s, you must first create the Group' % arg)

                    self.sessBuffer['group'] = group
                else:
                    # Buffer key for later User initiating
                    UserKey = UserKeyMap[cmd]
                    if UserKey not in UserConfigStringKeys:
                        self.sessBuffer[UserKey] = str2num(arg)
                    else:
                        self.sessBuffer[UserKey] = arg

            return self.protocol.sendData()
    return parse_args_and_call_with_instance
dataset/ETHPy150Open jookies/jasmin/jasmin/protocols/cli/usersm.py/UserBuild/original