text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: transfer
using AWS.Compat
using AWS.UUIDs

"""
    create_access(external_id, role, server_id)
    create_access(external_id, role, server_id, params::Dict{String,<:Any})

Used by administrators to choose which groups in the directory should have access to upload
and download files over the enabled protocols using Amazon Web Services Transfer Family.
For example, a Microsoft Active Directory might contain 50,000 users, but only a small
fraction might need the ability to transfer files to the server. An administrator can use
CreateAccess to limit the access to the correct set of users who need this ability.

# Arguments
- `external_id`: A unique identifier that is required to identify specific groups within
  your directory. The users of the group that you associate have access to your Amazon S3 or
  Amazon EFS resources over the enabled protocols using Amazon Web Services Transfer Family.
  If you know the group name, you can view the SID values by running the following command
  using Windows PowerShell.  Get-ADGroup -Filter {samAccountName -like \"YourGroupName*\"}
  -Properties * | Select SamAccountName,ObjectSid  In that command, replace YourGroupName
  with the name of your Active Directory group. The regex used to validate this parameter is
  a string of characters consisting of uppercase and lowercase alphanumeric characters with
  no spaces. You can also include underscores or any of the following characters: =,.@:/-
- `role`: Specifies the Amazon Resource Name (ARN) of the IAM role that controls your
  users' access to your Amazon S3 bucket or EFS file system. The policies attached to this
  role determine the level of access that you want to provide your users when transferring
  files into and out of your Amazon S3 bucket or EFS file system. The IAM role should also
  contain a trust relationship that allows the server to access your resources when servicing
  your users' transfer requests.
- `server_id`: A system-assigned unique identifier for a server instance. This is the
  specific server that you added your user to.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"HomeDirectory"`: The landing directory (folder) for a user when they log in to the
  server using the client. A HomeDirectory example is /bucket_name/home/mydirectory.
- `"HomeDirectoryMappings"`: Logical directory mappings that specify what Amazon S3 or
  Amazon EFS paths and keys should be visible to your user and how you want to make them
  visible. You must specify the Entry and Target pair, where Entry shows how the path is made
  visible and Target is the actual Amazon S3 or Amazon EFS path. If you only specify a
  target, it is displayed as is. You also must ensure that your Amazon Web Services Identity
  and Access Management (IAM) role provides access to paths in Target. This value can only be
  set when HomeDirectoryType is set to LOGICAL. The following is an Entry and Target pair
  example.  [ { \"Entry\": \"your-personal-report.pdf\", \"Target\":
  \"/bucket3/customized-reports/{transfer:UserName}.pdf\" } ]  In most cases, you can use
  this value instead of the scope-down policy to lock down your user to the designated home
  directory (\"chroot\"). To do this, you can set Entry to / and set Target to the
  HomeDirectory parameter value. The following is an Entry and Target pair example for
  chroot.  [ { \"Entry:\": \"/\", \"Target\": \"/bucket_name/home/mydirectory\" } ]   If the
  target of a logical directory entry does not exist in Amazon S3 or EFS, the entry is
  ignored. As a workaround, you can use the Amazon S3 API or EFS API to create 0 byte objects
  as place holders for your directory. If using the CLI, use the s3api or efsapi call instead
  of s3 or efs so you can use the put-object operation. For example, you use the following:
  aws s3api put-object --bucket bucketname --key path/to/folder/. Make sure that the end of
  the key name ends in a / for it to be considered a folder.
- `"HomeDirectoryType"`: The type of landing directory (folder) you want your users' home
  directory to be when they log into the server. If you set it to PATH, the user will see the
  absolute Amazon S3 bucket or EFS paths as is in their file transfer protocol clients. If
  you set it LOGICAL, you will need to provide mappings in the HomeDirectoryMappings for how
  you want to make Amazon S3 or EFS paths visible to your users.
- `"Policy"`: A scope-down policy for your user so that you can use the same IAM role
  across multiple users. This policy scopes down user access to portions of their Amazon S3
  bucket. Variables that you can use inside this policy include {Transfer:UserName},
  {Transfer:HomeDirectory}, and {Transfer:HomeBucket}.  This only applies when domain of
  ServerId is S3. Amazon EFS does not use scope-down policies. For scope-down policies,
  Amazon Web Services Transfer Family stores the policy as a JSON blob, instead of the Amazon
  Resource Name (ARN) of the policy. You save the policy as a JSON blob and pass it in the
  Policy argument. For an example of a scope-down policy, see Example scope-down policy. For
  more information, see AssumeRole in the Amazon Web Services Security Token Service API
  Reference.
- `"PosixProfile"`:
"""
create_access(ExternalId, Role, ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("CreateAccess", Dict{String, Any}("ExternalId"=>ExternalId, "Role"=>Role, "ServerId"=>ServerId); aws_config=aws_config)
create_access(ExternalId, Role, ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("CreateAccess", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ExternalId"=>ExternalId, "Role"=>Role, "ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    create_server()
    create_server(params::Dict{String,<:Any})

Instantiates an auto-scaling virtual server based on the selected file transfer protocol in
Amazon Web Services. When you make updates to your file transfer protocol-enabled server or
when you work with users, use the service-generated ServerId property that is assigned to
the newly created server.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Certificate"`: The Amazon Resource Name (ARN) of the Amazon Web Services Certificate
  Manager (ACM) certificate. Required when Protocols is set to FTPS. To request a new public
  certificate, see Request a public certificate in the  Amazon Web Services Certificate
  Manager User Guide. To import an existing certificate into ACM, see Importing certificates
  into ACM in the  Amazon Web Services Certificate Manager User Guide. To request a private
  certificate to use FTPS through private IP addresses, see Request a private certificate in
  the  Amazon Web Services Certificate Manager User Guide. Certificates with the following
  cryptographic algorithms and key sizes are supported:   2048-bit RSA (RSA_2048)   4096-bit
  RSA (RSA_4096)   Elliptic Prime Curve 256 bit (EC_prime256v1)   Elliptic Prime Curve 384
  bit (EC_secp384r1)   Elliptic Prime Curve 521 bit (EC_secp521r1)    The certificate must be
  a valid SSL/TLS X.509 version 3 certificate with FQDN or IP address specified and
  information about the issuer.
- `"Domain"`: The domain of the storage system that is used for file transfers. There are
  two domains available: Amazon Simple Storage Service (Amazon S3) and Amazon Elastic File
  System (Amazon EFS). The default value is S3.  After the server is created, the domain
  cannot be changed.
- `"EndpointDetails"`: The virtual private cloud (VPC) endpoint settings that are
  configured for your server. When you host your endpoint within your VPC, you can make it
  accessible only to resources within your VPC, or you can attach Elastic IP addresses and
  make it accessible to clients over the internet. Your VPC's default security groups are
  automatically assigned to your endpoint.
- `"EndpointType"`: The type of endpoint that you want your server to use. You can choose
  to make your server's endpoint publicly accessible (PUBLIC) or host it inside your VPC.
  With an endpoint that is hosted in a VPC, you can restrict access to your server and
  resources only within your VPC or choose to make it internet facing by attaching Elastic IP
  addresses directly to it.   After May 19, 2021, you won't be able to create a server using
  EndpointType=VPC_ENDPOINT in your Amazon Web Services account if your account hasn't
  already done so before May 19, 2021. If you have already created servers with
  EndpointType=VPC_ENDPOINT in your Amazon Web Services account on or before May 19, 2021,
  you will not be affected. After this date, use EndpointType=VPC. For more information, see
  https://docs.aws.amazon.com/transfer/latest/userguide/create-server-in-vpc.html#deprecate-vp
  c-endpoint. It is recommended that you use VPC as the EndpointType. With this endpoint
  type, you have the option to directly associate up to three Elastic IPv4 addresses (BYO IP
  included) with your server's endpoint and use VPC security groups to restrict traffic by
  the client's public IP address. This is not possible with EndpointType set to VPC_ENDPOINT.
- `"HostKey"`: The RSA private key as generated by the ssh-keygen -N \"\" -m PEM -f
  my-new-server-key command.  If you aren't planning to migrate existing users from an
  existing SFTP-enabled server to a new server, don't update the host key. Accidentally
  changing a server's host key can be disruptive.  For more information, see Change the host
  key for your SFTP-enabled server in the Amazon Web Services Transfer Family User Guide.
- `"IdentityProviderDetails"`: Required when IdentityProviderType is set to
  AWS_DIRECTORY_SERVICE or API_GATEWAY. Accepts an array containing all of the information
  required to use a directory in AWS_DIRECTORY_SERVICE or invoke a customer-supplied
  authentication API, including the API Gateway URL. Not required when IdentityProviderType
  is set to SERVICE_MANAGED.
- `"IdentityProviderType"`: Specifies the mode of authentication for a server. The default
  value is SERVICE_MANAGED, which allows you to store and access user credentials within the
  Amazon Web Services Transfer Family service. Use AWS_DIRECTORY_SERVICE to provide access to
  Active Directory groups in Amazon Web Services Managed Active Directory or Microsoft Active
  Directory in your on-premises environment or in Amazon Web Services using AD Connectors.
  This option also requires you to provide a Directory ID using the IdentityProviderDetails
  parameter. Use the API_GATEWAY value to integrate with an identity provider of your
  choosing. The API_GATEWAY setting requires you to provide an API Gateway endpoint URL to
  call for authentication using the IdentityProviderDetails parameter.
- `"LoggingRole"`: Specifies the Amazon Resource Name (ARN) of the Amazon Web Services
  Identity and Access Management (IAM) role that allows a server to turn on Amazon CloudWatch
  logging for Amazon S3 or Amazon EFS events. When set, user activity can be viewed in your
  CloudWatch logs.
- `"Protocols"`: Specifies the file transfer protocol or protocols over which your file
  transfer protocol client can connect to your server's endpoint. The available protocols
  are:    SFTP (Secure Shell (SSH) File Transfer Protocol): File transfer over SSH    FTPS
  (File Transfer Protocol Secure): File transfer with TLS encryption    FTP (File Transfer
  Protocol): Unencrypted file transfer    If you select FTPS, you must choose a certificate
  stored in Amazon Web Services Certificate Manager (ACM) which is used to identify your
  server when clients connect to it over FTPS. If Protocol includes either FTP or FTPS, then
  the EndpointType must be VPC and the IdentityProviderType must be AWS_DIRECTORY_SERVICE or
  API_GATEWAY. If Protocol includes FTP, then AddressAllocationIds cannot be associated. If
  Protocol is set only to SFTP, the EndpointType can be set to PUBLIC and the
  IdentityProviderType can be set to SERVICE_MANAGED.
- `"SecurityPolicyName"`: Specifies the name of the security policy that is attached to the
  server.
- `"Tags"`: Key-value pairs that can be used to group and search for servers.
"""
create_server(; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("CreateServer"; aws_config=aws_config)
create_server(params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("CreateServer", params; aws_config=aws_config)

"""
    create_user(role, server_id, user_name)
    create_user(role, server_id, user_name, params::Dict{String,<:Any})

Creates a user and associates them with an existing file transfer protocol-enabled server.
You can only create and associate users with servers that have the IdentityProviderType set
to SERVICE_MANAGED. Using parameters for CreateUser, you can specify the user name, set the
home directory, store the user's public key, and assign the user's Amazon Web Services
Identity and Access Management (IAM) role. You can also optionally add a scope-down policy,
and assign metadata with tags that can be used to group and search for users.

# Arguments
- `role`: Specifies the Amazon Resource Name (ARN) of the IAM role that controls your
  users' access to your Amazon S3 bucket or EFS file system. The policies attached to this
  role determine the level of access that you want to provide your users when transferring
  files into and out of your Amazon S3 bucket or EFS file system. The IAM role should also
  contain a trust relationship that allows the server to access your resources when servicing
  your users' transfer requests.
- `server_id`: A system-assigned unique identifier for a server instance. This is the
  specific server that you added your user to.
- `user_name`: A unique string that identifies a user and is associated with a as specified
  by the ServerId. This user name must be a minimum of 3 and a maximum of 100 characters
  long. The following are valid characters: a-z, A-Z, 0-9, underscore '_', hyphen '-', period
  '.', and at sign '@'. The user name can't start with a hyphen, period, or at sign.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"HomeDirectory"`: The landing directory (folder) for a user when they log in to the
  server using the client. A HomeDirectory example is /bucket_name/home/mydirectory.
- `"HomeDirectoryMappings"`: Logical directory mappings that specify what Amazon S3 or
  Amazon EFS paths and keys should be visible to your user and how you want to make them
  visible. You must specify the Entry and Target pair, where Entry shows how the path is made
  visible and Target is the actual Amazon S3 or Amazon EFS path. If you only specify a
  target, it is displayed as is. You also must ensure that your Amazon Web Services Identity
  and Access Management (IAM) role provides access to paths in Target. This value can only be
  set when HomeDirectoryType is set to LOGICAL. The following is an Entry and Target pair
  example.  [ { \"Entry\": \"your-personal-report.pdf\", \"Target\":
  \"/bucket3/customized-reports/{transfer:UserName}.pdf\" } ]  In most cases, you can use
  this value instead of the scope-down policy to lock your user down to the designated home
  directory (\"chroot\"). To do this, you can set Entry to / and set Target to the
  HomeDirectory parameter value. The following is an Entry and Target pair example for
  chroot.  [ { \"Entry:\": \"/\", \"Target\": \"/bucket_name/home/mydirectory\" } ]   If the
  target of a logical directory entry does not exist in Amazon S3 or EFS, the entry is
  ignored. As a workaround, you can use the Amazon S3 API or EFS API to create 0 byte objects
  as place holders for your directory. If using the CLI, use the s3api or efsapi call instead
  of s3 or efs so you can use the put-object operation. For example, you use the following:
  aws s3api put-object --bucket bucketname --key path/to/folder/. Make sure that the end of
  the key name ends in a / for it to be considered a folder.
- `"HomeDirectoryType"`: The type of landing directory (folder) you want your users' home
  directory to be when they log into the server. If you set it to PATH, the user will see the
  absolute Amazon S3 bucket or EFS paths as is in their file transfer protocol clients. If
  you set it LOGICAL, you will need to provide mappings in the HomeDirectoryMappings for how
  you want to make Amazon S3 or EFS paths visible to your users.
- `"Policy"`: A scope-down policy for your user so that you can use the same IAM role
  across multiple users. This policy scopes down user access to portions of their Amazon S3
  bucket. Variables that you can use inside this policy include {Transfer:UserName},
  {Transfer:HomeDirectory}, and {Transfer:HomeBucket}.  This only applies when domain of
  ServerId is S3. EFS does not use scope down policy. For scope-down policies, Amazon Web
  Services Transfer Family stores the policy as a JSON blob, instead of the Amazon Resource
  Name (ARN) of the policy. You save the policy as a JSON blob and pass it in the Policy
  argument. For an example of a scope-down policy, see Example scope-down policy. For more
  information, see AssumeRole in the Amazon Web Services Security Token Service API
  Reference.
- `"PosixProfile"`: Specifies the full POSIX identity, including user ID (Uid), group ID
  (Gid), and any secondary groups IDs (SecondaryGids), that controls your users' access to
  your Amazon EFS file systems. The POSIX permissions that are set on files and directories
  in Amazon EFS determine the level of access your users get when transferring files into and
  out of your Amazon EFS file systems.
- `"SshPublicKeyBody"`: The public portion of the Secure Shell (SSH) key used to
  authenticate the user to the server.
- `"Tags"`: Key-value pairs that can be used to group and search for users. Tags are
  metadata attached to users for any purpose.
"""
create_user(Role, ServerId, UserName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("CreateUser", Dict{String, Any}("Role"=>Role, "ServerId"=>ServerId, "UserName"=>UserName); aws_config=aws_config)
create_user(Role, ServerId, UserName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("CreateUser", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Role"=>Role, "ServerId"=>ServerId, "UserName"=>UserName), params)); aws_config=aws_config)

"""
    delete_access(external_id, server_id)
    delete_access(external_id, server_id, params::Dict{String,<:Any})

Allows you to delete the access specified in the ServerID and ExternalID parameters.

# Arguments
- `external_id`: A unique identifier that is required to identify specific groups within
  your directory. The users of the group that you associate have access to your Amazon S3 or
  Amazon EFS resources over the enabled protocols using Amazon Web Services Transfer Family.
  If you know the group name, you can view the SID values by running the following command
  using Windows PowerShell.  Get-ADGroup -Filter {samAccountName -like \"YourGroupName*\"}
  -Properties * | Select SamAccountName,ObjectSid  In that command, replace YourGroupName
  with the name of your Active Directory group. The regex used to validate this parameter is
  a string of characters consisting of uppercase and lowercase alphanumeric characters with
  no spaces. You can also include underscores or any of the following characters: =,.@:/-
- `server_id`: A system-assigned unique identifier for a server that has this user assigned.

"""
delete_access(ExternalId, ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteAccess", Dict{String, Any}("ExternalId"=>ExternalId, "ServerId"=>ServerId); aws_config=aws_config)
delete_access(ExternalId, ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteAccess", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ExternalId"=>ExternalId, "ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    delete_server(server_id)
    delete_server(server_id, params::Dict{String,<:Any})

Deletes the file transfer protocol-enabled server that you specify. No response returns
from this operation.

# Arguments
- `server_id`: A unique system-assigned identifier for a server instance.

"""
delete_server(ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteServer", Dict{String, Any}("ServerId"=>ServerId); aws_config=aws_config)
delete_server(ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteServer", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    delete_ssh_public_key(server_id, ssh_public_key_id, user_name)
    delete_ssh_public_key(server_id, ssh_public_key_id, user_name, params::Dict{String,<:Any})

Deletes a user's Secure Shell (SSH) public key. No response is returned from this operation.

# Arguments
- `server_id`: A system-assigned unique identifier for a file transfer protocol-enabled
  server instance that has the user assigned to it.
- `ssh_public_key_id`: A unique identifier used to reference your user's specific SSH key.
- `user_name`: A unique string that identifies a user whose public key is being deleted.

"""
delete_ssh_public_key(ServerId, SshPublicKeyId, UserName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteSshPublicKey", Dict{String, Any}("ServerId"=>ServerId, "SshPublicKeyId"=>SshPublicKeyId, "UserName"=>UserName); aws_config=aws_config)
delete_ssh_public_key(ServerId, SshPublicKeyId, UserName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteSshPublicKey", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId, "SshPublicKeyId"=>SshPublicKeyId, "UserName"=>UserName), params)); aws_config=aws_config)

"""
    delete_user(server_id, user_name)
    delete_user(server_id, user_name, params::Dict{String,<:Any})

Deletes the user belonging to a file transfer protocol-enabled server you specify. No
response returns from this operation.  When you delete a user from a server, the user's
information is lost.

# Arguments
- `server_id`: A system-assigned unique identifier for a server instance that has the user
  assigned to it.
- `user_name`: A unique string that identifies a user that is being deleted from a server.

"""
delete_user(ServerId, UserName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteUser", Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName); aws_config=aws_config)
delete_user(ServerId, UserName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DeleteUser", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName), params)); aws_config=aws_config)

"""
    describe_access(external_id, server_id)
    describe_access(external_id, server_id, params::Dict{String,<:Any})

Describes the access that is assigned to the specific file transfer protocol-enabled
server, as identified by its ServerId property and its ExternalID. The response from this
call returns the properties of the access that is associated with the ServerId value that
was specified.

# Arguments
- `external_id`: A unique identifier that is required to identify specific groups within
  your directory. The users of the group that you associate have access to your Amazon S3 or
  Amazon EFS resources over the enabled protocols using Amazon Web Services Transfer Family.
  If you know the group name, you can view the SID values by running the following command
  using Windows PowerShell.  Get-ADGroup -Filter {samAccountName -like \"YourGroupName*\"}
  -Properties * | Select SamAccountName,ObjectSid  In that command, replace YourGroupName
  with the name of your Active Directory group. The regex used to validate this parameter is
  a string of characters consisting of uppercase and lowercase alphanumeric characters with
  no spaces. You can also include underscores or any of the following characters: =,.@:/-
- `server_id`: A system-assigned unique identifier for a server that has this access
  assigned.

"""
describe_access(ExternalId, ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeAccess", Dict{String, Any}("ExternalId"=>ExternalId, "ServerId"=>ServerId); aws_config=aws_config)
describe_access(ExternalId, ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeAccess", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ExternalId"=>ExternalId, "ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    describe_security_policy(security_policy_name)
    describe_security_policy(security_policy_name, params::Dict{String,<:Any})

Describes the security policy that is attached to your file transfer protocol-enabled
server. The response contains a description of the security policy's properties. For more
information about security policies, see Working with security policies.

# Arguments
- `security_policy_name`: Specifies the name of the security policy that is attached to the
  server.

"""
describe_security_policy(SecurityPolicyName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeSecurityPolicy", Dict{String, Any}("SecurityPolicyName"=>SecurityPolicyName); aws_config=aws_config)
describe_security_policy(SecurityPolicyName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeSecurityPolicy", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("SecurityPolicyName"=>SecurityPolicyName), params)); aws_config=aws_config)

"""
    describe_server(server_id)
    describe_server(server_id, params::Dict{String,<:Any})

Describes a file transfer protocol-enabled server that you specify by passing the ServerId
parameter. The response contains a description of a server's properties. When you set
EndpointType to VPC, the response will contain the EndpointDetails.

# Arguments
- `server_id`: A system-assigned unique identifier for a server.

"""
describe_server(ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeServer", Dict{String, Any}("ServerId"=>ServerId); aws_config=aws_config)
describe_server(ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeServer", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    describe_user(server_id, user_name)
    describe_user(server_id, user_name, params::Dict{String,<:Any})

Describes the user assigned to the specific file transfer protocol-enabled server, as
identified by its ServerId property. The response from this call returns the properties of
the user associated with the ServerId value that was specified.

# Arguments
- `server_id`: A system-assigned unique identifier for a server that has this user assigned.
- `user_name`: The name of the user assigned to one or more servers. User names are part of
  the sign-in credentials to use the Amazon Web Services Transfer Family service and perform
  file transfer tasks.

"""
describe_user(ServerId, UserName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeUser", Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName); aws_config=aws_config)
describe_user(ServerId, UserName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("DescribeUser", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName), params)); aws_config=aws_config)

"""
    import_ssh_public_key(server_id, ssh_public_key_body, user_name)
    import_ssh_public_key(server_id, ssh_public_key_body, user_name, params::Dict{String,<:Any})

Adds a Secure Shell (SSH) public key to a user account identified by a UserName value
assigned to the specific file transfer protocol-enabled server, identified by ServerId. The
response returns the UserName value, the ServerId value, and the name of the SshPublicKeyId.

# Arguments
- `server_id`: A system-assigned unique identifier for a server.
- `ssh_public_key_body`: The public key portion of an SSH key pair.
- `user_name`: The name of the user account that is assigned to one or more servers.

"""
import_ssh_public_key(ServerId, SshPublicKeyBody, UserName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ImportSshPublicKey", Dict{String, Any}("ServerId"=>ServerId, "SshPublicKeyBody"=>SshPublicKeyBody, "UserName"=>UserName); aws_config=aws_config)
import_ssh_public_key(ServerId, SshPublicKeyBody, UserName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ImportSshPublicKey", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId, "SshPublicKeyBody"=>SshPublicKeyBody, "UserName"=>UserName), params)); aws_config=aws_config)

"""
    list_accesses(server_id)
    list_accesses(server_id, params::Dict{String,<:Any})

Lists the details for all the accesses you have on your server.

# Arguments
- `server_id`: A system-assigned unique identifier for a server that has users assigned to
  it.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the maximum number of access SIDs to return.
- `"NextToken"`: When you can get additional results from the ListAccesses call, a
  NextToken parameter is returned in the output. You can then pass in a subsequent command to
  the NextToken parameter to continue listing additional accesses.
"""
list_accesses(ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListAccesses", Dict{String, Any}("ServerId"=>ServerId); aws_config=aws_config)
list_accesses(ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListAccesses", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    list_security_policies()
    list_security_policies(params::Dict{String,<:Any})

Lists the security policies that are attached to your file transfer protocol-enabled
servers.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the number of security policies to return as a response to the
  ListSecurityPolicies query.
- `"NextToken"`: When additional results are obtained from the ListSecurityPolicies
  command, a NextToken parameter is returned in the output. You can then pass the NextToken
  parameter in a subsequent command to continue listing additional security policies.
"""
list_security_policies(; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListSecurityPolicies"; aws_config=aws_config)
list_security_policies(params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListSecurityPolicies", params; aws_config=aws_config)

"""
    list_servers()
    list_servers(params::Dict{String,<:Any})

Lists the file transfer protocol-enabled servers that are associated with your Amazon Web
Services account.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the number of servers to return as a response to the
  ListServers query.
- `"NextToken"`: When additional results are obtained from the ListServers command, a
  NextToken parameter is returned in the output. You can then pass the NextToken parameter in
  a subsequent command to continue listing additional servers.
"""
list_servers(; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListServers"; aws_config=aws_config)
list_servers(params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListServers", params; aws_config=aws_config)

"""
    list_tags_for_resource(arn)
    list_tags_for_resource(arn, params::Dict{String,<:Any})

Lists all of the tags associated with the Amazon Resource Name (ARN) that you specify. The
resource can be a user, server, or role.

# Arguments
- `arn`: Requests the tags associated with a particular Amazon Resource Name (ARN). An ARN
  is an identifier for a specific Amazon Web Services resource, such as a server, user, or
  role.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the number of tags to return as a response to the
  ListTagsForResource request.
- `"NextToken"`: When you request additional results from the ListTagsForResource
  operation, a NextToken parameter is returned in the input. You can then pass in a
  subsequent command to the NextToken parameter to continue listing additional tags.
"""
list_tags_for_resource(Arn; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListTagsForResource", Dict{String, Any}("Arn"=>Arn); aws_config=aws_config)
list_tags_for_resource(Arn, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListTagsForResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Arn"=>Arn), params)); aws_config=aws_config)

"""
    list_users(server_id)
    list_users(server_id, params::Dict{String,<:Any})

Lists the users for a file transfer protocol-enabled server that you specify by passing the
ServerId parameter.

# Arguments
- `server_id`: A system-assigned unique identifier for a server that has users assigned to
  it.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the number of users to return as a response to the ListUsers
  request.
- `"NextToken"`: When you can get additional results from the ListUsers call, a NextToken
  parameter is returned in the output. You can then pass in a subsequent command to the
  NextToken parameter to continue listing additional users.
"""
list_users(ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListUsers", Dict{String, Any}("ServerId"=>ServerId); aws_config=aws_config)
list_users(ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("ListUsers", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    start_server(server_id)
    start_server(server_id, params::Dict{String,<:Any})

Changes the state of a file transfer protocol-enabled server from OFFLINE to ONLINE. It has
no impact on a server that is already ONLINE. An ONLINE server can accept and process file
transfer jobs. The state of STARTING indicates that the server is in an intermediate state,
either not fully able to respond, or not fully online. The values of START_FAILED can
indicate an error condition. No response is returned from this call.

# Arguments
- `server_id`: A system-assigned unique identifier for a server that you start.

"""
start_server(ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("StartServer", Dict{String, Any}("ServerId"=>ServerId); aws_config=aws_config)
start_server(ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("StartServer", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    stop_server(server_id)
    stop_server(server_id, params::Dict{String,<:Any})

Changes the state of a file transfer protocol-enabled server from ONLINE to OFFLINE. An
OFFLINE server cannot accept and process file transfer jobs. Information tied to your
server, such as server and user properties, are not affected by stopping your server.
Stopping the server will not reduce or impact your file transfer protocol endpoint billing;
you must delete the server to stop being billed.  The state of STOPPING indicates that the
server is in an intermediate state, either not fully able to respond, or not fully offline.
The values of STOP_FAILED can indicate an error condition. No response is returned from
this call.

# Arguments
- `server_id`: A system-assigned unique identifier for a server that you stopped.

"""
stop_server(ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("StopServer", Dict{String, Any}("ServerId"=>ServerId); aws_config=aws_config)
stop_server(ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("StopServer", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    tag_resource(arn, tags)
    tag_resource(arn, tags, params::Dict{String,<:Any})

Attaches a key-value pair to a resource, as identified by its Amazon Resource Name (ARN).
Resources are users, servers, roles, and other entities. There is no response returned from
this call.

# Arguments
- `arn`: An Amazon Resource Name (ARN) for a specific Amazon Web Services resource, such as
  a server, user, or role.
- `tags`: Key-value pairs assigned to ARNs that you can use to group and search for
  resources by type. You can attach this metadata to user accounts for any purpose.

"""
tag_resource(Arn, Tags; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("TagResource", Dict{String, Any}("Arn"=>Arn, "Tags"=>Tags); aws_config=aws_config)
tag_resource(Arn, Tags, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("TagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Arn"=>Arn, "Tags"=>Tags), params)); aws_config=aws_config)

"""
    test_identity_provider(server_id, user_name)
    test_identity_provider(server_id, user_name, params::Dict{String,<:Any})

If the IdentityProviderType of a file transfer protocol-enabled server is
AWS_DIRECTORY_SERVICE or API_Gateway, tests whether your identity provider is set up
successfully. We highly recommend that you call this operation to test your authentication
method as soon as you create your server. By doing so, you can troubleshoot issues with the
identity provider integration to ensure that your users can successfully use the service.

# Arguments
- `server_id`: A system-assigned identifier for a specific server. That server's user
  authentication method is tested with a user name and password.
- `user_name`: The name of the user account to be tested.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"ServerProtocol"`: The type of file transfer protocol to be tested. The available
  protocols are:   Secure Shell (SSH) File Transfer Protocol (SFTP)   File Transfer Protocol
  Secure (FTPS)   File Transfer Protocol (FTP)
- `"SourceIp"`: The source IP address of the user account to be tested.
- `"UserPassword"`: The password of the user account to be tested.
"""
test_identity_provider(ServerId, UserName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("TestIdentityProvider", Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName); aws_config=aws_config)
test_identity_provider(ServerId, UserName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("TestIdentityProvider", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName), params)); aws_config=aws_config)

"""
    untag_resource(arn, tag_keys)
    untag_resource(arn, tag_keys, params::Dict{String,<:Any})

Detaches a key-value pair from a resource, as identified by its Amazon Resource Name (ARN).
Resources are users, servers, roles, and other entities. No response is returned from this
call.

# Arguments
- `arn`: The value of the resource that will have the tag removed. An Amazon Resource Name
  (ARN) is an identifier for a specific Amazon Web Services resource, such as a server, user,
  or role.
- `tag_keys`: TagKeys are key-value pairs assigned to ARNs that can be used to group and
  search for resources by type. This metadata can be attached to resources for any purpose.

"""
untag_resource(Arn, TagKeys; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UntagResource", Dict{String, Any}("Arn"=>Arn, "TagKeys"=>TagKeys); aws_config=aws_config)
untag_resource(Arn, TagKeys, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UntagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Arn"=>Arn, "TagKeys"=>TagKeys), params)); aws_config=aws_config)

"""
    update_access(external_id, server_id)
    update_access(external_id, server_id, params::Dict{String,<:Any})

Allows you to update parameters for the access specified in the ServerID and ExternalID
parameters.

# Arguments
- `external_id`: A unique identifier that is required to identify specific groups within
  your directory. The users of the group that you associate have access to your Amazon S3 or
  Amazon EFS resources over the enabled protocols using Amazon Web Services Transfer Family.
  If you know the group name, you can view the SID values by running the following command
  using Windows PowerShell.  Get-ADGroup -Filter {samAccountName -like \"YourGroupName*\"}
  -Properties * | Select SamAccountName,ObjectSid  In that command, replace YourGroupName
  with the name of your Active Directory group. The regex used to validate this parameter is
  a string of characters consisting of uppercase and lowercase alphanumeric characters with
  no spaces. You can also include underscores or any of the following characters: =,.@:/-
- `server_id`: A system-assigned unique identifier for a server instance. This is the
  specific server that you added your user to.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"HomeDirectory"`: The landing directory (folder) for a user when they log in to the
  server using the client. A HomeDirectory example is /bucket_name/home/mydirectory.
- `"HomeDirectoryMappings"`: Logical directory mappings that specify what Amazon S3 or
  Amazon EFS paths and keys should be visible to your user and how you want to make them
  visible. You must specify the Entry and Target pair, where Entry shows how the path is made
  visible and Target is the actual Amazon S3 or Amazon EFS path. If you only specify a
  target, it is displayed as is. You also must ensure that your Amazon Web Services Identity
  and Access Management (IAM) role provides access to paths in Target. This value can only be
  set when HomeDirectoryType is set to LOGICAL. The following is an Entry and Target pair
  example.  [ { \"Entry\": \"your-personal-report.pdf\", \"Target\":
  \"/bucket3/customized-reports/{transfer:UserName}.pdf\" } ]  In most cases, you can use
  this value instead of the scope-down policy to lock down your user to the designated home
  directory (\"chroot\"). To do this, you can set Entry to / and set Target to the
  HomeDirectory parameter value. The following is an Entry and Target pair example for
  chroot.  [ { \"Entry:\": \"/\", \"Target\": \"/bucket_name/home/mydirectory\" } ]   If the
  target of a logical directory entry does not exist in Amazon S3 or EFS, the entry is
  ignored. As a workaround, you can use the Amazon S3 API or EFS API to create 0 byte objects
  as place holders for your directory. If using the CLI, use the s3api or efsapi call instead
  of s3 or efs so you can use the put-object operation. For example, you use the following:
  aws s3api put-object --bucket bucketname --key path/to/folder/. Make sure that the end of
  the key name ends in a / for it to be considered a folder.
- `"HomeDirectoryType"`: The type of landing directory (folder) you want your users' home
  directory to be when they log into the server. If you set it to PATH, the user will see the
  absolute Amazon S3 bucket or EFS paths as is in their file transfer protocol clients. If
  you set it LOGICAL, you will need to provide mappings in the HomeDirectoryMappings for how
  you want to make Amazon S3 or EFS paths visible to your users.
- `"Policy"`: A scope-down policy for your user so that you can use the same IAM role
  across multiple users. This policy scopes down user access to portions of their Amazon S3
  bucket. Variables that you can use inside this policy include {Transfer:UserName},
  {Transfer:HomeDirectory}, and {Transfer:HomeBucket}.  This only applies when domain of
  ServerId is S3. Amazon EFS does not use scope down policy. For scope-down policies, Amazon
  Web ServicesTransfer Family stores the policy as a JSON blob, instead of the Amazon
  Resource Name (ARN) of the policy. You save the policy as a JSON blob and pass it in the
  Policy argument. For an example of a scope-down policy, see Example scope-down policy. For
  more information, see AssumeRole in the Amazon Web ServicesSecurity Token Service API
  Reference.
- `"PosixProfile"`:
- `"Role"`: Specifies the Amazon Resource Name (ARN) of the IAM role that controls your
  users' access to your Amazon S3 bucket or EFS file system. The policies attached to this
  role determine the level of access that you want to provide your users when transferring
  files into and out of your Amazon S3 bucket or EFS file system. The IAM role should also
  contain a trust relationship that allows the server to access your resources when servicing
  your users' transfer requests.
"""
update_access(ExternalId, ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UpdateAccess", Dict{String, Any}("ExternalId"=>ExternalId, "ServerId"=>ServerId); aws_config=aws_config)
update_access(ExternalId, ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UpdateAccess", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ExternalId"=>ExternalId, "ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    update_server(server_id)
    update_server(server_id, params::Dict{String,<:Any})

Updates the file transfer protocol-enabled server's properties after that server has been
created. The UpdateServer call returns the ServerId of the server you updated.

# Arguments
- `server_id`: A system-assigned unique identifier for a server instance that the user
  account is assigned to.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Certificate"`: The Amazon Resource Name (ARN) of the Amazon Web ServicesCertificate
  Manager (ACM) certificate. Required when Protocols is set to FTPS. To request a new public
  certificate, see Request a public certificate in the  Amazon Web ServicesCertificate
  Manager User Guide. To import an existing certificate into ACM, see Importing certificates
  into ACM in the  Amazon Web ServicesCertificate Manager User Guide. To request a private
  certificate to use FTPS through private IP addresses, see Request a private certificate in
  the  Amazon Web ServicesCertificate Manager User Guide. Certificates with the following
  cryptographic algorithms and key sizes are supported:   2048-bit RSA (RSA_2048)   4096-bit
  RSA (RSA_4096)   Elliptic Prime Curve 256 bit (EC_prime256v1)   Elliptic Prime Curve 384
  bit (EC_secp384r1)   Elliptic Prime Curve 521 bit (EC_secp521r1)    The certificate must be
  a valid SSL/TLS X.509 version 3 certificate with FQDN or IP address specified and
  information about the issuer.
- `"EndpointDetails"`: The virtual private cloud (VPC) endpoint settings that are
  configured for your server. When you host your endpoint within your VPC, you can make it
  accessible only to resources within your VPC, or you can attach Elastic IP addresses and
  make it accessible to clients over the internet. Your VPC's default security groups are
  automatically assigned to your endpoint.
- `"EndpointType"`: The type of endpoint that you want your server to use. You can choose
  to make your server's endpoint publicly accessible (PUBLIC) or host it inside your VPC.
  With an endpoint that is hosted in a VPC, you can restrict access to your server and
  resources only within your VPC or choose to make it internet facing by attaching Elastic IP
  addresses directly to it.   After May 19, 2021, you won't be able to create a server using
  EndpointType=VPC_ENDPOINT in your Amazon Web Servicesaccount if your account hasn't already
  done so before May 19, 2021. If you have already created servers with
  EndpointType=VPC_ENDPOINT in your Amazon Web Servicesaccount on or before May 19, 2021, you
  will not be affected. After this date, use EndpointType=VPC. For more information, see
  https://docs.aws.amazon.com/transfer/latest/userguide/create-server-in-vpc.html#deprecate-vp
  c-endpoint. It is recommended that you use VPC as the EndpointType. With this endpoint
  type, you have the option to directly associate up to three Elastic IPv4 addresses (BYO IP
  included) with your server's endpoint and use VPC security groups to restrict traffic by
  the client's public IP address. This is not possible with EndpointType set to VPC_ENDPOINT.
- `"HostKey"`: The RSA private key as generated by ssh-keygen -N \"\" -m PEM -f
  my-new-server-key.  If you aren't planning to migrate existing users from an existing
  server to a new server, don't update the host key. Accidentally changing a server's host
  key can be disruptive.  For more information, see Change the host key for your SFTP-enabled
  server in the Amazon Web ServicesTransfer Family User Guide.
- `"IdentityProviderDetails"`: An array containing all of the information required to call
  a customer's authentication API method.
- `"LoggingRole"`: Specifies the Amazon Resource Name (ARN) of the Amazon Web Services
  Identity and Access Management (IAM) role that allows a server to turn on Amazon CloudWatch
  logging for Amazon S3 or Amazon EFS events. When set, user activity can be viewed in your
  CloudWatch logs.
- `"ProtocolDetails"`:  The protocol settings that are configured for your server.   Use
  the PassiveIp parameter to indicate passive mode (for FTP and FTPS protocols). Enter a
  single dotted-quad IPv4 address, such as the external IP address of a firewall, router, or
  load balancer.
- `"Protocols"`: Specifies the file transfer protocol or protocols over which your file
  transfer protocol client can connect to your server's endpoint. The available protocols
  are:   Secure Shell (SSH) File Transfer Protocol (SFTP): File transfer over SSH   File
  Transfer Protocol Secure (FTPS): File transfer with TLS encryption   File Transfer Protocol
  (FTP): Unencrypted file transfer    If you select FTPS, you must choose a certificate
  stored in Amazon Web ServicesCertificate Manager (ACM) which will be used to identify your
  server when clients connect to it over FTPS. If Protocol includes either FTP or FTPS, then
  the EndpointType must be VPC and the IdentityProviderType must be AWS_DIRECTORY_SERVICE or
  API_GATEWAY. If Protocol includes FTP, then AddressAllocationIds cannot be associated. If
  Protocol is set only to SFTP, the EndpointType can be set to PUBLIC and the
  IdentityProviderType can be set to SERVICE_MANAGED.
- `"SecurityPolicyName"`: Specifies the name of the security policy that is attached to the
  server.
"""
update_server(ServerId; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UpdateServer", Dict{String, Any}("ServerId"=>ServerId); aws_config=aws_config)
update_server(ServerId, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UpdateServer", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId), params)); aws_config=aws_config)

"""
    update_user(server_id, user_name)
    update_user(server_id, user_name, params::Dict{String,<:Any})

Assigns new properties to a user. Parameters you pass modify any or all of the following:
the home directory, role, and policy for the UserName and ServerId you specify. The
response returns the ServerId and the UserName for the updated user.

# Arguments
- `server_id`: A system-assigned unique identifier for a server instance that the user
  account is assigned to.
- `user_name`: A unique string that identifies a user and is associated with a server as
  specified by the ServerId. This user name must be a minimum of 3 and a maximum of 100
  characters long. The following are valid characters: a-z, A-Z, 0-9, underscore '_', hyphen
  '-', period '.', and at sign '@'. The user name can't start with a hyphen, period, or at
  sign.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"HomeDirectory"`: The landing directory (folder) for a user when they log in to the
  server using the client. A HomeDirectory example is /bucket_name/home/mydirectory.
- `"HomeDirectoryMappings"`: Logical directory mappings that specify what Amazon S3 or
  Amazon EFS paths and keys should be visible to your user and how you want to make them
  visible. You must specify the Entry and Target pair, where Entry shows how the path is made
  visible and Target is the actual Amazon S3 or Amazon EFS path. If you only specify a
  target, it is displayed as is. You also must ensure that your Amazon Web Services Identity
  and Access Management (IAM) role provides access to paths in Target. This value can only be
  set when HomeDirectoryType is set to LOGICAL. The following is an Entry and Target pair
  example.  [ { \"Entry\": \"your-personal-report.pdf\", \"Target\":
  \"/bucket3/customized-reports/{transfer:UserName}.pdf\" } ]  In most cases, you can use
  this value instead of the scope-down policy to lock down your user to the designated home
  directory (\"chroot\"). To do this, you can set Entry to '/' and set Target to the
  HomeDirectory parameter value. The following is an Entry and Target pair example for
  chroot.  [ { \"Entry:\": \"/\", \"Target\": \"/bucket_name/home/mydirectory\" } ]   If the
  target of a logical directory entry does not exist in Amazon S3 or EFS, the entry is
  ignored. As a workaround, you can use the Amazon S3 API or EFS API to create 0 byte objects
  as place holders for your directory. If using the CLI, use the s3api or efsapi call instead
  of s3 or efs so you can use the put-object operation. For example, you use the following:
  aws s3api put-object --bucket bucketname --key path/to/folder/. Make sure that the end of
  the key name ends in a / for it to be considered a folder.
- `"HomeDirectoryType"`: The type of landing directory (folder) you want your users' home
  directory to be when they log into the server. If you set it to PATH, the user will see the
  absolute Amazon S3 bucket or EFS paths as is in their file transfer protocol clients. If
  you set it LOGICAL, you will need to provide mappings in the HomeDirectoryMappings for how
  you want to make Amazon S3 or EFS paths visible to your users.
- `"Policy"`: A scope-down policy for your user so that you can use the same IAM role
  across multiple users. This policy scopes down user access to portions of their Amazon S3
  bucket. Variables that you can use inside this policy include {Transfer:UserName},
  {Transfer:HomeDirectory}, and {Transfer:HomeBucket}.  This only applies when domain of
  ServerId is S3. Amazon EFS does not use scope-down policies. For scope-down policies,
  Amazon Web ServicesTransfer Family stores the policy as a JSON blob, instead of the Amazon
  Resource Name (ARN) of the policy. You save the policy as a JSON blob and pass it in the
  Policy argument. For an example of a scope-down policy, see Creating a scope-down policy.
  For more information, see AssumeRole in the Amazon Web Services Security Token Service API
  Reference.
- `"PosixProfile"`: Specifies the full POSIX identity, including user ID (Uid), group ID
  (Gid), and any secondary groups IDs (SecondaryGids), that controls your users' access to
  your Amazon Elastic File Systems (Amazon EFS). The POSIX permissions that are set on files
  and directories in your file system determines the level of access your users get when
  transferring files into and out of your Amazon EFS file systems.
- `"Role"`: Specifies the Amazon Resource Name (ARN) of the IAM role that controls your
  users' access to your Amazon S3 bucket or EFS file system. The policies attached to this
  role determine the level of access that you want to provide your users when transferring
  files into and out of your Amazon S3 bucket or EFS file system. The IAM role should also
  contain a trust relationship that allows the server to access your resources when servicing
  your users' transfer requests.
"""
update_user(ServerId, UserName; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UpdateUser", Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName); aws_config=aws_config)
update_user(ServerId, UserName, params::AbstractDict{String}; aws_config::AbstractAWSConfig=global_aws_config()) = transfer("UpdateUser", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ServerId"=>ServerId, "UserName"=>UserName), params)); aws_config=aws_config)


text:
module MC

using Shannon

ϵ = 0.0000001

# data[w', w, a] -> D_KL(p(w'|w,a)||p(w'|a)) = MI(W';W|A)
function MC_W(data::Matrix{Int64}; base=2)
  @assert (size(data)[2] == 2) "MC_W required two columns (W,A)."
  w2      = data[2:end,1]
  w1      = data[1:end-1,1]
  a1      = data[1:end-1,2]
  m       = maximum(data[:,1])
  pw2w1a1 = Shannon.fe3p(hcat(w2,w1,a1))
  return CMI(pw2w1a1, base=base) / log(base,m)
end

# data[w', w, a] -> D_KL(p(w'|w,a)||p(w'|w))
function MC_A(data::Matrix{Int64}; base=2)
  @assert (size(data)[2] == 2) "MC_W required two columns (W,A)."
  w2      = data[2:end,1]
  w1      = data[1:end-1,1]
  a1      = data[1:end-1,2]
  m       = maximum(data[:,1])
  pw2a1w1 = Shannon.fe3p(hcat(w2,a1,w1))
  return 1.0 - (CMI(pw2a1w1, base=base) / log(base,m))
end

# CA = ∑ p(s,a) p(s'|do(a)) log (p(s'|do(a)) / p(s'|do(s)))
function C_A(data::Matrix{Int64}; base=2)
  @assert (size(data)[2] == 2) "C_A required two columns (S,A)."
  s_max   = maximum(data[:,1])
  a_max   = maximum(data[:,2])
  s2      = data[2:end,1]
  s1      = data[1:end-1,1]
  a1      = data[1:end-1,2]
  m       = maximum(data[:,1])
  ps2s1a1 = Shannon.fe3p(hcat(s2,s1,a1))

  ps1a1   = sum(ps2s1a1, 1)
  pa1     = sum(ps2s1a1, (1,2))
  ps1     = sum(ps2s1a1, (1,3))

  # p(s'|s,a) = p(s',s,a) / p(s,a)
  ps2_c_s1a1 = zeros(size(ps2s1a1))
  for s2_index = 1:s_max
    for s1_index = 1:s_max
      for a1_index = 1:a_max
        if abs(ps1a1[1, s1_index, a1_index]) > ϵ
          ps2_c_s1a1[s2_index, s1_index, a1_index] = ps2s1a1[s2_index, s1_index, a1_index] / ps1a1[1, s1_index, a1_index]
        end
      end
    end
  end

  # p(s'| do(a)) = ∑ p(s'|s,a) * p(s)
  ps2_do_a1 = zeros(s_max, a_max)
  for s2_index = 1:s_max
    for a1_index = 1:a_max
      for s1_index = 1:s_max
        ps2_do_a1[s2_index, a1_index] = ps2_do_a1[s2_index, a1_index] + ps2_c_s1a1[s2_index, s1_index, a1_index] * ps1[1,s1_index, 1]
      end
    end
  end

  # p(a|s) = p(s,a) / p(s)
  pa1_c_s1 = zeros(a_max, s_max)
  for a1_index = 1:a_max
    for s1_index = 1:s_max
      if abs(ps1[s1_index]) > ϵ
        pa1_c_s1[a1_index, s1_index] = ps1a1[1, s1_index, a1_index] / ps1[1, s1_index, 1]
      end
    end
  end

  # p(s'|do(s)) = ∑ p(a|s) p(s'|do(a))
  ps2_do_s1 = zeros(s_max, s_max)
  for s2_index = 1:s_max
    for s1_index = 1:s_max
      for a1_index = 1:a_max
        ps2_do_s1[s2_index, s1_index] = ps2_do_s1[s2_index, s1_index] + pa1_c_s1[a1_index, s1_index] * ps2_do_a1[s2_index, a1_index]
      end
    end
  end

  # CA = ∑ p(s,a) p(s'|do(a)) log (p(s'|do(a)) / p(s'|do(s)))
  r = 0.0
  for s2_index = 1:s_max
    for s1_index = 1:s_max
      for a1_index = 1:a_max
        if abs(ps1a1[1, s1_index, a1_index]) > ϵ && abs(ps2_do_a1[s2_index, a1_index]) > ϵ && abs(ps2_do_s1[s2_index, s1_index]) > ϵ
          r = r + ps1a1[1, s1_index, a1_index] * ps2_do_a1[s2_index, a1_index] * (log(base, ps2_do_a1[s2_index, a1_index]) - log(base, ps2_do_s1[s2_index, s1_index]))
        end
      end
    end
  end
  return r / log(base, s_max)
end

# data[s', s, a] -> D_KL(p(s'|s,a)||p(s'|a))
function C_W(data::Matrix{Int64}; base=2)
  @assert (size(data)[2] == 2) "C_A required two columns (S,A)."
  s_max   = maximum(data[:,1])
  a_max   = maximum(data[:,2])
  s2      = data[2:end,1]
  s1      = data[1:end-1,1]
  a1      = data[1:end-1,2]
  m       = maximum(data[:,1])
  ps2s1a1 = Shannon.fe3p(hcat(s2,s1,a1))

  ps1     = sum(ps2s1a1, (1,3))
  pa1     = sum(ps2s1a1, (1,2))
  ps2s1   = sum(ps2s1a1, 3)
  ps1a1   = sum(ps2s1a1, 1)
  ps2a1   = sum(ps2s1a1, 2)

  # p(s'|s) = p(s',s) / p(s)
  ps2_c_s1 = zeros(s_max, s_max)
  for s2_index = 1:s_max
    for s1_index = 1:s_max
      if abs(ps1[s1_index]) > ϵ
        ps2_c_s1[s2_index, s1_index] = ps2s1[s2_index, s1_index] / ps1[s1_index]
      end
    end
  end

  # p(s'|a) = p(s',a) / p(a)
  ps2_c_a1 = zeros(s_max, a_max)
  for s2_index = 1:s_max
    for a1_index = 1:a_max
      if abs(pa1[a1_index]) > ϵ
        ps2_c_a1[s2_index, a1_index] = ps2a1[s2_index, 1, a1_index] / pa1[a1_index]
      end
    end
  end

  # p(a|s) = p(a,s) / p(s)
  pa1_c_s1 = zeros(a_max, s_max)
  for a1_index = 1:a_max
    for s1_index = 1:s_max
      if abs(ps1[1, s1_index, 1]) > ϵ
        pa1_c_s1[a1_index, s1_index] = ps1a1[1, s1_index, a1_index] / ps1[1, s1_index, 1]
      end
    end
  end

  #̂̂p^(s'|s) = ∑ p(s'|a) p(a|s)
  phat_s2_c_s1 = zeros(s_max, s_max)
  for s2_index = 1:s_max
    for s1_index = 1:s_max
      for a1_index = 1:a_max
        phat_s2_c_s1[s2_index, s1_index] = ps2_c_a1[s2_index, a1_index] * pa1_c_s1[a1_index, s1_index]
      end
    end
  end

  r = 0
  for s1_index = 1:s_max
    for s2_index = 1:s_max
      if abs(ps2s1[s2_index, s1_index, 1]) > ϵ && abs(ps2_c_s1[s2_index, s1_index]) > ϵ && abs(phat_s2_c_s1[s2_index, s1_index]) > ϵ
        r = r + ps2s1[s2_index, s1_index, 1] * (log(base, ps2_c_s1[s2_index, s1_index]) - log(base, phat_s2_c_s1[s2_index, s1_index]))
      end
    end
  end

  return r / log(base, s_max)
end

# Morphological Computation
function quantify(data::Matrix{Int64}; base=2, mode="MC A", pseudocount=0)
  modes      = ["MC A",
  "MC W",
  "ASOC A",
  "ASOC W",
  "C A",
  "C W"
  ]
  umodes     = map(x->uppercase(x), modes)
  known_mode = uppercase(mode) in umodes
  pmodes     = map(x->", $x",modes)
  pmodes     = foldl(*, pmodes[1][3:end], pmodes[2:end])
  @assert known_mode "Mode may be any of the following: [$pmodes]."

  if uppercase(mode) in umodes[1:2]
    return MC_A(data, base)
  elseif uppercase(mode) in umodes[3:4]
    return MC_W(data, base)
  elseif uppercase(mode) in umodes[5]
    return MC_A(data, base)
  elseif uppercase(mode) in umodes[6]
    return MC_W(data, base)
  elseif uppercase(mode) in umodes[7]
    return C_A(data, base)
  elseif uppercase(mode) in umodes[8]
    return C_W(data, base)
  end
end

end # module

ext: jl


text:
module FnMatch

export translate, fnmatchcase, fnfilter

"""
    fnmatchcase(name, pattern)

Test whether `name` matches `pattern`.

Patterns are Unix shell style:

    *       matches everything
    ?       matches any single character
    [seq]   matches any character in seq
    [!seq]  matches any char not in seq

An initial period in `name` is not special.
Both `name` and `pattern` are case sensitive regardless of the
operating system.

"""
function fnmatchcase(name, pat)
    re = translate(pat)
    return match(re, name) !== nothing
end

"""
    fnfilter(names, pattern)

Returns subset of `names` taht match `pattern`.

Equivalent to `[f for f in names if fnmatchcase(f)]` except optimized for batch
processing.

"""
function fnfilter(names, pat)
    re = translate(pat)
    return [n for n in names if match(re, n) !== nothing]
end

"""
    translate(pattern)

Translate a shell `pattern` to a regular expression.

There is no way to quote meta-characters.

"""
function translate(pat)
    i, n = firstindex(pat), lastindex(pat)
    res = ""
    while i <= n
        c = pat[i]
        i = nextind(pat, i)
        @debug "Start while" c i res
        if c == '*'
            res *= ".*"
        elseif c == '?'
            res *= "."
        elseif c == '['
            j = i
            if j <= n && pat[j] == '!'
                j = nextind(pat, j)
            end
            if j <= n && pat[j] == ']'
                j = nextind(pat, j)
            end
            while j <= n && pat[j] != ']'
                j = nextind(pat, j)
            end
            if j > n
                res *= "\\["
            else
                stuff = pat[i:prevind(pat, j)]
                @debug "in bracket" stuff
                if !occursin("--", stuff)
                    stuff = replace(stuff, "\\" => "\\\\")
                else
                    chunks = []
                    k = pat[i] == '!' ? nextind(pat, i, 2) : nextind(pat, i)
                    while true
                        f = findnext("-", pat, k)
                        if f === nothing || first(f) > j
                            break
                        end
                        k = first(f)
                        push!(chunks, pat[i:prevind(pat, k)])
                        i = nextind(pat, k)
                        k = nextind(pat, k, 3)
                    end
                    push!(chunks, pat[i:prevind(pat, j)])
                    # Escape backslashes and hyphens for set difference (--).
                    # Hyphens that create ranges shouldn't be escaped.
                    stuff = join(
                        map(chunks) do s
                            replace(replace(s, "\\" => "\\\\"), "-" => "\\-")
                        end,
                        "-",
                    )
                end
                # Escape set operations (&&, ~~, and ||).
                stuff = replace(stuff, r"([&~|])" => s"\\\1")
                i = nextind(pat, j)
                if first(stuff) == '!'
                    stuff_i2 = nextind(stuff, firstindex(stuff))
                    stuff = "^" * stuff[stuff_i2:end]
                elseif first(stuff) in ('^', '[')
                    stuff = "\\" * stuff
                end
                res = "$res[$stuff]"
            end
        else
            res *= escape(c)
        end
    end
    # In the reference implement of Python fnmatch there is not "\A" to start
    # the regex because Python's regex match only matches from the beginning by
    # default.
    Regex("\\A(?s:$(res))\\Z")
end

"""
    escape(character)

Returns `character` as string, escaping if it is special in `Regex`.

"""
function escape(c)
    # List of Regex special characters from
    #
    # https://github.com/python/cpython/blob/3.8/Lib/re.py#L261
    #
    # SPECIAL_CHARS
    # closing ')', '}' and ']'
    # '-' (a range in character set)
    # '&', '~', (extended character set operations)
    # '#' (comment) and WHITESPACE (ignored) in verbose mode
    # _special_chars_map = {
    #     i: '\\' + chr(i) for i in b'()[]{}?*+-|^$\\.&~# \t\n\r\v\f'}
    for special in "()[]{}?*+-|^\$\\.&~# \t\r\n\v\f"
        if c == special
            return string('\\', c)
        end
    end
    return string(c)
end

end # module


text:
# mcmcTools.jl

export adapt_cΣ, deepcopyFields, etr;

"""
    adapt_cΣ(accpt_rate, cΣ, tries, accpt_bnds=[0.23, 0.40], adjust=[0.5, 2.0]
"""
function adapt_cΣ(accpt_rate::T, cΣ::Matrix{T}, tries::Int,
  accpt_bnds::Vector{T}=[0.23, 0.40], adjust::Vector{T}=[0.5, 2.0]) where T <: Real

  pass = true
  cΣ_out = cΣ

  too_low = accpt_rate < accpt_bnds[1]
  too_high = accpt_rate > accpt_bnds[2]

  if too_low
    pass = false
    cΣ_out = adjust[1] .* cΣ
  elseif too_high
    pass = false
    cΣ_out = adjust[2] .* cΣ
  end

  return (pass, tries+1, cΣ_out)
end

## from Arthur Lui
function deepcopyFields(state::T, fields::Vector{Symbol}) where T
  substate = Dict{Symbol, Any}()

  for field in fields
    substate[field] = deepcopy(getfield(state, field))
  end

  return substate
end

## estimate time remaining
function etr(timestart::DateTime, n_keep::Int, thin::Int, outfilename::String)
    timeendburn = now()
    durperiter = (timeendburn - timestart).value / 1.0e5 # in milliseconds
    milsecremaining = durperiter * (n_keep * thin)
    estimatedfinish = now() + Dates.Millisecond(Int64(round(milsecremaining)))
    report_file = open(outfilename, "a+")
    write(report_file, "Completed burn-in at $(durperiter/1.0e3*1000.0) seconds per 1000 iterations \n
      $(durperiter/1.0e3/60.0*1000.0) minutes per 1000 iterations \n
      $(durperiter/1.0e3/60.0/60.0*1000.0) hours per 1000 iterations \n
      estimated completion time $(estimatedfinish)")
    close(report_file)
end
function etr(timestart::DateTime; n_iter_timed::Int, n_keep::Int, thin::Int, outfilename::String)
    timeendburn = now()
    durperiter = (timeendburn - timestart).value / float(n_iter_timed) # in milliseconds
    milsecremaining = durperiter * (n_keep * thin)
    estimatedfinish = now() + Dates.Millisecond(Int64(round(milsecremaining)))
    report_file = open(outfilename, "a+")
    write(report_file, "Completed burn-in at $(durperiter/1.0e3*1000.0) seconds per 1000 iterations \n
      $(durperiter/1.0e3/60.0*1000.0) minutes per 1000 iterations \n
      $(durperiter/1.0e3/60.0/60.0*1000.0) hours per 1000 iterations \n
      estimated completion time $(estimatedfinish)")
    close(report_file)
end


text:
mutable struct Gaussian_Pyramid
    dims::Array{Tuple{Int64,Int64},1}
    imgs::Array{Array{Float64,2},1}
    kerns::Array{Tuple{KernelFactors.ReshapedOneD,KernelFactors.ReshapedOneD},1}
end

function Gaussian_Pyramid(in_sz::Tuple)
    Gaussian_Pyramid([in_sz],[zeros(Float64,in_sz)],
        Array{Tuple{KernelFactors.ReshapedOneD,KernelFactors.ReshapedOneD},1}())
end

function Gaussian_Pyramid(in_sz::Tuple,out_sz::Tuple)
    mygauss=Gaussian_Pyramid(in_sz)

    if (in_sz[1] * in_sz[2]) > (out_sz[1] * out_sz[2])

        println("Downsampling pyramid")

        new_sz = (div(in_sz[1],2),div(in_sz[2],2))
        #push!(mygauss.dims,new_sz)
        #push!(mygauss.imgs,zeros(Float64,new_sz))
        #push!(mygauss.kerns,make_gaussian_kernel(in_sz,new_sz))
        old_sz = in_sz

        while ((new_sz[1] > out_sz[1])&&(new_sz[2] > out_sz[2]))

            push!(mygauss.dims,new_sz)
            push!(mygauss.imgs,zeros(Float64,new_sz))
            push!(mygauss.kerns,make_gaussian_kernel(old_sz,new_sz))

            old_sz = new_sz
            new_sz = (div(new_sz[1],2),div(new_sz[2],2))

        end
        if mygauss.dims[end] != out_sz
            push!(mygauss.dims,out_sz)
            push!(mygauss.imgs,zeros(Float64,out_sz))
            push!(mygauss.kerns,make_gaussian_kernel(new_sz,out_sz))
        end

    else
        println("Upsampling pyramid")

        new_sz = (in_sz[1]*2,in_sz[2]*2)
        old_sz = in_sz

        #push!(mygauss.dims,new_sz)
        #push!(mygauss.imgs,zeros(Float64,new_sz))
        #push!(mygauss.kerns,make_gaussian_kernel(in_sz,new_sz))

        while ((new_sz[1] <= out_sz[1]) && (new_sz[2] <= out_sz[2]))

            push!(mygauss.dims,new_sz)
            push!(mygauss.imgs,zeros(Float64,new_sz))
            push!(mygauss.kerns,make_gaussian_kernel(old_sz,new_sz))
            old_sz = new_sz
            new_sz = (new_sz[1]*2,new_sz[2]*2)
        end
        if mygauss.dims[end] != out_sz
            push!(mygauss.dims,out_sz)
            push!(mygauss.imgs,zeros(Float64,out_sz))
            push!(mygauss.kerns,make_gaussian_kernel(new_sz,out_sz))
        end
    end
    mygauss
end

function lowpass_filter_resize(gauss::Gaussian_Pyramid)

    for k=1:length(gauss.kerns)
       lowpass_filter_resize!(gauss.imgs[k],gauss.imgs[k+1],gauss.kerns[k])
    end
end

function lowpass_filter_resize(gauss::Gaussian_Pyramid,input::AbstractArray{Float64,2},output::AbstractArray{Float64,2})

   gauss.imgs[1][:] = input
    lowpass_filter_resize(gauss)
    output[:] = gauss.imgs[end]

    nothing
end


text:
abstract type Model end

struct TemplateModel <: Model
	n::Int # state dimension
	m::Int # control dimension
	d::Int # disturbance dimension

	# additional model parameters ...
end

function f(model::TemplateModel, x, u, w)
	# continuous-time dynamics
	nothing
end

function k(model::TemplateModel, x)
	# kinematics
	nothing
end

model = TemplateModel(0, 0, 0)

state_output(model, x) = x
state_output_idx(model, idx) = idx
control_output(model, u) = u

"""
	propagate dynamics with implicit integrator
"""
function propagate_dynamics(model, x, u, w, h, t)
    d(z) = fd(model, z, x, u, w, h, t)

    y = copy(x)
    r = d(y)

    iter = 0

    while norm(r, 2) > 1.0e-8 && iter < 10
        ∇r = ForwardDiff.jacobian(d, y)
        Δy = -1.0 * ∇r \ r

        α = 1.0

        while α > 1.0e-8
            ŷ = y + α * Δy
            r̂ = d(ŷ)
            if norm(r̂) < norm(r)
                y = ŷ
                r = r̂
                break
            else
                α *= 0.5
            end
        end

        iter += 1
    end

    # @show norm(r)
    # @show iter
    return y
end

function propagate_dynamics_jacobian(model, x, u, w, h, t)
	y = propagate_dynamics(model, x, u, w, h, t)

    dy(z) = fd(model, z, x, u, w, h, t)
	dx(z) = fd(model, y, z, u, w, h, t)
	du(z) = fd(model, y, x, z, w, h, t)

	Dy = ForwardDiff.jacobian(dy, y)
	A = -1.0 * Dy \ ForwardDiff.jacobian(dx, x)
	B = -1.0 * Dy \ ForwardDiff.jacobian(du, u)

	return y, A, B
end

max_stars_repo_name: zacmanchester/DirectMotionPlanning

max_issues_repo_name: zacmanchester/DirectMotionPlanning

max_forks_repo_name: zacmanchester/DirectMotionPlanning


text:
using Test, Random, Distributed, Statistics
Random.seed!(0)
using Hyperopt, Plots
f(a,b=true;c=10) = sum(@. 100 + (a-3)^2 + (b ? 10 : 20) + (c-100)^2) # This function must be defined outside testsets to avoid scoping issues

@testset "Hyperopt" begin

    @testset "Random sampler" begin
        @info "Testing Random sampler"

        # res = map(1:30) do i
        #     @info("Iteration ", i)
        hor = @hyperopt for i=100, sampler=RandomSampler(), a = LinRange(1,5,50), b = [true, false], c = exp10.(LinRange(-1,3,50))
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c)
        end
        @test minimum(hor)[2] < 300
        @test length(hor.history) == 100
        @test length(hor.results) == 100
        @test all(hor.history) do h
            all(hi in hor.candidates[i] for (i,hi) in enumerate(h))
        end

        printmax(hor)
        printmin(hor)
    end

    @testset "Latin hypercube" begin
        @info "Testing Latin hypercube"
        hol = @hyperopt for i=100, sampler=LHSampler(), a = LinRange(1,5,100), b = repeat([true, false],50), c = exp10.(LinRange(-1,3,100))
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c)
        end
        @test minimum(hol)[2] < 300

        hocl = @hyperopt for i=100, sampler=CLHSampler(dims=[Continuous(),Categorical(2),Continuous()]), a = LinRange(1,5,100), b = [true, false], c = exp10.(LinRange(-1,3,100))
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c)
        end
        @test minimum(hocl)[2] < 300

    end

    @testset "GP sampler" begin
        @info "Testing GP sampler"

        results = map(1:20) do _
            hogp = @hyperopt for i=50, sampler=GPSampler(Min), a = LinRange(1,5,100), b = repeat([true, false]',50)[:], c = exp10.(LinRange(-1,3,100))
                # println(i, "\t", a, "\t", b, "\t", c)
                f(a,Bool(b),c=c)
            end
            minimum(hogp)[2]
        end

        @show mean(results)
        @show mean(results .< 300)

        @test mean(results) < 300
        @test mean(results .< 300) > 0.8

        hogp = @hyperopt for i=50, sampler=GPSampler(Min), a = LinRange(1,5,100), b = repeat([true, false]',50)[:], c = exp10.(LinRange(-1,3,100))
            f(a,Bool(b),c=c)
        end
        minimum(hogp)[2]

        plot(hogp.sampler)
        plot(hogp)

        # One dimension case
        hogp = @hyperopt for i=50, sampler=GPSampler(Min), a = LinRange(1,5,100)
            a
        end
        plot(hogp.sampler)
        plot(hogp)

    end

    @testset "Error handling" begin
        @info "Testing Error handling"

        @test_throws ArgumentError @hyperopt for i=100, sampler=LHSampler(), a = LinRange(1,5,10), b = repeat([true, false],50), c = exp10.(LinRange(-1,3,100))
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c)
        end

        @test_throws ArgumentError @hyperopt for i=100, sampler=CLHSampler(dims=[Continuous(),Categorical(2),Continuous()]), a = LinRange(1,5,99), b = [true, false], c = exp10.(LinRange(-1,3,100))
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c)
        end
    end

    #     minimum.((hor,hob,hot,hof))
    # end

    # hor,hob,hot,hof = getindex.(res,1),getindex.(res,2),getindex.(res,3),getindex.(res,4)
    # hor,hob,hot,hof = getindex.(hor,2),getindex.(hob,2),getindex.(hot,2),getindex.(hof,2)
    # res = [hor hob hot hof]
    # sum(log.(res), dims=1)
    @testset "Manual" begin
        @info "Testing Manual"

        ho = Hyperoptimizer(10, a = range(1, stop=2, length=50), b = [true, false], c = randn(100))
        for (i,a,b,c) in ho
            println(i, "\t", a, "\t", b, "\t", c)
        end

    end

    @testset "Categorical" begin
        @info "Testing Categorical"

        ho = @hyperopt for i=100, a = LinRange(1,5,50), b = [true, false], c = exp10.(LinRange(-1,3,50)), d = [tanh, exp]
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c) + d(a)
        end
    end

    @testset "Utils" begin
        @test Hyperopt.islogspace(exp10.(LinRange(-3, 3, 10)))
        @test !Hyperopt.islogspace(LinRange(-3, 3, 10))
        @test !Hyperopt.islogspace([true, false])
    end

    @testset "Hyperband" begin
        using Optim
        f(a;c=10) = sum(@. 100 + (a-3)^2 + (c-100)^2)
        Hyperopt.Hyperband(10)
        # res = map(1:30) do i
        #     @info("Iteration ", i)
        hohb = @hyperopt for i=18, sampler=Hyperband(R=50, η=3, inner=RandomSampler()), a = LinRange(1,5,1800), c = exp10.(LinRange(-1,3,1800))
            # println(i, "\t", a, "\t", b, "\t", c)
            if !(state === nothing)
                a,c = state
            end
            res = Optim.optimize(x->f(x[1],c=x[2]), [a,c], SimulatedAnnealing(), Optim.Options(f_calls_limit=i))
            Optim.minimum(res), Optim.minimizer(res)
        end
        @test minimum(hohb)[2] < 300

    end

    @testset "Parallel" begin
        @info "Testing Parallel"

        horp = @phyperopt for i=100, sampler=RandomSampler(), a = LinRange(1,5,50), b = [true, false], c = exp10.(LinRange(-1,3,50))
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c)
        end
        @test minimum(horp)[2] < 300
        @test length(horp.history) == 100
        @test length(horp.results) == 100

        Distributed.nworkers() == 0 && addprocs(2)
        @everywhere using Hyperopt
        @everywhere f(a,b=true;c=10) = sum(@. 100 + (a-3)^2 + (b ? 10 : 20) + (c-100)^2)
        horp = @phyperopt for i=100, sampler=RandomSampler(), a = LinRange(1,5,50), b = [true, false], c = exp10.(LinRange(-1,3,50))
            # println(i, "\t", a, "\t", b, "\t", c)
            f(a,b,c=c)
        end
        @test minimum(horp)[2] < 300
        @test length(horp.history) == 100
        @test length(horp.results) == 100

    end

end


text:
#using ITensorsGateEvolution
#using ITensors
using Test

@testset "ITensorsGateEvolution.jl" begin
  include("movesites.jl")
  include("apply.jl")
end


text:
using ModelingToolkit
using DifferentialEquations
#using TensorOperations
#using Einsum

x=1
nAge=9
nHS=8
nIso=4
nI=4
nTest=4

# yeah
include("utils.jl")
param=(nAge=nAge, nHS=nHS, nIso=nIso, nTest=nTest)

# include("diseaseProg.jl")
include("diseaseProg_basic.jl")

# todo replace with inital state tensor
stateTensor= 100*ones((nAge, nHS, nIso, nTest))
dstateTensor=100*ones((nAge, nHS, nIso, nTest))
trTensor_complete = zeros((nAge, nHS, nIso, nTest, nHS, nIso, nTest))
  #@einsum dstateTensor[i,m,n,p]:=stateTensor[i,j,k,l]* trTensor_complete[i,j,k,l,m,n,p]

  # if we want to use TensorOperations which seems maintained this is the way to go
  # does not work @tensor ddstateTensor[1,m,n,p]:=stateTensor[1,j,k,l]* trTensor_complete[1,j,k,l,m,n,p]
  # for i=1:nAge
  #
  #   v=view(ddstateTensor,i,:,:,:)
  #   @tensor v[m,n,p]=stateTensor[1,j,k,l]* trTensor_complete[1,j,k,l,m,n,p]
  # end
ssize=(nAge, nHS, nIso, nTest)

# Playground
# #dydt = np.einsum('ijkl,ijklmnp->imnp', stateTensor, trTensor_complete) # contract the HS axis, keep age
# A=ones((3,3))
# b=ones(3)
# C=ones((3,3))
# @einsum C[1,i]=A[i,j]*b[j]
# c=view(C,1,:)

# # https://tutorials.sciml.ai/html/introduction/03-optimizing_diffeq_code.html
# c[:]=zeros(3)
# C

using PyCall
using Test

PyCall.pyimport_conda("pandas", "pandas")
PyCall.pyimport_conda("numpy", "numpy")
PyCall.pyimport_conda("scipy", "scipy")
PyCall.pyimport_conda("dask", "dask")
PyCall.pyimport_conda("cloudpickle", "cloudpickle")
PyCall.pyimport_conda("distributed", "distributed")
PyCall.pyimport_conda("xlrd", "xlrd")

py"""
import os
# print(os.getcwd())
exec(open(os.path.join(os.getcwd(), "coexist_python", "model_COVID_testing-ODEoutput.py")).read())
"""

function f_vec!(dstateTensor_vec,stateTensor_vec,p,t)
  trTensor_diseaseProgression=trFunc_diseaseProgression(param...)
  stateTensor=reshape(stateTensor_vec,(nTest, nIso, nHS, nAge))
  dstateTensor = zeros(size(stateTensor)...)
  trTensor_complete = zeros((nTest, nIso, nHS, nTest, nIso, nHS, nAge))

  # Efficient Python version
  # # Get disease condition updates with no isolation or test transition ("diagonal along those")
  #    for k1 in [0,1,2,3]:
  #        np.einsum('ijlml->ijlm',
  #            trTensor_complete[:,:,k1,:,:,k1,:])[:] += np.expand_dims(
  #                trTensor_diseaseProgression[:,:,k1,:]
  #                ,[2])  # AUTOMATICALLY broadcast all non-hospitalised disease progression is same
  # @cast from TensorCast only works with two repeeated index
  # https://discourse.julialang.org/t/einstein-convention-for-slicing-or-how-to-view-diagonals-in-tensors/38553/3
  # maybe with CartesianIndex or @views
  for iso=1:nIso
    for ag=1:nAge
      for hs=1:nHS
        for tst=1:nTest
          for nhs=1:nHS #new health state
              trTensor_complete[ag,hs,iso,tst,nhs,iso,tst]+=trTensor_diseaseProgression[ag,hs,iso,nhs]
          end
        end
      end
    end
  end
  @testset "Checking intermediate state" begin
    @test py"test1" == trTensor_diseaseProgression
    @test py"test" == trTensor_diseaseProgression
  end

  for ag=1:nAge
    for hs=1:nHS
      for iso=1:nIso
        for tst=1:nTest
          for nhs=1:nHS
            for niso=1:nIso
              for ntst=1:nTest
                dstateTensor[ag,hs,iso,tst]+=stateTensor[1,nhs,niso,ntst,]* trTensor_complete[ag,nhs,niso,ntst,hs,iso,tst]
              end
            end
          end
        end
      end
    end
  end
  dstate[:]=reshape(dstateTensor,(nIso*nAge*nHS*nTest))
end
#= This one is close
  for ag=1:nAge
    for hs=1:nHS
      for iso=1:nIso
        for tst=1:nTest
          for nhs=1:nHS
            for niso=1:nIso
              for ntst=1:nTest
                dstateTensor[ag,hs,iso,ntst]+=stateTensor[tst,nhs,niso,ntst]* trTensor_complete[ag,hs,iso,tst,nhs,niso,ntst]
              end
            end
          end
        end
      end
    end
  end
=#

n=prod([ssize...])
println(n)
dstate= 50*ones(n)
state= ones(n)
println(size(state))
out = f_vec!(dstate,state,nothing,0.0)
@testset "Testing output" begin
	@test py"out" == out
end
# println(out)
# println(size(out))

# prob = ODEProblem(f_vec!,100*ones(n),(0.0,80.0),p=nothing)
# # sys = modelingtoolkitize(prob)
# dstate=100*ones(n)
# state=100*ones(n)
# f_vec!(dstate,state,[],0.0)

# sol = solve(prob,Tsit5())
# println(sol)

# sum to get all infacted sum(stateTensor[:,:,8,:],[1,2,3])

#using Pkg
#Pkg.add(PackageSpec(url="https://github.com/JuliaDiffEq/SciPyDiffEq.jl", rev="master"))
# using SciPyDiffEq
# SciPyDiffEq.RK23

#
# fun = lambda t,y: dydt_Complete(t,y, **kwargs),
# t_span=(0.,total_days),
# y0 = cur_stateTensor,
# method='RK23',
# t_eval=range(total_days),
# rtol = 1e-3, #default 1e-3
# atol = 1e-3, # default 1e-6
# SciPyDiffEq code is compiling forever
# sol = solve(prob,SciPyDiffEq.RK23(),rtol=1.0e-3,atol=1.0e-3,saveat=1.0*[0:80;])

max_forks_repo_name: vollmersj/coexist-julia


text:
#EasyPlotInspect: Implement EasyPlot interface with InspectDR.jl
#-------------------------------------------------------------------------------
module EasyPlotInspect

#Hoping that environment of including module points to *correct* CMDimData:
import CMDimData
#Ensure *correct* modules are imported:
import CMDimData.EasyPlot #Avoid name collisions
using CMDimData.MDDatasets
using CMDimData.Colors
using InspectDR

import CMDimData.EasyPlot: Optional, Target, DS
import InspectDR: LineAttributes, GlyphAttributes

include("base.jl")
include("builder.jl")

#==Initialization
===============================================================================#
function __init__()
	EasyPlot.register(:InspectDR)
	return
end

end

#Last line


text:
using LinearAlgebra
using LightGraphs
using NPZ
using SpinGlassPEPS
using Logging
using ArgParse
using CSV
using Test

disable_logging(LogLevel(0))

# this is axiliary function for npz write

function vecvec2matrix(v::Vector{Vector{Int}})
    M = v[1]
    for i in 2:length(v)
        M = hcat(M, v[i])
    end
    M
end

s = ArgParseSettings("description")
  @add_arg_table! s begin
    "--file", "-i"
    arg_type = String
    help = "the file name"
    "--beta", "-b"
    default = 2.
    arg_type = Float64
    help = "beta value"
    "--chi", "-c"
    default = typemax(Int)
    arg_type = Int
    help = "cutoff size"
    "--n_sols", "-n"
    default = 10
    arg_type = Int
    help = "number of solutions"
    "--node_size1", "-r"
    default = 1
    arg_type = Int
    help = "chimera node size in rows"
    "--node_size2", "-o"
    default = 1
    arg_type = Int
    help = "chimera node size in columns"
    "--spectrum_cutoff", "-u"
    default = 256
    arg_type = Int
    help = "size of the lower spectrum"
end

fi = parse_args(s)["file"]
file = split(fi, "/")[end]
folder = fi[1:end-length(file)]
println(file)
println(folder)

ig = ising_graph(fi)

β = parse_args(s)["beta"]
χ = parse_args(s)["chi"]
spectrum_cutoff = parse_args(s)["spectrum_cutoff"]
node_size = (parse_args(s)["node_size1"], parse_args(s)["node_size2"])
s1 = isqrt(div( nv(ig) ,8))

n = div(s1, node_size[1])
m = div(s1, node_size[2])

D = Dict{Int, Int}()
for v in vertices(ig)
  push!(D, (v => spectrum_cutoff))
end

fg = factor_graph(
      ig,
      D,
      spectrum=brute_force,
      cluster_assignment_rule=super_square_lattice((m, node_size[1], n, node_size[2], 8))
  )

  control_params = Dict(
       "bond_dim" => χ,
       "var_tol" => 1E-12,
       "sweeps" => 4.
   )

peps = PEPSNetwork(m, n, fg, β, :NW, control_params)

n_sols = parse_args(s)["n_sols"]

println(node_size)

@time sols = low_energy_spectrum(peps, n_sols)

energies = sols.energies
println("energies from peps")
for energy in energies
    println(energy)
end

fil = folder*"groundstates_otn2d.txt"
data = split.(readlines(open(fil)))

i = findall(x->x[1]==file, data)[1]
ground_ref = [parse(Int, el) for el in data[i][4:end]]

f(i) = (i == 0)  ? -1 : 1
ground_spins = map(f, ground_ref)
energy_ref = energy(ground_spins, ig)

println("reference energy form real ground state = ", energy_ref)
println("reference energy form file = ", data[i][3])

spins_mat = vecvec2matrix(sols.states)

d = Dict("energies" => energies, "energy_ref" => energy_ref, "chi" => χ, "beta" => β)
npzwrite(folder*file[1:3]*"output.npz", d)


text:
"""
    EnsembleGibbs

A module mixing the ensemble walker stretch move on a subset of
parameter space with a Gibbs sampler on the complimentary subset.  Can
be useful for foward modelling in the presence of selection effects:
the stretch move adjusts model parameters given observed data and a
draw from the latent (un-observed) data, then the Gibbs step draws new
latent data."""

module EnsembleGibbs

using Distributed

"""
    propose(ps, qs)

Return `(ps_new, zs)`, the proposed updates to `ps` and the associated
one-dimensional parameters, `zs`.
"""
function propose(ps::Array{Float64, 2}, qs::Array{Float64, 2})
    n = size(ps,2)
    nd = size(ps,1)

    zs = exp.(log(0.5) .+ (log(2.0) - log(0.5)).*rand(n))
    inds = rand(1:n, (n,))

    ps_new = zeros(size(ps))
    for i in 1:n
        for j in 1:nd
            ps_new[j,i] = qs[j,inds[i]] + zs[i]*(ps[j,i] - qs[j,inds[i]])
        end
    end

    return ps_new, zs
end

"""
    lnprobs(ps, gs, lnprobfn)

Return an array of log-probabilities given parameters `ps` and
corresponding Gibbs samples `gs`.

The log-probability function, `lnprobfn`, should accept these
arguments as `lnprobfn(p, g)`.
"""
function lnprobs(ps, gs, lnprobfn)
    ps_any = Any[ps[:,i] for i in 1:size(ps,2)]
    Array{Float64, 1}(pmap(lnprobfn, ps_any, gs))
end

"""
    gibbses(ps, gs, gupdate)

Return new Gibbs samples to replace `gs` using `gupdate(p, g)`.
"""
function gibbses(ps, gs, gupdate)
    ps_any = Any[ps[:,i] for i in 1:size(ps,2)]
    Array{Any, 1}(pmap(gupdate, ps_any, gs))
end

"""
    update_half(ps, gs, lnps, qs, lnprobfn)

Return `(ps_new, gs, lnps_new)` from an MCMC step for half the system,
treating `qs` as fixed.
"""
function update_half(ps, gs, lnps, qs, lnprobfn)
    nd = size(ps, 1)
    n = size(ps, 2)

    ps_new, zs = propose(ps, qs)

    lnps_new = lnprobs(ps_new, gs, lnprobfn)

    lnpacc = lnps_new - lnps + nd*log.(zs)

    acc = log.(rand(n)) .< lnpacc

    ps_out = zeros(size(ps))
    lnps_out = zeros(size(lnps))

    ps_out[:,acc] = ps_new[:,acc]
    lnps_out[acc] = lnps_new[acc]

    nacc = .~(acc)

    ps_out[:,nacc] = ps[:,nacc]
    lnps_out[nacc] = lnps[nacc]

    ps_out, gs, lnps_out
end

"""
    update(ensemble, gibbs, lnprobs, lnprobfn, gibbsupdate)

Return `(new_ensemble, new_gibbs, new_lnprobs)` from a complete MCMC
then Gibbs cycle.
"""
function update(ensemble, gibbs, lnprob, lnprobfn, gibbsupdate)
    @assert(size(ensemble, 2) % 2 == 0)

    n = size(ensemble, 2)
    nd = size(ensemble, 1)

    ensemble = copy(ensemble)
    gibbs = copy(gibbs)
    lnprob = copy(lnprob)

    nh = div(n, 2)

    ps, gs, lnps = update_half(ensemble[:,1:nh], gibbs[1:nh], lnprob[1:nh], ensemble[:,nh+1:end], lnprobfn)

    ensemble[:,1:nh] = ps
    gibbs[1:nh] = gs
    lnprob[1:nh] = lnps

    ps, gs, lnps = update_half(ensemble[:,nh+1:end], gibbs[nh+1:end], lnprob[nh+1:end], ensemble[:,1:nh], lnprobfn)

    ensemble[:,nh+1:end] = ps
    gibbs[nh+1:end] = gs
    lnprob[nh+1:end] = lnps

    gibbs = gibbses(ensemble, gibbs, gibbsupdate)
    lnprob = lnprobs(ensemble, gibbs, lnprobfn)

    ensemble, gibbs, lnprob
end

"""
    run_mcmc(ensemble, gibbs, lnprobs, lnprobfn, gibbsupdate, steps; thin=1)

Run `steps` worth of `update` and return an array of the result,
optionally thinned.
"""
function run_mcmc(ensemble, gibbs, lnprobs, lnprobfn, gibbsupdate, steps; thin=1)
    nsave = div(steps, thin)

    chain = zeros((size(ensemble, 1), size(ensemble, 2), nsave))
    gibbschain = Array{Any,2}(size(ensemble, 2), nsave)
    chainlnprob = zeros((size(ensemble, 2), nsave))
    isave = 1
    for i in 1:steps
        ensemble, gibbs, lnprobs = update(ensemble, gibbs, lnprobs, lnprobfn, gibbsupdate)

        if i % thin == 0
            chain[:,:,isave] = ensemble
            gibbschain[:, isave] = gibbs
            chainlnprob[:,isave] = lnprobs

            isave = isave + 1
        end
    end

    chain, gibbschain, chainlnprob
end

end


text:
#=
This is a macro used by modules to import various Finch items.
=#

finch_symbols_to_import = [
    #Constants
    :JULIA, :CPP, :MATLAB, :DENDRO, :HOMG, :CUSTOM_GEN_TARGET,
    :SQUARE, :IRREGULAR, :UNIFORM_GRID, :TREE, :UNSTRUCTURED,
    :CG, :DG, :HDG, :FV, :MIXED,
    :NODAL, :MODAL, :CELL, :LEGENDRE, :UNIFORM, :GAUSS, :LOBATTO,
    :NONLINEAR_NEWTON, :NONLINEAR_SOMETHING,
    :EULER_EXPLICIT, :EULER_IMPLICIT, :CRANK_NICHOLSON, :RK4, :LSRK4, :PECE, :MIXED_STEPPER,
    :DEFAULT_SOLVER, :PETSC_SOLVER, :CUDA_SOLVER,
    :VTK, :RAW_OUTPUT, :CUSTOM_OUTPUT,
    :DIRICHLET, :NEUMANN, :ROBIN, :NO_BC, :FLUX, :SYMMETRIC,
    :MSH_V2, :MSH_V4,
    :SCALAR, :VECTOR, :TENSOR, :SYM_TENSOR, :VAR_ARRAY,
    :LHS, :RHS,
    :LINEMESH, :QUADMESH, :TRIMESH, :HEXMESH,

    #Structs
    :Variable, :Coefficient, :GenFunction, :CallbackFunction, :Indexer, :Grid, :Refel, :Finch_prob, :GeometricFactors, :VariableTransform,
    :SymExpression, :SymEntity, :CachesimOut,

    #Functions
    :geometric_factors, :geometric_factors_face, :build_deriv_matrix, :evaluate_coefficient, :exchange_ghosts,

    #Log
    :log_entry, :printerr,

    #Data
    :config, :prob, :variables, :coefficients, :parameters, :test_functions, :indexers, :variable_transforms,
    :mesh_data, :grid_data, :refel, :time_stepper, :language, :gen_framework,
    :elemental_order, :genfunctions, :callback_functions, :geo_factors, :use_cachesim, :custom_gen_funcs,

    #Specific to FV
    :FVInfo, :fv_info, :FV_cell_to_node, :FV_node_to_cell, :FV_reconstruct_value_left_right,
    :ParentMaps, :parent_maps, :fv_grid, :fv_refel, :fv_geo_factors, :build_local_patch,

    # Packages? should this be done here?
    :MPI, :PETSc
];

macro import_finch_symbols()
    # Form an Expr that looks like:
    #     import ..Finch: a, b, c, d, ...
    num_symbols = length(finch_symbols_to_import);
    ex = :(import ..Finch: a);
    symb = Array{Expr,1}(undef,num_symbols);
    for i=1:num_symbols
        symb[i] = Expr(:., finch_symbols_to_import[i]);
    end
    ex.args[1].args = [ex.args[1].args[1]];
    append!(ex.args[1].args, symb);

    ex
end

max_stars_repo_name: paralab/Finch

max_issues_repo_name: paralab/Finch

max_forks_repo_name: paralab/Finch


text:
mutable struct BadStack
    data::Int
    ref::Ref{BadStack}
    function BadStack(x::Int=0,r::Ref{BadStack}=Ref{BadStack}())
        return new(x,r)
    end
end

function push(p::Ref{BadStack},x::Int)
    return Ref(BadStack(x,p))
end
function empty(p::Ref{BadStack})
    return isassigned(p)
end
function top(p::Ref{BadStack})
    return p[].data
end
function pop(p::Ref{BadStack})
    return p[].ref
end

t=Ref(BadStack(6))
t=push(t,8)
println(top(t))
t=pop(t)
println(top(t))

max_stars_repo_name: Rratic/JuliaGuide

max_issues_repo_name: Rratic/JuliaGuide

max_forks_repo_name: Rratic/JuliaGuide


text:
# Tokenizer
# =========

mutable struct Tokenizer{S<:IO}
    # input stream
    input::S
    # data buffer
    buffer::Buffer
    # current line number
    linenum::Int
    # next token (if any)
    next::Union{Tuple{Token,Bool,Int},Nothing}
end

function Tokenizer(input::IO)
    return Tokenizer(input, Buffer(), 1, nothing)
end

isnoneol(char::Char) = ' ' ≤ char ≤ '\U10FFFF' || char == '\t'
iswhitespace(char::Char) = char == ' ' || char == '\t'
isbarekeychar(char::Char) = 'A' ≤ char ≤ 'Z' || 'a' ≤ char ≤ 'z' || '0' ≤ char ≤ '9' || char == '-' || char == '_'

# When rhs=true, it prefers right-hand side tokens to other tokens
# (e.g. "true" will be a boolean value rather than a bare key).
function readtoken(tokenizer::Tokenizer; rhs::Bool=false)::Token
    if tokenizer.next != nothing
        if tokenizer.next[2] == rhs
            token = tokenizer.next[1]
            tokenizer.next = nothing
            return token
        end
        reset(tokenizer)
    end
    input = tokenizer.input
    buffer = tokenizer.buffer
    char, w = peekchar(input, buffer)
    if w == 0
        # no more data
        return TOKEN_EOF
    end
    if iswhitespace(char)  # whitespace
        n = scanwhitespace(input, buffer)
        if n == 1
            consume!(buffer, 1)
            if char == ' '
                return TOKEN_WHITESPACE_SPACE
            else @assert char == '\t'
                return TOKEN_WHITESPACE_TAB
            end
        else
            return Token(:whitespace, taketext!(buffer, n))
        end
    elseif char == '\r' || char == '\n'
        if char == '\r'  # CR+LF
            if peekchar(input, buffer, offset=1)[1] == '\n'
                consume!(buffer, 2)
                tokenizer.linenum += 1
                return TOKEN_NEWLINE_CRLF
            else
                return Token(:unknown, "\r")
            end
        else
            consume!(buffer, 1)
            tokenizer.linenum += 1
            return TOKEN_NEWLINE_LF
        end
    elseif char == '#'  # comment
        n = scanwhile(isnoneol, input, buffer)
        return Token(:comment, taketext!(buffer, n))
    elseif char == '='
        consume!(buffer, 1)
        return TOKEN_EQUAL
    elseif !rhs && isbarekeychar(char)
        n = scanbarekey(input, buffer)
        return Token(:bare_key, taketext!(buffer, n))
    elseif !rhs && (char == '"' || char == '\'')  # quoted key
        n = scanpattern(char == '"' ? RE_BASIC_STRING : RE_LITERAL_STRING, input, buffer)
        if n < 2
            return Token(:unknown, char == '"' ? "\"" : "'")
        end
        return Token(:quoted_key, taketext!(buffer, n))
    elseif char == '.'  # dot
        consume!(buffer, 1)
        return TOKEN_DOT
    elseif char == ','  # comma
        consume!(buffer, 1)
        return TOKEN_COMMA
    elseif char == '['  # table or array of tables
        if !rhs && peekchar(input, buffer, offset=1)[1] == '['
            consume!(buffer, 2)
            return TOKEN_DOUBLE_BRACKETS_LEFT
        else
            consume!(buffer, 1)
            return TOKEN_SINGLE_BRACKET_LEFT
        end
    elseif char == ']'
        if !rhs && peekchar(input, buffer, offset=1)[1] == ']'
            consume!(buffer, 2)
            return TOKEN_DOUBLE_BRACKETS_RIGHT
        else
            consume!(buffer, 1)
            return TOKEN_SINGLE_BRACKET_RIGHT
        end
    elseif char == '{'  # inline table begin
        consume!(buffer, 1)
        return TOKEN_CURLY_BRACE_LEFT
    elseif char == '}'  # inline table end
        consume!(buffer, 1)
        return TOKEN_CURLY_BRACE_RIGHT
    else  # value
        kind, n = scanvalue(input, buffer)
        if kind == :novalue
            return Token(:unknown, taketext!(buffer, w))
        else
            text = taketext!(buffer, n)
            if kind ∈ (:multiline_basic_string, :multiline_literal_string)
                tokenizer.linenum += count(isequal('\n'), text)
            end
            return Token(kind, text)
        end
    end
end

function peektoken(tokenizer::Tokenizer; rhs::Bool=false)::Token
    if tokenizer.next == nothing
        linenum = tokenizer.linenum
        tokenizer.next = (readtoken(tokenizer, rhs=rhs), rhs, linenum)
    elseif tokenizer.next[2] != rhs && !isempty(tokenizer.next[1].text)
        reset(tokenizer)
        linenum = tokenizer.linenum
        tokenizer.next = (readtoken(tokenizer, rhs=rhs), rhs, linenum)
    end
    return tokenizer.next[1]
end

function reset(tokenizer::Tokenizer)
    @assert tokenizer.next != nothing
    takeback!(tokenizer.buffer, tokenizer.next[1].text)
    tokenizer.linenum = tokenizer.next[3]
    tokenizer.next = nothing
    return
end