Skip to content

Commit a1b4da6

Browse files
nickreynkeNicklas Reincke
authored andcommitted
feat(parser): added to_gedcom_string method
1 parent b294332 commit a1b4da6

File tree

3 files changed

+145
-26
lines changed

3 files changed

+145
-26
lines changed

docs/gedcom/parser.html

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ <h1 class="title">Module <code>gedcom.parser</code></h1>
464464
for family_member in family.get_child_elements():
465465

466466
if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
467-
and family_member.get_value() == individual.get_pointer():
467+
and family_member.get_value() == individual.get_pointer():
468468

469469
for child in family_member.get_child_elements():
470470
if child.get_value() == &#34;Natural&#34;:
@@ -546,19 +546,32 @@ <h1 class="title">Module <code>gedcom.parser</code></h1>
546546

547547
# Other methods
548548

549+
def to_gedcom_string(self, recursive=False):
550+
&#34;&#34;&#34;Formats all elements and optionally all of the sub-elements into a GEDCOM string
551+
:type recursive: bool
552+
&#34;&#34;&#34;
553+
is_gte_python_3 = version_info[0] &gt;= 3
554+
output = &#39;&#39; if is_gte_python_3 else b&#39;&#39;
555+
556+
for element in self.get_root_child_elements():
557+
if is_gte_python_3:
558+
output += element.to_gedcom_string(recursive)
559+
else:
560+
output += element.to_gedcom_string(recursive).encode(&#39;utf-8-sig&#39;)
561+
562+
return output
563+
549564
def print_gedcom(self):
550565
&#34;&#34;&#34;Write GEDCOM data to stdout&#34;&#34;&#34;
551566
from sys import stdout
552567
self.save_gedcom(stdout)
553568

554-
def save_gedcom(self, open_file):
569+
def save_gedcom(self, open_file, recursive=True):
555570
&#34;&#34;&#34;Save GEDCOM data to a file
556571
:type open_file: file
572+
:type recursive: bool
557573
&#34;&#34;&#34;
558-
if version_info[0] &gt;= 3:
559-
open_file.write(self.get_root_element().to_gedcom_string(True))
560-
else:
561-
open_file.write(self.get_root_element().to_gedcom_string(True).encode(&#39;utf-8-sig&#39;))</code></pre>
574+
open_file.write(self.to_gedcom_string(recursive))</code></pre>
562575
</details>
563576
</section>
564577
<section>
@@ -989,7 +1002,7 @@ <h3>Ancestors</h3>
9891002
for family_member in family.get_child_elements():
9901003

9911004
if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
992-
and family_member.get_value() == individual.get_pointer():
1005+
and family_member.get_value() == individual.get_pointer():
9931006

9941007
for child in family_member.get_child_elements():
9951008
if child.get_value() == &#34;Natural&#34;:
@@ -1071,19 +1084,32 @@ <h3>Ancestors</h3>
10711084

10721085
# Other methods
10731086

1087+
def to_gedcom_string(self, recursive=False):
1088+
&#34;&#34;&#34;Formats all elements and optionally all of the sub-elements into a GEDCOM string
1089+
:type recursive: bool
1090+
&#34;&#34;&#34;
1091+
is_gte_python_3 = version_info[0] &gt;= 3
1092+
output = &#39;&#39; if is_gte_python_3 else b&#39;&#39;
1093+
1094+
for element in self.get_root_child_elements():
1095+
if is_gte_python_3:
1096+
output += element.to_gedcom_string(recursive)
1097+
else:
1098+
output += element.to_gedcom_string(recursive).encode(&#39;utf-8-sig&#39;)
1099+
1100+
return output
1101+
10741102
def print_gedcom(self):
10751103
&#34;&#34;&#34;Write GEDCOM data to stdout&#34;&#34;&#34;
10761104
from sys import stdout
10771105
self.save_gedcom(stdout)
10781106

1079-
def save_gedcom(self, open_file):
1107+
def save_gedcom(self, open_file, recursive=True):
10801108
&#34;&#34;&#34;Save GEDCOM data to a file
10811109
:type open_file: file
1110+
:type recursive: bool
10821111
&#34;&#34;&#34;
1083-
if version_info[0] &gt;= 3:
1084-
open_file.write(self.get_root_element().to_gedcom_string(True))
1085-
else:
1086-
open_file.write(self.get_root_element().to_gedcom_string(True).encode(&#39;utf-8-sig&#39;))</code></pre>
1112+
open_file.write(self.to_gedcom_string(recursive))</code></pre>
10871113
</details>
10881114
<h3>Methods</h3>
10891115
<dl>
@@ -1450,7 +1476,7 @@ <h3>Methods</h3>
14501476
for family_member in family.get_child_elements():
14511477

14521478
if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
1453-
and family_member.get_value() == individual.get_pointer():
1479+
and family_member.get_value() == individual.get_pointer():
14541480

14551481
for child in family_member.get_child_elements():
14561482
if child.get_value() == &#34;Natural&#34;:
@@ -1649,23 +1675,48 @@ <h3>Methods</h3>
16491675
</details>
16501676
</dd>
16511677
<dt id="gedcom.parser.Parser.save_gedcom"><code class="name flex">
1652-
<span>def <span class="ident">save_gedcom</span></span>(<span>self, open_file)</span>
1678+
<span>def <span class="ident">save_gedcom</span></span>(<span>self, open_file, recursive=True)</span>
16531679
</code></dt>
16541680
<dd>
16551681
<section class="desc"><p>Save GEDCOM data to a file
1656-
:type open_file: file</p></section>
1682+
:type open_file: file
1683+
:type recursive: bool</p></section>
16571684
<details class="source">
16581685
<summary>
16591686
<span>Expand source code</span>
16601687
</summary>
1661-
<pre><code class="python">def save_gedcom(self, open_file):
1688+
<pre><code class="python">def save_gedcom(self, open_file, recursive=True):
16621689
&#34;&#34;&#34;Save GEDCOM data to a file
16631690
:type open_file: file
1691+
:type recursive: bool
16641692
&#34;&#34;&#34;
1665-
if version_info[0] &gt;= 3:
1666-
open_file.write(self.get_root_element().to_gedcom_string(True))
1667-
else:
1668-
open_file.write(self.get_root_element().to_gedcom_string(True).encode(&#39;utf-8-sig&#39;))</code></pre>
1693+
open_file.write(self.to_gedcom_string(recursive))</code></pre>
1694+
</details>
1695+
</dd>
1696+
<dt id="gedcom.parser.Parser.to_gedcom_string"><code class="name flex">
1697+
<span>def <span class="ident">to_gedcom_string</span></span>(<span>self, recursive=False)</span>
1698+
</code></dt>
1699+
<dd>
1700+
<section class="desc"><p>Formats all elements and optionally all of the sub-elements into a GEDCOM string
1701+
:type recursive: bool</p></section>
1702+
<details class="source">
1703+
<summary>
1704+
<span>Expand source code</span>
1705+
</summary>
1706+
<pre><code class="python">def to_gedcom_string(self, recursive=False):
1707+
&#34;&#34;&#34;Formats all elements and optionally all of the sub-elements into a GEDCOM string
1708+
:type recursive: bool
1709+
&#34;&#34;&#34;
1710+
is_gte_python_3 = version_info[0] &gt;= 3
1711+
output = &#39;&#39; if is_gte_python_3 else b&#39;&#39;
1712+
1713+
for element in self.get_root_child_elements():
1714+
if is_gte_python_3:
1715+
output += element.to_gedcom_string(recursive)
1716+
else:
1717+
output += element.to_gedcom_string(recursive).encode(&#39;utf-8-sig&#39;)
1718+
1719+
return output</code></pre>
16691720
</details>
16701721
</dd>
16711722
</dl>
@@ -1710,6 +1761,7 @@ <h4><code><a title="gedcom.parser.Parser" href="#gedcom.parser.Parser">Parser</a
17101761
<li><code><a title="gedcom.parser.Parser.parse_file" href="#gedcom.parser.Parser.parse_file">parse_file</a></code></li>
17111762
<li><code><a title="gedcom.parser.Parser.print_gedcom" href="#gedcom.parser.Parser.print_gedcom">print_gedcom</a></code></li>
17121763
<li><code><a title="gedcom.parser.Parser.save_gedcom" href="#gedcom.parser.Parser.save_gedcom">save_gedcom</a></code></li>
1764+
<li><code><a title="gedcom.parser.Parser.to_gedcom_string" href="#gedcom.parser.Parser.to_gedcom_string">to_gedcom_string</a></code></li>
17131765
</ul>
17141766
</li>
17151767
</ul>

gedcom/parser.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def get_parents(self, individual, parent_type="ALL"):
435435
for family_member in family.get_child_elements():
436436

437437
if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
438-
and family_member.get_value() == individual.get_pointer():
438+
and family_member.get_value() == individual.get_pointer():
439439

440440
for child in family_member.get_child_elements():
441441
if child.get_value() == "Natural":
@@ -517,16 +517,29 @@ def get_family_members(self, family, members_type=FAMILY_MEMBERS_TYPE_ALL):
517517

518518
# Other methods
519519

520+
def to_gedcom_string(self, recursive=False):
521+
"""Formats all elements and optionally all of the sub-elements into a GEDCOM string
522+
:type recursive: bool
523+
"""
524+
is_gte_python_3 = version_info[0] >= 3
525+
output = '' if is_gte_python_3 else b''
526+
527+
for element in self.get_root_child_elements():
528+
if is_gte_python_3:
529+
output += element.to_gedcom_string(recursive)
530+
else:
531+
output += element.to_gedcom_string(recursive).encode('utf-8-sig')
532+
533+
return output
534+
520535
def print_gedcom(self):
521536
"""Write GEDCOM data to stdout"""
522537
from sys import stdout
523538
self.save_gedcom(stdout)
524539

525-
def save_gedcom(self, open_file):
540+
def save_gedcom(self, open_file, recursive=True):
526541
"""Save GEDCOM data to a file
527542
:type open_file: file
543+
:type recursive: bool
528544
"""
529-
if version_info[0] >= 3:
530-
open_file.write(self.get_root_element().to_gedcom_string(True))
531-
else:
532-
open_file.write(self.get_root_element().to_gedcom_string(True).encode('utf-8-sig'))
545+
open_file.write(self.to_gedcom_string(recursive))

tests/test_parser.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,60 @@ def test_parse_from_string():
9898
assert element_2_children[3].get_value() == '@I84@'
9999

100100

101+
def test_to_gedcom_string():
102+
# From string
103+
case_1 = """0 @I5@ INDI
104+
1 NAME First /Last/
105+
1 SEX M
106+
1 BIRT
107+
2 DATE 1 JAN 1900
108+
2 PLAC Kirkland, King, Washington, USA
109+
3 MAP
110+
4 LATI N47.680663
111+
4 LONG W122.234319
112+
"""
113+
114+
gedcom_parser = Parser()
115+
gedcom_parser.parse([(a + '\n').encode('utf-8-sig') for a in case_1.splitlines()])
116+
117+
case_1_string_array = case_1.splitlines()
118+
gedcom_string = gedcom_parser.to_gedcom_string(True)
119+
gedcom_string_array = gedcom_string.splitlines()
120+
121+
# Check number of lines
122+
assert len(case_1_string_array) == len(gedcom_string_array) == 9
123+
124+
# Check each line
125+
for i in range(len(case_1_string_array)):
126+
assert case_1_string_array[i] == gedcom_string_array[i]
127+
128+
# Check whole file string
129+
assert gedcom_string == case_1
130+
131+
# From file
132+
case_2 = ""
133+
134+
with open('tests/files/Musterstammbaum.ged', 'rb') as gedcom_stream:
135+
for line in gedcom_stream:
136+
case_2 += line.decode('utf-8-sig')
137+
138+
gedcom_parser.parse_file('tests/files/Musterstammbaum.ged')
139+
140+
case_2_string_array = case_2.splitlines()
141+
gedcom_string = gedcom_parser.to_gedcom_string(True)
142+
gedcom_string_array = gedcom_string.splitlines()
143+
144+
# Check number of lines
145+
assert len(case_2_string_array) == len(gedcom_string_array) == 396
146+
147+
# Check each line
148+
for i in range(len(case_2_string_array)):
149+
assert case_2_string_array[i] == gedcom_string_array[i]
150+
151+
# Check whole file string
152+
assert gedcom_string == case_2
153+
154+
101155
def test___parse_line():
102156
# @TODO Add appropriate testing cases
103157
pass

0 commit comments

Comments
 (0)