def _get_next_content_type(keypath, next_counter): """ Return next content-type value from the low level bit-field "/user-storage/ownership{}/content-type". """ v_user_id = helper.get_user_id_value(keypath[-2][0]) if v_user_id is None: return None v_bitmask = optional_get_elem( maapi_socket, helper.th, kp_content_type(v_user_id, keypath[1][0])) if v_bitmask is None: return None bitmask_int = int(v_bitmask) # get all the bits that are set in the low level value bits_set = [ bit for bit in ct_bits_to_enum.keys() if bool(bitmask_int & bit) ] # return N-th bit from the set depending on the "next" if next_counter + 1 < len(bits_set): mapping_index = bits_set[next_counter + 1] return confd.Value(ct_bits_to_enum[mapping_index], confd.C_ENUM_VALUE) else: return None
def _update_content_type(user_id_v, folder_id_v, content_v): """ Add the specific content-type to the low-level bitmask. """ path = kp_content_type(user_id_v, folder_id_v) bit_to_add = ct_enum_to_bits[int(content_v)] # /folder-user{bob}/managed-folder{myhome}/content-type{archive} v_bits_set = optional_get_elem( maapi_socket, helper.th, kp_content_type(user_id_v, folder_id_v)) if v_bits_set is not None: bits_set = int(v_bits_set) if not bool(bits_set & bit_to_add): new_bitmask = confd.Value(bits_set + bit_to_add, confd.C_BIT32) maapi.set_elem(maapi_socket, helper.th, new_bitmask, path) else: new_bitmask = confd.Value(bit_to_add, confd.C_BIT32) maapi.set_elem(maapi_socket, helper.th, new_bitmask, path) return confd.CONFD_OK
def parse_folder_id_value(v_storage_id): """ Extract high level value to be used as: "/folder-user{}/managed-folder{}/folder-id" from the input low level: "/user-storage/storage{}/storage-id" Example: storage-id == "123||home-dif" --> folder-id == "home-dir" """ folder_id = str(v_storage_id).split(STORAGE_ID_SEPARATOR)[1] return confd.Value(folder_id, confd.C_BUF)
def cb_get_elem(self, tctx, kp): v_result = None server_name = str(kp[1][0]) if server_name in running_db: leaf_tag = kp[0].tag if ns.smp_name == leaf_tag: v_result = confd.Value(server_name, confd.C_BUF) elif ns.smp_ip == leaf_tag: ip = running_db[server_name][0] v_result = confd.Value(ip, confd.C_IPV4) elif ns.smp_port == leaf_tag: port = int(running_db[server_name][1]) v_result = confd.Value(port, confd.C_UINT16) else: raise NotImplementedError (dp.data_reply_value(tctx, v_result) if v_result is not None else dp.data_reply_not_found(tctx)) return confd.CONFD_OK
def cb_get_case(self, tctx, kp, choice): helper = TransformDataHelper(tctx.th) v_user_id = helper.get_user_id_value(kp[-2][0]) if v_user_id is None: dp.data_reply_not_found(tctx) return confd.CONFD_OK v_auth_type = helper.get_auth_type_value(v_user_id) tag = at_enum_to_tags[int(v_auth_type)] dp.data_reply_value( tctx, confd.Value((tag, ns_folders.hash), confd.C_XMLTAG)) return confd.CONFD_OK
def cb_get_next(self, tctx, kp, next): list_tag = kp[0].tag next_key = None if ns.smp_server == list_tag: sorted_server_names = sorted(running_db.keys()) num_servers = len(sorted_server_names) index = next - (-1) if index < num_servers: next_key = confd.Value(sorted_server_names[index], confd.C_BUF) else: raise NotImplementedError if next_key is None: dp.data_reply_next_key(tctx, keys=None, next=-1) else: dp.data_reply_next_key(tctx, keys=[next_key], next=next + 1) return confd.CONFD_OK
def _remove_content_type(user_id_v, folder_id_v, content_v): """ Remove specific content-type from low-level bitmask. """ path = kp_content_type(user_id_v, folder_id_v) bit_to_remove = ct_enum_to_bits[int(content_v)] # /folder-user{bob}/managed-folder{myhome}/content-type{archive} v_bits_set = optional_get_elem( maapi_socket, helper.th, kp_content_type(user_id_v, folder_id_v)) if v_bits_set is not None: bits_set = int(v_bits_set) if bool(bits_set & bit_to_remove): if bits_set == bit_to_remove: maapi.delete(maapi_socket, helper.th, path) else: new_bitmask = confd.Value(bits_set - bit_to_remove, confd.C_BIT32) maapi.set_elem(maapi_socket, helper.th, new_bitmask, path)
def send_shipping_notif(stream, user, title, fmt, copies): values = [ confd.TagValue( confd.XmlTag(ns.hash, ns.bz_shipping), confd.Value((ns.bz_shipping, ns.hash), confd.C_XMLBEGIN)), confd.TagValue(confd.XmlTag(ns.hash, ns.bz_user), confd.Value(user)), confd.TagValue(confd.XmlTag(ns.hash, ns.bz_title), confd.Value(title)), confd.TagValue(confd.XmlTag(ns.hash, ns.bz_format), confd.Value((ns.hash, fmt), confd.C_IDENTITYREF)), confd.TagValue(confd.XmlTag(ns.hash, ns.bz_number_of_copies), confd.Value(copies, confd.C_UINT32)), confd.TagValue(confd.XmlTag(ns.hash, ns.bz_shipping), confd.Value((ns.bz_shipping, ns.hash), confd.C_XMLEND)) ] gm = time.gmtime(time.time()) now = confd.DateTime(gm.tm_year, gm.tm_mon, gm.tm_mday, gm.tm_hour, gm.tm_min, gm.tm_sec, 0, 0, 0) lowdp.notification_send(stream, now, values)
def _create_managed_folder(user_id_v, folder_id_v): """ Handle all steps needed for creation of "/folder-user{}/managed-folder{}" record. """ v_storage_id = concat_storage_id_value(user_id_v, folder_id_v) paths_to_create = [ kp_storage(v_storage_id), kp_ownership(user_id_v, v_storage_id) ] for path in paths_to_create: maapi.create(maapi_socket, helper.th, path) # + set an artificial mount-point that is not shown in high-level # YANG; For example case here, use a string prefix: # "/mnt/user-id/storage-id". mountpoint_str = '/mnt/user-storages/{0}/{1}'.format( user_id_v, folder_id_v) val = confd.Value(mountpoint_str, confd.C_BUF) maapi.set_elem(maapi_socket, helper.th, val, kp_storage(v_storage_id) + '/mountpoint') return confd.CONFD_OK
def concat_storage_id_value(v_user_id, v_folder_id): """ Retrieve the key value of "/user-storage/storage{}" record that corresponds to a specific user's managed-folder. """ storage_id = str(v_user_id) + STORAGE_ID_SEPARATOR + str(v_folder_id) return confd.Value(storage_id, confd.C_BUF)
def set_auth_type(self, v_user_id, tag): """ Set user's low level authentication type to a specific value. """ val = confd.Value(tag, confd.C_ENUM_VALUE) maapi.set_elem(maapi_socket, self.th, val, kp_auth_type(v_user_id))