Menu

[8f66b6]: / HighlighterConfiguration.py  Maximize  Restore  History

Download this file

113 lines (97 with data), 3.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
# -*- coding: utf-8 -*-
"""
Copyright (C) 2011 Oliver Tengler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from typing import Optional, List, Tuple
from fnmatch import fnmatch
import unittest
from tools import Config
class ExtensionMapping:
def __init__(self, extPattern: str, configFile: str) -> None:
# The significance is the number of none wildcard chars. E.g. CPP has a significance of 3, C* has only 1.
self.significance = sum ((lambda c: c!="*" and c!="?")(c) for c in extPattern)
# A pattern which matches the extension
self.extPattern = extPattern
self.configFile = configFile
def match (self, ext: str) -> int:
"""Returns the significance if the configuration matches the pattern , -1 otherwise"""
if ext.startswith("."):
ext = ext[1:]
if fnmatch (ext, self.extPattern):
return self.significance
return -1
class Highlighter:
"""
Contains a list of ExtensionMapping objects. Its main function is to lookup the right ExtensionMapping for
a given extension.
"""
def __init__(self, conf: Config.Config) -> None:
self.mappings = self.readConfig (conf)
def readConfig (self, conf: Config.Config) -> List[ExtensionMapping]:
"""
Highlighter_Default {
config = C++.txt
extensions = *
}
"""
mappings: List[ExtensionMapping] = []
for c in conf:
if c.startswith("highlighter"):
settings = conf[c]
extensions = settings.extensions
configFile = settings.config
for pattern in extensions.split(","):
mappings.append(ExtensionMapping(pattern, configFile))
return mappings
def lookup (self, ext: str) -> Optional[str]:
"""Lookup highlighter config file by extension"""
matches: List[Tuple[int, ExtensionMapping]] = []
for mapping in self.mappings:
significance = mapping.match(ext)
if significance != -1:
matches.append((significance, mapping))
# Sort by significance
matches.sort(key=lambda i: i[0], reverse=True)
if matches:
return matches[0][1].configFile
return None
class HighlighterCache:
highlighter: Optional[Highlighter] = None
def highlighter(conf: Config.Config) -> Highlighter:
if not HighlighterCache.highlighter:
HighlighterCache.highlighter = Highlighter(conf)
return HighlighterCache.highlighter
#Highlighter_Default {
# config = default.txt
# extensions = *
#}
#
#Highlighter_C {
# config = C.txt
# extensions = c*,h*,inl
#}
#
#Highlighter_CPP {
# config = C++.txt
# extensions = cpp
#}
class TestHighlighterConfig(unittest.TestCase):
def test(self) -> None:
h = highlighter(Config.Config("tests\\SourceViewer.txt"))
self.assertEqual (h.lookup("py"), "default.txt")
self.assertEqual (h.lookup("cpp"), "C++.txt")
self.assertEqual (h.lookup("CPP"), "C++.txt")
self.assertEqual (h.lookup("c"), "C.txt")
self.assertEqual (h.lookup("c"), "C.txt")
if __name__ == "__main__":
unittest.main()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.