carsonyl / pypac

Find and use proxy auto-config (PAC) files with Python and Requests.
https://pypac.readthedocs.io
Apache License 2.0
71 stars 19 forks source link

IndentationError: too many levels of indentation #22

Closed KiudLyrl closed 6 years ago

KiudLyrl commented 6 years ago

Hi,

I just installed pypac with pip and replaced my Session by a PACSession, and I get :

Traceback (most recent call last):
  File "D:\TimecardAutomation\absReaderWitPAC.py", line 41, in <module>
    login_page = s.get(login_url)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 521, in get
    return self.request('GET', url, **kwargs)
  File "C:\Python27\lib\site-packages\pypac\api.py", line 184, in request
    self.get_pac()
  File "C:\Python27\lib\site-packages\pypac\api.py", line 249, in get_pac
    pac = get_pac(recursion_limit=self._recursion_limit)
  File "C:\Python27\lib\site-packages\pypac\api.py", line 57, in get_pac
    return PACFile(downloaded_pac, **kwargs)
  File "C:\Python27\lib\site-packages\pypac\parser.py", line 60, in __init__
    context.execute(pac_js)
  File "C:\Python27\lib\site-packages\js2py\evaljs.py", line 176, in execute
    compiled = cache[hashkey] = compile(code, '<EvalJS snippet>', 'exec')
  File "<EvalJS snippet>", line 221
    def PyJs_LONG_87_(var=var):
                              ^
IndentationError: too many levels of indentation

code is the following :

[from pypac import PACSession
from bs4 import BeautifulSoup
import os

username_field_name = "ctl00$ContentPlaceHolder1$Login1$UserName"
password_field_name = "ctl00$ContentPlaceHolder1$Login1$Password"
form_action_name = "***"
username = "***"
password = "***"

login_url = "***"
abscence_history_url = "***"

payload = {
    username_field_name: username,
    password_field_name: password,
    "ctl00$ContentPlaceHolder1$HiddenUrlPage" : "***",
    "ctl00$ContentPlaceHolder1$Login1$LoginButton" : "***",
    "__EVENTTARGET" : "",
    "__EVENTARGUMENT" : ""
}

with PACSession() as s:
login_page = s.get(login_url)
    print "getting page 1"
    login_page = s.get(login_url
    login_soup = BeautifulSoup(login_page.content)
    payload["__VIEWSTATE"] = login_soup.select_one("#__VIEWSTATE")["value"]
    payload["__VIEWSTATEGENERATOR"] = login_soup.select_one("#__VIEWSTATEGENERATOR")["value"]
    first_rep = s.post(login_url, data=payload)
    response = s.get(abscence_history_url)](url)
carsonyl commented 6 years ago

It sounds like your PAC file is complicated enough that Js2Py generates Python with too much indentation. Unfortunately I don't have a solution for you at the moment.

It would be interesting if you could share your PAC file in order to get a better idea of what kind of JavaScript triggers this problem.

KiudLyrl commented 6 years ago

Yup, I understood that too later. I don't think I can share it, I think it could get me in trouble, but I can tell you why it's not working.

Inside you can find :

if [}
else {
 if {} 
  else {
         17 levels totals
         }
}

Python has an indentation limit (but 17 imbricated if/else is still permitted, the limit is 100 I think) so apparently J2sPy generates a python file with much more levels of imbrication than the original pac file.

I could change it, but that would defeat the purpose because I want my user to directly be able to use my script.

I can't ask the guys behind the PAC file to change it (big corporation)

Well I don't know what to do, write my own pac parser maybe. I'm very unfamilliar with PAC files, I discovered them yesterday.

Inside there is only "shExpMatch()", "dnsDomainIs()" and "isInNet()" methods, is that the only kind of method you can find in a PAC file? I could easily write a python converter for that.

carsonyl commented 6 years ago

The 3 functions in the PAC file that you mention probably cover the vast majority of use cases. Here's the complete list of PAC functions. PyPAC is trying to be comprehensive by implementing all the functions in the specification, and also using a full JavaScript engine to interpret the PAC file.

I'll experiment with some ideas to resolve your problem.

carsonyl commented 6 years ago

I've just released PyPAC 0.10.0, which should solve this issue. Please let me know.

KiudLyrl commented 5 years ago

It works :D