#!/usr/bin/python

# semicolon_file.py
#       --copyright--                   Copyright 2007 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       January 12, 2007        bar
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       May 27, 2012            bar     doxygen namespace
#       July 7, 2018            bar     pyflakes
#       --eodstamps--
##      \file
#       \namespace              tzpython.semicolon_file
#
#       File input that ignores semi-colon comment lines.
#
#

import  re

import  pushfile


##                  Parse a text line to detect a semicolon line (a comment line, in practice)
semicolon_re    =   re.compile(r"^\s*;")


class   a_semicolon_file(pushfile.a_pushfile) :
    """ Class to implement a 'file' whose comment lines (beginning with semi-colon) are ignored. """

    def __init__(me, name, mode = "r") :
        """ Constructor. """

        pushfile.a_pushfile.__init__(me, name, mode)


    def next(me) :
        """ Overload the routine that gets the next text line from the file, ignoring comments. """

        while True :
            li  = pushfile.a_pushfile.next(me)
            if  not li :
                break
            if  not semicolon_re.match(li) :
                break
            pass

        return(li)


    def readline(me) :
        """ Overload the 'file.readline()' routine - ignores semi-colon lines. """

        while True :
            li  = pushfile.a_pushfile.readline(me)
            if  not li :
                break
            if  not semicolon_re.match(li) :
                break
            pass

        return(li)

    pass        # a_semicolon_file


#
#
#
if __name__ == '__main__' :

    f   = a_semicolon_file("x.y")
    while True :
        li  = f.readline()
        if  not li :
            break
        print li,

    f.close()

#
#
#
# eof
