1
0

outputhelper.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #
  2. # This file is part of stov, written by Helmut Pozimski in 2012.
  3. #
  4. # stov is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, version 2 of the License.
  7. #
  8. # stov is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  15. # -*- coding: utf8 -*-
  16. import sys
  17. """
  18. A little helper function used to do some additional checks before actually
  19. using print to generate the output"""
  20. def printf(string, outputlevel, descriptor="stdout", level="default"):
  21. if descriptor == "stdout":
  22. if level == "default" and outputlevel == "default":
  23. if isinstance(string, unicode):
  24. print string.encode("utf8")
  25. elif string is str:
  26. print string
  27. else:
  28. print str(string)
  29. elif level == "verbose":
  30. if isinstance(string, unicode):
  31. print string.encode("utf8")
  32. elif string is str:
  33. print string
  34. else:
  35. print str(string)
  36. elif level == "quiet":
  37. pass
  38. elif descriptor == "stderr":
  39. if level == "default" and outputlevel == "default":
  40. if isinstance(string, unicode):
  41. print >> sys.stderr, string.encode("utf8")
  42. elif string is str:
  43. print >> sys.stderr, string
  44. else:
  45. print str(string)
  46. elif level == "verbose":
  47. if isinstance(string, unicode):
  48. print >> sys.stderr, string.encode("utf8")
  49. elif string is str:
  50. print >> sys.stderr, string
  51. else:
  52. print >> sys.stderr, str(string)
  53. elif level == "quiet":
  54. pass