# Path: version_metadata/utils.py
# def sanity_check_fail(msg):
# 	def __init__(self, message, iterable):
# 	def __init__(self):
# 	def __call__(self, m):
# 	def entry(self, m): 
# 	def warn(self, m):
# 	def debugonly(self, *args):
# 	def trystr(self, x):
# 	def __init__(self):
# 	def fread(self, f, encoding=''):
# 	def fentries(self, f):
# 	def writel(self, f, entries, sep=b'\n', encoding=None):
# 	def dumpf(self, f, data):
# 	def fexists(self, filename):
# 	def demanduniq(self, iterable):
# 	def tryuniq(self, iterable):
# 	def safedict(self, tuples):
# 	def now(self):
# 	def basename(self, f):
# 	def getpath(data, ifmissing, *args):
# 	def __init__(self):
# 	def loadf(self, f):
# 	def dumpf(self, f, data):
# 	def multidictLoadf(self, f):
# 	def loads(self, data):
# 	def multidictLoadStr(self, data):
# 		def multidictParser(pairs):
# 	def pretty(self, arg):
# 	def pp(self, arg):
# 	def copyobj(self, x):
# class NonUniqException(Exception):
# class Logger:
# class Utils:
# class Json:


from lxml import etree
from collections import defaultdict
from .utils import cmn, json2, logger, NonUniqException
	def __init__(self, xml):
		self.xml = xml
		self.pretty = True
	def add_attribute(self, tag, value):
		obj = self.xml
		objtype = obj.tag
		if not objtype in ['SAMPLE', 'EXPERIMENT']:
			utils.sanity_check_fail('__xml_is_unknown_type:' + objtype)
		attrtag = '{0}_ATTRIBUTES'.format(objtype)
		attribute_list = obj.findall(attrtag)
		attrs = cmn.demanduniq(list(obj.findall(attrtag)))
		attribute_list = attrs.getchildren()
		attribute_list[-1].addnext(self.new_tag_value(objtype, tag, value))

	def new_tag_value(self, objtype, tag, value):
		name = '{0}_ATTRIBUTE'.format(objtype)
		new_attr = etree.Element(name)
		t = etree.SubElement(new_attr, 'TAG')
		v = etree.SubElement(new_attr, 'VALUE')
		t.text = tag
		v.text = value
		return new_attr

	def tostring(self):
		return etree.tostring(self.xml, pretty_print=self.pretty )


class SRAParseObjSet:
	@staticmethod
	def from_file(f):

# Path: version_metadata/utils.py
# def sanity_check_fail(msg):
# 	def __init__(self, message, iterable):
# 	def __init__(self):
# 	def __call__(self, m):
# 	def entry(self, m): 
# 	def warn(self, m):
# 	def debugonly(self, *args):
# 	def trystr(self, x):
# 	def __init__(self):
# 	def fread(self, f, encoding=''):
# 	def fentries(self, f):
# 	def writel(self, f, entries, sep=b'\n', encoding=None):
# 	def dumpf(self, f, data):
# 	def fexists(self, filename):
# 	def demanduniq(self, iterable):
# 	def tryuniq(self, iterable):
# 	def safedict(self, tuples):
# 	def now(self):
# 	def basename(self, f):
# 	def getpath(data, ifmissing, *args):
# 	def __init__(self):
# 	def loadf(self, f):
# 	def dumpf(self, f, data):
# 	def multidictLoadf(self, f):
# 	def loads(self, data):
# 	def multidictLoadStr(self, data):
# 		def multidictParser(pairs):
# 	def pretty(self, arg):
# 	def pp(self, arg):
# 	def copyobj(self, x):
# class NonUniqException(Exception):
# class Logger:
# class Utils:
# class Json:


# Path: version_metadata/sraparse.py
from lxml import etree
from collections import defaultdict
from .utils import cmn, json2, logger, NonUniqException

	def __init__(self, xml):
		self.xml = xml
		self.pretty = True
	def add_attribute(self, tag, value):
		obj = self.xml
		objtype = obj.tag
		if not objtype in ['SAMPLE', 'EXPERIMENT']:
			utils.sanity_check_fail('__xml_is_unknown_type:' + objtype)
		attrtag = '{0}_ATTRIBUTES'.format(objtype)
		attribute_list = obj.findall(attrtag)
		attrs = cmn.demanduniq(list(obj.findall(attrtag)))
		attribute_list = attrs.getchildren()
		attribute_list[-1].addnext(self.new_tag_value(objtype, tag, value))

	def new_tag_value(self, objtype, tag, value):
		name = '{0}_ATTRIBUTE'.format(objtype)
		new_attr = etree.Element(name)
		t = etree.SubElement(new_attr, 'TAG')
		v = etree.SubElement(new_attr, 'VALUE')
		t.text = tag
		v.text = value
		return new_attr

	def tostring(self):
		return etree.tostring(self.xml, pretty_print=self.pretty )


class SRAParseObjSet:
	@staticmethod
	def from_file(f):

		xml = etree.parse(f)