Skip to content

Commit

Permalink
Merge pull request pyfa-org#1538 from fsufitch/issue/1533
Browse files Browse the repository at this point in the history
Improve user experience customizing jargon.yaml
  • Loading branch information
blitzmann committed May 3, 2018
2 parents d52c249 + 95c1f7b commit f123703
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
7 changes: 7 additions & 0 deletions service/jargon/header.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@
# Syntax:
#
# abbreviation: full name
#
# The default jargon definitions are stored in Pyfa itself as well, and are
# listed here for convenience overriding them. To disable a jargon definition,
# set it as an empty string. For example, if you do not want "web" to return
# anything containing "stasis":
#
# web: ""
33 changes: 17 additions & 16 deletions service/jargon/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ def __init__(self, jargon_path: str):
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()
Expand All @@ -47,10 +42,12 @@ def _is_stale(self):
self.jargon_mtime != self._get_jargon_file_mtime())

def _load_jargon(self):
jargondata = yaml.load(DEFAULT_DATA)
with open(JARGON_PATH) as f:
rawdata = yaml.load(f)
userdata = yaml.load(f)
jargondata.update(userdata)
self.jargon_mtime = self._get_jargon_file_mtime()
self._jargon = Jargon(rawdata)
self._jargon = Jargon(jargondata)

def _get_jargon_file_mtime(self) -> int:
if not os.path.exists(self.jargon_path):
Expand All @@ -60,15 +57,19 @@ def _get_jargon_file_mtime(self) -> int:
@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)

## Disabled for issue/1533; do not overwrite existing user config
# if os.path.exists(jargon_path):
# with open(jargon_path) as f:
# custom_values = yaml.load(f)
# if custom_values:
# values.update(custom_values)

if not os.path.exists(jargon_path):
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
Expand Down

0 comments on commit f123703

Please sign in to comment.