Skip to content

Commit

Permalink
Merge pull request #323 from statisticsnorway/add_create_filename
Browse files Browse the repository at this point in the history
Added function create_isee_filename. updated tests, readme.md
  • Loading branch information
vidstra authored Feb 16, 2024
2 parents bbf1161 + 6db616a commit b2524a5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ FELTNAVN: the name of the xml-tags concatenated together for each level in the X
FELTVERDI: the value of the xml-tag.
LEVEL: A list with information about the concatenation level. If one or more of the values is greater than 1, it means there are repeating values in the tag.

### Create filename for use in ISEE
If you need to transfer ISEE-data to the On-Prem-platform, the .csv-filename need a spesific format. The function `create_isee_filename` can create this filename from the filepath and contents of a XML-file.

```python
from altinn import create_isee_filename

file = "gs://ssb-prod-dapla-felles-data-delt/altinn3/RA-0595/2023/2/6/810409282_460784f978a2_ebc7af7e-4ebe-4883-b844-66ee6292a93a/form_460784f978a2.xml"

create_isee_filename(file)
```
In the example above the output will be `RA-0595A3_460784f978a2.csv`. This can be used to build a new filepath to where you need to store the result after the XML is transformed to ISEE-format.


### Get information about a file
Expand Down
9 changes: 8 additions & 1 deletion src/altinn/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""SSB Altinn Python."""

from .file import FileInfo
from .flatten import create_isee_filename
from .flatten import isee_transform
from .flatten import xml_transform
from .parser import ParseSingleXml

__all__ = ["FileInfo", "ParseSingleXml", "isee_transform", "xml_transform"]
__all__ = [
"FileInfo",
"ParseSingleXml",
"isee_transform",
"xml_transform",
"create_isee_filename",
]
37 changes: 37 additions & 0 deletions src/altinn/flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import re
import xml.etree.ElementTree as ET
from collections.abc import MutableMapping
from typing import Any
from typing import Optional
Expand Down Expand Up @@ -344,3 +345,39 @@ def xml_transform(file_path: str) -> pd.DataFrame:
else:
error_message = f"File is not a valid XML-file: {file_path}"
raise ValueError(error_message)


def create_isee_filename(file_path: str) -> str | None:
"""Creates a filename based on the contents of an XML file and the provided file path.
Args:
file_path: The path to the XML file.
Returns:
The generated filename if successful, otherwise None.
"""
# Read XML-file
if utils.is_gcs(file_path):
fs = FileClient.get_gcs_file_system()
with fs.open(file_path, mode="r") as f:
xml_content = f.read()

else:
with open(file_path) as f:
xml_content = f.read()

# Parse the XML content
root = ET.fromstring(xml_content)

# Find the value of raNummer
ra_nummer_element = root.find(".//InternInfo/raNummer")
if ra_nummer_element is not None:
ra_nummer_value = ra_nummer_element.text

# find angiver_id
angiver_id = _extract_angiver_id(file_path)

# Create the filename
filename = f"{ra_nummer_value}A3_{angiver_id}.csv"

return filename

0 comments on commit b2524a5

Please sign in to comment.