forked from pyfa-org/Pyfa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jargon service translates search strings for the market using community-sourced abbreviations. It also creates the jargon.yaml file in the user's save directory, for future customization (either manual, or via the UI) and provides the code hooks to load and update it. The JargonLoader is capable of hot-loading new jargon.yaml configurations without restarting Pyfa.
- Loading branch information
Showing
8 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ sqlalchemy >= 1.0.5 | |
markdown2 | ||
packaging | ||
roman | ||
beautifulsoup4 | ||
beautifulsoup4 | ||
PyYAML |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# ============================================================================= | ||
# Copyright (C) 2018 Filip Sufitchi | ||
# | ||
# This file is part of pyfa. | ||
# | ||
# pyfa is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# pyfa 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 General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with pyfa. If not, see <http://www.gnu.org/licenses/>. | ||
# ============================================================================= | ||
|
||
from .jargon import Jargon | ||
from .loader import JargonLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
2: II | ||
haml: Heavy Assault Missile Launcher | ||
mwd: Microwarpdrive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# This is the default Pyfa jargon file. | ||
# | ||
# It is essentially a giant set of find/replace statements in order to translate | ||
# abbreviated Eve community terms into more useful full terms. It is intended | ||
# for translation of strings such as "haml 2" "into "Heavy Assault Missile Launcher II".. | ||
# | ||
# These abbreviations are not case-sensitive. If abbreviations collide, the | ||
# later one is used. | ||
# | ||
# Abbreviations with spaces are not supported. | ||
# | ||
# Syntax: | ||
# | ||
# abbreviation: full name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# ============================================================================= | ||
# Copyright (C) 2018 Filip Sufitchi | ||
# | ||
# This file is part of pyfa. | ||
# | ||
# pyfa is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# pyfa 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 General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with pyfa. If not, see <http://www.gnu.org/licenses/>. | ||
# ============================================================================= | ||
|
||
import config | ||
import pkg_resources | ||
|
||
class Jargon(object): | ||
def __init__(self, rawdata: dict): | ||
self._rawdata = rawdata | ||
|
||
# copy the data to lowercase keys, ignore blank keys | ||
self._data = {str(k).lower():v for k,v in rawdata.items() if k} | ||
|
||
def get(self, term: str) -> str: | ||
return self._data.get(term.lower()) | ||
|
||
def get_rawdata() -> dict: | ||
return self._rawdata | ||
|
||
def apply(self, query): | ||
query_words = query.split() | ||
parts = [] | ||
|
||
for word in query_words: | ||
replacement = self.get(word) | ||
if replacement: | ||
parts.append(replacement) | ||
else: | ||
parts.append(word) | ||
|
||
return ' '.join(parts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# ============================================================================= | ||
# Copyright (C) 2018 Filip Sufitchi | ||
# | ||
# This file is part of pyfa. | ||
# | ||
# pyfa is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# pyfa 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 General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with pyfa. If not, see <http://www.gnu.org/licenses/>. | ||
# ============================================================================= | ||
|
||
import os | ||
import config | ||
import yaml | ||
|
||
from .jargon import Jargon | ||
from .resources import DEFAULT_DATA, DEFAULT_HEADER | ||
|
||
JARGON_PATH = os.path.join(config.savePath, 'jargon.yaml') | ||
|
||
class JargonLoader(object): | ||
def __init__(self, jargon_path: str): | ||
self.jargon_path = jargon_path | ||
self._jargon_mtime = 0 # type: int | ||
self._jargon = None # type: Jargon | ||
|
||
def save_jargon(self, data: Jargon): | ||
rawdata = data.get_rawdata() | ||
with open(JARGON_PATH, 'w') as f: | ||
yaml.dump(rawdata, stream=f, default_flow_style=False) | ||
|
||
def get_jargon(self) -> Jargon: | ||
if self._is_stale(): | ||
self._load_jargon() | ||
return self._jargon | ||
|
||
def _is_stale(self): | ||
return (not self._jargon or not self._jargon_mtime or | ||
self.jargon_mtime != self._get_jargon_file_mtime()) | ||
|
||
def _load_jargon(self): | ||
with open(JARGON_PATH) as f: | ||
rawdata = yaml.load(f) | ||
self.jargon_mtime = self._get_jargon_file_mtime() | ||
self._jargon = Jargon(rawdata) | ||
|
||
def _get_jargon_file_mtime(self) -> int: | ||
if not os.path.exists(self.jargon_path): | ||
return 0 | ||
return os.stat(self.jargon_path).st_mtime | ||
|
||
@staticmethod | ||
def init_user_jargon(jargon_path): | ||
values = yaml.load(DEFAULT_DATA) | ||
if os.path.exists(jargon_path): | ||
with open(jargon_path) as f: | ||
custom_values = yaml.load(f) | ||
if custom_values: | ||
values.update(custom_values) | ||
with open(jargon_path, 'w') as f: | ||
f.write(DEFAULT_HEADER) | ||
f.write('\n\n') | ||
yaml.dump(values, stream=f, default_flow_style=False) | ||
|
||
_instance = None | ||
@staticmethod | ||
def instance(jargon_path=None): | ||
if not JargonLoader._instance: | ||
jargon_path = jargon_path or JARGON_PATH | ||
JargonLoader._instance = JargonLoader(jargon_path) | ||
return JargonLoader._instance | ||
|
||
JargonLoader.init_user_jargon(JARGON_PATH) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# ============================================================================= | ||
# Copyright (C) 2018 Filip Sufitchi | ||
# | ||
# This file is part of pyfa. | ||
# | ||
# pyfa is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# pyfa 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 General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with pyfa. If not, see <http://www.gnu.org/licenses/>. | ||
# ============================================================================= | ||
|
||
import pkg_resources | ||
|
||
DEFAULT_DATA = pkg_resources.resource_string(__name__, 'defaults.yaml').decode() | ||
DEFAULT_HEADER = pkg_resources.resource_string(__name__, 'header.yaml').decode() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters