1
0

usage.rst 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. =====
  2. Usage
  3. =====
  4. Installation
  5. ------------
  6. First you will need to install the EditorConfig Python Core package.
  7. To install from PyPI using pip::
  8. pip install editorconfig
  9. Discovering EditorConfig properties
  10. -----------------------------------
  11. The ``get_properties`` function can be used to discover EditorConfig properties
  12. for a given file. Example:
  13. .. code-block:: python
  14. import logging
  15. from editorconfig import get_properties, EditorConfigError
  16. filename = "/home/zoidberg/humans/anatomy.md"
  17. try:
  18. options = get_properties(filename)
  19. except EditorConfigError:
  20. logging.warning("Error getting EditorConfig properties", exc_info=True)
  21. else:
  22. for key, value in options.items():
  23. print "%s=%s" % (key, value)
  24. The ``get_properties`` method returns a dictionary representing EditorConfig
  25. properties found for the given file. If an error occurs while parsing a file
  26. an exception will be raised. All raised exceptions will inherit from the
  27. ``EditorConfigError`` class.
  28. Handling Exceptions
  29. -------------------
  30. All exceptions raised by EditorConfig will subclass ``EditorConfigError``. To
  31. handle certain exceptions specially, catch them first. More exception classes
  32. may be added in the future so it is advisable to always handle general
  33. ``EditorConfigError`` exceptions in case a future version raises an exception
  34. that your code does not handle specifically.
  35. Exceptions module reference
  36. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. Exceptions can be found in the ``editorconfig.exceptions`` module. These are
  38. the current exception types:
  39. .. autoexception:: editorconfig.exceptions.EditorConfigError
  40. .. autoexception:: editorconfig.exceptions.ParsingError
  41. .. autoexception:: editorconfig.exceptions.PathError
  42. .. autoexception:: editorconfig.exceptions.VersionError
  43. Exception handling example
  44. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. An example of custom exception handling:
  46. .. code-block:: python
  47. import logging
  48. from editorconfig import get_properties
  49. from editorconfig import exceptions
  50. filename = "/home/zoidberg/myfile.txt"
  51. try:
  52. options = get_properties(filename)
  53. except exceptions.ParsingError:
  54. logging.warning("Error parsing an .editorconfig file", exc_info=True)
  55. except exceptions.PathError:
  56. logging.error("Invalid filename specified", exc_info=True)
  57. except exceptions.EditorConfigError:
  58. logging.error("An unknown EditorConfig error occurred", exc_info=True)
  59. for key, value in options.iteritems():
  60. print "%s=%s" % (key, value)