mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Add GObject Introspection integration
- Parse .gir files - Validate class, property, and signal names
This commit is contained in:
parent
2ad2f1c54a
commit
e553e5db29
10 changed files with 734 additions and 58 deletions
67
gtkblueprinttool/utils.py
Normal file
67
gtkblueprinttool/utils.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
# utils.py
|
||||
#
|
||||
# Copyright 2021 James Westman <james@jwestman.net>
|
||||
#
|
||||
# This file 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 file 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/>.
|
||||
#
|
||||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
import typing as T
|
||||
|
||||
|
||||
def lazy_prop(func):
|
||||
key = "_lazy_prop_" + func.__name__
|
||||
|
||||
@property
|
||||
def real_func(self):
|
||||
if key not in self.__dict__:
|
||||
self.__dict__[key] = func(self)
|
||||
return self.__dict__[key]
|
||||
|
||||
return real_func
|
||||
|
||||
|
||||
def did_you_mean(word: str, options: [str]) -> T.Optional[str]:
|
||||
if len(options) == 0:
|
||||
return None
|
||||
|
||||
def levenshtein(a, b):
|
||||
# see https://en.wikipedia.org/wiki/Levenshtein_distance
|
||||
m = len(a)
|
||||
n = len(b)
|
||||
|
||||
distances = [[0 for j in range(n)] for i in range(m)]
|
||||
|
||||
for i in range(m):
|
||||
distances[i][0] = i
|
||||
for j in range(n):
|
||||
distances[0][j] = j
|
||||
|
||||
for j in range(1, n):
|
||||
for i in range(1, m):
|
||||
cost = 0
|
||||
if a[i] != b[j]:
|
||||
if a[i].casefold() == b[j].casefold():
|
||||
cost = 1
|
||||
else:
|
||||
cost = 2
|
||||
distances[i][j] = min(distances[i-1][j] + 2, distances[i][j-1] + 2, distances[i-1][j-1] + cost)
|
||||
|
||||
return distances[m-1][n-1]
|
||||
|
||||
distances = [(option, levenshtein(word, option)) for option in options]
|
||||
closest = min(distances, key=lambda item:item[1])
|
||||
if closest[1] <= 5:
|
||||
return closest[0]
|
||||
return None
|
Loading…
Add table
Add a link
Reference in a new issue