Refactor task/flow flattening

- Instead of having to pass context data between the
  recursive function calls, create an type/object to hold
  this context data, which also contains the flattening
  functionality itself.
- Allow the flattening functions to be more easily
  extended by having a selection function that matches
  item type to flattening function.
- Add pre/post flattening functions (which can be
  extended/inherited to do different activites as
  needed).

Change-Id: I2f2087097f5934e02d9d775eea709673b63f01b1
Be more resilent around import/detection/setup errors

It appears that some of our current testr failures
are due to this module not handling exception cases
very well; this is making it hard to debug and hard
to determine what the errors are, so handle more of
the general exceptions better in more of the places
where they are problematic.

Change-Id: I7c71d3a68852c0093f27f3b16d6475cc0a8bf8ef
Merge "Refactor task/flow flattening"