# Path: xdfile/utils.py
# def parse_pathname(path):
#     # Fix to proper split names like file.xml.1
#     ext = os.extsep + os.extsep.join(os.path.basename(path).split(os.extsep)[1:])
#     path, fn = os.path.split(path)
#     ext = ext if fn else ''
#     base = os.path.splitext(fn)[0]
#     nt = namedtuple('Pathname', 'path base ext filename')
#     return nt(path=path, base=base, ext=ext, filename=fn)
# 
# def parse_tsv(fn, objname=None):
#     try:
#         fp = codecs.open(fn, encoding='utf-8')
#         rows = parse_tsv_data(fp.read(), objname)
#         return dict((r[0], r) for r in rows)
#     except Exception as e:
#         error("parse_tsv('%s') %s" % (fn, str(e)))
#         if g_args.debug:
#             raise
#         return {}
# 
# def progress(rest=None, every=1):
#     global g_currentProgress, g_numProgress
#     if not sys.stdout.isatty():
#         return
#     if rest:
#         g_numProgress += 1
#         g_currentProgress = rest
#         if g_numProgress % every == 0:
#             print("\r% 6d %s " % (g_numProgress, rest), end="")
#             sys.stdout.flush()
#     else:
#         g_currentProgress = ""
#         g_numProgress = 0
#         print()
#         sys.stdout.flush()
# 
# def parse_pubid(fn):
#     m = re.search("(^[A-Za-z]*)", parse_pathname(fn).base)
#     return m.group(1).lower()
# 
# def find_files(*paths, **kwargs):
#     for fn, data, dt in find_files_with_time(*paths, **kwargs):
#         yield fn, data
# 
# def get_args(desc="", parser=None):
#     global g_args, g_scriptname
#     g_scriptname = parse_pathname(sys.argv[0]).base
# 
#     if g_args:
#         return g_args
# 
#     if not parser:
#         parser = args_parser(desc)
# 
#     parser.add_argument('inputs', nargs='*', help='toplevel input(s)')
#     parser.add_argument('-o', '--output', dest='output', action='store')
#     parser.add_argument('-q', '--quiet', dest='verbose', action='store_const', const=-1, default=0)
#     parser.add_argument('-v', '--verbose', dest='verbose', action='count', default=0)
#     parser.add_argument('-d', '--debug', dest='debug', action='store_true', default=False, help='abort on exception')
#     parser.add_argument('-c', '--corpus', dest='corpusdir', default='crosswords', help='corpus source')
#     g_args = parser.parse_args()
# 
#     return g_args
# 
# def memoize(obj):
#     cache = obj.cache = {}
# 
#     @functools.wraps(obj)
#     def memoizer(*args, **kwargs):
#         if args not in cache:
#             cache[args] = obj(*args, **kwargs)
#         return cache[args]
#     return memoizer
# 
# def parse_xdid(path):
#     a = path.rindex('/')
#     b = path.rindex('.')
#     return path[a+1:b]
# 
# Path: xdfile/utils.py
# def log(s, minverbose=0, severity='INFO'):
#     # This can be made in more Python way
#     if g_logfp.isatty(): # Colors only for atty term
#         if severity.lower() == 'warning':
#             s = bcolors.WARNING + s + bcolors.ENDC
#         if severity.lower() == 'error':
#             s = bcolors.FAIL + s + bcolors.ENDC
# 
#     if g_logfp:
#         g_logfp.write("%s: %s\n" % (severity.upper(), s))
#     g_logs.append("%s: [%s] %s" % (g_currentProgress or g_scriptname, severity.upper(), s))
# 
# def error(_s, _m=0):
#     log(_s, minverbose=_m, severity='error')
# 
# def warn(_s, _m=0):
#     log(_s, minverbose=_m, severity='warning')


import string
import operator
import functools
import re
import datetime
from .utils import parse_pathname, parse_tsv, progress, parse_pubid, find_files, get_args, memoize, parse_xdid
from .utils import log, error, warn
    from .utils import log
        self.date = dt
        self.answer = answer
        self.clue = clue

    def pubyear(self):
        return (self.pubid, year_from_date(self.date))

    def xdid(self):
        return self.pubid + self.date

    def __str__(self):
        return '[%s%s] %s' % (self.pubid, self.date, self.clue)


def clues():
    if not g_all_clues:
        for xd in corpus():  # r in parse_tsv("clues.tsv", "AnswerClue"):
            progress(xd.filename)
            pubid = xd.publication_id()
            dt = xd.date() or ""
            for pos, clue, answer in xd.iterclues():
                ca = ClueAnswer(pubid, dt, answer, clue)
                g_all_clues.append(ca)

        progress()

    return g_all_clues


def get_shelf(path):

# Path: xdfile/utils.py
# def parse_pathname(path):
#     # Fix to proper split names like file.xml.1
#     ext = os.extsep + os.extsep.join(os.path.basename(path).split(os.extsep)[1:])
#     path, fn = os.path.split(path)
#     ext = ext if fn else ''
#     base = os.path.splitext(fn)[0]
#     nt = namedtuple('Pathname', 'path base ext filename')
#     return nt(path=path, base=base, ext=ext, filename=fn)
# 
# def parse_tsv(fn, objname=None):
#     try:
#         fp = codecs.open(fn, encoding='utf-8')
#         rows = parse_tsv_data(fp.read(), objname)
#         return dict((r[0], r) for r in rows)
#     except Exception as e:
#         error("parse_tsv('%s') %s" % (fn, str(e)))
#         if g_args.debug:
#             raise
#         return {}
# 
# def progress(rest=None, every=1):
#     global g_currentProgress, g_numProgress
#     if not sys.stdout.isatty():
#         return
#     if rest:
#         g_numProgress += 1
#         g_currentProgress = rest
#         if g_numProgress % every == 0:
#             print("\r% 6d %s " % (g_numProgress, rest), end="")
#             sys.stdout.flush()
#     else:
#         g_currentProgress = ""
#         g_numProgress = 0
#         print()
#         sys.stdout.flush()
# 
# def parse_pubid(fn):
#     m = re.search("(^[A-Za-z]*)", parse_pathname(fn).base)
#     return m.group(1).lower()
# 
# def find_files(*paths, **kwargs):
#     for fn, data, dt in find_files_with_time(*paths, **kwargs):
#         yield fn, data
# 
# def get_args(desc="", parser=None):
#     global g_args, g_scriptname
#     g_scriptname = parse_pathname(sys.argv[0]).base
# 
#     if g_args:
#         return g_args
# 
#     if not parser:
#         parser = args_parser(desc)
# 
#     parser.add_argument('inputs', nargs='*', help='toplevel input(s)')
#     parser.add_argument('-o', '--output', dest='output', action='store')
#     parser.add_argument('-q', '--quiet', dest='verbose', action='store_const', const=-1, default=0)
#     parser.add_argument('-v', '--verbose', dest='verbose', action='count', default=0)
#     parser.add_argument('-d', '--debug', dest='debug', action='store_true', default=False, help='abort on exception')
#     parser.add_argument('-c', '--corpus', dest='corpusdir', default='crosswords', help='corpus source')
#     g_args = parser.parse_args()
# 
#     return g_args
# 
# def memoize(obj):
#     cache = obj.cache = {}
# 
#     @functools.wraps(obj)
#     def memoizer(*args, **kwargs):
#         if args not in cache:
#             cache[args] = obj(*args, **kwargs)
#         return cache[args]
#     return memoizer
# 
# def parse_xdid(path):
#     a = path.rindex('/')
#     b = path.rindex('.')
#     return path[a+1:b]
# 
# Path: xdfile/utils.py
# def log(s, minverbose=0, severity='INFO'):
#     # This can be made in more Python way
#     if g_logfp.isatty(): # Colors only for atty term
#         if severity.lower() == 'warning':
#             s = bcolors.WARNING + s + bcolors.ENDC
#         if severity.lower() == 'error':
#             s = bcolors.FAIL + s + bcolors.ENDC
# 
#     if g_logfp:
#         g_logfp.write("%s: %s\n" % (severity.upper(), s))
#     g_logs.append("%s: [%s] %s" % (g_currentProgress or g_scriptname, severity.upper(), s))
# 
# def error(_s, _m=0):
#     log(_s, minverbose=_m, severity='error')
# 
# def warn(_s, _m=0):
#     log(_s, minverbose=_m, severity='warning')


# Path: xdfile/xdfile.py
import string
import operator
import functools
import re
import datetime
from .utils import parse_pathname, parse_tsv, progress, parse_pubid, find_files, get_args, memoize, parse_xdid
from .utils import log, error, warn
    from .utils import log

        self.date = dt
        self.answer = answer
        self.clue = clue

    def pubyear(self):
        return (self.pubid, year_from_date(self.date))

    def xdid(self):
        return self.pubid + self.date

    def __str__(self):
        return '[%s%s] %s' % (self.pubid, self.date, self.clue)


def clues():
    if not g_all_clues:
        for xd in corpus():  # r in parse_tsv("clues.tsv", "AnswerClue"):
            progress(xd.filename)
            pubid = xd.publication_id()
            dt = xd.date() or ""
            for pos, clue, answer in xd.iterclues():
                ca = ClueAnswer(pubid, dt, answer, clue)
                g_all_clues.append(ca)

        progress()

    return g_all_clues


def get_shelf(path):

    return parse_pathname(path).base.split('-')[0]