# this should capture all the print statements
class Capturing(list):
    def __enter__(self):
        self._stdout = sys.stdout
        sys.stdout = self._stringio = StringIO()
        return self

    def __exit__(self, *args):
        self.extend(self._stringio.getvalue().splitlines())
        sys.stdout = self._stdout


# =============================================================================
# make the survey xml file
# =============================================================================
survey_xml = archive.XMLMetadata()
survey_xml.read_config_file(survey_cfg)

st = datetime.datetime.now()
for station in os.listdir(survey_dir):
    station_path = os.path.join(survey_dir, station)
    station_save_dir = os.path.join(save_dir, station)

    if os.path.isdir(station_path):
        zc = archive.Z3DCollection()
        # check to see if there are .z3d files in the folder, if not continue
        try:
            fn_list = zc.get_time_blocks(station_path)
        except IndexError:
            print("*** Skipping folder {0} ***".format(station))
            continue
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 10 08:22:27 2018

@author: jpeacock
"""
import mtpy.usgs.usgs_archive as archive
# =============================================================================
# Test
# =============================================================================
cfg_fn = r"C:\Users\jpeacock\Documents\imush\xml_config_test.txt"

m = archive.XMLMetadata()
m.read_config_file(cfg_fn)
m.write_xml_file(r"c:\Users\jpeacock\Documents\imush\test.xml")

m = archive.XMLMetadata()
m.read_config_file(cfg_fn)
m.survey.east = -121.34
m.survey.west = -121.34
m.survey.north = 38.75
m.survey.south = 38.75

m.title += ' station006'
m.supplement_info += 'file list: file1, file2, file3'
m.survey.begin_date = '20170101T10:30:10 UTC'
m.survey.end_date = '20170103T18:10:40 UTC'

m.write_xml_file("c:\Users\jpeacock\Documents\imush\station_test.xml",
                 write_station=True)