diff --git a/docs/gedcom/parser.html b/docs/gedcom/parser.html index d85b354..1e6d4aa 100644 --- a/docs/gedcom/parser.html +++ b/docs/gedcom/parser.html @@ -464,7 +464,7 @@

Module gedcom.parser

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": @@ -546,19 +546,32 @@

Module gedcom.parser

# 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))
@@ -989,7 +1002,7 @@

Ancestors

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": @@ -1071,19 +1084,32 @@

Ancestors

# 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))

Methods

@@ -1450,7 +1476,7 @@

Methods

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": @@ -1649,23 +1675,48 @@

Methods

-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 open_file: file +:type recursive: bool

Expand source code -
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))
+
+ +
+def to_gedcom_string(self, recursive=False) +
+
+

Formats all elements and optionally all of the sub-elements into a GEDCOM string +:type recursive: bool

+
+ +Expand source code + +
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
@@ -1710,6 +1761,7 @@

Parserparse_file
  • print_gedcom
  • save_gedcom
  • +
  • to_gedcom_string
  • diff --git a/gedcom/parser.py b/gedcom/parser.py index 4f1f113..473ad9e 100644 --- a/gedcom/parser.py +++ b/gedcom/parser.py @@ -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": @@ -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)) diff --git a/tests/test_parser.py b/tests/test_parser.py index 86df008..451a3fa 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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