feat: renamed Crx to crx. Added proxy support. Minor code improvements.

This commit is contained in:
Rick Rongen
2022-07-01 11:28:27 +02:00
parent 25bee5ee07
commit 305c1c9b31
10 changed files with 33 additions and 19 deletions

6
.gitignore vendored
View File

@@ -108,3 +108,9 @@ venv.bak/
# Idea # Idea
.idea/ .idea/
*.iml *.iml
*.txt
!requirements.txt
*.csv
*.xlsx
scripts/

View File

@@ -2,7 +2,8 @@ import json
from typing import Union, List, Optional from typing import Union, List, Optional
from urllib.parse import urljoin from urllib.parse import urljoin
import requests from requests import Session, Response
from requests.exceptions import RequestException
from .patchbuilder import PatchBuilder from .patchbuilder import PatchBuilder
from .simplenode import SimpleNode from .simplenode import SimpleNode
@@ -57,7 +58,7 @@ class CrxException(ValueError):
class CrxNodeNotFound(CrxException): class CrxNodeNotFound(CrxException):
def __init__(self, path: str, response: requests.Response): def __init__(self, path: str, response: Response):
self.path = path self.path = path
self.response = response self.response = response
@@ -77,14 +78,14 @@ class Connection:
query: str = CRX_QUERY, query: str = CRX_QUERY,
image_references: str = WCM_REFERENCES, image_references: str = WCM_REFERENCES,
wcm_replicate: str = WCM_REPLICATE): wcm_replicate: str = WCM_REPLICATE):
self._protocol = protocol
self._host = f'{protocol}://{host}:{port}' self._host = f'{protocol}://{host}:{port}'
self._data_root = self._host + root self._data_root = self._host + root
self._query_path = self._host + query self._query_path = self._host + query
self._image_references = self._host + image_references self._image_references = self._host + image_references
self._wcm_replicate = self._host + wcm_replicate self._wcm_replicate = self._host + wcm_replicate
self._session = requests.session() self._session = Session()
self._patch_builder: Optional[PatchBuilder] = None self._patch_builder: Optional[PatchBuilder] = None
@@ -98,6 +99,12 @@ class Connection:
""" """
self._session.auth = (username, password) self._session.auth = (username, password)
def proxy(self, proxy: str):
if proxy:
self._session.proxies[self._protocol] = proxy
elif self._session.proxies[self._protocol]:
del self._session.proxies[self._protocol]
def query(self, query: str, query_type: str = 'SQL2', raise_on_error: bool = True) -> List[str]: def query(self, query: str, query_type: str = 'SQL2', raise_on_error: bool = True) -> List[str]:
""" """
Perform an query and return the matching paths. Perform an query and return the matching paths.
@@ -223,7 +230,7 @@ class Connection:
url = urljoin(self._data_root, '.' + path + JSON_DATA_EXTENSION) url = urljoin(self._data_root, '.' + path + JSON_DATA_EXTENSION)
try: try:
response = self._session.get(url) response = self._session.get(url)
except requests.exceptions.RequestException as exception: except RequestException as exception:
raise CrxException() # todo more specific exceptions raise CrxException() # todo more specific exceptions
if response.status_code == 404: if response.status_code == 404:

View File

@@ -1,12 +1,11 @@
from typing import Dict, List, Union from typing import Dict, List, Union, TYPE_CHECKING
from logging import Logger from logging import Logger
import json import json
if False: if TYPE_CHECKING:
from .connection import Connection from .connection import Connection
from .simplenode import SimpleNode from .simplenode import SimpleNode
LOGGER = Logger(__name__) LOGGER = Logger(__name__)
@@ -32,11 +31,13 @@ _patch_builders = {
class PatchBuilder: class PatchBuilder:
changes: List[Dict] changes: List[Dict]
dry_run: bool
def __init__(self, connection: 'Connection'): def __init__(self, connection: 'Connection'):
self.connection = connection self.connection = connection
self.changes = [] self.changes = []
self.saved = False self.saved = False
self.dry_run = False
def set_value(self, path: str, value: Union[str, int, float], value_type: str): def set_value(self, path: str, value: Union[str, int, float], value_type: str):
self.changes.append({ self.changes.append({
@@ -51,7 +52,6 @@ class PatchBuilder:
'action': 'delete', 'action': 'delete',
'path': path 'path': path
}) })
pass
def save(self): def save(self):
patch = [] patch = []
@@ -62,7 +62,10 @@ class PatchBuilder:
LOGGER.warning("No patch to submit") LOGGER.warning("No patch to submit")
return return
self.connection.apply_diff('\n'.join(patch)) if self.dry_run:
print('\n'.join(patch))
else:
self.connection.apply_diff('\n'.join(patch))
self.saved = True self.saved = True
def __enter__(self): def __enter__(self):

View File

@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING
from logging import Logger from logging import Logger
if False: if TYPE_CHECKING:
from .connection import Connection from .connection import Connection

View File

@@ -1,4 +1,4 @@
from Crx import Connection, get_simple_con from crx import Connection, get_simple_con
def main(): def main():

View File

@@ -1,5 +1,2 @@
requests >= 2.21.0 requests ~= 2.0
#lxml >= 4.3.0 urllib3[socks] ~= 1.0
# For unprotect function
# pywin32 >= 224