[PATCH 1/1] Refactor class and main module per comments
Dannie Huang
danxue.huang at gmail.com
Fri Aug 3 02:48:46 UTC 2018
Address multiple comments:
1. Refactored makedoc2rst class.
2. Refactored main module
3. Removed strcmp.rst
On Thu, Aug 2, 2018 at 9:44 PM, Dannie Huang <danxue.huang at gmail.com> wrote:
> ---
> README.md | 4 +-
> gen_rst_from_makedoc.py | 31 +++++----------
> makedoc2rst.py | 99 ++++++++++++++++++++++++++++++
> ++++++++++++++++++
> makedoc2rst_converter.py | 85 -----------------------------------------
> strcmp.rst | 47 -----------------------
> 5 files changed, 110 insertions(+), 156 deletions(-)
> create mode 100644 makedoc2rst.py
> delete mode 100644 makedoc2rst_converter.py
> delete mode 100644 strcmp.rst
>
> diff --git a/README.md b/README.md
> index 8ebb224..64a3ba9 100644
> --- a/README.md
> +++ b/README.md
> @@ -1,2 +1,2 @@
> -# NewlibMarkup2SphinxConvertorPrivate
> -(PRIVATE) This repo contains code for NewlibMarkup2SphinxConvertorPr
> ivate.
> +# NewlibMarkup2SphinxConverter
> +This repo contains code for NewlibMarkup2SphinxConverter
> diff --git a/gen_rst_from_makedoc.py b/gen_rst_from_makedoc.py
> index 4100756..8e4d9b0 100755
> --- a/gen_rst_from_makedoc.py
> +++ b/gen_rst_from_makedoc.py
> @@ -30,7 +30,7 @@
> #
>
> import argparse
> -import makedoc2rst_converter
> +import makedoc2rst
>
>
> def get_parser():
> @@ -38,37 +38,24 @@ def get_parser():
> description='Convert newlib style markup to rst markup'
> )
> parser.add_argument(
> - '-s',
> - '--source_file_path',
> + '-c',
> + '--c_file_path',
> type=str,
> - help='Path of source file with newlib style comments',
> + help='Path of c source file with newlib style comments',
> )
> parser.add_argument(
> - '-d',
> - '--dest_file_path',
> + '-r',
> + '--rst_file_path',
> type=str,
> help='Path of destination file with rst markup',
> )
> return parser
>
>
> -def main(source_file_dir, dest_file_dir):
> - with open(source_file_dir, 'r') as source_file, open(dest_file_dir,
> 'w') as dest_file:
> - file_content = source_file.read()
> -
> - makedoc2rst = makedoc2rst_converter.makedoc2rst_converter()
> - # Get comments inside of /* */
> - comments = makedoc2rst.extract_comments(file_content)
> -
> - # Parse comments
> - command_text_dict = makedoc2rst.extract_command_
> and_text(comments)
> -
> - # Process text based on command type
> - rst_str = makedoc2rst.generate_rst(command_text_dict)
> -
> - dest_file.write(rst_str)
> +def main(c_file, rst_file):
> + makedoc2rst.makedoc2rst(c_file, rst_file).convert()
>
>
> if __name__ == '__main__':
> args = get_parser().parse_args()
> - main(args.source_file_path, args.dest_file_path)
> + main(args.c_file_path, args.rst_file_path)
> diff --git a/makedoc2rst.py b/makedoc2rst.py
> new file mode 100644
> index 0000000..d887323
> --- /dev/null
> +++ b/makedoc2rst.py
> @@ -0,0 +1,99 @@
> +#!/usr/bin/env python
> +#
> +# RTEMS Tools Project (http://www.rtems.org/)
> +# Copyright 2018 Danxue Huang (danxue.huang at gmail.com)
> +# All rights reserved.
> +#
> +# This file is part of the RTEMS Tools package in 'rtems-tools'.
> +#
> +# Redistribution and use in source and binary forms, with or without
> +# modification, are permitted provided that the following conditions are
> met:
> +#
> +# 1. Redistributions of source code must retain the above copyright
> notice,
> +# this list of conditions and the following disclaimer.
> +#
> +# 2. Redistributions in binary form must reproduce the above copyright
> notice,
> +# this list of conditions and the following disclaimer in the
> documentation
> +# and/or other materials provided with the distribution.
> +#
> +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
> IS"
> +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
> THE
> +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> PURPOSE
> +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
> BE
> +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
> THE
> +# POSSIBILITY OF SUCH DAMAGE.
> +#
> +
> +import re
> +import rst
> +
> +
> +class makedoc2rst():
> + """ Convert c source file with makedoc markup to rst markup file
> + c_file: c source file containing block comments (/* */) at the
> beginning
> + rst_file: destination file with rst markup
> + """
> + def __init__(self, c_file, rst_file):
> + self.c_file = c_file
> + self.rst_file = rst_file
> +
> + def convert(self):
> + """ Implementation of converting c file to rst file """
> + rst_str = ''
> + with open(self.c_file, 'r') as c_file:
> + # Get comments inside of /* */
> + comments = self._extract_comments(c_file.read())
> + # Parse comments
> + command_text_dict = self._extract_command_and_text(comments)
> + # Process text based on command type
> + for command, text in command_text_dict.items():
> + rst_str += rst.get_command_processor(command)(command,
> text)
> +
> + with open(self.rst_file, 'w') as rst_file:
> + rst_file.write(rst_str)
> + return rst_str
> +
> + def _is_command(self, s):
> + """
> + A command is a single word of at least 3 characters, all uppercase
> + :param s: input string
> + :return: True if s is a single command, otherwise False
> + """
> + return True if re.match('^[A-Z_]{3,}\s*$', s) else False
> +
> + def _extract_comments(self, content):
> + """
> + Extract content inside of /* */
> + :param content: input file content
> + :return: extracted comments
> + """
> + comments = ''
> + comments_match = re.match('/\*(\*(?!/)|[^*])*\*/', content)
> + if comments_match:
> + wrapped_comments = comments_match.group()
> + comments = wrapped_comments.lstrip('/*').
> rstrip('*/').lstrip().rstrip()
> + return comments
> +
> + def _extract_command_and_text(self, comments):
> + """
> + Extract command and text from input string content
> + :param comments: input string containing command and text
> + :return: a tuple containing command and text
> + """
> + command = ''
> + text = ''
> + command_text_dict = {}
> + for line in comments.splitlines():
> + if self._is_command(line):
> + if command and text:
> + command_text_dict[command] = text
> + command = line.rstrip()
> + text = ''
> + else:
> + text += line + '\n'
> + return command_text_dict
> diff --git a/makedoc2rst_converter.py b/makedoc2rst_converter.py
> deleted file mode 100644
> index e4d2d72..0000000
> --- a/makedoc2rst_converter.py
> +++ /dev/null
> @@ -1,85 +0,0 @@
> -#!/usr/bin/env python
> -#
> -# RTEMS Tools Project (http://www.rtems.org/)
> -# Copyright 2018 Danxue Huang (danxue.huang at gmail.com)
> -# All rights reserved.
> -#
> -# This file is part of the RTEMS Tools package in 'rtems-tools'.
> -#
> -# Redistribution and use in source and binary forms, with or without
> -# modification, are permitted provided that the following conditions are
> met:
> -#
> -# 1. Redistributions of source code must retain the above copyright
> notice,
> -# this list of conditions and the following disclaimer.
> -#
> -# 2. Redistributions in binary form must reproduce the above copyright
> notice,
> -# this list of conditions and the following disclaimer in the
> documentation
> -# and/or other materials provided with the distribution.
> -#
> -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
> IS"
> -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
> THE
> -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
> PURPOSE
> -# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
> BE
> -# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
> -# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
> -# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
> -# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
> -# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
> -# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
> THE
> -# POSSIBILITY OF SUCH DAMAGE.
> -#
> -
> -import re
> -import rst
> -
> -
> -class makedoc2rst_converter():
> - @staticmethod
> - def is_command(s):
> - """
> - A command is a single word of at least 3 characters, all uppercase
> - :param s: input string
> - :return: True if s is a single command, otherwise False
> - """
> - return True if re.match('^[A-Z_]{3,}\s*$', s) else False
> -
> - @staticmethod
> - def extract_comments(content):
> - """
> - Extract content inside of /* */
> - :param content: input file content
> - :return: extracted comments
> - """
> - comments = ''
> - comments_match = re.match('/\*(\*(?!/)|[^*])*\*/', content)
> - if comments_match:
> - wrapped_comments = comments_match.group()
> - comments = wrapped_comments.lstrip('/*').
> rstrip('*/').lstrip().rstrip()
> - return comments
> -
> - @staticmethod
> - def extract_command_and_text(content):
> - """
> - Extract command and text from input string content
> - :param content: input string containing command and text
> - :return: a tuple containing command and text
> - """
> - command = ''
> - text = ''
> - command_text_dict = {}
> - for line in content.splitlines():
> - if makedoc2rst_converter.is_command(line):
> - if command and text:
> - command_text_dict[command] = text
> - command = line.rstrip()
> - text = ''
> - else:
> - text += line + '\n'
> - return command_text_dict
> -
> - @staticmethod
> - def generate_rst(command_text_dict):
> - rst_str = ''
> - for command, text in command_text_dict.items():
> - rst_str += rst.get_command_processor(command)(command, text)
> - return rst_str
> diff --git a/strcmp.rst b/strcmp.rst
> deleted file mode 100644
> index c1dc543..0000000
> --- a/strcmp.rst
> +++ /dev/null
> @@ -1,47 +0,0 @@
> -.. strcmp:
> -
> -strcmp - Character string compare
> ------------------------------------------
> -.. index:: strcmp
> -.. index:: character string compare
> -
> -**INDEX:**
> -
> - strcmp
> -
> -
> -
> -**SYNOPSIS:**
> -
> -.. code-block:: c
> -
> - #include <string.h>
> - int strcmp(const char *<[a]>, const char *<[b]>);
> -
> -
> -
> -**DESCRIPTION:**
> -
> - <<strcmp>> compares the string at <[a]> to
> - the string at <[b]>.
> -
> -
> -
> -**RETURNS:**
> -
> - If <<*<[a]>>> sorts lexicographically after <<*<[b]>>>,
> - <<strcmp>> returns a number greater than zero. If the two
> - strings match, <<strcmp>> returns zero. If <<*<[a]>>>
> - sorts lexicographically before <<*<[b]>>>, <<strcmp>> returns a
> - number less than zero.
> -
> -
> -
> -**PORTABILITY:**
> -
> -<<strcmp>> is ANSI C.
> -
> -<<strcmp>> requires no supporting OS subroutines.
> -
> -
> -
> --
> 2.7.4
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/devel/attachments/20180802/5d9f057d/attachment-0002.html>
More information about the devel
mailing list