コード例 #1
0
def x12_validate(
    src: Union[str, IO[str]],
    params: Union[Any, None] = None,
    generate_html: bool = False,
    generate_997: bool = False,
    generate_xml: bool = False,
) -> Dict[str, Union[str, bool, list]]:
    """
    Validate x12 EDI string or contents of file descriptor.
    """
    src_fd: IO[str]
    if isinstance(src, str):
        src_fd = StringIO(src)
    else:
        src_fd = src

    pyx12_params = params
    if not isinstance(pyx12_params, type(x12n_params)):
        pyx12_params = x12n_params.params(None)

    nn7: StringIO = StringIO()
    html: StringIO = StringIO()
    xml: StringIO = StringIO()

    # Capture error logging output of pyx12
    # The pyx12 module uses a logger named pyx12,
    # which we get here. If pyx12 changes this in
    # a future version, it will break this code.
    # The line of code defining the logger is at
    # https://github.com/azoner/pyx12/blob/2e3529e31a167a53ebcd9da348cece9f284b6710/pyx12/scripts/x12valid.py#L78
    logger_output: StringIO = StringIO()
    pyx12_logger = logging.getLogger('pyx12')
    handler = logging.StreamHandler(logger_output)
    pyx12_logger.addHandler(handler)
    pyx12_logger.setLevel(logging.ERROR)

    # Capture errors from pyx12
    ok = x12n_document.x12n_document(
        param=pyx12_params,
        src_file=src_fd,
        fd_997=nn7 if generate_997 else None,
        fd_html=html if generate_html else None,
        fd_xmldoc=xml if generate_xml else None,
        map_path=None,
    )

    # Get string values
    html_val = html.getvalue()
    nn7_val = nn7.getvalue()
    error_val = logger_output.getvalue().splitlines()
    xml_val = xml.getvalue()

    return {
        "html": html_val,
        "997": nn7_val,
        "xml": xml_val,
        "errors": error_val,
        "ok": ok,
    }
コード例 #2
0
ファイル: dump.py プロジェクト: azoner/pyx12
#! /usr/bin/env python

import os.path
import sys

import pyx12.error_handler
import pyx12.map_if
from pyx12.params import params
import pyx12.segment


def donode(node):
    print(node.get_path())
    for child in node.children:
        if child.is_loop() or child.is_segment():
            donode(child)

param = params()
param.set('map_path', os.path.expanduser('~/src/pyx12/map/'))
map = pyx12.map_if.load_map_file(sys.argv[1], param)
donode(map)
コード例 #3
0
def main():
    """
    Set up environment for processing
    """
    parser = argparse.ArgumentParser(description='X12 Validation')
    parser.add_argument('--config-file',
                        '-c',
                        action='store',
                        dest="configfile",
                        default=None)
    parser.add_argument('--log-file',
                        '-l',
                        action='store',
                        dest="logfile",
                        default=None)
    parser.add_argument('--map-path',
                        '-m',
                        action='store',
                        dest="map_path",
                        default=None,
                        type=check_map_path_arg)
    parser.add_argument('--verbose', '-v', action='count')
    parser.add_argument('--debug', '-d', action='store_true')
    parser.add_argument('--quiet', '-q', action='store_true')
    parser.add_argument('--html', '-H', action='store_true')
    parser.add_argument('--exclude-external-codes',
                        '-x',
                        action='append',
                        dest="exclude_external",
                        default=[],
                        help='External Code Names to ignore')
    parser.add_argument('--charset',
                        '-s',
                        choices=('b', 'e'),
                        help='Specify X12 character set: b=basic, e=extended')
    #parser.add_argument('--background', '-b', action='store_true')
    #parser.add_argument('--test', '-t', action='store_true')
    parser.add_argument('--profile',
                        action='store_true',
                        help='Profile the code with plop')
    parser.add_argument('input_files', nargs='*')
    args = parser.parse_args()

    logger = logging.getLogger('pyx12')
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

    stdout_hdlr = logging.StreamHandler()
    stdout_hdlr.setFormatter(formatter)
    logger.addHandler(stdout_hdlr)
    logger.setLevel(logging.INFO)

    param = params.params(args.configfile)
    if args.debug:
        logger.setLevel(logging.DEBUG)
        param.set('debug', True)
    if args.verbose and args.verbose > 0:
        logger.setLevel(logging.DEBUG)
    if args.quiet:
        logger.setLevel(logging.ERROR)
    fd_997 = None
    fd_html = None
    flag_997 = True
    param.set('exclude_external_codes', ','.join(args.exclude_external))
    if args.map_path:
        param.set('map_path', args.map_path)

    if args.logfile:
        try:
            hdlr = logging.FileHandler(args.logfile)
            hdlr.setFormatter(formatter)
            logger.addHandler(hdlr)
        except IOError:
            logger.exception('Could not open log file: %s' % (args.logfile))

    for src_filename in args.input_files:
        try:
            if not os.path.isfile(src_filename):
                logger.error('Could not open file "%s"' % (src_filename))
                continue
            #fd_src = open(src_filename, 'U')
            if flag_997:
                fd_997 = tempfile.TemporaryFile()
            if args.html:
                if os.path.splitext(src_filename)[1] == '.txt':
                    target_html = os.path.splitext(src_filename)[0] + '.html'
                else:
                    target_html = src_filename + '.html'
                fd_html = open(target_html, 'w')

            if args.profile:
                from plop.collector import Collector
                p = Collector()
                p.start()
                if x12n_document.x12n_document(param=param,
                                               src_file=src_filename,
                                               fd_997=fd_997,
                                               fd_html=fd_html,
                                               fd_xmldoc=None,
                                               map_path=args.map_path):
                    sys.stderr.write('%s: OK\n' % (src_filename))
                else:
                    sys.stderr.write('%s: Failure\n' % (src_filename))
                #import profile
                #prof_str = 'pyx12.x12n_document.x12n_document(param, src_filename, ' \
                #        + 'fd_997, fd_html, None, None)'
                #print prof_str
                #print param
                #profile.run(prof_str, 'pyx12.prof')
                p.stop()
                try:
                    pfile = os.path.splitext(
                        os.path.basename(src_filename))[0] + '.plop.out'
                    pfull = os.path.join(
                        os.path.expanduser('~/.plop.profiles'), pfile)
                    print(pfull)
                    with open(pfull, 'w') as fdp:
                        fdp.write(repr(dict(p.stack_counts)))
                except Exception:
                    logger.exception('Failed to write profile data')
                    sys.stderr.write('%s: bad profile save\n' % (src_filename))
            else:
                if x12n_document.x12n_document(param=param,
                                               src_file=src_filename,
                                               fd_997=fd_997,
                                               fd_html=fd_html,
                                               fd_xmldoc=None,
                                               map_path=args.map_path):
                    sys.stderr.write('%s: OK\n' % (src_filename))
                else:
                    sys.stderr.write('%s: Failure\n' % (src_filename))
            if flag_997 and fd_997.tell() != 0:
                fd_997.seek(0)
                if os.path.splitext(src_filename)[1] == '.txt':
                    target_997 = os.path.splitext(src_filename)[0] + '.997'
                else:
                    target_997 = src_filename + '.997'
                codecs.open(target_997, mode='w',
                            encoding='ascii').write(fd_997.read())

            if fd_997:
                fd_997.close()
            if fd_html:
                fd_html.close()
        except IOError:
            logger.exception('Could not open files')
            return False
        except KeyboardInterrupt:
            print("\n[interrupt]")

    return True
コード例 #4
0
#! /usr/bin/env python

import os.path
import sys

import pyx12.error_handler
import pyx12.map_if
from pyx12.params import params
import pyx12.segment


def donode(node):
    print(node.get_path())
    for child in node.children:
        if child.is_loop() or child.is_segment():
            donode(child)


param = params()
param.set('map_path', os.path.expanduser('~/src/pyx12/map/'))
map = pyx12.map_if.load_map_file(sys.argv[1], param)
donode(map)
コード例 #5
0
ファイル: x12valid.py プロジェクト: CloverHealth/pyx12
def main():
    """
    Set up environment for processing
    """
    parser = argparse.ArgumentParser(description='X12 Validation')
    parser.add_argument('--config-file', '-c', action='store',
                        dest="configfile", default=None)
    parser.add_argument(
        '--log-file', '-l', action='store', dest="logfile", default=None)
    parser.add_argument('--map-path', '-m', action='store', dest="map_path", default=None, type=check_map_path_arg)
    parser.add_argument('--verbose', '-v', action='count')
    parser.add_argument('--debug', '-d', action='store_true')
    parser.add_argument('--quiet', '-q', action='store_true')
    parser.add_argument('--html', '-H', action='store_true')
    parser.add_argument('--exclude-external-codes', '-x', action='append', dest="exclude_external",
                        default=[], help='External Code Names to ignore')
    parser.add_argument('--charset', '-s', choices=(
        'b', 'e'), help='Specify X12 character set: b=basic, e=extended')
    #parser.add_argument('--background', '-b', action='store_true')
    #parser.add_argument('--test', '-t', action='store_true')
    parser.add_argument('--profile', action='store_true', help='Profile the code with plop')
    parser.add_argument('input_files', nargs='*')
    args = parser.parse_args()

    logger = logging.getLogger('pyx12')
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

    stdout_hdlr = logging.StreamHandler()
    stdout_hdlr.setFormatter(formatter)
    logger.addHandler(stdout_hdlr)
    logger.setLevel(logging.INFO)

    param = params.params(args.configfile)
    if args.debug:
        logger.setLevel(logging.DEBUG)
        param.set('debug', True)
    if args.verbose and args.verbose > 0:
        logger.setLevel(logging.DEBUG)
    if args.quiet:
        logger.setLevel(logging.ERROR)
    fd_997 = None
    fd_html = None
    flag_997 = True
    param.set('exclude_external_codes', ','.join(args.exclude_external))
    if args.map_path:
        param.set('map_path', args.map_path)

    if args.logfile:
        try:
            hdlr = logging.FileHandler(args.logfile)
            hdlr.setFormatter(formatter)
            logger.addHandler(hdlr)
        except IOError:
            logger.exception('Could not open log file: %s' % (args.logfile))

    for src_filename in args.input_files:
        try:
            if not os.path.isfile(src_filename):
                logger.error('Could not open file "%s"' % (src_filename))
                continue
            #fd_src = open(src_filename, 'U')
            if flag_997:
                fd_997 = tempfile.TemporaryFile()
            if args.html:
                if os.path.splitext(src_filename)[1] == '.txt':
                    target_html = os.path.splitext(src_filename)[0] + '.html'
                else:
                    target_html = src_filename + '.html'
                fd_html = open(target_html, 'w')

            if args.profile:
                from plop.collector import Collector
                p = Collector()
                p.start()
                if x12n_document.x12n_document(param=param, src_file=src_filename,
                        fd_997=fd_997, fd_html=fd_html, fd_xmldoc=None, map_path=args.map_path):
                    sys.stderr.write('%s: OK\n' % (src_filename))
                else:
                    sys.stderr.write('%s: Failure\n' % (src_filename))
                #import profile
                #prof_str = 'pyx12.x12n_document.x12n_document(param, src_filename, ' \
                #        + 'fd_997, fd_html, None, None)'
                #print prof_str
                #print param
                #profile.run(prof_str, 'pyx12.prof')
                p.stop()
                try:
                    pfile = os.path.splitext(os.path.basename(
                        src_filename))[0] + '.plop.out'
                    pfull = os.path.join(os.path.expanduser(
                        '~/.plop.profiles'), pfile)
                    print(pfull)
                    with open(pfull, 'w') as fdp:
                        fdp.write(repr(dict(p.stack_counts)))
                except Exception:
                    logger.exception('Failed to write profile data')
                    sys.stderr.write('%s: bad profile save\n' % (src_filename))
            else:
                if x12n_document.x12n_document(param=param, src_file=src_filename,
                        fd_997=fd_997, fd_html=fd_html, fd_xmldoc=None, map_path=args.map_path):
                    sys.stderr.write('%s: OK\n' % (src_filename))
                else:
                    sys.stderr.write('%s: Failure\n' % (src_filename))
            if flag_997 and fd_997.tell() != 0:
                fd_997.seek(0)
                if os.path.splitext(src_filename)[1] == '.txt':
                    target_997 = os.path.splitext(src_filename)[0] + '.997'
                else:
                    target_997 = src_filename + '.997'
                codecs.open(target_997, mode='w',
                            encoding='ascii').write(fd_997.read())

            if fd_997:
                fd_997.close()
            if fd_html:
                fd_html.close()
        except IOError:
            logger.exception('Could not open files')
            return False
        except KeyboardInterrupt:
            print("\n[interrupt]")

    return True