# -*- coding: utf-8 -*-

# Copyright (c) 2010-2019 Christopher Brown
#
# This file is part of Psylab.
#
# Psylab is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Psylab is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Psylab.  If not, see <http://www.gnu.org/licenses/>.
#
# Bug reports, bug fixes, suggestions, enhancements, or other 
# contributions are welcome. Go to http://github.com/cbrown1/psylab/ 
# for more information and to contribute. Or send an e-mail to: 
# cbrown1@pitt.edu.
#

"""
path_tools

Some convenience functions for extracting bits of info from filenames.
The only usefulness of this module is to have all of these common tasks 
in one place.
"""

from os import path

def get_fileext(filename):
    return path.splitext(path.basename(filename))[1]

def get_filebase(filename):
    return path.splitext(path.basename(filename))[0]

def get_filename(filename):
    return path.basename(filename)

def get_path(filename):
    return path.dirname(filename)

def tail(filename):
    """A simple python version of tail

    	tail listens to a file and returns any new text

    	Usage
    	-----
	    fh = open(logfile,"r")
	    loglines = tail(fh)
	    for line in loglines:
	        print(line, end='')
    """
    while True:
        line = filename.readline()
        if not line:
            time.sleep(0.1)
        else:
            yield line
