Skip to content

Commit

Permalink
upgrade to the latest prompt_toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-24 committed Jan 10, 2024
1 parent f1e8b9f commit 00d783f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
48 changes: 27 additions & 21 deletions nachos/core/making.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import os

from prompt_toolkit import document as pt_document, prompt as prompt_pt
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.contrib.completers import WordCompleter, PathCompleter
from prompt_toolkit.completion import WordCompleter, PathCompleter, Completer, Completion
from prompt_toolkit.validation import Validator, ValidationError
from prompt_toolkit.contrib.validators.base import SentenceValidator

from qcip_tools.chemistry_files import helpers, PropertyNotPresent, PropertyNotDefined, gaussian

from nachos.core import files, CONFIG


class SentenceValidatorWithDefault(SentenceValidator):
class ChoicesValidator(Validator):

def __init__(self, default=None, **kwargs):
def __init__(self, choices=None, default=None, **kwargs):
super().__init__(**kwargs)

self.default_sentence = default

if default is not None and default not in self.sentences:
if default is not None and default not in choices:
raise ValueError('default value {} not in list'.format(default))

def validate(self, document):
self.choices = [] if choices is None else choices
self.default = default

if document.text == '' and self.default_sentence is not None:
document.text = self.default_sentence
else:
super().validate(document)
def validate(self, document):
if document.text == '' and self.default is not None:
document.text = self.default
elif document.text not in self.choices:
raise ValidationError(message='Invalid choice: {}'.format(document.text))


class ChemistryFileValidator(Validator):
Expand Down Expand Up @@ -403,7 +401,7 @@ def make(self, args):
'What flavor for you, today?',
SetRecipeAction(recipe),
completer=WordCompleter(words=CONFIG.keys()),
validator=SentenceValidatorWithDefault(sentences=CONFIG.keys())
validator=ChoicesValidator(choices=CONFIG.keys())
)

config = CONFIG[recipe['flavor']]
Expand All @@ -413,7 +411,7 @@ def make(self, args):
'What type of differentiation?',
SetRecipeAction(recipe),
completer=WordCompleter(words=config['types']),
validator=SentenceValidatorWithDefault(sentences=config['types']))
validator=ChoicesValidator(choices=config['types']))

methods = [a[0] for a in config['methods']]
self._make_var(
Expand All @@ -422,7 +420,7 @@ def make(self, args):
'With which method?',
SetRecipeAction(recipe),
completer=WordCompleter(words=methods),
validator=SentenceValidatorWithDefault(sentences=methods))
validator=ChoicesValidator(choices=methods))

if recipe['method'] == 'DFT':
self._make_var(
Expand Down Expand Up @@ -495,13 +493,20 @@ def make(self, args):
SetRecipeAction(recipe),
default=files.DEFAULT_RECIPE['name'])

# change default for type='G'
min_field = files.DEFAULT_RECIPE['min_field']
k_max = files.DEFAULT_RECIPE['k_max']
if recipe['type'] == 'G':
min_field = 0.01
k_max = 3

self._make_var(
args,
'min_field',
'Minimum field (F0)?',
SetRecipeWithConversionAction(recipe),
validator=TypeFloatValidator(default=files.DEFAULT_RECIPE['min_field']),
default=files.DEFAULT_RECIPE['min_field'])
validator=TypeFloatValidator(default=min_field),
default=min_field)

self._make_var(
args,
Expand All @@ -516,8 +521,8 @@ def make(self, args):
'k_max',
'Maximum k?',
SetRecipeWithConversionAction(recipe),
validator=TypeIntValidator(default=files.DEFAULT_RECIPE['k_max']),
default=files.DEFAULT_RECIPE['k_max'])
validator=TypeIntValidator(default=k_max),
default=k_max)

self._make_var(
args,
Expand Down Expand Up @@ -599,7 +604,8 @@ def _prompt_toolkit(message, validator, completer, default=None):
v = prompt_pt(
message='{}{} '.format(message, ' [{}]'.format(default) if default is not None else ''),
validator=validator,
completer=completer
completer=completer,
validate_while_typing=False
)

if default is not None and validator is None and v == '':
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies = [
'pyyaml>=5.0',
'h5py',
'qcip-tools @ git+https://github.com/pierre-24/[email protected]',
'prompt_toolkit==1.0.15',
'prompt_toolkit',
'pandas>=1.2',
'scipy>=1.7',
'numpy>=1.20'
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pandas==2.1.4
# qcip-tools
pint==0.23
# via qcip-tools
prompt-toolkit==1.0.15
prompt-toolkit==3.0.43
# via nachos
pycodestyle==2.11.1
# via
Expand Down Expand Up @@ -99,7 +99,6 @@ scipy==1.11.4
# qcip-tools
six==1.16.0
# via
# prompt-toolkit
# python-dateutil
# sphinxcontrib-autoprogram
snowballstemmer==2.2.0
Expand Down

0 comments on commit 00d783f

Please sign in to comment.