Exemplo n.º 1
0
def get_element_processor(element, send_cache, verbose, dryrun):
    """Gets an instance of element that contains cached data, depending on 
    arguments passed.

    Will skip any cached data if send_cache is False or dryrun is True.

    @param element: Element class that should be fetched.
    @type element: L{MetaElement} sub class.
    @param send_cache: Indicates whether cache should be included.
    @type send_cache: bool
    @param verbose: Indicates whether or not the script runs in verbose mode.
    @type verbose: bool
    @param dryrun: Indicates whether the script is doing a dry run.
    @type dryrun: bool
    @return: L{MetaElement} sub class. The sub class is given by the element 
            parameter.

    """
    if dryrun or not send_cache:
        return element()
    if send_cache:
        c = Cacher(element.xml_tag_name)
        cached_data = c.get_cache()
        if cached_data is not None:
            if element.resend_cache:
                element_processor = element.from_xml_element(cached_data, 
                                                                element)
            else:
                logging.info(("Found cached data for \"%s\", but element "
                    "type declares not to resend this cache. "
                    "Removing cache.") % element.xml_tag_name)
                c.remove_cache()
                element_processor = element()
            if element_processor is None:
                logging.error(("Found cached data for \"%s\", but unable to "
                    "load. Check file \"%s\" for possible errors. "
                    "Continuing without cached data") % 
                                (element.xml_tag_name, c.file_path))
                element_processor = element()
            else:
                # We have successfully loaded the cached data.
                if element.resend_cache:
                    logging.debug(("Succesfully loaded cache for \"%s\", "
                        "removing cached file \"%s\".") % 
                        (element.xml_tag_name, c.file_path))
                    c.remove_cache()
        else:
            logging.debug(("Found no cached data for \"%s\".") % 
                            element.xml_tag_name)
            element_processor = element()
    return element_processor
Exemplo n.º 2
0
     if event_type == "resourceDown":
         if reason is None:
             print "Recieved resource down handle, but missing reason."
             print __doc__
             sys.exit(2)
         if share_down is None:
             print "Recieved resource down handle, but missing share down."
             print __doc__
             sys.exit(2)
         if date_down is None:
             print "Recieved resource down handle, but missing date down."
 # We have everything we require to create an event.
 # Attempt to find already cached data:
 m = MetaDoc(site_name)
 c = Cacher("events")
 cached_data = c.get_cache()
 if cached_data is not None:
     processor = Events.from_xml_element(cached_data, Events)
     if processor is None:
         print "Found previous event cache, but could not load. Please check "
         print "\"%s\" for errors. " % c.file_path
         print "Halting."
         sys.exit(2)
     else:
         c.remove_cache()
 else:
     processor = Events()
 if event_type == "resourceUp":
     e = ResourceUpEntry(date_up, reason, remarks)
 else:
     e = ResourceDownEntry(reason, date_down, date_up, share_down, remarks)