Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

validator.py 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2003 Zope Corporation and Contributors.
  4. # All Rights Reserved.
  5. #
  6. # This software is subject to the provisions of the Zope Public License,
  7. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  9. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  10. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  11. # FOR A PARTICULAR PURPOSE.
  12. #
  13. ##############################################################################
  14. """Script to check validity of a configuration file.
  15. """
  16. from __future__ import print_function
  17. import argparse
  18. import sys
  19. import ZConfig
  20. def main(args=None):
  21. optparser = argparse.ArgumentParser(
  22. description="Script to check validity of a configuration file",
  23. epilog="""
  24. Each file named on the command line is checked for syntactical errors
  25. and schema conformance. The schema must be specified. If no files
  26. are specified and standard input is not a TTY, standard in is treated
  27. as a configuration file. Specifying a schema and no configuration
  28. files causes the schema to be checked.""",
  29. )
  30. optparser.add_argument(
  31. "-s", "--schema", dest="schema",
  32. required=True,
  33. help="use the schema in FILE (can be a URL)",
  34. metavar="FILE"
  35. )
  36. optparser.add_argument(
  37. "file",
  38. nargs='*',
  39. help="Optional configuration file to check",
  40. type=argparse.FileType('r'),
  41. )
  42. options = optparser.parse_args(args=args)
  43. schema = ZConfig.loadSchema(options.schema)
  44. if not options.file:
  45. if sys.stdin.isatty():
  46. # just checking the schema
  47. return 0
  48. # stdin is a pipe
  49. options.file = [sys.stdin]
  50. errors = False
  51. for f in options.file:
  52. try:
  53. ZConfig.loadConfigFile(schema, f)
  54. except ZConfig.ConfigurationError as e:
  55. print(str(e), file=sys.stderr)
  56. errors = True
  57. return int(errors)
  58. if __name__ == "__main__":
  59. sys.exit(main())