0

I'm working on a flask project and in doing so I used the shareplum library. I realized that there were methods that I needed that were only a slight change to some of the existing methods in that library. I added my methods to one of the files in that library.

I'm working in a venv so this only affects this project but I'm pretty sure this isn't the correct way to do this.

My structure is a basic flask structure.

integrator.xxx.int/
├── app
│   ├── __pycache__
│   ├── static
│   └── templates
├── __pycache__
├── test_scripts
└── venv
    ├── bin
    ├── include
    ├── lib
    ├── lib64 -> lib
    └── share

File resides in venv/lib/python3.x/site-packages/shareplum/folder.py. In that file there is a get_image, get_file, and get_folder. I've added get_images, get_files, and get_folders.

What is the correct way, and the correct place to do this?

I've added that file to git so that it's tracked. That seemed to work, but I ran into issues with not being on the exact same version of python. That's my problem for not setting up my venv properly, and maybe if it was adding that file to vcs is the correct answer.

Thank you

More information. I'm confused as to exactly what to override here.

From the documentation from Shareplum docs:

folder = site.Folder('Shared Documents/This Folder')

I created a access class to access shareplum.

Relevant parts of app/sharepointacces.py

from shareplum import Site
from shareplum import Office365
from shareplum.site import Version
import os.path
from os import path

class SPAccess:

    def __init__(self):
        server_url = "https://x.sharepoint.com/"
        site_url = "sites/x/"

        un = "x"
        pw = "x"
        authcookie = Office365(server_url, username=un, password=pw).GetCookies()
        self.site = Site(server_url + site_url, version=Version.v2016, authcookie=authcookie)


    def getFolderList(self, folderURL):
        folder = site.Folder(folderURL)
        return(folder.get_folders())

    def getFileList(self, folderURL):
        folderURL = folderURL.replace(".", "")
        folder = self.site.Folder(folderURL)
        return(folder.get_files())

folder.get_folders() is one of the methods I've added.

This is instantiated from routes.py by:

   spa = SPAccess()
fileInfo = spa.getFileList(docURL)

So going by this previous answer I can just extend folder correct?

I created a app/sptools.py and did as it said which was to have every method from the original.

from shareplum import folder

class SPTools(folder.SPTools):
    def __init__(self, session, folder_name, url):
        self._session = session
        self.folder_name = folder_name
        self.site_url = url
        self.timeout = 3

        self.info = self._create_folder()

    @property
    def contextinfo(self):
        pass

    def _create_folder(self):
        pass

    def delete_folder(self, relative_url):
        pass

    def delete_file(self, file_name):
        pass

    @property
    def items(self):
        pass

    @property
    def files(self):
        pass

    def upload_file(self, content, file_name):
        pass

    def check_out(self, file_name):
        pass

    def check_in(self, file_name, comment):
        pass

    def get_file(self, file_name):
        pass

# custom methods below.

    def get_files(self):
        response = self._session.get(self.site_url + f"/_api/web/GetFolderByServerRelativeUrl('{self.folder_name}')/files")
        data = json.loads(response.text)
        return data['value']


    def get_folders(self):
        response = self._session.get(self.site_url + f"/_api/web/GetFolderByServerRelativeUrl('{self.folder_name}')/folders")
        data = json.loads(response.text)
        return data['value']

    def get_image(self, file_name):
        response = self._session.get(self.site_url + f"/_api/web/GetFileByServerRelativeUrl('{self.info['d']['ServerRelativeUrl']}/{file_name}')/$value")
        if response.status_code== 200:
            with open('/sites/integrator.cng.int/app/static/images/' +
                      file_name + '.jpg', 'wb') as f:
                f.write(response.content)
        return "ok"

#this pulls the images down and saves them locally if they're not there already. 
    def get_images(self, fileName):
            response = self._session.get(self.site_url + f"/_api/web/GetFileByServerRelativeUrl('{self.info['d']['ServerRelativeUrl']}/{fileName}')/$value")
            if response.status_code== 200:
                fileName = fileName[-22:]
                with open('/sites/integrator.cng.int/app/static/images/' +
                          fileName, 'wb') as f:
                    f.write(response.content)
            return

But now I'm confused. Since folder is called as site.Folder from sharepointaccess.py do I simply import my new class into sharepointaccess.py and instead of

site.Folder 

I do

site.SPTools

I don't think so since Folder is imported from site.

Is that correct? I'm confused as to how I import mine. Since Folder is never implicitly imported in sharepoint access and I think relies on this inside of venv/lib/python3.8/site-packates/shareplum/site.py

from .folder import _Folder
TheEditor
  • 486
  • 1
  • 4
  • 18
  • 1
    You can extend a class and rewrite / overide the functions you want leaving the remaining functions in the class intact. then you would call your own class which would be a subclass of the library – Chris Doyle Apr 28 '20 at 10:50
  • @ChrisDoyle I've edited my post, my original was very vague. I understand what I need to do but in this case I"m not sure how to actually accomplish it. It's the whole Folder is called from site that has me confused I think. – TheEditor May 01 '20 at 17:16

0 Answers0