API Documentation

fhost.cli

class fhost.cli.Application(name=None, description=None, version=None, epilog=None, *args, **kwargs)

fhost entry tool class.

Implements a command-subcommand pattern wich is used as a main entry point of this tool.

A very basic CRUD is provided specifically create, list and remove commands to manage hosts.

Example:

usage: fhost [-h] [-v] {import,add,export,list,delete} ...

optional arguments:
  -h, --help            show this help message and exit
  -v, --version

subcommands:
  valid subcommands

  {import,add,export,list,delete}
                        additional help
    add                 Add new host
    delete              Delete existing hostname
    export              Export hosts as a backup
    import              Import hosts from a backup
    list                List available hosts

The above message shows the usage message help that fhost prints if no command is executed.

description = 'Create, list, and modify local hostnames.'
do_add(options)

Add new host command.

Each host is composed of three parts:

  • IP name
  • Main hostname
  • An alias to the main hostname.

Both three arguments are requied.

Results:None
do_delete(options)

Delete existing hostname.

A valid hostname must be passed, no IP or alias are valid arguments.

Results:None
static do_list(options)

List available hosts command.

No argument is required for this too.

Results:list - A list of triplets ( ip, host, alias) hostnames.
name = 'fhost'
version = '0.2'

fhost.exceptions

exception fhost.exceptions.BaseExceptionError

Base Exception

This is the base exception. All other exceptions from fhost module inherits from this one.

Example:

try:
    ...
except BaseException:
    raise BaseException("base exception raised!")
    sys.exit(1)
exception fhost.exceptions.InvalidHostnameException

Invalid Hostname

This exception is raised when some part of fhost code validates a hostname format.

As /etc/hosts file is quite delicated beacuse it can affect the whole system, fhost performs aggressive validations on all input and output data.

A common string message is shown to the user if this exeption raises.

Example:

try:
    ...
except InvalidHostnameException:
    raise InvalidHostnameException("invalid hostname exception raised")
    sys.exit(1)
exception fhost.exceptions.InvalidIPException

Invalid IP

This exception is raised when some part of fhost code validates an IP address format.

As /etc/hosts file is quite delicated beacuse it can affect the whole system, fhost performs aggressive validations on all input and output data.

A common string message is shown to the user if this exeption raises.

Example:

try:
    ...
except InvalidIPException:
    raise InvalidIPException("invalid ip exception raised!")
    sys.exit(1)

fhost.hosts

class fhost.hosts.Hosts

A collection of Hosts

This is a collection of Hosts implemented as a Python container. It is iterable and have CRUD methods helpers to maintain a registry of available Hosts during fhost process execution.

It also have hardcoded the common path where /etc/hosts file is located.

In the future discover code will be implemented, both to detect the OS and its version and /etc/hosts location.

add(ip_address, canonical_hostname, *args)

Add new hostname to the container.

Each host is composed by:

  • IP address
  • Main hostname
  • Aliases to the main hostname

At this moment multiple aliases are accepted and inserted but only one is used and saved back.

Results:None
delete(canonical_hostname)

Delete existing hostname.

A valid hostname must be passed, no IP or alias are valid arguments.

Results:None
hosts_file = '/etc/hosts'

fhost.utils

fhost.utils.is_valid_hostname(hostname)

Check if this is a valid hostname

:rtype : object :param hostname: Example:

hostname = "example.com"
if isValidHostName(hostname):
    print "Valid Hostname!"
else:
    print "Invalid Hostname"
Results:Boolean value that indicates if the hostname is valid.
fhost.utils.is_valid_ip(ip)

Check if this is a valid IP

Example:

hostname = "173.194.34.208"
if isValidIP(hostname):
    print "Valid IP!"
else:
    print "Invalid IP"
Results:Boolean value that indicates if the IP address is valid.