# Path: opentaxforms/config.py
# def parseCmdline():
# def getFileList(dirName):
# def setLogname(rootForms,cfg):
# def copyStaticDir(appname):
# def setupStaticDir(dirName):
# def setup(**overrideArgs):
# def unsetup():
# 
# Path: opentaxforms/ut.py
# NL = '\n'
# TAB = '\t'
# def merge(bb1, bb2):
# def numerify(s):
# def compactify(multilineRegex):
#     def crunch(seg):
# def pickle(data, pickleFilePrefix):
# def unpickle(pickleFilePrefix, default=None):
# def flattened(l):
# def hasdups(l, key=None):
# def uniqify(l):
# def uniqify2(l):
# def setupLogging(loggerId, args=None):
# def unsetupLogging():
# def logg(msg, outputs=None):
# def jj(*args, **kw):
# def jdb(*args, **kw):
# def run0(cmd):
# def run(cmd, logprefix='run', loglevel='INFO'):
#     def __init__(self, pkgname, fpath=None):
#     def path(self):
#     def content(self):
#     def keys(cls):
#     def vals(cls):
#     def items(cls):
#     def __init__(self):
#     def __call__(self, **kw):
#     def __init__(self, *maps, **kw):
#     def _getGetitems(self, mapp):
#     def __getitem__(self, key):
#     def __setitem__(self, key, val):
#     def __len__(self):
#     def __call__(self, *keys):
#     def clear(self):
#     def update(self, *maps):
#     def __add__(self, *maps):
#     def __iadd__(self, *maps):
#     def __iter__(self):
#     def iterkeys(self):
#     def keys(self):
#     def values(self):
#     def items(self):
#     def iteritems(self):
#     def get(self, key, dflt=None):
#     def __str__(self):
#     def __repr__(self):
# def notequalpatch(self, o):
#     def fromstring(cls, s):
#     def __hash__(self):
# def playQnty():
# def nth(n):
# def skip(s, substr):
# def until(s, substr):
# def ensure_dir(folder):
# def now(format=None):
# def readImgSize(fname, dirName):
# def asciiOnly(s):
# class NoSuchPickle(Exception):
# class PickleException(Exception):
# class Resource(object):
# class CharEnum(object):
# class ChainablyUpdatableOrderedDict(odict):
# class Bag(object):
# class Qnty(qq):
# 
# Path: opentaxforms/irs.py
# class CrypticXml(Exception):
# def computeFormId(formName):
# def computeTitle(prefix, form = None):
# def sortableFieldname(fieldname):
#     def intify(s):
# def possibleFilePrefixes(formName):
#         def trailingLetters(s):


import sys
import six
import re
import os
        import doctest
from sys import exc_info
from itertools import chain
from argparse import ArgumentParser
from lxml import etree
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdftypes import PDFStream, PDFNotImplementedError
    from cPickle import dump
    from pickle import dump
from .config import cfg
from .ut import log, setupLogging, skip, Qnty, pf
from .irs import commandPtn, possibleColTypes, CrypticXml


def computePath(el, namespaces):
    def ancestry(ele, namespacez):
        return reversed(list(
            elem.attrib.get(
                'name', '%s#%d' % (
                    elem.tag,
                    indexAmongSibs(elem, elem.tag, namespacez)))
            for elem in chain([ele], ele.iterancestors())))
    return '.'.join(ancestry(el, namespaces))


def indexAmongSibs(el, tag=None, namespaces=None):
    if not tag:
        tag = '*'
    elif callable(tag):
        tag = str(tag).replace(' ', '').replace('-', '').strip('<>')
    if '}' in tag:
        tag = 't:' + skip(tag, '}')
    p = el.getparent()
    if p is None:
        return 1
    return p.xpath('%s' % (tag, ), namespaces=namespaces).index(el)


def ensurePathsAreUniq(fields):
    fieldsbyid = set()
    for f in fields:
        if f['path'] in fieldsbyid:

# Path: opentaxforms/config.py
# def parseCmdline():
# def getFileList(dirName):
# def setLogname(rootForms,cfg):
# def copyStaticDir(appname):
# def setupStaticDir(dirName):
# def setup(**overrideArgs):
# def unsetup():
# 
# Path: opentaxforms/ut.py
# NL = '\n'
# TAB = '\t'
# def merge(bb1, bb2):
# def numerify(s):
# def compactify(multilineRegex):
#     def crunch(seg):
# def pickle(data, pickleFilePrefix):
# def unpickle(pickleFilePrefix, default=None):
# def flattened(l):
# def hasdups(l, key=None):
# def uniqify(l):
# def uniqify2(l):
# def setupLogging(loggerId, args=None):
# def unsetupLogging():
# def logg(msg, outputs=None):
# def jj(*args, **kw):
# def jdb(*args, **kw):
# def run0(cmd):
# def run(cmd, logprefix='run', loglevel='INFO'):
#     def __init__(self, pkgname, fpath=None):
#     def path(self):
#     def content(self):
#     def keys(cls):
#     def vals(cls):
#     def items(cls):
#     def __init__(self):
#     def __call__(self, **kw):
#     def __init__(self, *maps, **kw):
#     def _getGetitems(self, mapp):
#     def __getitem__(self, key):
#     def __setitem__(self, key, val):
#     def __len__(self):
#     def __call__(self, *keys):
#     def clear(self):
#     def update(self, *maps):
#     def __add__(self, *maps):
#     def __iadd__(self, *maps):
#     def __iter__(self):
#     def iterkeys(self):
#     def keys(self):
#     def values(self):
#     def items(self):
#     def iteritems(self):
#     def get(self, key, dflt=None):
#     def __str__(self):
#     def __repr__(self):
# def notequalpatch(self, o):
#     def fromstring(cls, s):
#     def __hash__(self):
# def playQnty():
# def nth(n):
# def skip(s, substr):
# def until(s, substr):
# def ensure_dir(folder):
# def now(format=None):
# def readImgSize(fname, dirName):
# def asciiOnly(s):
# class NoSuchPickle(Exception):
# class PickleException(Exception):
# class Resource(object):
# class CharEnum(object):
# class ChainablyUpdatableOrderedDict(odict):
# class Bag(object):
# class Qnty(qq):
# 
# Path: opentaxforms/irs.py
# class CrypticXml(Exception):
# def computeFormId(formName):
# def computeTitle(prefix, form = None):
# def sortableFieldname(fieldname):
#     def intify(s):
# def possibleFilePrefixes(formName):
#         def trailingLetters(s):


# Path: opentaxforms/extractFillableFields.py
import sys
import six
import re
import os
        import doctest
from sys import exc_info
from itertools import chain
from argparse import ArgumentParser
from lxml import etree
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdftypes import PDFStream, PDFNotImplementedError
    from cPickle import dump
    from pickle import dump
from .config import cfg
from .ut import log, setupLogging, skip, Qnty, pf
from .irs import commandPtn, possibleColTypes, CrypticXml



def computePath(el, namespaces):
    def ancestry(ele, namespacez):
        return reversed(list(
            elem.attrib.get(
                'name', '%s#%d' % (
                    elem.tag,
                    indexAmongSibs(elem, elem.tag, namespacez)))
            for elem in chain([ele], ele.iterancestors())))
    return '.'.join(ancestry(el, namespaces))


def indexAmongSibs(el, tag=None, namespaces=None):
    if not tag:
        tag = '*'
    elif callable(tag):
        tag = str(tag).replace(' ', '').replace('-', '').strip('<>')
    if '}' in tag:
        tag = 't:' + skip(tag, '}')
    p = el.getparent()
    if p is None:
        return 1
    return p.xpath('%s' % (tag, ), namespaces=namespaces).index(el)


def ensurePathsAreUniq(fields):
    fieldsbyid = set()
    for f in fields:
        if f['path'] in fieldsbyid:

            log.error('dup paths [%s]', f['path'])