#!/usr/bin/env python

from genologics.epp import EppLogger

import logging
import sys

from shutil import copy
import os

from time import strftime, localtime
from requests import HTTPError

class CopyField(object):
    """Class to copy any filed (or udf) from any lims element to any 
    udf on any other lims element

    argumnets:

    s_elt           source elemement - instance of a type
    d_elt           destination element - instance of a type
    s_field_name    name of source field (or udf) to be copied 
    d_udf_name      name of destination udf name. If not specifyed
                    s_field_name will be used.

    The copy_udf() function takes a logfile as optional argument.
    If this is given the changes will be logged there.

    Written by Maya Brandi and Johannes Alnberg
    """
    def __init__(self, s_elt, d_elt, s_field_name, d_udf_name = None):
        if not d_udf_name:
            d_udf_name = s_field_name
        self.s_elt = s_elt
        self.s_field_name = s_field_name
        self.s_field = self._get_field(s_elt, s_field_name)
        self.d_elt = d_elt
        self.d_udf_name = d_udf_name
        self.old_dest_udf = self._get_field(d_elt, d_udf_name)

    def _current_time(self):
        return strftime("%Y-%m-%d %H:%M:%S", localtime())

    def _get_field(self, elt, field):
        if field in elt.udf:
            return elt.udf[field]
        elif udf_name in dir(elt):
            return elt.field
        else:
            return None

    def _set_udf(self, elt, udf_name, val):
        try:
            self.elt.udf[udf_name] = val
            self.elt.put()
        except (TypeError, HTTPError) as e:
            print >> sys.stderr, "Error while updating element: {0}".format(e)
            sys.exit(-1)

    def _log_before_change(self, changelog_f=None):
        if changelog_f:
            d = {'ct' : self._current_time(),
                 's_udf' : self.s_field_name,
                 'sn' : self.d_elt.name,
                 'si' : self.d_elt.id,
                 'su' : self.old_dest_udf,
                 'nv' : self.s_field}

            changelog_f.write(("{ct}: udf: {s_udf} on {sn} (id: {si}) from "
                               "{su} to {nv}.\n").format(**d))

        logging.info(("Copying from element with id: {0} to sample with "
                      " id: {1}").format(self.s_elt.id, self.d_elt.id))
         
    def _log_after_change(self):
        d = {'s_udf': self.s_field_name, 
             'd_udf': self.d_udf_name,
             'su': self.old_dest_udf,
             'nv': self.s_field, 
             'd_elt_type': self.d_elt._URI}

        logging.info("Updated {d_elt_type} udf: {d_udf}, from {su} to {nv}.".format(**d))
    
    def copy_udf(self, changelog_f = None):
        if self.s_field != self.old_dest_udf:
            self._log_before_change(changelog_f)
            self._set_udf(self.d_elt, self.d_udf_name, self.s_field)
            self._log_after_change()

def get_run_info(fc):
	fc_summary={}
	for iom in fc.input_output_maps:
		art = iom[0]['uri']
		lane = art.location[1].split(':')[0]
		if not fc_summary.has_key(lane):
     			fc_summary[lane]= dict(art.udf.items()) #"%.2f" % val ----round??
	return fc_summary








@@ -189,2787 +189,8 @@
 or%0A%0A
-class CopyField(object):%0A    %22%22%22Class to copy any filed (or udf) from any lims element to any %0A    udf on any other lims element%0A%0A    argumnets:%0A%0A    s_elt           source elemement - instance of a type%0A    d_elt           destination element - instance of a type%0A    s_field_name    name of source field (or udf) to be copied %0A    d_udf_name      name of destination udf name. If not specifyed%0A                    s_field_name will be used.%0A%0A    The copy_udf() function takes a logfile as optional argument.%0A    If this is given the changes will be logged there.%0A%0A    Written by Maya Brandi and Johannes Alnberg%0A    %22%22%22%0A    def __init__(self, s_elt, d_elt, s_field_name, d_udf_name = None):%0A        if not d_udf_name:%0A            d_udf_name = s_field_name%0A        self.s_elt = s_elt%0A        self.s_field_name = s_field_name%0A        self.s_field = self._get_field(s_elt, s_field_name)%0A        self.d_elt = d_elt%0A        self.d_udf_name = d_udf_name%0A        self.old_dest_udf = self._get_field(d_elt, d_udf_name)%0A%0A    def _current_time(self):%0A        return strftime(%22%25Y-%25m-%25d %25H:%25M:%25S%22, localtime())%0A%0A    def _get_field(self, elt, field):%0A        if field in elt.udf:%0A            return elt.udf%5Bfield%5D%0A        elif udf_name in dir(elt):%0A            return elt.field%0A        else:%0A            return None%0A%0A    def _set_udf(self, elt, udf_name, val):%0A        try:%0A            self.elt.udf%5Budf_name%5D = val%0A            self.elt.put()%0A        except (TypeError, HTTPError) as e:%0A            print %3E%3E sys.stderr, %22Error while updating element: %7B0%7D%22.format(e)%0A            sys.exit(-1)%0A%0A    def _log_before_change(self, changelog_f=None):%0A        if changelog_f:%0A            d = %7B'ct' : self._current_time(),%0A                 's_udf' : self.s_field_name,%0A                 'sn' : self.d_elt.name,%0A                 'si' : self.d_elt.id,%0A                 'su' : self.old_dest_udf,%0A                 'nv' : self.s_field%7D%0A%0A            changelog_f.write((%22%7Bct%7D: udf: %7Bs_udf%7D on %7Bsn%7D (id: %7Bsi%7D) from %22%0A                               %22%7Bsu%7D to %7Bnv%7D.%5Cn%22).format(**d))%0A%0A        logging.info((%22Copying from element with id: %7B0%7D to sample with %22%0A                      %22 id: %7B1%7D%22).format(self.s_elt.id, self.d_elt.id))%0A         %0A    def _log_after_change(self):%0A        d = %7B's_udf': self.s_field_name, %0A             'd_udf': self.d_udf_name,%0A             'su': self.old_dest_udf,%0A             'nv': self.s_field, %0A             'd_elt_type': self.d_elt._URI%7D%0A%0A        logging.info(%22Updated %7Bd_elt_type%7D udf: %7Bd_udf%7D, from %7Bsu%7D to %7Bnv%7D.%22.format(**d))%0A    %0A    def copy_udf(self, changelog_f = None):%0A        if self.s_field != self.old_dest_udf:%0A            self._log_before_change(changelog_f)%0A            self._set_udf(self.d_elt, self.d_udf_name, self.s_field)%0A            self._log_after_change()%0A%0A
 def 

clean up