Esempio n. 1
0
 def send_content(self,**kwargs):
     import requests
     from os import path
     ## FileContent
     src_filepath = kwargs.get('src_filepath', None)
     if self.mz_imageid:
         _endpoint = self.set_endpoint_uri(**kwargs)["endpoint_resource_doc_content"]
         print "send 1"
     elif not kwargs.get("bf_imageid", self.bf_imageid) and src_filepath is not None:
         self.bf_imageid = src_filepath.split('/')[-1].split('.')[0]
         kwargs['bf_imageid'] = self.bf_imageid
         self.ext = src_filepath.split('.')[-1]
         _endpoint = self.set_endpoint_uri(**kwargs)["endpoint_resource_doc_tree_content"]
         print "send 2"
     else:
         from mozu_image_util_functions import netsrv101_path_maker
         netsrv_src = netsrv101_path_maker(kwargs.get("bf_imageid", self.bf_imageid))
         kwargs['src_filepath'] = netsrv_src #kwargs.get('src_filepath', netsrv_src)
         src_filepath = kwargs.get('src_filepath', '')
         self.ext = src_filepath.split('.')[-1]
         _endpoint = self.set_endpoint_uri(**kwargs)["endpoint_resource_doc_tree_content"]
         print "send 3"
     ## debug logging
     print _endpoint, " <-- Endpoint Choice"
     #import generic_logger
     # mylogger = generic_logger.basic_log_file_obj()
     # mylogger.info(_endpoint)
     if not self.ext:
         self.ext = 'png'
     self.mimetype = "image/{}".format(self.ext.lower().replace('jpg','jpeg'))
     self.headers["Content-type"] = self.mimetype
     self.set_document_payload(**kwargs)
     # mylogger.debug(self.document_payload)
     # mylogger.debug(self.headers)
     ## end debug logging
     print 'Start Send Streaming Data Try'
     try:
         stream = open(path.abspath(src_filepath), 'rb').read()
         _content_response = requests.put(_endpoint, data=stream, headers=self.headers, verify=False)
         MozuRestClient.http_status_code = _content_response.status_code
         print "ContentPutResponse Send: {0}\n{1}".format(_content_response.status_code, _endpoint)
         # mylogger.warning(_content_response)
         return _content_response
     except AttributeError as e:
         # mylogger.exception(e)
         print "OIO Error 171 Failed send_content"
Esempio n. 2
0
def main(fileslist):
    import sqlalchemy
    from db import mozu_image_table_instance
    from mozu_image_util_functions import compile_todict_for_class_instance_variables, magick_convert_to_jpeg, netsrv101_path_maker
    # Compiles Data Payload and other Vars per Doc -- Including src_filepath -- **values keys set per instance
    # print type(fileslist), '<--Type\tLenLoFilepaths', len(fileslist), '\t', fileslist
    ### Date Defs
    from os import path # chdir , curdir
    import datetime #, glob, shutil

    todaysdatefullsecs = '{:%Y%m%d%H%M%S}'.format(datetime.datetime.now())
    todaysdatefull = todaysdatefullsecs[:12]
    todaysdate = todaysdatefull[:8]  # '{:%Y,%m,%d}'.format(datetime.datetime.now())

    # Define for Creating Archive dirs
    archive = '/mnt/Post_Complete/Complete_Archive/Uploaded'
    # archive_uploaded = path.join(archive, "dateloaded_" + str(todaysdate).replace(",", ""), "uploaded_" + str(todaysdatefullsecs).replace(",", ""))
    archive_uploaded_day = path.join(archive, "dateloaded_" + str(todaysdate).replace(",", ""))
    imgdest_jpg_mozu = path.join(archive_uploaded_day, 'JPG_MOZU_LOAD')
    # imgdest_jpg_mozu_loaded = path.join(imgdest_jpg_mozu, 'LOADED')
    if path.dirname(fileslist[0]).split('/')[-1] == 'JPG_MOZU_LOAD':
        #         fileslistX= [magick_convert_to_jpeg(f) for f in fileslist if f.split('.')[-1] == 'png']
        fileslist = [magick_convert_to_jpeg(f, destdir=imgdest_jpg_mozu) for f in fileslist if f]
    else:
        fileslist = fileslist # [ path.abspath(f) for f in fileslist if f ]
    if not path.isfile(fileslist[0]):
        fileslist = netsrv101_path_maker(fileslist)
    compiled_instance_vars = compile_todict_for_class_instance_variables(fileslist=fileslist)
    # print type(compiled_instance_vars), '<--Type\tLenCompiledInsVars', len(compiled_instance_vars), '\tKeys: ', compiled_instance_vars.keys()
    # print compiled_instance_vars, "186-MZEXECY"
    styles_incr_media_version = []
    for key,values in compiled_instance_vars.iteritems():
        # v = include_keys(values, __mozu_image_table_valid_keys__)
        # print "IncludedKeys: {}\n\tkey:\t{}\n\tvalues:\t{}".format(v.items(), key , values.popitem())
        if not values.get('mz_imageid'):
            # ### --> src_filepath = k # will need src_filepath in order to perfom any image manipulation
            # ## ---> before loading(would actually need to redo the md5checksum from compiler)
            # Insert -- Then try Update if Insert to DB fails or Create NewDoc Fails to Mozu
            try:
                values['mz_imageid'], response = upload_new(**values)
                create_resource_resp = upload_new(**values)
                mozu_image_table = mozu_image_table_instance()
                print 'Initial Post to Mozu {0}'.format(create_resource_resp.keys()[0])
                if int(create_resource_resp.keys()[0]) == int(201):
                    table_args = include_keys(values, __mozu_image_table_valid_keys__)
                    insert_db = mozu_image_table.insert(values=dict(**table_args))
                    res_insrt = insert_db.execute()
                    print 'Inserted --> ', values.items(), ' <-- ', dir(res_insrt)
                    styles_incr_media_version.append(table_args.get('bf_imageid', locals().get('bf_imageid')))
                elif int(create_resource_resp.keys()[0]) <= int(409):
                    table_args = include_keys(values, __mozu_image_table_valid_keys__)
                    mz_imageid = mozu_image_table.select( whereclause=( (mozu_image_table.c.bf_imageid == table_args['bf_imageid']) ) ).execute().fetchone()['mz_imageid']
                    bf_imageid = mozu_image_table.select( whereclause=( (mozu_image_table.c.mz_imageid == table_args['mz_imageid']) ) ).execute().fetchone()['bf_imageid']
                    table_args['bf_imageid'] = values['bf_imageid'] = bf_imageid
                    table_args['mz_imageid'] = values['mz_imageid'] = mz_imageid
                    resp = update_content_mz_image(**values)
                    print('RESP 207 mzexec: {}'.format(resp))
                    #upsert_content_resp = upsert_data_mz_image(**values)  # ,dict(**values))
                    if resp.http_status_code < 400:
                        update_db = mozu_image_table.update(values=dict(**table_args),whereclause=mozu_image_table.c.bf_imageid == table_args['bf_imageid'])
                        res = update_db.execute()
                        print res, 'Updated--> ', values.items(), ' <-- ', update_db
                    styles_incr_media_version.append(table_args.get('bf_imageid', locals().get('bf_imageid')))
                else:
                    print "HTTP Status: {}\n Raising Integrity Error".format(create_resource_resp.http_status_code)
                    raise sqlalchemy.exc.IntegrityError()

            except ValueError as ve: #sqlalchemy.exc.IntegrityError:
                print 'VALUE Error and everything is or will be commented out below because it is in the db already', ve
                #return 'IntegrityError'
            except KeyError as ke:  # sqlalchemy.exc.IntegrityError:
                print 'Key Error and everything is or will be commented out below because it is in the db already', ke
                #return 'IntegrityError'
                #pass
                # except IOError:
                #     print "ENDING ERROR...", values
        elif values.get('mz_imageid'):
            print "KWARGS has MZID: {}".format(values.get('mz_imageid'))
            styles_incr_media_version.append(values.get('bf_imageid', locals().get('bf_imageid')))
    print 'styles list ', styles_incr_media_version
    return list(set(sorted(styles_incr_media_version)))