Skip to content

Feature/sof 5462 - get name from file content #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: epic/SOF-5386
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
steps:
- name: Checkout this repository
uses: actions/checkout@v2
with:
lfs: true

- name: Checkout actions repository
uses: actions/checkout@v2
Expand All @@ -40,7 +42,8 @@ jobs:
steps:
- name: Checkout this repository
uses: actions/checkout@v2

with:
lfs: true
- name: Checkout actions repository
uses: actions/checkout@v2
with:
Expand Down
22 changes: 22 additions & 0 deletions src/parsers/parsers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import cif from "./cif";
import espresso from "./espresso";
import poscar from "./poscar";
import { getLineFromContent } from "./utils";
import xyz from "./xyz";

/**
* Function returns the of the file that should contain file contents based on the fileExtension value
* @param {String} fileContent
* @param {String} fileExtension
* @param {String} failoverName
* @returns {String}
*/
export function getNameFromContents(fileContent, fileExtension, failoverName = "material") {
let lineNumber;
if (fileExtension === "poscar") {
lineNumber = 0;
}
if (fileExtension === "xyz") {
lineNumber = 1;
}
const nameFromContent = getLineFromContent(fileContent, lineNumber);
if (nameFromContent) return nameFromContent;
return failoverName;
}

export default {
xyz,
poscar,
cif,
espresso,
getNameFromContents,
};
4 changes: 4 additions & 0 deletions src/parsers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function getLineFromContent(fileContent, lineNumber) {
const lineString = fileContent.split(/\r?\n/)[lineNumber];
return lineString.trim();
}
2 changes: 2 additions & 0 deletions tests/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export const SiPWSCFInput = readFile(path.join(FIXTURES_DIR, "Si-pwscf.in"));
export const Zr1H23Zr1H1 = readJSONFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.json"));
export const Zr1H23Zr1H1Poscar = readFile(path.join(FIXTURES_DIR, "Zr1H23Zr1H1.poscar"));
export const H2O = readFile(path.join(FIXTURES_DIR, "H2O.poscar"));
export const GenericPoscar = readFile(path.join(FIXTURES_DIR, "POSCAR.poscar"));
export const GenericXYZ = readFile(path.join(FIXTURES_DIR, "Structure.xyz"));
3 changes: 3 additions & 0 deletions tests/fixtures/POSCAR.poscar
Git LFS file not shown
3 changes: 3 additions & 0 deletions tests/fixtures/Structure.xyz
Git LFS file not shown
6 changes: 5 additions & 1 deletion tests/parsers/poscar.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { expect } from "chai";

import { Material } from "../../src/material";
import { getNameFromContents } from "../../src/parsers/parsers";
import { atomsCount } from "../../src/parsers/poscar";
import { H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums";
import { GenericPoscar, H2O, Na4Cl4, Na4Cl4Poscar, Zr1H23Zr1H1, Zr1H23Zr1H1Poscar } from "../enums";

describe("Parsers.POSCAR", () => {
it("should return a valid poscar", () => {
Expand All @@ -18,4 +19,7 @@ describe("Parsers.POSCAR", () => {
it("should return the number of atoms for a molecule in a poscar file", () => {
expect(atomsCount(H2O)).to.be.equal(3);
});
it("should return the material name based on the poscar file contents", () => {
expect(getNameFromContents(GenericPoscar, "poscar")).to.be.equal("H2S");
});
});
9 changes: 7 additions & 2 deletions tests/parsers/xyz.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import parsers from "../../src/parsers/parsers";
import { Si } from "../enums";
import { expect } from "chai";

import parsers, { getNameFromContents } from "../../src/parsers/parsers";
import { GenericXYZ, Si } from "../enums";
import { assertDeepAlmostEqual } from "../utils";

describe("Parsers:XYZ", () => {
Expand All @@ -11,4 +13,7 @@ describe("Parsers:XYZ", () => {
"units",
]);
});
it("should return the material name based on the xyz file contents", () => {
expect(getNameFromContents(GenericXYZ, "xyz")).to.be.equal("Methane");
});
});