Export function parseDossierLegislatif
This commit is contained in:
parent
45b6d8942c
commit
4d12eab0c3
4 changed files with 90 additions and 69 deletions
|
@ -184,3 +184,4 @@ export {
|
||||||
type TmLienTxt,
|
type TmLienTxt,
|
||||||
type Versions,
|
type Versions,
|
||||||
} from "./legal"
|
} from "./legal"
|
||||||
|
export { parseDossierLegislatif } from "./parsers"
|
||||||
|
|
69
src/lib/parsers/dole.ts
Normal file
69
src/lib/parsers/dole.ts
Normal 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
1
src/lib/parsers/index.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export { parseDossierLegislatif } from "./dole"
|
|
@ -1,31 +1,13 @@
|
||||||
import { auditChain, auditRequire, strictAudit } from "@auditors/core"
|
|
||||||
import assert from "assert"
|
import assert from "assert"
|
||||||
import { XMLParser } from "fast-xml-parser"
|
|
||||||
import fs from "fs-extra"
|
import fs from "fs-extra"
|
||||||
import he from "he"
|
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import type { JSONValue } from "postgres"
|
import type { JSONValue } from "postgres"
|
||||||
import sade from "sade"
|
import sade from "sade"
|
||||||
|
|
||||||
import { auditDossierLegislatif } from "$lib/auditors/dole"
|
import { parseDossierLegislatif } from "$lib/parsers"
|
||||||
import type { DossierLegislatif, XmlHeader } from "$lib/legal"
|
|
||||||
import { db } from "$lib/server/databases"
|
import { db } from "$lib/server/databases"
|
||||||
import { walkDir } from "$lib/server/file_systems"
|
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(
|
async function importDole(
|
||||||
dilaDir: string,
|
dilaDir: string,
|
||||||
{ resume }: { resume?: string } = {},
|
{ resume }: { resume?: string } = {},
|
||||||
|
@ -65,32 +47,10 @@ async function importDole(
|
||||||
const xmlString: string = await fs.readFile(filePath, {
|
const xmlString: string = await fs.readFile(filePath, {
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
})
|
})
|
||||||
const xmlData = xmlParser.parse(xmlString)
|
const dossierLegislatif = parseDossierLegislatif(filePath, xmlString)
|
||||||
for (const [key, element] of Object.entries(xmlData) as [
|
if (dossierLegislatif === undefined) {
|
||||||
string,
|
break iterXmlFiles
|
||||||
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)}`,
|
|
||||||
)
|
|
||||||
await db`
|
await db`
|
||||||
INSERT INTO dossier_legislatif (
|
INSERT INTO dossier_legislatif (
|
||||||
id,
|
id,
|
||||||
|
@ -106,16 +66,6 @@ async function importDole(
|
||||||
dossierLegislatifRemainingIds.delete(
|
dossierLegislatifRemainingIds.delete(
|
||||||
dossierLegislatif.META.META_COMMUN.ID,
|
dossierLegislatif.META.META_COMMUN.ID,
|
||||||
)
|
)
|
||||||
break
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
console.warn(
|
|
||||||
`Unexpected root element "${key}" in XML file: ${filePath}`,
|
|
||||||
)
|
|
||||||
break iterXmlFiles
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("An error occurred while parsing XML file", filePath)
|
console.error("An error occurred while parsing XML file", filePath)
|
||||||
throw e
|
throw e
|
||||||
|
|
Loading…
Reference in a new issue