Skip to content

Commit

Permalink
feat(parser): added to_gedcom_string method
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyaurel committed May 16, 2020
1 parent bd67e21 commit 99b7ac5
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 26 deletions.
92 changes: 72 additions & 20 deletions docs/gedcom/parser.html
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ <h1 class="title">Module <code>gedcom.parser</code></h1>
for family_member in family.get_child_elements():

if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
and family_member.get_value() == individual.get_pointer():
and family_member.get_value() == individual.get_pointer():

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

# Other methods

def to_gedcom_string(self, recursive=False):
&#34;&#34;&#34;Formats all elements and optionally all of the sub-elements into a GEDCOM string
:type recursive: bool
&#34;&#34;&#34;
is_gte_python_3 = version_info[0] &gt;= 3
output = &#39;&#39; if is_gte_python_3 else b&#39;&#39;

for element in self.get_root_child_elements():
if is_gte_python_3:
output += element.to_gedcom_string(recursive)
else:
output += element.to_gedcom_string(recursive).encode(&#39;utf-8-sig&#39;)

return output

def print_gedcom(self):
&#34;&#34;&#34;Write GEDCOM data to stdout&#34;&#34;&#34;
from sys import stdout
self.save_gedcom(stdout)

def save_gedcom(self, open_file):
def save_gedcom(self, open_file, recursive=True):
&#34;&#34;&#34;Save GEDCOM data to a file
:type open_file: file
:type recursive: bool
&#34;&#34;&#34;
if version_info[0] &gt;= 3:
open_file.write(self.get_root_element().to_gedcom_string(True))
else:
open_file.write(self.get_root_element().to_gedcom_string(True).encode(&#39;utf-8-sig&#39;))</code></pre>
open_file.write(self.to_gedcom_string(recursive))</code></pre>
</details>
</section>
<section>
Expand Down Expand Up @@ -989,7 +1002,7 @@ <h3>Ancestors</h3>
for family_member in family.get_child_elements():

if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
and family_member.get_value() == individual.get_pointer():
and family_member.get_value() == individual.get_pointer():

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

# Other methods

def to_gedcom_string(self, recursive=False):
&#34;&#34;&#34;Formats all elements and optionally all of the sub-elements into a GEDCOM string
:type recursive: bool
&#34;&#34;&#34;
is_gte_python_3 = version_info[0] &gt;= 3
output = &#39;&#39; if is_gte_python_3 else b&#39;&#39;

for element in self.get_root_child_elements():
if is_gte_python_3:
output += element.to_gedcom_string(recursive)
else:
output += element.to_gedcom_string(recursive).encode(&#39;utf-8-sig&#39;)

return output

def print_gedcom(self):
&#34;&#34;&#34;Write GEDCOM data to stdout&#34;&#34;&#34;
from sys import stdout
self.save_gedcom(stdout)

def save_gedcom(self, open_file):
def save_gedcom(self, open_file, recursive=True):
&#34;&#34;&#34;Save GEDCOM data to a file
:type open_file: file
:type recursive: bool
&#34;&#34;&#34;
if version_info[0] &gt;= 3:
open_file.write(self.get_root_element().to_gedcom_string(True))
else:
open_file.write(self.get_root_element().to_gedcom_string(True).encode(&#39;utf-8-sig&#39;))</code></pre>
open_file.write(self.to_gedcom_string(recursive))</code></pre>
</details>
<h3>Methods</h3>
<dl>
Expand Down Expand Up @@ -1450,7 +1476,7 @@ <h3>Methods</h3>
for family_member in family.get_child_elements():

if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
and family_member.get_value() == individual.get_pointer():
and family_member.get_value() == individual.get_pointer():

for child in family_member.get_child_elements():
if child.get_value() == &#34;Natural&#34;:
Expand Down Expand Up @@ -1649,23 +1675,48 @@ <h3>Methods</h3>
</details>
</dd>
<dt id="gedcom.parser.Parser.save_gedcom"><code class="name flex">
<span>def <span class="ident">save_gedcom</span></span>(<span>self, open_file)</span>
<span>def <span class="ident">save_gedcom</span></span>(<span>self, open_file, recursive=True)</span>
</code></dt>
<dd>
<section class="desc"><p>Save GEDCOM data to a file
:type open_file: file</p></section>
:type open_file: file
:type recursive: bool</p></section>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def save_gedcom(self, open_file):
<pre><code class="python">def save_gedcom(self, open_file, recursive=True):
&#34;&#34;&#34;Save GEDCOM data to a file
:type open_file: file
:type recursive: bool
&#34;&#34;&#34;
if version_info[0] &gt;= 3:
open_file.write(self.get_root_element().to_gedcom_string(True))
else:
open_file.write(self.get_root_element().to_gedcom_string(True).encode(&#39;utf-8-sig&#39;))</code></pre>
open_file.write(self.to_gedcom_string(recursive))</code></pre>
</details>
</dd>
<dt id="gedcom.parser.Parser.to_gedcom_string"><code class="name flex">
<span>def <span class="ident">to_gedcom_string</span></span>(<span>self, recursive=False)</span>
</code></dt>
<dd>
<section class="desc"><p>Formats all elements and optionally all of the sub-elements into a GEDCOM string
:type recursive: bool</p></section>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def to_gedcom_string(self, recursive=False):
&#34;&#34;&#34;Formats all elements and optionally all of the sub-elements into a GEDCOM string
:type recursive: bool
&#34;&#34;&#34;
is_gte_python_3 = version_info[0] &gt;= 3
output = &#39;&#39; if is_gte_python_3 else b&#39;&#39;

for element in self.get_root_child_elements():
if is_gte_python_3:
output += element.to_gedcom_string(recursive)
else:
output += element.to_gedcom_string(recursive).encode(&#39;utf-8-sig&#39;)

return output</code></pre>
</details>
</dd>
</dl>
Expand Down Expand Up @@ -1710,6 +1761,7 @@ <h4><code><a title="gedcom.parser.Parser" href="#gedcom.parser.Parser">Parser</a
<li><code><a title="gedcom.parser.Parser.parse_file" href="#gedcom.parser.Parser.parse_file">parse_file</a></code></li>
<li><code><a title="gedcom.parser.Parser.print_gedcom" href="#gedcom.parser.Parser.print_gedcom">print_gedcom</a></code></li>
<li><code><a title="gedcom.parser.Parser.save_gedcom" href="#gedcom.parser.Parser.save_gedcom">save_gedcom</a></code></li>
<li><code><a title="gedcom.parser.Parser.to_gedcom_string" href="#gedcom.parser.Parser.to_gedcom_string">to_gedcom_string</a></code></li>
</ul>
</li>
</ul>
Expand Down
25 changes: 19 additions & 6 deletions gedcom/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def get_parents(self, individual, parent_type="ALL"):
for family_member in family.get_child_elements():

if family_member.get_tag() == gedcom.tags.GEDCOM_TAG_CHILD \
and family_member.get_value() == individual.get_pointer():
and family_member.get_value() == individual.get_pointer():

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

# Other methods

def to_gedcom_string(self, recursive=False):
"""Formats all elements and optionally all of the sub-elements into a GEDCOM string
:type recursive: bool
"""
is_gte_python_3 = version_info[0] >= 3
output = '' if is_gte_python_3 else b''

for element in self.get_root_child_elements():
if is_gte_python_3:
output += element.to_gedcom_string(recursive)
else:
output += element.to_gedcom_string(recursive).encode('utf-8-sig')

return output

def print_gedcom(self):
"""Write GEDCOM data to stdout"""
from sys import stdout
self.save_gedcom(stdout)

def save_gedcom(self, open_file):
def save_gedcom(self, open_file, recursive=True):
"""Save GEDCOM data to a file
:type open_file: file
:type recursive: bool
"""
if version_info[0] >= 3:
open_file.write(self.get_root_element().to_gedcom_string(True))
else:
open_file.write(self.get_root_element().to_gedcom_string(True).encode('utf-8-sig'))
open_file.write(self.to_gedcom_string(recursive))
54 changes: 54 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,60 @@ def test_parse_from_string():
assert element_2_children[3].get_value() == '@I84@'


def test_to_gedcom_string():
# From string
case_1 = """0 @I5@ INDI
1 NAME First /Last/
1 SEX M
1 BIRT
2 DATE 1 JAN 1900
2 PLAC Kirkland, King, Washington, USA
3 MAP
4 LATI N47.680663
4 LONG W122.234319
"""

gedcom_parser = Parser()
gedcom_parser.parse([(a + '\n').encode('utf-8-sig') for a in case_1.splitlines()])

case_1_string_array = case_1.splitlines()
gedcom_string = gedcom_parser.to_gedcom_string(True)
gedcom_string_array = gedcom_string.splitlines()

# Check number of lines
assert len(case_1_string_array) == len(gedcom_string_array) == 9

# Check each line
for i in range(len(case_1_string_array)):
assert case_1_string_array[i] == gedcom_string_array[i]

# Check whole file string
assert gedcom_string == case_1

# From file
case_2 = ""

with open('tests/files/Musterstammbaum.ged', 'rb') as gedcom_stream:
for line in gedcom_stream:
case_2 += line.decode('utf-8-sig')

gedcom_parser.parse_file('tests/files/Musterstammbaum.ged')

case_2_string_array = case_2.splitlines()
gedcom_string = gedcom_parser.to_gedcom_string(True)
gedcom_string_array = gedcom_string.splitlines()

# Check number of lines
assert len(case_2_string_array) == len(gedcom_string_array) == 396

# Check each line
for i in range(len(case_2_string_array)):
assert case_2_string_array[i] == gedcom_string_array[i]

# Check whole file string
assert gedcom_string == case_2


def test___parse_line():
# @TODO Add appropriate testing cases
pass

0 comments on commit 99b7ac5

Please sign in to comment.