Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/add-cli.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Add CLI interface to run DSL input file

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
Comment thread
ycexiao marked this conversation as resolved.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ exclude = [] # exclude packages matching these glob patterns (empty by default)
namespaces = false # to disable scanning PEP 420 namespaces (true by default)

[project.scripts]
pdfbl-sequential = "pdfbl.sequential.pdfbl_sequential_app:main"
pdfbl = "pdfbl.sequential.pdfbl_sequential_app:main"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pdfbl.

pdfbl run input.dp-in

is tested locally.


[tool.setuptools.dynamic]
dependencies = {file = ["requirements/pip.txt"]}
Expand Down
33 changes: 29 additions & 4 deletions src/pdfbl/sequential/diffpy_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from pathlib import Path

import yaml
Expand All @@ -13,7 +14,7 @@
;

Command:
LoadCommand | SetCommand | CreateCommand
LoadCommand | SetCommand | CreateCommand | SaveCommand
;

LoadCommand:
Expand All @@ -29,6 +30,10 @@
'create' 'equation' 'variables' value+=Value[eolterm]
;

SaveCommand:
'save' 'to' source=STRING
;

VariableBlock:
'variables:' '---' content=/[\s\S]*?(?=---)/ '---'
;
Expand All @@ -53,6 +58,7 @@ def __init__(self):
"LoadCommand": self.load_command_processor,
"VariableBlock": self.variable_block_processor,
"CreateCommand": self.create_command_processor,
"SaveCommand": self.save_command_processor,
}
)
self.inputs = {}
Expand Down Expand Up @@ -136,6 +142,9 @@ def create_command_processor(self, command):
v for v in command.value if isinstance(v, str)
]

def save_command_processor(self, command):
self.inputs["result_path"] = command.source

def configure_adapter(self):
self.pdfadapter.initialize_profile(
self.inputs["profile_path"], **self.inputs["profiles_config"]
Expand Down Expand Up @@ -170,8 +179,23 @@ def run(self):
least_squares(
self.pdfadapter.recipe.residual, self.pdfadapter.recipe.values
)
if "result_path" in self.inputs:
with open(self.inputs["result_path"], "w") as f:
json.dump(self.pdfadapter.get_results(), f, indent=4)
return self.pdfadapter.get_results()

def run_app(self, args):
dpin_path = Path(args.input_file)
if not dpin_path.exists():
raise FileNotFoundError(
f"{str(dpin_path)} not found. Please check if this file "
"exists and provide the correct path to it."
)
dsl_code = dpin_path.read_text()
self.interpret(dsl_code)
self.configure_adapter()
self.run()
Comment thread
ycexiao marked this conversation as resolved.


if __name__ == "__main__":
interpreter = DiffpyInterpreter()
Expand All @@ -184,13 +208,14 @@ def run(self):
set exp_ni calculation_range as 1.5 50 0.01
create equation variables s0
set equation as "s0*G1"
save to "results.json"

variables:
---
- G1_a: 3.52
- G1.a: 3.52
- s0: 0.4
- G1_Uiso_0: 0.005
- G1_delta2: 2
- G1.Uiso_0: 0.005
- G1.delta2: 2
- qdamp: 0.04
- qbroad: 0.02
---
Expand Down
19 changes: 15 additions & 4 deletions src/pdfbl/sequential/pdfbl_sequential_app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import argparse

from pdfbl.sequential import __version__
from pdfbl.sequential.diffpy_interpreter import DiffpyInterpreter


def main():
"""Entry point for the pdfbl-cli.

Examples
--------
>>> pdfbl-cli --version
>>> pdfbl --version
>>> pdfbl run input.dp-in
"""
parser = argparse.ArgumentParser(
description=(
Expand All @@ -19,7 +21,16 @@ def main():
parser.add_argument(
"--version",
action="version",
version=f"pdfbl.sequential {__version__}",
help="Show the version of pdfbl.sequential and exit.",
version=f"pdfbl {__version__}",
help="Show the version of pdfbl and exit.",
)
parser.parse_args()
subparsers = parser.add_subparsers(dest="subcommand")
run_parser = subparsers.add_parser("run")
run_parser.add_argument("input_file", help="Input .dp-in file.")
run_parser.set_defaults(func=DiffpyInterpreter().run_app)
args = parser.parse_args()
if hasattr(args, "func"):
args.func(args)
Comment thread
ycexiao marked this conversation as resolved.
else:
parser.print_help()
parser.exit()
Loading