Export function parseDossierLegislatif

This commit is contained in:
Emmanuel 2023-05-19 14:12:43 +02:00
parent 45b6d8942c
commit 4d12eab0c3
4 changed files with 90 additions and 69 deletions

View file

@ -184,3 +184,4 @@ export {
type TmLienTxt,
type Versions,
} from "./legal"
export { parseDossierLegislatif } from "./parsers"

69
src/lib/parsers/dole.ts Normal file
View file

@ -0,0 +1,69 @@
import { auditChain, auditRequire, strictAudit } from "@auditors/core"
import assert from "assert"
import { XMLParser } from "fast-xml-parser"
import he from "he"
import { auditDossierLegislatif } from "$lib/auditors/dole"
import type { DossierLegislatif, XmlHeader } from "$lib/legal"
const xmlParser = new XMLParser({
attributeNamePrefix: "@",
ignoreAttributes: false,
stopNodes: [
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_1",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_2",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_3",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_4",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_5",
"DOSSIER_LEGISLATIF.CONTENU.EXPOSE_MOTIF",
],
tagValueProcessor: (_tagName, tagValue) => he.decode(tagValue),
})
export function parseDossierLegislatif(
filePath: string,
xmlString: string,
): DossierLegislatif | undefined {
try {
const xmlData = xmlParser.parse(xmlString)
for (const [key, element] of Object.entries(xmlData) as [
string,
DossierLegislatif | XmlHeader,
][]) {
switch (key) {
case "?xml": {
const xmlHeader = element as XmlHeader
assert.strictEqual(xmlHeader["@encoding"], "UTF-8", filePath)
assert.strictEqual(xmlHeader["@version"], "1.0", filePath)
break
}
case "DOSSIER_LEGISLATIF": {
const [dossierLegislatif, error] = auditChain(
auditDossierLegislatif,
auditRequire,
)(strictAudit, element) as [DossierLegislatif, unknown]
assert.strictEqual(
error,
null,
`Unexpected format for DOSSIER_LEGISLATIF:\n${JSON.stringify(
dossierLegislatif,
null,
2,
)}\nError:\n${JSON.stringify(error, null, 2)}`,
)
return dossierLegislatif
}
default: {
console.warn(
`Unexpected root element "${key}" in XML file: ${filePath}`,
)
return undefined
}
}
}
return undefined
} catch (e) {
console.error("An error occurred while parsing XML file", filePath)
throw e
}
}

1
src/lib/parsers/index.ts Normal file
View file

@ -0,0 +1 @@
export { parseDossierLegislatif } from "./dole"

View file

@ -1,31 +1,13 @@
import { auditChain, auditRequire, strictAudit } from "@auditors/core"
import assert from "assert"
import { XMLParser } from "fast-xml-parser"
import fs from "fs-extra"
import he from "he"
import path from "path"
import type { JSONValue } from "postgres"
import sade from "sade"
import { auditDossierLegislatif } from "$lib/auditors/dole"
import type { DossierLegislatif, XmlHeader } from "$lib/legal"
import { parseDossierLegislatif } from "$lib/parsers"
import { db } from "$lib/server/databases"
import { walkDir } from "$lib/server/file_systems"
const xmlParser = new XMLParser({
attributeNamePrefix: "@",
ignoreAttributes: false,
stopNodes: [
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_1",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_2",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_3",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_4",
"DOSSIER_LEGISLATIF.CONTENU.CONTENU_DOSSIER_5",
"DOSSIER_LEGISLATIF.CONTENU.EXPOSE_MOTIF",
],
tagValueProcessor: (_tagName, tagValue) => he.decode(tagValue),
})
async function importDole(
dilaDir: string,
{ resume }: { resume?: string } = {},
@ -65,32 +47,10 @@ async function importDole(
const xmlString: string = await fs.readFile(filePath, {
encoding: "utf8",
})
const xmlData = xmlParser.parse(xmlString)
for (const [key, element] of Object.entries(xmlData) as [
string,
DossierLegislatif | XmlHeader,
][]) {
switch (key) {
case "?xml": {
const xmlHeader = element as XmlHeader
assert.strictEqual(xmlHeader["@encoding"], "UTF-8", filePath)
assert.strictEqual(xmlHeader["@version"], "1.0", filePath)
break
const dossierLegislatif = parseDossierLegislatif(filePath, xmlString)
if (dossierLegislatif === undefined) {
break iterXmlFiles
}
case "DOSSIER_LEGISLATIF": {
const [dossierLegislatif, error] = auditChain(
auditDossierLegislatif,
auditRequire,
)(strictAudit, element) as [DossierLegislatif, unknown]
assert.strictEqual(
error,
null,
`Unexpected format for DOSSIER_LEGISLATIF:\n${JSON.stringify(
dossierLegislatif,
null,
2,
)}\nError:\n${JSON.stringify(error, null, 2)}`,
)
await db`
INSERT INTO dossier_legislatif (
id,
@ -106,16 +66,6 @@ async function importDole(
dossierLegislatifRemainingIds.delete(
dossierLegislatif.META.META_COMMUN.ID,
)
break
}
default: {
console.warn(
`Unexpected root element "${key}" in XML file: ${filePath}`,
)
break iterXmlFiles
}
}
}
} catch (e) {
console.error("An error occurred while parsing XML file", filePath)
throw e