<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def share_network(network_id, usernames, read_only, share,**kwargs):
 """ Share a network with a list of users, identified by their usernames. The read_only flag ('Y' or 'N') must be set to 'Y' to allow write access or sharing. The share flat ('Y' or 'N') must be set to 'Y' to allow the project to be shared with other users """


    user_id = kwargs.get('user_id')
    net_i = _get_network(network_id)
    net_i.check_share_permission(user_id)

    if read_only == 'Y':
        write = 'N'
        share = 'N'
    else:
        write = 'Y'

    if net_i.created_by != int(user_id) and share == 'Y':
        raise HydraError("Cannot share the 'sharing' ability as user %s is not"
                     " the owner of network %s"%
                     (user_id, network_id))

    for username in usernames:
        user_i = _get_user(username)
        #Set the owner ship on the network itself
        net_i.set_owner(user_i.id, write=write, share=share)
        for o in net_i.project.owners:
            if o.user_id == user_i.id:
                break
        else:
            #Give the user read access to the containing project
            net_i.project.set_owner(user_i.id, write='N', share='N')
    db.DBSession.flush()