tdorssers / TeslaPy

A Python module to use the Tesla Motors Owner API
MIT License
372 stars 82 forks source link

You allow override of the cache functions, but only on INIT, your internal functions still use your own code. #99

Closed gbizeau closed 2 years ago

gbizeau commented 2 years ago

I did a test and passed an override function for reading the cache file, While that worked on Init of the class, It appears that your builtin functions still utilize your existing functions. I need to be able to override your READ/WRITE in order to store this data in a DB for creating a multi user web based application.

tdorssers commented 2 years ago

Okay, I reviewed my code, but I can't find what you are pointing out. Can you provide some example code, so I can reproduce this?

gbizeau commented 2 years ago

Sure,

So in the init of the class, you allow an override of the cache_loader and cache_dumper functions.

def my_loader(): print("Loader") return dict()

def my_dumper(): print("Dumper") return dict()

with teslapy.Tesla('{email Address}', cache_loader=my_loader(), cache_dumper=my_dumper()) as tesla:

This works as expected, and these functions get called, however, you internal code doesn't honor this overloading of the class. Your _token_updater() function calls cache = self.cache_loader() on line 313 and self.cache_dumper(cache) on line 319. These should be referring back to the overloading functions I referred to on the init.

I put an inspect with a print on your _cache_load()

def _cache_load(self):
    """ Default cache loader method """
    import inspect
    print(inspect.stack()[1])
    try:
        with open(self.cache_file) as infile:
            cache = json.load(infile)
    except (IOError, ValueError):
        cache = {}
    return cache

    When I run the program, you can clearly see that it calls my functions, but then your _token_updater() calls your original functions instead.

Loader <- from my loader

Dumper <- from my dumper

Below were printed from your loader functions with the inspect I added.

FrameInfo(frame=<frame at 0x105c3c5b0, file '/PycharmProjects/venv/lib/python3.8/site-packages/teslapy/init.py', line 313, code _token_updater>, filename='/PycharmProjects/venv/lib/python3.8/site-packages/teslapy/init.py', lineno=313, function='_token_updater', code_context=[' cache = self.cache_loader()\n'], index=0)

Which is clearly coming from your "function='_token_updater'"

I know your setting the values to self on init, but they are not sticking.

    self.cache_loader = cache_loader or self._cache_load
    self.cache_dumper = cache_dumper or self._cache_dump    

If I print this at the end of your init

    logger.debug('Using SSO service URL %s', self.sso_base_url)

    print(cache_loader, cache_dumper)
    print(self.cache_loader, self.cache_dumper)

    I get 

{}{} <bound method Tesla._cache_load of <teslapy.Tesla object at 0x1003509d0>> <bound method Tesla._cache_dump of <teslapy.Tesla object at 0x1003509d0>> [{'id': 1492932703981922, 'vehicle_id': 535726691, 'vin': '5YJYGDEE4LF036810', 'display_name': 'Sulley', 'option_codes': 'AD15,AF00,APFB,APH4,AU3P,BC3B,BT37,CDM0,CH16,COCA,DRLH,DV4W,FC01,FG31,FM3B,GLFR,HL31,HM30,ID3W,IL31,LTPB,MDLY,MR30,PPSB,PC30,RENA,RF3G,RS3H,S3PB,SA3P,SC04,STCP,SU3C,TY9A,TM00,TW01,UT3P,WY9S,WR00,ZINV,MI00,PL30,SLR0,ST30,BG31,I36M,USSB,AUF1,RSF1,ILF1,FGF1,CPF0,TSHP,TR00,SR05,RL32,BY00,FCH1', 'color': None, 'access_type': 'OWNER', 'tokens': ['dd1cccf218ec15ea', 'e937c92a888e80ce'], 'state': 'asleep', 'in_service': False, 'id_s': '1492932703981922', 'calendar_enabled': True, 'api_version': 42, 'backseat_token': None, 'backseat_token_updated_at': None}]

Which is weird, if I am giving you an empty dict on the cache_load under _token_loder, it should be raising an error... It's not

Or maybe I am misunderstanding this whole thing?

I hope I explained it better this time :)

tdorssers commented 2 years ago

Crystal! There is an error in your code: with teslapy.Tesla('{email Address}', cache_loader=my_loader(), cache_dumper=my_dumper()) as tesla: Please remove () from my_loader and my_dumper and then it should work.

gbizeau commented 2 years ago

Ah Yes, when I added it with PyCharm it auto completed with the () :)

Thanks