# Path: ififuncs.py
# def append_csv(csv_file, *args):
#     f = open(csv_file, 'a', newline='')
#     try:
#         writer = csv.writer(f)
#         writer.writerow(*args)
#     finally:
#         f.close()
# 
# Path: ififuncs.py
# def create_csv(csv_file, *args):
#     f = open(csv_file, 'w', newline='')
#     try:
#         writer = csv.writer(f)
#         writer.writerow(*args)
#     finally:
#         f.close()


import lxml.etree as ET
import lxml.builder as builder
import uuid
import time
import sys
import subprocess
import os
import hashlib
import csv
from glob import glob
from collections import OrderedDict
from ififuncs import append_csv
from ififuncs import create_csv
            linkingAgentIdentifierRole          = create_unit(2,linkingAgentIdentifier,'linkingAgentRole')
            linkingAgentIdentifierRole.text     = 'implementer'
            linkingAgentIdentifierType.text     = i[0]
            linkingAgentIdentifierValue.text    = i[1]


def main():
    print 'This is not a standalone script. It is a library of functions that other scripts can use'
    sys.exit()

def setup_xml(source_file):
    premisxml           = os.path.dirname(os.path.dirname(source_file)) + '/metadata' '/' + os.path.basename(os.path.dirname(os.path.dirname(source_file))) + '_premis.xml'
    namespace           = '<premis:premis xmlns:premis="http://www.loc.gov/premis/v3" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/premis/v3 https://www.loc.gov/standards/premis/premis.xsd" version="3.0"></premis:premis>'
    premis_namespace    = "http://www.loc.gov/premis/v3"
    xsi_namespace       = "http://www.w3.org/2001/XMLSchema-instance"
    print premisxml
    if os.path.isfile(premisxml):
        print 'looks like premis already exists?'
        parser      = ET.XMLParser(remove_blank_text=True)
        doc         = ET.parse(premisxml,parser=parser)
        premis      = doc.getroot()
    else:
        premis              = ET.fromstring(namespace)
        doc                 = ET.ElementTree(premis)
    return premisxml, premis_namespace, doc, premis


def representation_uuid_csv(filmographic, source_accession, uuid):
    uuid_csv = os.path.expanduser('~/Desktop/uuid.csv')
    if not os.path.isfile(uuid_csv):

# Path: ififuncs.py
# def append_csv(csv_file, *args):
#     f = open(csv_file, 'a', newline='')
#     try:
#         writer = csv.writer(f)
#         writer.writerow(*args)
#     finally:
#         f.close()
# 
# Path: ififuncs.py
# def create_csv(csv_file, *args):
#     f = open(csv_file, 'w', newline='')
#     try:
#         writer = csv.writer(f)
#         writer.writerow(*args)
#     finally:
#         f.close()


# Path: premis.py
import lxml.etree as ET
import lxml.builder as builder
import uuid
import time
import sys
import subprocess
import os
import hashlib
import csv
from glob import glob
from collections import OrderedDict
from ififuncs import append_csv
from ififuncs import create_csv

            linkingAgentIdentifierRole          = create_unit(2,linkingAgentIdentifier,'linkingAgentRole')
            linkingAgentIdentifierRole.text     = 'implementer'
            linkingAgentIdentifierType.text     = i[0]
            linkingAgentIdentifierValue.text    = i[1]


def main():
    print 'This is not a standalone script. It is a library of functions that other scripts can use'
    sys.exit()

def setup_xml(source_file):
    premisxml           = os.path.dirname(os.path.dirname(source_file)) + '/metadata' '/' + os.path.basename(os.path.dirname(os.path.dirname(source_file))) + '_premis.xml'
    namespace           = '<premis:premis xmlns:premis="http://www.loc.gov/premis/v3" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/premis/v3 https://www.loc.gov/standards/premis/premis.xsd" version="3.0"></premis:premis>'
    premis_namespace    = "http://www.loc.gov/premis/v3"
    xsi_namespace       = "http://www.w3.org/2001/XMLSchema-instance"
    print premisxml
    if os.path.isfile(premisxml):
        print 'looks like premis already exists?'
        parser      = ET.XMLParser(remove_blank_text=True)
        doc         = ET.parse(premisxml,parser=parser)
        premis      = doc.getroot()
    else:
        premis              = ET.fromstring(namespace)
        doc                 = ET.ElementTree(premis)
    return premisxml, premis_namespace, doc, premis


def representation_uuid_csv(filmographic, source_accession, uuid):
    uuid_csv = os.path.expanduser('~/Desktop/uuid.csv')
    if not os.path.isfile(uuid_csv):

        create_csv(uuid_csv, ('reference number','source accession number' 'uuid'))