HiveBrain v1.2.0
Get Started
← Back to all entries
debugpythonMinor

Application that processes XML based on configuration

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
applicationxmlthatbasedprocessesconfiguration

Problem

I used CodeClimate to evaluate my code and marked that the following piece of code appeared twice in my source:

except IOError as e:
        log.error("I/O error({0}): {1}".format(e.errno, e.strerror))
        app_exit(1)


The justification is that the amount of duplication mass is 33, which is above default limit of 32.

How would you solve this problem? Would you disable the check? Would you wrap the 2 lines in a new method? Something else?

The duplicate part in question is in main:

#!/usr/bin/env python

import logging as log

def get_cfg(xmlfile):
    # gets configuration from file which 
    # has the same name as XML
    # but with different extension.
    pass

def get_root(xmlfile):
    pass

def app_exit(status):
    log.info('Exiting...')
    exit(status)

def setup_logging(args):
    if args.verbose == 2:
        log.basicConfig(level=log.DEBUG)
    elif args.verbose == 1:
        log.basicConfig(level=log.INFO)
    else:
        log.basicConfig(level=log.WARN)

def main():
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--file', help='XML file to be parsed')
    parser.add_argument("-v", "--verbose", action="count",
                        help="increase output verbosity")

    args = parser.parse_args()

    setup_logging(args)

    xmlfile = args.file
    log.info('Getting root element from XML {}.'.format(xmlfile))
    try:
        root = get_root(xmlfile)
    except ET.ParseError as e:
        log.error("Error while parsing {0}: {1}".format(xmlfile, e))
        app_exit(1)
    except IOError as e:
        log.error("I/O error({0}): {1}".format(e.errno, e.strerror))
        app_exit(1)

    log.info('Getting configuration of XML {}.'.format(xmlfile))
    try:
        cfg = get_cfg(xmlfile)
    except IOError as e:
        log.error("I/O error({0}): {1}".format(e.errno, e.strerror))
        app_exit(1)

    # processing of XML using configuration

if __name__ == '__main__':
    main()

Solution

Pardon my lack of Python knowledge, but I think a function something like this might work:

def fatal_error(message):
    log.error(message)
    app_exit(1)


And now your catch cases look more like this:

try:
    root = get_root(xmlfile)
except ET.ParseError as e:
    fatal_error("Error while parsing {0}: {1}".format(xmlfile, e))
except IOError as e:
    fatal_error("I/O error({0}): {1}".format(e.errno, e.strerror))

log.info('Getting configuration of XML {}.'.format(xmlfile))
try:
    cfg = get_cfg(xmlfile)
except IOError as e:
    fatal_error("I/O error({0}): {1}".format(e.errno, e.strerror))

Code Snippets

def fatal_error(message):
    log.error(message)
    app_exit(1)
try:
    root = get_root(xmlfile)
except ET.ParseError as e:
    fatal_error("Error while parsing {0}: {1}".format(xmlfile, e))
except IOError as e:
    fatal_error("I/O error({0}): {1}".format(e.errno, e.strerror))

log.info('Getting configuration of XML {}.'.format(xmlfile))
try:
    cfg = get_cfg(xmlfile)
except IOError as e:
    fatal_error("I/O error({0}): {1}".format(e.errno, e.strerror))

Context

StackExchange Code Review Q#121329, answer score: 4

Revisions (0)

No revisions yet.