|
| 1 | +#!python |
| 2 | +# Copyright (c) 2014-2014 ActiveState Software Inc. |
| 3 | +# See the file LICENSE.txt for licensing information. |
| 4 | + |
| 5 | +"""The main PyXPCOM module for .editorconfig""" |
| 6 | + |
| 7 | +from xpcom.components import interfaces as Ci |
| 8 | +from xpcom.components import classes as Cc |
| 9 | +from editorconfig import get_properties, EditorConfigError |
| 10 | + |
| 11 | +import logging |
| 12 | +import json |
| 13 | + |
| 14 | +log = logging.getLogger("editorconfig") |
| 15 | +log.setLevel(10) |
| 16 | + |
| 17 | +class koEditorConfig: |
| 18 | + |
| 19 | + _com_interfaces_ = [Ci.koIEditorConfig] |
| 20 | + _reg_desc_ = "EditorConfig" |
| 21 | + _reg_clsid_ = "{2cf77db4-c923-4a37-bfea-91764163ff84}" |
| 22 | + _reg_contractid_ = "@activestate.com/editorconfig/koEditorConfig;1" |
| 23 | + |
| 24 | + def get_properties(self, filename, interpret = True): |
| 25 | + try: |
| 26 | + options = get_properties(filename) |
| 27 | + except exceptions.ParsingError: |
| 28 | + log.warning("Error parsing an .editorconfig file") |
| 29 | + return "" |
| 30 | + except exceptions.PathError: |
| 31 | + log.error("Invalid filename specified") |
| 32 | + return "" |
| 33 | + except exceptions.EditorConfigError: |
| 34 | + log.error("An unknown EditorConfig error occurred") |
| 35 | + return "" |
| 36 | + |
| 37 | + if interpret: |
| 38 | + items = {} |
| 39 | + for key, value in options.iteritems(): |
| 40 | + if value.isdigit(): |
| 41 | + value = int(value) |
| 42 | + elif value == "true" or value == "false": |
| 43 | + value = value == "true" |
| 44 | + |
| 45 | + if key == "indent_style": |
| 46 | + items["useTabs"] = value == "tab" |
| 47 | + elif key == "indent_size": |
| 48 | + if value != "tab": |
| 49 | + items["indentWidth"] = value |
| 50 | + elif key == "tab_width": |
| 51 | + items["tabWidth"] = value |
| 52 | + elif key == "end_of_line": |
| 53 | + items["endOfLine"] = value.upper() |
| 54 | + elif key == "charset": |
| 55 | + items["encodingDefault"] = value |
| 56 | + elif key == "trim_trailing_whitespace": |
| 57 | + items["cleanLineEnds"] = value |
| 58 | + elif key == "insert_final_newline": |
| 59 | + items["ensureFinalEOL"] = value |
| 60 | + elif key == "max_line_length": |
| 61 | + # not exactly the same, we're just setting the guide line |
| 62 | + items["editAutoWrapColumn"] = value |
| 63 | + else: |
| 64 | + items[key] = value |
| 65 | + else: |
| 66 | + items = options.items(); |
| 67 | + |
| 68 | + return json.dumps(items) |
0 commit comments