重構舊代碼:TACTIC Execl導出

#編者注 在2010年末左右編寫的這個功能的代碼,在當時爲了知足生產部門的需求,只對鏡頭列表實施了execl導出,並把系統當中的縮略圖導出,可是該代碼併爲按照TACTIC內部邏輯進行實現。問題1:縮略圖的命名規範是手動編寫到代碼中。問題2:只能獲取鏡頭的縮略圖,其餘縮略圖沒法實現。html

#CSV代碼調用位置與跟蹤 ##瀏覽器訪問的Debug 經過Debug信息,咱們能夠知道瀏覽器經過rpc的接口,調用了pyasm.widget.CsvDownloadWdghtml5

request_id:  139918269470464 - #0000001
timestamp:  2016-11-14 13:29:31
user:  admin
simple method:  <function get_widget at 0x21cb050>
ticket:  727bb5e01e791bbc76ae5755919a377a
(   'pyasm.widget.CsvDownloadWdg',
    {   'args': {   'column_names': [   'files',
                                        'general_checkin',
                                        'history',
                                        'description',
                                        'shot_code'],
                    'filename': 'vfx_leica_project_vfx_leica.csv',
                    'include_id': True,
                    'search_keys': [],
                    'search_type': 'vfx/leica?project=vfx',
                    'view': 'leica'},
        'libraries': {   'spt_button': True,
                         'spt_calendar': True,
                         'spt_html5upload': True,
                         'spt_icon_button': True,
                         'spt_popup': True,
                         'spt_tab': True,
                         'spt_table': True,
                         'spt_view_panel': True},
        'values': {   }})
/vfx_leica_project_vfx_leica.csv
SQL Query Count:  13
BVR Count:  None
Sending: 0 KB
Num SObjects: 7
Duration: 0.065 seconds (request_id: 139918269470464 - #0000001)
Memory: 50232 KB
Increment: 12420 KB
127.0.0.1 - - [14/Nov/2016:13:29:32] "POST /tactic/default/Api/ HTTP/1.1" 200 222 "http://localhost/tactic/vfx/link/leica" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"
DEPRECATED: dynamic file in app_server.py
127.0.0.1 - - [14/Nov/2016:13:29:32] "GET /tactic/default/WidgetServer/?project=vfx&dynamic_file=true&widget=pyasm.widget.CsvGenerator&filepath=%2Fhome%2Fapache%2Ftactic_temp%2Fupload%2F727bb5e01e791bbc76ae5755919a377a%2Fvfx_leica_project_vfx_leica.csv%0A& HTTP/1.1" 200 120 "http://localhost/tactic/vfx/link/leica" "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"

##定位代碼 在CsvDownloadWdg中添加自定代碼,查找實際執行的命令與參數web

def get_display(my):
        web = WebContainer.get_web()
      
        column_names = my.column_names
        column_names = [ x for x in column_names if x ]
        # create the file path
        tmp_dir = web.get_upload_dir()
        file_path = "%s/%s" % (tmp_dir, my.filename)

        from pyasm.command import CsvExportCmd
        cmd = CsvExportCmd(my.search_type, my.view, column_names, file_path)
        if my.search_keys:
            cmd.set_search_keys(my.search_keys)

        cmd.set_include_id(my.include_id)
        try:
            cmd.execute()
        except Exception, e:
            raise

        print "[file_path]:" + file_path
        return file_path

printapache

Class CsvDownloadWdg Method get_display Debug:[search_type]:vfx/leica?project=vfx
Class CsvDownloadWdg Method get_display Debug:[view]:leica
Class CsvDownloadWdg Method get_display Debug:[column_names]:['files', 'general_checkin', 'history', 'description', 'shot_code']
Class CsvDownloadWdg Method get_display Debug:[file_path]:/home/apache/tactic_temp/upload/727bb5e01e791bbc76ae5755919a377a/vfx_leica_project_vfx_leica.csv

經過分析能夠了解,該代碼使用CsvExportCmd,添加相關參數,最終執行execute函數,生成csv代碼內容,在返回csv路徑。 ##執行代碼 經過查詢CsvExportCmd在pyasm.command.CsvExportCmd路徑,能夠經過execute代碼查看瀏覽器

def execute(my):
        assert my.search_type
        assert my.view
        assert my.file_path

        search = Search(my.search_type)
        if my.search_ids:
            search.add_enum_order_by("id", my.search_ids)
            search.add_filters("id", my.search_ids)
            sobjects = search.get_sobjects()
        elif my.search_keys:

            sobjects = Search.get_by_search_keys(my.search_keys, keep_order=True)
            """
            search_codes = [SearchKey.extract_code(i) for i in my.search_keys if SearchKey.extract_code(i) ]
            if search_codes:
                search.add_filters("code", search_codes)
            else:
                search_ids = [SearchKey.extract_id(i) for i in my.search_keys if SearchKey.extract_id(i) ]
                search.add_filters("id", search_ids)
            """
        else:
            sobjects = search.get_sobjects()

        from pyasm.widget import WidgetConfigView
        from pyasm.web import Widget
        config = WidgetConfigView.get_by_search_type(my.search_type, my.view)

        columns = []
        if my.column_names:
            columns = my.column_names
        # should allow exporting ids only
        """
        else:
            if not config:
                columns = search.get_columns()
            else:
                columns = config.get_element_names()
        """
        if my.include_id:
            columns.insert(0, "id")

        # create the csv file
        org_file = file(my.file_path, 'w')
        csvwriter = csv.writer(org_file, quoting=csv.QUOTE_NONNUMERIC)

        # write the titles
        csvwriter.writerow(columns)

        elements = my.get_elements(config, columns)
        display_option_dict = {}
        # this is for widgets that do preprocessing on all sobjects
        for idx, element in enumerate(elements):
            element.set_sobjects(sobjects)
            element.preprocess()
            display_options = config.get_display_options(columns[idx])
            display_option_dict[element] = display_options
            
        for idx, sobject in enumerate(sobjects):
            values = []
            
            for element in elements:
                
                element.set_current_index(idx)
                value = element.get_text_value()
                if isinstance(value, Widget):
                    value = value.get_buffer_display()
                elif isinstance(value, basestring):
                    if isinstance(value, unicode):
                        value = value.encode('UTF-8', 'ignore')
                else:
                    value = str(value)

                options = display_option_dict.get(element)
                if options.get('csv_force_string')=='true' and value:
                    value= '#FORCESTRING#%s'%value
                values.append( value )
            # write the values as list
            csvwriter.writerow(values)

        org_file.close()


        file2 = open(my.file_path, 'r')
        mod_file_path = '%s_mod' %my.file_path
        mod_file = open(mod_file_path, 'w')
        for line in file2:
            mod_line = re.sub(r'(\'|\"|)(#FORCESTRING#)', '=\\1', line)
            mod_file.write(mod_line)

        # new file
        file2.close()
        mod_file.close()

        #os.unlink(my.file_path)
        shutil.move(mod_file_path, my.file_path)

#xls writeapp

##xlwt插入圖片24bit異常 異常報告函數

24bit

解決方法this

snapshot_image = Image.open(snapshot_path).convert('RGB')
bmp = snapshot_image.save('/tmp/temp.bmp')
sheet.insert_bitmap('/tmp/temp.bmp',row=row,col=col)

##image to bigcode

snapshot_path = server.get_path_from_snapshot(snapshot_code, file_type='web', mode='local_repo')

##codeserver

def execute(my,temp_dir):
        print "Class CsvExportCmd Method execute Debug:run"
        assert my.search_type
        assert my.view
        assert my.file_path

        print "Class CsvExportCmd Method execute Debug:[my.search_type]:" + my.search_type
        print "Class CsvExportCmd Method execute Debug:[my.view]:" + my.view
        print "Class CsvExportCmd Method execute Debug:[my.file_path]:" + my.file_path
        print "Class CsvExportCmd Method execute Debug:[my.column_names]:" + str(my.column_names)
        print "Class CsvExportCmd Method execute Debug:[my.search_ids]:" + str(my.search_ids)
        print "Class CsvExportCmd Method execute Debug:[my.search_keys]:" + str(my.search_keys)

        print "Class CsvExportCmd Method execute Debug: by search_type,search_ids,search_keys get sobjects"
        search = Search(my.search_type)
        if my.search_ids:
            search.add_enum_order_by("id", my.search_ids)
            search.add_filters("id", my.search_ids)
            sobjects = search.get_sobjects()
        elif my.search_keys:
            print 'elif my.search_keys'
            sobjects = Search.get_by_search_keys(my.search_keys, keep_order=True)
            """
            search_codes = [SearchKey.extract_code(i) for i in my.search_keys if SearchKey.extract_code(i) ]
            if search_codes:
                search.add_filters("code", search_codes)
            else:
                search_ids = [SearchKey.extract_id(i) for i in my.search_keys if SearchKey.extract_id(i) ]
                search.add_filters("id", search_ids)
            """
        else:
            sobjects = search.get_sobjects()

        print "Class CsvExportCmd Method execute Debug:[sobjects]:" + str(sobjects)

        from pyasm.widget import WidgetConfigView
        from pyasm.web import Widget
        config = WidgetConfigView.get_by_search_type(my.search_type, my.view)

        print "Class CsvExportCmd Method execute Debug:[config]:" + str(config)

        columns = []
        if my.column_names:
            columns = my.column_names
        # should allow exporting ids only
        """
        else:
            if not config:
                columns = search.get_columns()
            else:
                columns = config.get_element_names()
        """
        if my.include_id:
            columns.insert(0, "id")

        # create the csv file
        # org_file = file(my.file_path, 'w')
        # csvwriter = csv.writer(org_file, quoting=csv.QUOTE_NONNUMERIC)

# by hava
        # xls path
        xls_path = my.file_path.replace('.csv','.xls')
        print "Class CsvExportCmd Method execute Debug:[xls_path]:" + xls_path

        wbk = xlwt.Workbook()
        sheet = wbk.add_sheet(my.view, cell_overwrite_ok=True)

        # save the preview column in the data
        preview_col = -1

        # write the titles to the xls
        for col,title in enumerate(columns):
            if title == 'preview':
                preview_col = col
            sheet.write(0,col,title)
            print "[title]:" + title + " [col]:" + str(col)

        # old code get the columns config
        elements = my.get_elements(config, columns)
        display_option_dict = {}
        # this is for widgets that do preprocessing on all sobjects
        for idx, element in enumerate(elements):
            element.set_sobjects(sobjects)
            element.preprocess()
            display_options = config.get_display_options(columns[idx])
            display_option_dict[element] = display_options


        row = 1
        for idx,sobject in enumerate(sobjects):
            print "[idx]:" + str(idx)
            values = []

            for element in elements:
                element.set_current_index(idx)
                value = element.get_text_value()
                if isinstance(value, Widget):
                    value = value.get_buffer_display()
                elif isinstance(value, basestring):
                    if isinstance(value, unicode):
                        value = value.encode('UTF-8', 'ignore')
                else:
                    value = str(value)

                options = display_option_dict.get(element)
                if options.get('csv_force_string') == 'true' and value:
                    value = '#FORCESTRING#%s' % value
                values.append(value)

            row = idx + 1
            # write the context to the xls
            for col,item in enumerate(values):
                sheet.write(row,col,item.decode('utf-8','ignore'))

            # write the icon to the xls
            if preview_col != -1:
                # 1.get the icon path
                from tactic_client_lib import TacticServerStub
                server = TacticServerStub()

                if len(my.search_keys) == 0:
                    continue
                search_key = my.search_keys[idx]
                print "[search_key]:" + search_key
                snapshot = server.get_snapshot(search_key, context='icon', version=-1)
                snapshot_code = snapshot.get("code")
                # fixed:some demo project export xls not image
                if snapshot_code is None:
                    snapshot = server.get_snapshot(search_key, context='publish', version=-1)
                    snapshot_code = snapshot.get('code')
                # end
                snapshot_path = server.get_path_from_snapshot(snapshot_code, file_type='web', mode='local_repo')
                print "[snapshot_path]:" + snapshot_path
                for col,item in enumerate(values):
                    if col == preview_col:
                        try:
                            snapshot_image = Image.open(snapshot_path).convert('RGB')
                            bmp = snapshot_image.save(temp_dir + '/temp.bmp')
                            sheet.insert_bitmap(temp_dir + '/temp.bmp',row=row,col=col)
                        except Exception,ex:
                            print "[Exception]:" + str(ex)
                        finally:
                            try:
                                os.remove(temp_dir + '/temp.bmp')
                            except Exception,ex:
                                print "[Exception]:" + str(ex)

        wbk.save(xls_path)
        return xls_path
# end hava

change the xls

def get_display(my):
        web = WebContainer.get_web()
      
        column_names = my.column_names
        column_names = [ x for x in column_names if x ]
        # create the file path
        tmp_dir = web.get_upload_dir()
        file_path = "%s/%s" % (tmp_dir, my.filename)

        from pyasm.command import CsvExportCmd
        print "Class CsvDownloadWdg Method get_display Debug:[search_type]:" + my.search_type
        print "Class CsvDownloadWdg Method get_display Debug:[view]:" + my.view
        print "Class CsvDownloadWdg Method get_display Debug:[column_names]:" + str(column_names)
        print "Class CsvDownloadWdg Method get_display Debug:[file_path]:" + file_path
        cmd = CsvExportCmd(my.search_type, my.view, column_names, file_path)
        if my.search_keys:
            print "Class CsvDownloadWdg Method get_display Debug:[search_keys]:" + str(my.search_keys)
            cmd.set_search_keys(my.search_keys)

        print "Class CsvDownloadWdg Method get_display Debug:[include_id]:" + str(my.include_id)
        cmd.set_include_id(my.include_id)
        try:
            # cmd.execute()
            xls_path = cmd.execute(tmp_dir)
            print "[xls_path]:" + xls_path
            return xls_path
        except Exception, e:
            raise

        print "[file_path]:" + file_path
        return file_path
相關文章
相關標籤/搜索