Skip to content

Commit 39734f0

Browse files
authored
Allow EDI file to have something before UNA or UNB. It can happens with files generated by the software SAGE COALA. (#77)
Allow EDI file to have something before UNA or UNB. This can happen with files generated by the software SAGE COALA. * Added a unit test with a file * Improve syntax quality * Improve algo to find UNA when the file does not start by UNA
1 parent d9bb320 commit 39734f0

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

pydifact/parser.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,25 @@ def parse(
5252
# If there is a UNA, take the following 6 characters
5353
# unconditionally, strip them, and make control Characters()
5454
# for further parsing
55-
una_found = message[0:3] == "UNA"
55+
56+
# If it starts by UNA
57+
una_pattern = "UNA"
58+
if message.startswith(una_pattern):
59+
idx_una = 0
60+
# Otherwise we look for UNA, so to avoid finding "lorem ipsum UNA lorem ipsum" we look for the segment separator following by UNA.
61+
else:
62+
una_pattern = "'UNA"
63+
idx_una = message.find(una_pattern)
64+
una_found = idx_una != -1
5665

5766
if una_found:
58-
characters = Characters.from_str("UNA" + message[3:9])
67+
idx_begin = idx_una + len(una_pattern)
68+
idx_end = idx_begin + 6
69+
characters = Characters.from_str(f"UNA{message[idx_begin: idx_end]}")
5970

60-
# remove the UNA segment from the string
61-
message = message[9:].lstrip("\r\n")
71+
# remove the UNA segment from the string,
72+
# ignore everything before UNA because it should be the first segment if una_found.
73+
message = message[idx_end:].lstrip("\r\n")
6274

6375
else:
6476
# if no UNA header present, use default control characters

tests/data/sage_coala.ped

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DOC+01=serveur\grps\dossiers\BLABLApatr+02=9215001+03=04/07/2024+04=FD2401+05=24+06=DGFiP+07=EDITDFC+08=32489+09=99999999800028+10=BLABLApatr+11=BLABLA PATRIMOINE+12=99999999900017++14=FR96999999999+15=corinne+16=isrn+17=01/01/2023+18=31/12/[email protected]+27=++29=++++++35={55D57981-DA87-426D-911E-464844992C9C}'UNA:+,? 'UNB+UNOL:3+99999999800028:5:I+9215001:146+240704:1032+20241861032cor+++++TDT-PED-IN-TD2401'UNG+INFENT+NON_SECURISE_NON_SIGNE+MULTI_DISTRIBUTION+240704:1032+1+UN+D:00B:FD2401'UNH+00001+INFENT:D:00B:UN:FD2401'BGM+IDF:71:211+INFENT BLABLApatr BI RN IS'DTM+242:20240704:102'RFF+AUM:SAGE Experts Comptables'RFF+AUN:COALA EXPORT EDI::1.0:240315'RFF+AUO:2012.01.0310'NAD+DT+999999999:100:107++BLABLA PATRIMOINE+0000 55 Boulevard grbbaf+Bibor++75016'RFF+AWR:20241861032cor'RFF+ACD:IS1'NAD+FR+99999999800028:100:107++CEC_EDI_TDFC:BLABLADEXPERT::::3+0000 323, rue Saint-Martin+Bibor++75003'RFF+ACD:BLABLApatr BLABLA PATRIMOINE SAGE COALA'NAD+MS+99999999800028:100:107++BLABLADEXPERT+0000 323, rue Saint-Martin+Bibor++75003'RFF+ACD:BLABLApatr BLABLA PATRIMOINE'NAD+MR+9215001:100:268++SAGE+0000 10 traoe de transpor+La petropa taratata++92250'RFF+CU:BLABLApatr BLABLA PATRIMOINE'NAD+HP+++DGI_EDI_TDFC:::::3'SEQ++1'IND++F-IDENTIF 2400000000AANAD'NAD+ZZZ+999999999:100:ZZZ++BLABLA PATRIMOINE+0000 55 Boulevard grbbaf+Bibor++75016'SEQ++2'IND++F-IDENTIF 2400000000ABRFF'RFF+ZZZ:7022Z'SEQ++3'IND++F-IDENTIF 2400000000BACCI'CCI+++TCF:LIS:211'CAV+BI:TCF:211'SEQ++4'IND++F-IDENTIF 2400000000BBCCI'CCI+++TRF:LIS:211'CAV+RN:TRF:211'SEQ++5'IND++F-IDENTIF 2400000000BCCCI'CCI+++TBS:LIS:211'CAV+IS:TBS:211'SEQ++6'IND++F-IDENTIF 2400000000BFCCI'CCI+++TDP:LIS:211'CAV+NOR:TDP:211'SEQ++7'IND++F-IDENTIF 2400000000CADTM'DTM+ZZZ:20230101:102'SEQ++8'IND++F-IDENTIF 2400000000CBDTM'DTM+ZZZ:20231231:102'SEQ++9'IND++F-IDENTIF 2400000000CCDTM'DTM+ZZZ:12:802'SEQ++10'IND++F-IDENTIF 2400000000CDDTM'DTM+ZZZ:20221231:102'SEQ++11'IND++F-IDENTIF 2400000000CEDTM'DTM+ZZZ:12:802'SEQ++12'IND++F-IDENTIF 2400000000DACUX'CUX+3:EUR'SEQ++13'IND++F-IDENTIF 2400000000KDRFF'RFF+ZZZ:IS1'SEQ++14'IND++2050 2400000000ATMOA'MOA+ZZZ:1387'SEQ++15'IND++2050 2400000000AUMOA'MOA+ZZZ:870'SEQ++16'IND++2050 2400000000BJMOA'MOA+ZZZ:265563'SEQ++17'IND++2050 2400000000BKMOA'MOA+ZZZ:870'SEQ++18'IND++2050 2400000000BXMOA'MOA+ZZZ:85899'SEQ++19'IND++2050 2400000000CFMOA'MOA+ZZZ:18439'SEQ++20'IND++2050 2400000000CJMOA'MOA+ZZZ:104338'SEQ++21'IND++2050 2400000000COMOA'MOA+ZZZ:369901'SEQ++22'IND++2050 2400000000CUMOA'MOA+ZZZ:264176'SEQ++23'IND++2050 2400000000DSMOA'MOA+ZZZ:517'SEQ++24'IND++2050 2400000000DWMOA'MOA+ZZZ:264176'SEQ++25'IND++2050 2400000000EBMOA'IND++2069RCI 2400010001BBMOA'MOA+ZZZ:2112'UNT+542+00001'UNE+1+1'UNZ+1+20241861032cor'

tests/test_sage_coala.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# pydifact - a python edifact library
2+
# Copyright (C) 2017-2024 Christian González
3+
#
4+
# This program is free software: you can redistribute it and/or modify
5+
# it under the terms of the GNU Lesser General Public License as published
6+
# by the Free Software Foundation, either version 3 of the License, or
7+
# (at your option) any later version.
8+
#
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU Lesser General Public License for more details.
13+
#
14+
# You should have received a copy of the GNU Lesser General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
import os
17+
from pydifact.segmentcollection import Interchange
18+
from pydifact.segments import Segment
19+
20+
21+
path = os.path.dirname(os.path.realpath(__file__)) + "/data"
22+
23+
24+
def test_sage_coala_file():
25+
"""test parsing a file generated by SAGE COALA"""
26+
interchange = Interchange.from_file("{}/sage_coala.ped".format(path))
27+
assert interchange
28+
assert interchange.get_header_segment() == Segment(
29+
"UNB",
30+
["UNOL", "3"],
31+
["99999999800028", "5", "I"],
32+
["9215001", "146"],
33+
["240704", "1032"],
34+
"20241861032cor",
35+
"",
36+
"",
37+
"",
38+
"",
39+
"TDT-PED-IN-TD2401",
40+
)
41+
assert interchange.get_segment("RFF") == Segment(
42+
"RFF", ["AUM", "SAGE Experts Comptables"]
43+
)
44+
45+
46+
if __name__ == "__main__":
47+
test_sage_coala_file()

0 commit comments

Comments
 (0)