initBasti / Amazon2PlentySync (public) (License: GPLv3) (since 2019-01-27) (hash sha1)
Transfer your data from you Amazon Flatfile spreadsheet over to the Plentymarkets system. How to is included in the readme

/product_import.py (421fc25b4a08091e5680f0de9e999a99d01c9575) (11481 bytes) (mode 100644) (type blob)

import tkinter
from tkinter.filedialog import askopenfilename
import sys
import platform
import os
import ntpath
from packages.item_upload import itemUpload, WrongEncodingException, check_encoding, check_flatfile, itemPropertyUpload
from packages.barcode import EmptyFieldWarning
from packages.amazon_data_upload import featureUpload
from packages.log_files import fileNotFoundLog, keyErrorLog, wrongEncodingLog, unboundLocalLog, emptyFieldWarningLog
from packages.gui.category_chooser import CategoryChooser
from packages.config import config_creation, config_read, config_write


def main():

    #define variable used throughout the script
    upload_folder = ''
    log_folder = ''
    recent_path = ''
    config_path = ''
    attribute_date = ''
    sheet = {'path':'', 'encoding':''}
    intern_number = {'path':'', 'encoding':''}
    stocklist = {'path':'', 'encoding':''}
    attributefile = {'path':'', 'encoding':''}
    step = int(0)
    fexc = ''
    # Create a list of step names where every name fits to the index of a step number
    step_name = ['environment-creation',
                 'config-creation',
                 'config-reading',
                 'import-flatfile',
                 'GUI',
                 'import-internlist',
                 'import-stocklist',
                 'item-upload',
                 'feature_upload',
                 'property_upload'
                 ]

    # define the features for plentymarkets
    features = {
                'color_map':1,
                'model':4,
                'sleeve_type':8,
                'pattern_type':11,
                'collar_style':12,
                'item_name':13,
                'closure_type':14,
                'style_name':15,
                'care_instructions':16,
                'package_length':17,
                'package_width':18,
                'package_height':19,
                'package_weight':20
                }
    # Check if the os is Linux, in that case the initial directory is Documents
    # Unless Documents is not available in which case it is ~
    initial_directory = '../'

    if(platform.system() == 'Linux'):
        if(os.path.exists(path='/home/' + os.getlogin() + '/Documents/')):
            initial_directory = '/home/' + os.getlogin() + '/Documents/'
        else:
            initial_directory = '/home/' + os.getlogin()

    step += 1;
    # CONFIG CREATION
    # Create a config if there is none
    config_path = config_creation()

    step += 1;
    # CONFIG READING
    # Get the Upload and data folder from the config if possible
    if(config_read(config_path)['upload_folder']):
        upload_folder = config_read(config_path)['upload_folder']
    if(config_read(config_path)['data_folder']):
        recent_path = config_read(config_path)['data_folder']
    if(config_read(config_path)['attribute_file']):
        attributefile['path'] = config_read(config_path)['attribute_file']
        attributefile = check_encoding(attributefile)
    if(config_read(config_path)['file_change_date']):
        attribute_date = config_read(config_path)['file_change_date']
    if(not(recent_path)):
        recent_path = tkinter.filedialog.askdirectory(initialdir=initial_directory,
                                                      title="Choose a folder from where to upload")
    # END of CONFIG READING

    # Import Flatfile
    step += 1
    sheet['path'] = askopenfilename(initialdir=recent_path,
                            title="Amazon Flatfile as .csv",
                            filetypes=[ ("csv files", "*.csv") ])

    sheet = check_encoding(sheet)

    # Check if the file was loaded properly and got the correct format
    try:
        if(not( check_flatfile(sheet) )):
            print('Please fix the flatfile and try again.\n')
            print('Press Enter to continue...')
            input()
            sys.exit()
    except OSError as err:
        print('flatfile not found\n')
        print('Press Enter to continue...')
        input()
        sys.exit()

    step += 1;
    # GUI
    # Ask the user for input inside a gui asking for categories and name
    cchooser = CategoryChooser(None, upload_folder, sheet, attributefile, attribute_date)
    cchooser.title("Choose the category and name")
    LOOP_ACTIVE = True
    while (LOOP_ACTIVE):
        if(cchooser):
            cchooser.update()
        if(cchooser.data['name'] and cchooser.data['categories']):
            LOOP_ACTIVE = False
            cchooser.destroy()
    # END GUI

    user_data = cchooser.data
    # Writing the changes into the config for the next start of the script
    if(cchooser.newpath['upload-path'] and cchooser.newpath['attribute-path']):
        config_update = {'row1':{ 'title': 'upload_folder', 'value': cchooser.newpath['upload-path'] },
                         'row2':{ 'title':'data_folder', 'value':recent_path },
                         'row3':{ 'title':'attribute_file', 'value': cchooser.newpath['attribute-path'] },
                         'row4':{ 'title':'file_change_date', 'value':cchooser.atrdate }}
        try:
            config_write(config_path, config_update)
        except Exception as err:
            print("ERROR: {0}\n".format(err))
        upload_folder = cchooser.newpath
        attributefile['path'] = cchooser.newpath['attribute-path']
        attribute_date = cchooser.atrdate
    elif(not( cchooser.newpath['upload-path'] ) and cchooser.newpath['attribute-path']):
        config_update = {'row1':{ 'title': 'upload_folder', 'value': upload_folder },
                         'row2':{ 'title':'data_folder', 'value':recent_path },
                         'row3':{ 'title':'attribute_file', 'value': cchooser.newpath['attribute-path'] },
                         'row4':{ 'title':'file_change_date', 'value':cchooser.atrdate }}
        try:
            config_write(config_path, config_update)
        except Exception as err:
            print("ERROR: {0}\n".format(err))
        attributefile['path'] = cchooser.newpath['attribute-path']
        attribute_date = cchooser.atrdate
    elif(cchooser.newpath['upload-path'] and not( cchooser.newpath['attribute-path'] )):
        config_update = {'row1':{ 'title': 'upload_folder', 'value': cchooser.newpath['upload-path'] },
                         'row2':{ 'title':'data_folder', 'value':recent_path },
                         'row3':{ 'title':'attribute_file', 'value': attributefile['path'] },
                         'row4':{ 'title':'file_change_date', 'value': attribute_date }}
        try:
            config_write(config_path, config_update)
        except Exception as err:
            print("ERROR: {0}\n".format(err))
        upload_folder = cchooser.newpath
    # END of Writing config part

    if(user_data):
        # Check if there is already a log folder within the upload folder
        if( not(os.path.exists(os.path.join(upload_folder, 'log'))) ):
            log_folder = os.path.join(upload_folder, 'log')
            os.makedirs(log_folder)
        elif( os.path.exists(os.path.join(upload_folder, 'log')) ):
            log_folder = os.path.join(upload_folder, 'log')

        step += 1
        intern_number['path'] = askopenfilename(initialdir=recent_path,
                                title="The Intern Numbers as .csv",
                                filetypes=[ ("csv files", "*.csv") ])

        intern_number = check_encoding(intern_number)

        step += 1;
        try:
            stocklist['path'] = askopenfilename(initialdir=recent_path,
                                    title="The Stockreport from Amazon as .csv",
                                    filetypes=[ ("csv files", "*.csv") ])

            stocklist = check_encoding(stocklist)
        except OSError as fexc:
            fileNotFoundLog(log_path=log_folder, step_number=step, step_desc=step_name[step], file_name=fexc)

        step += 1
        try:
            print("\nItem Upload\n")
            itemUpload(sheet, intern_number, stocklist, attributefile, upload_folder, user_data)
        except WrongEncodingException:
            wrongEncodingLog(log_path=log_folder, step_number=step, step_desc=step_name[step], file_name="flatfile")
        except KeyError as kexc:
            keyErrorLog(log_path=log_folder, step_number=step, step_desc=step_name[step], key_name=kexc, file_name=ntpath.basename(sheet))
        except OSError as fexc:
            fileNotFoundLog(log_path=log_folder, step_number=step, step_desc=step_name[step], file_name="intern_numbers")
        except TypeError:
            fileNotFoundLog(log_path=log_folder, step_number=step, step_desc=step_name[step], file_name="flatfile")
        except UnboundLocalError as uexc:
            unboundLocalLog(log_path=log_folder, step_number=step, step_desc=step_name[step], filename=ntpath.basename(sheet), variable_name=uexc.args)
        except EmptyFieldWarning as eexc:
            emptyFieldWarningLog(log_path=log_folder, step_number=step, step_desc=step_name[step], field_name=eexc.errorargs, file_name=ntpath.basename(sheet))
        except Exception as exc:
            print("Item Upload failed!\n")
            if(exc == 'item_sku'):
                print("It is very likely that you don't have the proper headers, use the english ones!\n")
            e = sys.exc_info()
            print("Error @ FILE: {0}, LINE: {1}\n".format( e[2].tb_frame.f_code.co_filename, e[2].tb_lineno ))
            for element in e:
                print(element)

        try:
            print("Feature Upload")
            step += 1
            featureUpload(flatfile=sheet, features=features, folder=upload_folder)
        except KeyError as kexc:
            keyErrorLog(log_path=log_folder, step_number=step, step_desc=step_name[step], key_name=kexc, file_name=ntpath.basename(sheet))
        except UnboundLocalError as uexc:
            unboundLocalLog(log_path=log_folder, step_number=step, step_desc=step_name[step], filename=ntpath.basename(sheet), variable_name=uexc.args)
        except EmptyFieldWarning as eexc:
            emptyFieldWarningLog(log_path=log_folder, step_number=step, step_desc=step_name[step], field_name=eexc.errorargs, file_name=ntpath.basename(sheet))
        except OSError as err:
            print(err)
            print("Missing Data, check if you have\n - a flatfile\n - a intern file table\n - export file from plentymarkets\n - a sheet with the stock numbers!\n")
        try:
            print("Property Upload")
            step += 1
            itemPropertyUpload(flatfile=sheet, folder=upload_folder)
        except KeyError as kexc:
            keyErrorLog(log_path=log_folder, step_number=step, step_desc=step_name[step], key_name=kexc, file_name=ntpath.basename(sheet))
        except UnboundLocalError as uexc:
            unboundLocalLog(log_path=log_folder, step_number=step, step_desc=step_name[step], filename=ntpath.basename(sheet), variable_name=uexc.args)
        except EmptyFieldWarning as eexc:
            emptyFieldWarningLog(log_path=log_folder, step_number=step, step_desc=step_name[step], field_name=eexc.errorargs, file_name=ntpath.basename(sheet))
        except OSError as err:
            print(err)
            print("Missing Data, check if you have\n - a flatfile\n - a intern file table\n - export file from plentymarkets\n - a sheet with the stock numbers!\n")

        del fexc
    else:
        print("Choose a category and a name.\n")
if __name__ == '__main__':
    main()


Mode Type Size Ref File
100644 blob 5364 4bc518fabd35f03be275c799d4946690379072fd Instructions.md
100644 blob 10299 816a1d0ee7f4496c32fdb92c7811eaf877d0efc0 LICENSE.md
100644 blob 3811 87e1df60c4c99c8400bcdcf1641a33e1ade245d8 README.md
100755 blob 546 98298b26c8cc746806228865e30466bee9d65c13 build_new_version.sh
100755 blob 35 842a6affcaddec0f7dc134889a531b100c62f473 clean_upload.sh
040000 tree - 44fdb6c76203afd839100ce4b7aa76eecb4edffa packages
100644 blob 11481 421fc25b4a08091e5680f0de9e999a99d01c9575 product_import.py
100644 blob 178 627b92178d30145f0dedc2f4a31ac856f6a15ea1 todo.md
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/initBasti/Amazon2PlentySync

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/initBasti/Amazon2PlentySync

Clone this repository using git:
git clone git://git.rocketgit.com/user/initBasti/Amazon2PlentySync

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main