Esempio n. 1
0
    def add_all_data_to_delta_list(
        self,delta_to_add_to,current_internal_val,action_event):
        '''
        Run through entire map.  Create an add action for each element.
        '''
        for key in current_internal_val.keys():
            map_action = delta_to_add_to.map_actions.add()

            map_action.container_action = VarStoreDeltas.ContainerAction.ADD_KEY
            
            add_action = map_action.added_key
            add_action.parent_type = VarStoreDeltas.CONTAINER_ADDED

            if isinstance(key,numbers.Number):
                add_action.added_key_num = key
            elif util.is_string(key):
                add_action.added_key_text = key
            else:
                add_action.added_key_tf = key
            
            
            # now actually add the value to the map
            map_val = current_internal_val[key]

            if isinstance(map_val,numbers.Number):
                add_action.added_what_num = map_val
            elif util.is_string(map_val):
                add_action.added_what_text = map_val
            elif isinstance(map_val,bool):
                add_action.added_what_tf = map_val

            elif isinstance(map_val,WaldoObj):
                map_val.serializable_var_tuple_for_network(
                    add_action,'',action_event,True)
Esempio n. 2
0
def get_sr_alloc(filename):
    """Return the SR allocation type

        Check for the existence of 'filename' in <config_dir>. If the
        file exists, the SR is 'xlvhd'. If not, it is 'thick'. The
        filename can be either 'VG_XenStorage-<UUID>' or the device's
        SCSI id.

        Input:
            filename -- (str) Name of file to look for

        Return:
            sr_alloc -- (str) SR allocation type

        Raise:
            TypeError 
    """

    if not util.is_string(filename):
        raise TypeError("'filename' is not of type 'str'.")

    global config_dir
    path = config_dir + filename

    if os.path.isfile(path):
        sr_alloc = 'xlvhd'
    else:
        sr_alloc = 'thick'

    return sr_alloc
Esempio n. 3
0
    def mount(self, mountpoint=None):
        """Mount the remote CIFS export at 'mountpoint'"""
        if mountpoint == None:
            mountpoint = self.mountpoint
        elif not util.is_string(mountpoint) or mountpoint == "":
            raise CifsException("mountpoint not a string object")

        missing_params = set()

        if not self.dconf.has_key('username'):
            missing_params.add('username')

        if not (self.dconf.has_key('password') or
                self.dconf.has_key('password_secret')):
            missing_params.add('password')

        if missing_params:
            errstr = 'device-config is missing the following parameters: ' + \
                     ', '.join([param for param in missing_params])
            raise xs_errors.XenError('ConfigParamsMissing', opterr=errstr)

        try:
            if not util.ioretry(lambda: util.isdir(mountpoint)):
                util.ioretry(lambda: util.makedirs(mountpoint))
        except util.CommandException, inst:
            raise CifsException("Failed to make directory: code is %d" %
                                inst.code)
Esempio n. 4
0
def get_sr_alloc(filename):
    """Return the SR allocation type

        Check for the existence of 'filename' in <config_dir>. If the
        file exists, the SR is 'xlvhd'. If not, it is 'thick'. The
        filename can be either 'VG_XenStorage-<UUID>' or the device's
        SCSI id.

        Input:
            filename -- (str) Name of file to look for

        Return:
            sr_alloc -- (str) SR allocation type

        Raise:
            TypeError 
    """

    if not util.is_string(filename):
        raise TypeError("'filename' is not of type 'str'.")

    global config_dir
    path = config_dir + filename

    if os.path.isfile(path):
        sr_alloc = 'xlvhd'
    else:
        sr_alloc = 'thick'

    return sr_alloc
Esempio n. 5
0
def send_audio(token, chat_id, audio, caption=None, duration=None, performer=None, title=None, reply_to_message_id=None,
               reply_markup=None, disable_notification=None, timeout=None):
    method_url = r'sendAudio'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(audio):
        files = {'audio': audio}
    else:
        payload['audio'] = audio
    if caption:
        payload['caption'] = caption
    if duration:
        payload['duration'] = duration
    if performer:
        payload['performer'] = performer
    if title:
        payload['title'] = title
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    if timeout:
        payload['connect-timeout'] = timeout
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 6
0
File: lvutil.py Progetto: xcp-ng/sm
def extract_vgname(str_in):
    """Search for and return a VG name

        Search 'str_in' for a substring in the form of 'VG_XenStorage-<UUID>'. 
        If there are more than one VG names, the first is returned.

        Input:
            str_in -- (str) string to search for a VG name
                            in the format specified above.

        Return:
            vgname -- if found     -> (str)
                      if not found -> None

        Raise:
            TypeError
    """

    if not util.is_string(str_in):
        raise TypeError("'str_in' not of type 'str'.")

    i = str_in.find(VG_PREFIX)
    prefix = VG_PREFIX

    if i == -1:
        i = str_in.find(EXT_PREFIX)
        prefix = EXT_PREFIX

    uuid_start = i + len(prefix)
    re_obj = util.match_uuid(str_in[uuid_start:])

    if i != -1 and re_obj:
        return prefix + re_obj.group(0)  # vgname

    return None
Esempio n. 7
0
def extract_vgname(str_in):
    """Search for and return a VG name

        Search 'str_in' for a substring in the form of 'VG_XenStorage-<UUID>'. 
        If there are more than one VG names, the first is returned.

        Input:
            str_in -- (str) string to search for a VG name
                            in the format specified above.

        Return:
            vgname -- if found     -> (str)
                      if not found -> None

        Raise:
            TypeError
    """

    if not util.is_string(str_in):
        raise TypeError("'str_in' not of type 'str'.")

    i = str_in.find(VG_PREFIX)
    prefix = VG_PREFIX

    if i == -1:
        i = str_in.find(EXT_PREFIX)
        prefix = EXT_PREFIX

    uuid_start = i + len(prefix)
    re_obj = util.match_uuid(str_in[uuid_start:])

    if i != -1 and re_obj:
        return prefix + re_obj.group(0) # vgname

    return None
Esempio n. 8
0
def convert_uuids(obj):
    id = obj.get("uuid", None)
    if util.is_string(id):
        id = uuid.UUID(id)
    if id:
        obj["uuid"] = id.bytes
    return id
Esempio n. 9
0
def set_chat_photo(token, chat_id, photo):
    method_url = 'setChatPhoto'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(photo):
        files = {'photo': photo}
    else:
        payload['photo'] = photo
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 10
0
File: lvutil.py Progetto: stormi/sm
def cmd_lvm(cmd, pread_func=util.pread2, *args):
    """ Construct and run the appropriate lvm command.

        For PV commands, the full path to the device is required.

        Input:
            cmd -- (list) lvm command
                cmd[0]  -- (str) lvm command name
                cmd[1:] -- (str) lvm command parameters

            pread_func -- (function) the flavor of util.pread to use
                                     to execute the lvm command
                Default: util.pread2()

            *args -- extra arguments passed to cmd_lvm will be passed
                     to 'pread_func'

        Return:
            stdout -- (str) stdout after running the lvm command.

        Raise:
            util.CommandException
    """

    if type(cmd) is not list:
        util.SMlog("CMD_LVM: Argument 'cmd' not of type 'list'")
        return None
    if not len(cmd):
        util.SMlog("CMD_LVM: 'cmd' list is empty")
        return None

    lvm_cmd, lvm_args = cmd[0], cmd[1:]

    if lvm_cmd not in LVM_COMMANDS:
        util.SMlog("CMD_LVM: '{}' is not a valid lvm command".format(lvm_cmd))
        return None

    for arg in lvm_args:
        if not util.is_string(arg):
            util.SMlog("CMD_LVM: Not all lvm arguments are of type 'str'")
            return None

    lvm_lock = get_lvm_lock()

    try:
        start_time = time.time()
        stdout = pread_func([os.path.join(LVM_BIN, lvm_cmd)] + lvm_args, *args)
        end_time = time.time()
    finally:
        lvm_lock.release()

    if (end_time - start_time > MAX_OPERATION_DURATION):
        util.SMlog("***** Long LVM call of '%s' took %s" %
                   (lvm_cmd, (end_time - start_time)))

    return stdout
Esempio n. 11
0
 def __contains__(self, x):
     if util.is_map(x):
         return all((self.__contains__(v) for v in x.values()))
     if util.is_array(x):
         return all((self.__contains__(v) for v in x))
     if util.is_string(x):
         k = util.safe_decode(x)
     else:
         k = unicode(x)
     return k in self._elements
Esempio n. 12
0
    def set_metadata(self, path, *metadata):
        if len(metadata) > 1:
            metadata = dict([metadata])
        else: metadata = metadata[0]

        for v in metadata.itervalues():
            if not util.is_string(v):
                raise SmapException("set_metadata: values must be strings!")

        o = self.lookup(path)
        o.set_metadata(metadata)
Esempio n. 13
0
def add_sticker_to_set(token, user_id, name, png_sticker, emojis, mask_position):
    method_url = 'addStickerToSet'
    payload = {'user_id': user_id, 'name': name, 'emojis': emojis}
    files = None
    if not util.is_string(png_sticker):
        files = {'png_sticker': png_sticker}
    else:
        payload['png_sticker'] = png_sticker
    if mask_position:
        payload['mask_position'] = mask_position.to_json()
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 14
0
    def set_metadata(self, path, *metadata):
        if len(metadata) > 1:
            metadata = dict([metadata])
        else:
            metadata = metadata[0]

        for v in metadata.itervalues():
            if not util.is_string(v):
                raise SmapException("set_metadata: values must be strings!")

        o = self.lookup(path)
        o.set_metadata(metadata)
Esempio n. 15
0
 def __variable_apply( self ):
     
     var_rx = re.compile( r"<\|\s*(.+)\s*\|>" )
     
     varlist = {}
     ## first go through all key in config to add all string based basic variables.
     ## Variables depending on variables are still stricky though due to order of key loading.
     ## Should have made config into list to make sure it's all good at all times..
     for item in self.__config:
         if util.is_string( self.__config[ item ] ):
             if not var_rx.match( self.__config[ item ] ):
                 varlist[ item ] = self.__config[ item ]
                 
     for item in self.__config:
         if util.is_string( self.__config[ item ] ):
             m = var_rx.match( self.__config[ item ] )
             if m:
                 for v in m.groups( ):
                     vstr = r"<\|%s\|>" % ( v )
                     self.__config[ item ] = re.sub( vstr, varlist[ v ], self.__config[ item ] )
             varlist[ item ] = self.__config[ item ]
Esempio n. 16
0
    def mount(self, mountpoint=None):
        """Mount the remote gluster export at 'mountpoint'"""
        if mountpoint is None:
            mountpoint = self.mountpoint
        elif not util.is_string(mountpoint) or mountpoint == "":
            raise GlusterFSException("mountpoint not a string object")

        try:
            if not util.ioretry(lambda: util.isdir(mountpoint)):
                util.ioretry(lambda: util.makedirs(mountpoint))
        except util.CommandException, inst:
            raise GlusterFSException("Failed to make directory: code is %d" % inst.code)
Esempio n. 17
0
    def mount(self, mountpoint=None):
        """Mount the remote SMB export at 'mountpoint'"""
        if mountpoint == None:
            mountpoint = self.mountpoint
        elif not util.is_string(mountpoint) or mountpoint == "":
            raise SMBException("mountpoint not a string object")

        try:
            if not util.ioretry(lambda: util.isdir(mountpoint)):
                util.ioretry(lambda: util.makedirs(mountpoint))
        except util.CommandException, inst:
            raise SMBException("Failed to make directory: code is %d" %
                                inst.code)
Esempio n. 18
0
def create_new_sticker_set(token, user_id, name, title, png_sticker, emojis, contains_masks=None, mask_position=None):
    method_url = 'createNewStickerSet'
    payload = {'user_id': user_id, 'name': name, 'title': title, 'emojis': emojis}
    files = None
    if not util.is_string(png_sticker):
        files = {'png_sticker': png_sticker}
    else:
        payload['png_sticker'] = png_sticker
    if contains_masks:
        payload['contains_masks'] = contains_masks
    if mask_position:
        payload['mask_position'] = mask_position.to_json()
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 19
0
def generate_struct(fp,str,struct_name):
    head_str = "struct" + " " + struct_name + " {\n"
    fp.write(head_str)

    if type(str) != type([]):
        print "generate_struct str is not list"
        return

    for i in range(0,len(str)):
        element = str[i]
        key = element[0]
        value = element[1]

        #   组装字段类型
        if util.is_string(value) and is_inner_type(value):
            # 内部数据
            element_str = "    " + value + " " + key + ";\n"
        elif util.is_list(value) and len(value) == 1:
            # 如果为元组 则为vector
            element_str = "    " + "std::vector<" + value[0] + "> " + key + ";\n"
        elif util.is_list(value) and len(value) == 2:
            # 如果为元组 两个元素则为map
            element_str = "    " + "std::map<" + value[0] + "," + value[1] + "> " + key + ";\n"
        fp.write(element_str)
        #   组装ToPacket和FromPacket
    fp.write("    void ToPacket(INetPacket& packet)\n")
    fp.write("    {\n")

    for i in range(0,len(str)):
        element = str[i]
        key = element[0]
        value = element[1]
        parse_element_to_packet(fp, key, value)
    fp.write("    }\n")

    fp.write("    void FromPacket(INetPacket& packet)\n")
    fp.write("    {\n")
    fp.write("        int size;\n")

    for i in range(0, len(str)):
        element = str[i]
        key = element[0]
        value = element[1]
        parse_element_from_packet(fp,key,value)

    fp.write("    }\n")

    fp.write("}\n")

    fp.flush()
    fp.close()
Esempio n. 20
0
def cmd_lvm(cmd, pread_func=util.pread2, *args):
    """ Construct and run the appropriate lvm command.

        For PV commands, the full path to the device is required.

        Input:
            cmd -- (list) lvm command
                cmd[0]  -- (str) lvm command name
                cmd[1:] -- (str) lvm command parameters

            pread_func -- (function) the flavor of util.pread to use
                                     to execute the lvm command
                Default: util.pread2()

            *args -- extra arguments passed to cmd_lvm will be passed
                     to 'pread_func'

        Return:
            stdout -- (str) stdout after running the lvm command.

        Raise:
            util.CommandException
    """

    if type(cmd) is not list:
        util.SMlog("CMD_LVM: Argument 'cmd' not of type 'list'")
        return None
    if not len(cmd):
        util.SMlog("CMD_LVM: 'cmd' list is empty")
        return None

    lvm_cmd, lvm_args = cmd[0], cmd[1:]

    if lvm_cmd not in LVM_COMMANDS:
        util.SMlog("CMD_LVM: '{}' is not a valid lvm command".format(lvm_cmd))
        return None

    for arg in lvm_args:
        if not util.is_string(arg):
            util.SMlog("CMD_LVM: Not all lvm arguments are of type 'str'")
            return None

    start_time = time.time()
    stdout = pread_func([os.path.join(LVM_BIN, lvm_cmd)] + lvm_args, *args)
    end_time = time.time()

    if (end_time - start_time > MAX_OPERATION_DURATION):
        util.SMlog("***** Long LVM call of '%s' took %s" % (lvm_cmd, (end_time - start_time)))

    return stdout
Esempio n. 21
0
 def __delitem__(self, key):
     if util.is_map(key):
         return self.__delitem__(key.values())
     if util.is_array(key):
         for k in key:
             self.__delitem__(k)
         return
     if util.is_string(key):
         k = util.safe_decode(key)
     else:
         k = unicode(key)
     if k in self._elements:
         del self._elements[k]
     return
Esempio n. 22
0
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, disable_notification=None):
    method_url = get_method_by_type(data_type)
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(data):
        files = {data_type: data}
    else:
        payload[data_type] = data
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 23
0
 def __setitem__(self, key, value):
     if util.is_map(key):
         return self.__setitem__(key.values(), value)
     if util.is_array(key):
         for k in key:
             self.__setitem__(k, value)
         return
     if value is None:
         self.__delitem__(key)
         return
     if util.is_string(key):
         k = util.safe_decode(key)
     else:
         k = unicode(key)
     self._elements[k] = util.rec_decode(value)
     return
Esempio n. 24
0
def _dict_append(d, **kws):
    for k,v in kws.items():
        if k not in d:
            d[k] = v
            continue
        dv = d[k]
        if isinstance(dv, tuple):
            d[k] = dv + tuple(v)
        elif isinstance(dv, list):
            d[k] = dv + list(v)
        elif isinstance(dv, dict):
            _dict_append(dv, **v)
        elif util.is_string(dv):
            d[k] = dv + v
        else:
            raise TypeError, repr(type(dv))
Esempio n. 25
0
    def get_for_iter(self,to_iter_over,active_event):
        '''
        When call for loop on Waldo variables, need to get item to
        iterate over
        '''

        if (isinstance(to_iter_over,dict) or
            isinstance(to_iter_over,list) or
            util.is_string(to_iter_over)):
            return iter(to_iter_over)

        if isinstance(to_iter_over,wVariables.WaldoTextVariable):
            return iter(to_iter_over.get_val(active_event))

        if isinstance(to_iter_over,wVariables.WaldoMapVariable):
            return iter(to_iter_over.get_val(active_event).get_keys(active_event))

        if isinstance(to_iter_over,wVariables.WaldoListVariable):
            # FIXME: This is an inefficient way of reading all values
            # over list.
            to_return = []
            for i in range(0, to_iter_over.get_val(active_event).get_len(active_event)):
                to_append = to_iter_over.get_val(active_event).get_val_on_key(active_event,i)

                # The reason that we do this here is that in a for
                # loop, the operation we perform in the compiled code
                # immediately following the actual for header is
                # assign to another Waldo variable the variable being
                # held by to_append.  (We do this through a write_val
                # call.  write_val must take in InternalMaps or
                # InternalLists.  Therefore, get_val on the
                # WaldoList/WaldoMap first.  Note this is unnecessary
                # for all other for iterations because none of the
                # others possibly return a WaldoObject to iterate over.
                if (isinstance(to_append,wVariables.WaldoListVariable) or
                    isinstance(to_append,wVariables.WaldoMapVariable)):
                    to_append = to_append.get_val(active_event)
                
                to_return.append(to_append)


            return iter(to_return)
        
        util.emit_assert(
            'Calling get_for_iter on an object that does not support iteration')
Esempio n. 26
0
def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
               disable_notification=None):
    method_url = r'sendPhoto'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(photo):
        files = {'photo': photo}
    else:
        payload['photo'] = photo
    if caption:
        payload['caption'] = caption
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 27
0
def send_photo(token, chat_id, photo, caption=None, reply_to_message_id=None, reply_markup=None,
               disable_notification=None):
    method_url = r'sendPhoto'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(photo):
        files = {'photo': photo}
    else:
        payload['photo'] = photo
    if caption:
        payload['caption'] = caption
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 28
0
def send_voice(token, chat_id, voice, duration=None, reply_to_message_id=None, reply_markup=None,
               disable_notification=None):
    method_url = r'sendVoice'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(voice):
        files = {'voice': voice}
    else:
        payload['voice'] = voice
    if duration:
        payload['duration'] = duration
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 29
0
 def __getitem__(self, key):
     if util.is_map(key):
         return util.cons_map(
             ((k, self.__getitem__(v)) for k, v in key.items()),
             key.__class__
         )
     if isinstance(key, collections.Set):
         return {k: self.__getitem__(k) for k in util.rec_decode(key)}
     if util.is_array(key):
         return util.cons_array(
             (self.__getitem__(k) for k in key),
             key.__class__
         )
     if util.is_string(key):
         k = util.safe_decode(key)
     else:
         k = unicode(key)
     return self._get(k)
Esempio n. 30
0
def send_data(token, chat_id, data, data_type, reply_to_message_id=None, reply_markup=None, disable_notification=None,
              timeout=None, caption=None):
    method_url = get_method_by_type(data_type)
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(data):
        files = {data_type: data}
    else:
        payload[data_type] = data
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    if timeout:
        payload['connect-timeout'] = timeout
    if caption:
        payload['caption'] = caption
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 31
0
def send_video(token, chat_id, data, duration=None, caption=None, reply_to_message_id=None, reply_markup=None,
               disable_notification=None):
    method_url = r'sendVideo'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(data):
        files = {'video': data}
    else:
        payload['video'] = data
    if duration:
        payload['duration'] = duration
    if caption:
        payload['caption'] = caption
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 32
0
    def lookup(self, id, pred=None):
        """Retrieve an object in the resource hierarchy by path or uuid.  If
        *id* is a string not starting with ``/``, it will be passed to the
        :py:class:`uuid.UUID` constructor; otherwise it will be treated as a
        pathname.  *pred* is an optional predicate which can be used to test
        the result.
"""
        if util.is_string(id):
            path = util.split_path(id)
            if len(path) > 0 and path[-1][0] == "+":
                return self._lookup_r(util.join_path(path[:-1]), pred=pred)
            else:
                obj = self.OBJS_PATH.get(util.join_path(path), None)
        elif isinstance(id, uuid.UUID):
            return self.OBJS_UUID.get(id, None)
        else:
            obj = None
        
        if not pred or pred(obj):
            return obj
        else: return None
Esempio n. 33
0
    def lookup(self, id, pred=None):
        """Retrieve an object in the resource hierarchy by path or uuid.  If
        *id* is a string not starting with ``/``, it will be passed to the
        :py:class:`uuid.UUID` constructor; otherwise it will be treated as a
        pathname.  *pred* is an optional predicate which can be used to test
        the result.
"""
        if util.is_string(id):
            path = util.split_path(id)
            if len(path) > 0 and path[-1][0] == "+":
                return self._lookup_r(util.join_path(path[:-1]), pred=pred)
            else:
                obj = self.OBJS_PATH.get(util.join_path(path), None)
        elif isinstance(id, uuid.UUID):
            return self.OBJS_UUID.get(id, None)
        else:
            obj = None

        if not pred or pred(obj):
            return obj
        else:
            return None
Esempio n. 34
0
def send_voice(token, chat_id, voice, caption=None, duration=None, reply_to_message_id=None, reply_markup=None,
               disable_notification=None, timeout=None):
    method_url = r'sendVoice'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(voice):
        files = {'voice': voice}
    else:
        payload['voice'] = voice
    if caption:
        payload['caption'] = caption
    if duration:
        payload['duration'] = duration
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    if timeout:
        payload['connect-timeout'] = timeout
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 35
0
    def handle_len(self,what_calling_len_on, active_event):
        '''
        Can support python lists, dicts, strings, or waldo lists, waldo maps,
        waldo texts.
        
        @returns {int}
        '''
        if (isinstance(what_calling_len_on, dict) or
            isinstance(what_calling_len_on, list) or
            util.is_string(what_calling_len_on)):
            return len(what_calling_len_on)

        if wVariables.is_non_ext_text_var(what_calling_len_on):
            return len(what_calling_len_on.get_val(active_event))


        if (wVariables.is_non_ext_list_var(what_calling_len_on) or
            wVariables.is_non_ext_map_var(what_calling_len_on)):
            return what_calling_len_on.get_val(active_event).get_len(active_event)

        util.logger_assert(
            'Calling len on an object that does not support the function')
Esempio n. 36
0
def send_audio(token, chat_id, audio, duration=None, performer=None, title=None, reply_to_message_id=None,
               reply_markup=None, disable_notification=None):
    method_url = r'sendAudio'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(audio):
        files = {'audio': audio}
    else:
        payload['audio'] = audio
    if duration:
        payload['duration'] = duration
    if performer:
        payload['performer'] = performer
    if title:
        payload['title'] = title
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 37
0
    def handle_in_check(self,lhs,rhs,active_event):
        '''
        Call has form:
            lhs in rhs
        
        rhs can have three basic types: it can be a list, a map, or a
        string.  That means that it can either be a WaldoMapVariable,
        a WaldoListVariable, a WaldoStringVariable, or a Python
        string.

        Instead of using static type inference at compile time to
        determine, for sake of development, just doing dynamic check
        to determine which type it is and do the in processing here.

        FIXME: it is faster to do the static checks with type
        inference, etc. at compile time rather than at run time.
        '''
        lhs_val = self.get_val_if_waldo(lhs,active_event)

        # handles Python string case
        if util.is_string(rhs):
        # if isinstance(rhs,basestring):
            return lhs_val in rhs

        elif isinstance(rhs,wVariables.WaldoTextVariable):
            return lhs_val in rhs.get_val(active_event)

        elif isinstance(rhs,wVariables.WaldoMapVariable):
            return rhs.get_val(active_event).contains_key_called(
                active_event,lhs_val)

        elif isinstance(rhs,wVariables.WaldoListVariable):
            return rhs.get_val(active_event).contains_val_called(
                active_event,lhs_val)

        util.logger_assert(
            'Error when calling in: unknown right hand side of expression')
Esempio n. 38
0
def send_video_note(token, chat_id, data, duration=None, length=None, reply_to_message_id=None, reply_markup=None,
                    disable_notification=None, timeout=None):
    method_url = r'sendVideoNote'
    payload = {'chat_id': chat_id}
    files = None
    if not util.is_string(data):
        files = {'video_note': data}
    else:
        payload['video_note'] = data
    if duration:
        payload['duration'] = duration
    if length:
        payload['length'] = length
    else:
        payload['length'] = 639  # seems like it is MAX length size
    if reply_to_message_id:
        payload['reply_to_message_id'] = reply_to_message_id
    if reply_markup:
        payload['reply_markup'] = _convert_markup(reply_markup)
    if disable_notification:
        payload['disable_notification'] = disable_notification
    if timeout:
        payload['connect-timeout'] = timeout
    return _make_request(token, method_url, params=payload, files=files, method='post')
Esempio n. 39
0
    def turn_into_waldo_var(
        self,val,force_copy,active_event, host_uuid,new_peered,
        new_multi_threaded):
        '''
        @param {Anything} val

        @param {bool} force_copy

        @param {_ActiveEvent object} active_event
        
        @param {uuid} host_uuid

        @param {bool} new_peered --- True if in the case that we have
        to copy a value, the copy should be peered.  Used for loading
        arguments into sequence local data when message send is
        called.  @see convert_for_seq_local.

        @returns {WaldoVariable}

        Used when copying arguments in to function.  Compiler's caller
        can pass in Python literals or WaldoReference objects when it
        emits a function call.  However, the emitted function
        definition requires all arguments to be WaldoVariables.
        Therefore, at the beginning of each emitted function, copy all
        arguments in to WaldoVariables if they are not already.  

        If val is not a WaldoReference, then based on its type, assign
        it as the initial value of a Waldo Variable and return.

        If val is a WaldoReference, then check force_copy.  force_copy
        represents whether or not we want to make a copy of the
        WaldoReference object.  (It would be True for instance if we
        were passed in a WaldoNumber because numbers are passed by
        value; it would be false if the arg was external or if we were
        passed in a reference type.)

        If it is false, then just return val.  Otherwise, make copy.
        
        '''

        # FIXME: Start using some of the single threaded constructors
        # as well.
        
        if isinstance(val,WaldoObj):
            if force_copy:
                # means that it was a WaldoVariable: just call its copy
                # method
                return val.copy(active_event,new_peered,new_multi_threaded)
            # otherwise, just return val
            return val

        # means that val was not a reference object.... turn it into one.
        constructor = None

        if new_multi_threaded:

            if util.is_string(val):
                # not using isinstance here because python 3 and python
                # 2.7 have different ways of testing for string.
                constructor = wVariables.WaldoTextVariable
            elif isinstance(val,numbers.Number):
                constructor = wVariables.WaldoNumVariable
            elif isinstance(val,bool):
                constructor = wVariables.WaldoTrueFalseVariable
            elif isinstance(val,dict):
                constructor = wVariables.WaldoMapVariable
            elif isinstance(val,list):
                constructor = wVariables.WaldoListVariable
            elif isinstance(val,waldoEndpoint._Endpoint):
                constructor = wVariables.WaldoEndpointVariable
            #### DEBUG
            elif hasattr(val,'__call__'):
                # checks if is function
                util.logger_assert(
                    'Should use special call func_turn_into_waldo_var for function objects')
            else:
                util.logger_assert(
                    'Unknown object type to call turn_into_waldo_var on')
            #### END DEBUG

                
        else:
            if util.is_string(val):
                # not using isinstance here because python 3 and python
                # 2.7 have different ways of testing for string.
                constructor = wVariables.WaldoSingleThreadTextVariable
            elif isinstance(val,numbers.Number):
                constructor = wVariables.WaldoSingleThreadNumVariable
            elif isinstance(val,bool):
                constructor = wVariables.WaldoSingleThreadTrueFalseVariable
            elif isinstance(val,dict):
                constructor = wVariables.WaldoSingleThreadMapVariable
            elif isinstance(val,list):
                constructor = wVariables.WaldoSingleThreadListVariable
            elif isinstance(val,waldoEndpoint._Endpoint):
                constructor = wVariables.WaldoSingleThreadEndpointVariable
            #### DEBUG
            elif hasattr(val,'__call__'):
                # checks if is function
                util.logger_assert(
                    'Should use special call func_turn_into_waldo_var for function objects')
            else:
                util.logger_assert(
                    'Unknown object type to call turn_into_waldo_var on')
            #### END DEBUG
            
                
        return constructor(
            'garbage', # actual name of variable isn't important
            host_uuid,
            new_peered, # not peered
            val # used as initial value
            )
Esempio n. 40
0
def setup(**attr):
    if len(sys.argv)<=1 and not attr.get('script_args',[]):
        try:
            from numpy.distutils.interactive import interactive_sys_argv
            from numpy.distutils.core import _exit_interactive_session
            import atexit
            atexit.register(_exit_interactive_session)
            sys.argv[:] = interactive_sys_argv(sys.argv)
            if len(sys.argv)>1:
                return setup(**attr)
        except:
            pass

    cmdclass = my_cmdclass.copy()

    new_attr = attr.copy()
    if 'cmdclass' in new_attr:
        cmdclass.update(new_attr['cmdclass'])
    new_attr['cmdclass'] = cmdclass

    if 'configuration' in new_attr:
        # To avoid calling configuration if there are any errors
        # or help request in command in the line.
        configuration = new_attr.pop('configuration')

        old_dist = distutils.core._setup_distribution
        old_stop = distutils.core._setup_stop_after
        distutils.core._setup_distribution = None
        distutils.core._setup_stop_after = "commandline"
        try:
            dist = setup(**new_attr)
        finally:
            distutils.core._setup_distribution = old_dist
            distutils.core._setup_stop_after = old_stop
        if dist.help or not _command_line_ok():
            # probably displayed help, skip running any commands
            return dist

        # create setup dictionary and append to new_attr
        config = configuration()
        if hasattr(config,'todict'):
            config = config.todict()
        _dict_append(new_attr, **config)

    # Move extension source libraries to libraries
    libraries = []
    for ext in new_attr.get('ext_modules',[]):
        new_libraries = []
        for item in ext.libraries:
            #[item] = util.convert_ulist([item])
            if util.is_sequence(item):
                lib_name, build_info = item
                _check_append_ext_library(libraries, item)
                new_libraries.append(lib_name)
            elif util.is_string(item):
                new_libraries.append(item)
            else:
                raise TypeError("invalid description of extension module "
                                "library %r" % (item,))
        ext.libraries = new_libraries
    if libraries:
        if 'libraries' not in new_attr:
            new_attr['libraries'] = []
        for item in libraries:
            _check_append_library(new_attr['libraries'], item)

    # sources in ext_modules or libraries may contain header files
    if ('ext_modules' in new_attr or 'libraries' in new_attr) \
       and 'headers' not in new_attr:
        new_attr['headers'] = []

    # Use our custom Distribution class instead of distutils' one
    new_attr['distclass'] = CustomDistribution

    return old_setup(**new_attr)
Esempio n. 41
0
 def test_positive_types( self ):
     self.assertTrue( util.is_string( "abcd" ) )
     self.assertTrue( util.is_boolean( True ) )
     self.assertTrue( util.is_integer( 1234 ) )
     self.assertTrue( util.is_list( [1,2,3,4]) )
     self.assertTrue( util.is_dict( {"foo":"bar"} ) )
Esempio n. 42
0
def cmd_lvm(cmd, sr_alloc=None, pread_func=util.pread2, *args):
    """ Construct and run the appropriate lvm command

        depending on the SR's allocation type; 'thick' or 'xlvhd'. For
        PV commands, the full path to the device is required.

        Input:
            cmd -- (list) lvm command
                cmd[0]  -- (str) lvm command name
                cmd[1:] -- (str) lvm command parameters

            sr_alloc -- (str) SR's allocation type; 'thick' or 'xlvhd'
                              if it's not supplied, the function will
                              figure it out
                Default: None

            pread_func -- (function) the flavor of util.pread to use
                                     to execute the lvm command
                Default: util.pread2()

            *args -- extra arguments passed to cmd_lvm will be passed
                     to 'pread_func'

        Return:
            stdout -- (str) stdout after running the lvm command.

        Raise:
            util.CommandException
    """

    if type(cmd) is not list:
        util.SMlog("CMD_LVM: Argument 'cmd' not of type 'list'")
        return None
    if not len(cmd):
        util.SMlog("CMD_LVM: 'cmd' list is empty")
        return None

    lvm_cmd, lvm_args = cmd[0], cmd[1:]

    if lvm_cmd not in LVM_COMMANDS:
        util.SMlog("CMD_LVM: '{}' is not a valid lvm command".format(lvm_cmd))
        return None

    for arg in lvm_args:
        if not util.is_string(arg):
            util.SMlog("CMD_LVM: Not all lvm arguments are of type 'str'")
            return None

    if sr_alloc is None:
        if lvm_cmd not in PV_COMMANDS:
            for arg in lvm_args:
                try:
                    filename = extract_vgname(arg)
                except:
                    util.logException('CMD_LVM')
                    return None
                if filename:
                    break
            else:  # if for loop doesn't break
                util.SMlog("CMD_LVM: Could not find VG "
                           "name in lvm argument list")
                util.SMlog([lvm_cmd] + lvm_args)
                return None
        else:
            for arg in lvm_args:
                if os.path.exists(arg):
                    try:
                        filename = getSCSIid(arg)
                    except:
                        util.logException('CMD_LVM')
                        return None
                    break
            else:  # if for loop doesn't break
                util.SMlog("CMD_LVM: Could not find PV "
                           "name in lvm argument list")
                util.SMlog([lvm_cmd] + lvm_args)
                return None

        sr_alloc = get_sr_alloc(filename)

    if sr_alloc == 'xlvhd':
        stdout = pread_func(['/bin/xenvm', lvm_cmd] + lvm_args, *args)
    elif sr_alloc == 'thick':
        stdout = pread_func([os.path.join(LVM_BIN, lvm_cmd)] + lvm_args, *args)
    else:
        util.SMlog("CMD_LVM: ERROR: 'sr_alloc' neither 'xlvhd' nor 'thick'")
        return None

    return stdout
Esempio n. 43
0
 def test_neg_strings( self ):
     self.assertFalse( util.is_string( ["a","b","c"] ) )
Esempio n. 44
0
 def _setTemplateType(self,value):
     if not util.is_string(value): raise ValueError("Template type is a string value.")
     if not filenames.has_key(value): raise ValueError("Template type '%s' is not valid." % value)
     self.template_type = value
Esempio n. 45
0
 def test_is_string(self):
     self.assertTrue( util.is_string(self.ssimple))
Esempio n. 46
0
    def serializable_var_tuple_for_network(
        self,parent_delta,var_name,invalid_listener,force):
        '''
        The runtime automatically synchronizes data between both
        endpoints.  When one side has updated a peered variable, the
        other side needs to attempt to apply those changes before
        doing further work.  This method grabs the val and version
        object of the dirty element associated with invalid_listener.
        Using these data, plus var_name, it constructs a named tuple
        for serialization.  (@see
        util._generate_serialization_named_tuple)

        Note: if the val of this object is another Reference object,
        then we recursively keep generating named tuples and embed
        them in the one we return.

        Note: we only serialize peered data.  No other data gets sent
        over the network; therefore, it should not be serialized.

        @param {*Delta or VarStoreDeltas} parent_delta --- Append any
        message that we create here to this message.
        
        @param {String} var_name --- Both sides of the connection need
        to agree on a common name for the variable being serialized.
        This is to ensure that when the data are received by the other
        side we know which variable to put them into.  This value is
        only really necessary for the outermost wrapping of the named
        type tuple, but we pass it through anyways.

        @param {bool} force --- True if regardless of whether modified
        or not we should serialize.  False otherwise.  (We migth want
        to force for instance the first time we send sequence data.)
        
        @returns {bool} --- True if some subelement was modified,
        False otherwise.
        '''
        self._lock()
        self._add_invalid_listener(invalid_listener)
        dirty_element = self._dirty_map[invalid_listener.uuid]
        self._unlock()
        version_obj = dirty_element.version_obj

        # a val can either point to a waldo reference, a python value,
        # or a list/map of waldo references or a list/map of python
        # values.
        var_data = dirty_element.val

        
        if (not force) and (not version_obj.has_been_written_since_last_message):
            if (isinstance(var_data,numbers.Number) or
                util.is_string(var_data) or isinstance(var_data,bool)):
                # nothing to do because this value has not been
                # written.  NOTE: for list/dict types, must actually
                # go through to ensure no subelements were written.
                return False

        sub_element_modified = False
        if self.py_val_serialize(parent_delta,var_data,var_name):
            sub_element_modified = True
        
        elif isinstance(var_data,list):
            list_delta = parent_delta.internal_list_delta
            list_delta.parent_type = VarStoreDeltas.INTERNAL_LIST_CONTAINER

            if force:
                # perform each operation as a write...
                version_obj.add_all_data_to_delta_list(
                    list_delta,var_data,invalid_listener)
                sub_element_modified = True
            else:
                # if all subelements have not been modified, then we
                # do not need to keep track of these changes.
                # wVariable.waldoMap, wVariable.waldoList, or
                # wVariable.WaldoUserStruct will get rid of it later.
                sub_element_modified = version_obj.add_to_delta_list(
                    list_delta,var_data,invalid_listener)


        elif isinstance(var_data,dict):
            map_delta = parent_delta.internal_map_delta
            map_delta.parent_type = VarStoreDeltas.INTERNAL_MAP_CONTAINER

            if force:
                # perform each operation as a write...
                version_obj.add_all_data_to_delta_list(
                    map_delta,var_data,invalid_listener)
                sub_element_modified = True
            else:
                # if all subelements have not been modified, then we
                # do not need to keep track of these changes.
                # wVariable.waldoMap, wVariable.waldoList, or
                # wVariable.WaldoUserStruct will get rid of it later.
                sub_element_modified = version_obj.add_to_delta_list(
                    map_delta,var_data,invalid_listener)

                
        else:
            # creating deltas for cases where internal data are waldo
            # references.... should have been overridden in
            # wVariables.py
            util.logger_assert('Serializing unknown type.')

        version_obj.has_been_written_since_last_message = False
        return sub_element_modified
Esempio n. 47
0
 def __join_id(self, id):
     if util.is_string(id) and id.startswith('/'):
         return util.norm_path(self.__attach_point + '/' + id)
     else:
         return id
Esempio n. 48
0
def convert_uuids(obj):
    id = obj.get('uuid', None)
    if util.is_string(id):
        id = uuid.UUID(id)
    if id: obj['uuid'] = id.bytes
    return id
Esempio n. 49
0
 def _setLoadStructureString(self,value):
     if not util.is_string(value): raise ValueError("Load structure string is a string value.")
     self.load_structure_string = value + "\n"
Esempio n. 50
0
    def py_val_serialize(self,parent,var_data,var_name):
        '''
        @param {} parent --- Either a ContainerAction a VarStoreDeltas.

        FIXME: unclear if actually need var_name for all elements
        py_serialize-ing, or just py variables that are in the
        top-level.

        @returns {bool} --- True if var_data was a python value type
        and we put it into parent.  False otherwise.
        
        If is python value type, then adds a delta message to
        parent.  Otherwise, does nothing.
        '''
        is_value_type = False
        delta = None
        if isinstance(var_data, numbers.Number):
            # can only add a pure number to var store a holder or to
            # an added key
            if parent.parent_type == VarStoreDeltas.VAR_STORE_DELTA:
                delta = parent.num_deltas.add()
            elif parent.parent_type == VarStoreDeltas.CONTAINER_ADDED:
                parent.added_what_num = var_data
            elif parent.parent_type == VarStoreDeltas.CONTAINER_WRITTEN:
                parent.what_written_num = var_data
            #### DEBUG
            else:
                util.logger_assert('Unexpected parent type in py_serialize')
            #### END DEBUG
                
            is_value_type = True
            
        elif util.is_string(var_data):
            if parent.parent_type == VarStoreDeltas.VAR_STORE_DELTA:
                delta = parent.text_deltas.add()
            elif parent.parent_type == VarStoreDeltas.CONTAINER_ADDED:
                parent.added_what_text = var_data
            elif parent.parent_type == VarStoreDeltas.CONTAINER_WRITTEN:
                parent.what_written_text = var_data
                
            #### DEBUG
            else:
                util.logger_assert('Unexpected parent type in py_serialize')
            #### END DEBUG                
                
            is_value_type = True
            
        elif isinstance(var_data,bool):
            if parent.parent_type == VarStoreDeltas.VAR_STORE_DELTA:
                delta = parent.true_false_deltas.add()
            elif parent.parent_type == VarStoreDeltas.CONTAINER_ADDED:
                parent.added_what_tf = var_data
            elif parent.parent_type == VarStoreDeltas.CONTAINER_WRITTEN:
                parent.what_written_tf = var_data                
            #### DEBUG
            else:
                util.logger_assert('Unexpected parent type in py_serialize')
            #### END DEBUG                

                
            is_value_type = True

        if delta != None:
            # all value types have same format
            delta.var_name = var_name
            delta.var_data = var_data
            
        return is_value_type
Esempio n. 51
0
def cmd_lvm(cmd, sr_alloc=None, pread_func=util.pread2, *args):
    """ Construct and run the appropriate lvm command

        depending on the SR's allocation type; 'thick' or 'xlvhd'. For
        PV commands, the full path to the device is required.

        Input:
            cmd -- (list) lvm command
                cmd[0]  -- (str) lvm command name
                cmd[1:] -- (str) lvm command parameters

            sr_alloc -- (str) SR's allocation type; 'thick' or 'xlvhd'
                              if it's not supplied, the function will
                              figure it out
                Default: None

            pread_func -- (function) the flavor of util.pread to use
                                     to execute the lvm command
                Default: util.pread2()

            *args -- extra arguments passed to cmd_lvm will be passed
                     to 'pread_func'

        Return:
            stdout -- (str) stdout after running the lvm command.

        Raise:
            util.CommandException
    """

    if type(cmd) is not list:
        util.SMlog("CMD_LVM: Argument 'cmd' not of type 'list'")
        return None
    if not len(cmd):
        util.SMlog("CMD_LVM: 'cmd' list is empty")
        return None

    lvm_cmd, lvm_args = cmd[0], cmd[1:]

    if lvm_cmd not in LVM_COMMANDS:
        util.SMlog("CMD_LVM: '{}' is not a valid lvm command".format(lvm_cmd))
        return None

    for arg in lvm_args:
        if not util.is_string(arg):
            util.SMlog("CMD_LVM: Not all lvm arguments are of type 'str'")
            return None

    if sr_alloc is None:
        if lvm_cmd not in PV_COMMANDS:
            for arg in lvm_args:
                try:
                    filename = extract_vgname(arg)
                except:
                    util.logException('CMD_LVM')
                    return None
                if filename:
                    break
            else: # if for loop doesn't break
                util.SMlog("CMD_LVM: Could not find VG "
                           "name in lvm argument list")
                util.SMlog([lvm_cmd] + lvm_args)
                return None
        else:
            for arg in lvm_args:
                if os.path.exists(arg):
                    try:
                        filename = getSCSIid(arg)
                    except:
                        util.logException('CMD_LVM')
                        return None
                    break
            else: # if for loop doesn't break
                util.SMlog("CMD_LVM: Could not find PV "
                           "name in lvm argument list")
                util.SMlog([lvm_cmd] + lvm_args)
                return None

        sr_alloc = get_sr_alloc(filename)

    if sr_alloc == 'xlvhd':
        stdout = pread_func(['/bin/xenvm', lvm_cmd] + lvm_args, *args)
    elif sr_alloc == 'thick':
        stdout = pread_func([os.path.join(LVM_BIN, lvm_cmd)] + lvm_args, *args)
    else:
        util.SMlog("CMD_LVM: ERROR: 'sr_alloc' neither 'xlvhd' nor 'thick'")
        return None

    return stdout