Skip to content

Fix bugs and enhance #14

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions doxygen/configParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ConfigParser:
"""

def __init__(self):
self.__single_line_option_regex = re.compile("^\s*(\w+)\s*=\s*([^\\\\]*)\s*$")
self.__first_line_of_multine_option_regex = re.compile("^\s*(\w+)\s*=\s*(.*)\\\\$")
self.__single_line_option_regex = re.compile(r"^\s*(\w+)\s*=\s*([^\\]*)\s*$")
self.__first_line_of_multiline_option_regex = re.compile(r"^\s*(\w+)\s*=\s*(|.*\S)\s*\\$")

def load_configuration(self, doxyfile: str) -> dict:
"""
Expand Down Expand Up @@ -45,7 +45,8 @@ def load_configuration(self, doxyfile: str) -> dict:
if not line.endswith('\\'):
in_multiline_option = False
option_value = line.rstrip('\\').strip()
configuration[current_multiline_option_name].append(option_value)
unquoted_option_value = self.__remove_double_quote_if_required(option_value)
configuration[current_multiline_option_name].append(unquoted_option_value)

elif self.__is_first_line_of_multiline_option(line):
current_multiline_option_name, option_value = self.__extract_multiline_option_name_and_first_value(line)
Expand Down Expand Up @@ -89,7 +90,7 @@ def __extract_multiline_option_name_and_first_value(self, line) -> (str, str):
:raise ParseException: When process fail to extract data
"""

matches = self.__first_line_of_multine_option_regex.search(line)
matches = self.__first_line_of_multiline_option_regex.search(line)
if matches is None or len(matches.groups()) != 2:
logging.error("Impossible to extract first value off multi line option from: {}" % line)
raise ParseException("Impossible to extract first value off multi line option from: {}" % line)
Expand Down Expand Up @@ -120,7 +121,7 @@ def __is_comment_line(self, line: str) -> bool:
return line.startswith("#")

def __is_first_line_of_multiline_option(self, line) -> bool:
return self.__first_line_of_multine_option_regex.match(line) is not None
return self.__first_line_of_multiline_option_regex.match(line) is not None

@staticmethod
def __remove_double_quote_if_required(option_value: str) -> str:
Expand Down
49 changes: 47 additions & 2 deletions tests/test_doxygenConfigParser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import unittest

import os
import unittest
from typing import List

from doxygen import ConfigParser

Expand All @@ -13,6 +13,7 @@ def __init__(self, *args, **kwargs):
logging.disable(logging.CRITICAL)
self.doxyfile_original = os.path.join(os.path.dirname(__file__), "assets/Doxyfile")
self.doxyfile_working = "./Doxyfile.tmp"
self.doxyfile_clone = "./Cloned_Doxyfile.tmp"

def test_try_use_not_existing_doxyfile(self):
config_parser = ConfigParser()
Expand All @@ -22,6 +23,9 @@ def tearDown(self):
if os.path.exists(self.doxyfile_working):
os.remove(self.doxyfile_working)

if os.path.exists(self.doxyfile_clone):
os.remove(self.doxyfile_clone)

def test_load_configuration(self):
config_parser = ConfigParser()
configuration = config_parser.load_configuration(self.doxyfile_original)
Expand Down Expand Up @@ -51,6 +55,47 @@ def test_update_configuration(self):
self.assertEqual(configuration_updated['PROJECT_NUMBER'], '1.2.3.4')
self.assertTrue("*.dtc" in configuration_updated['FILE_PATTERNS'])

def test_multiline_option_with_empty_lines(self):
configuration = self.get_configuration_from_lines([
'MULTILINE_OPTION = \\',
' \\',
' line3',
])

self.assertEqual(['', '', 'line3'], configuration['MULTILINE_OPTION'])

def test_multiline_option(self):
configuration = self.get_configuration_from_lines([
'MULTILINE_OPTION = line1 \\',
' line2 \\',
' line3',
])

self.assertEqual(['line1', 'line2', 'line3'], configuration['MULTILINE_OPTION'])

def test_quoted_multiline_option(self):
configuration = self.get_configuration_from_lines([
'MULTILINE_OPTION = "line 1" \\',
' "line 2" \\',
' "line 3"',
])

self.assertEqual(['line 1', 'line 2', 'line 3'], configuration['MULTILINE_OPTION'])

def get_configuration_from_lines(self, lines: List[str]) -> dict:
"""Writes lines into a Doxyfile and reads it. This will also write and load the configuration again to check if
the config_parser changes it
"""
with open(self.doxyfile_working, 'w') as io:
io.write('\n'.join(lines))

parser = ConfigParser()
configuration = parser.load_configuration(self.doxyfile_working)
parser.store_configuration(configuration, self.doxyfile_clone)
other_configuration = parser.load_configuration(self.doxyfile_clone)
self.assertEqual(configuration, other_configuration)
return configuration


if __name__ == '__main__':
unittest.main()