Skip to content

Sourcery refactored master branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/robotkernel/builders_32.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ def build_suite(code: str, cell_history: Dict[str, str], data_only: bool = False
SuiteBuilder(suite, defaults).visit(ast)

# Strip duplicate keywords
keywords = {}
for keyword in suite.resource.keywords:
keywords[keyword.name] = keyword
keywords = {keyword.name: keyword for keyword in suite.resource.keywords}
suite.resource.keywords._items = list(keywords.values())

# Strip duplicate variables
variables = {}
for variable in suite.resource.variables:
variables[variable.name] = variable
variables = {variable.name: variable for variable in suite.resource.variables}
Comment on lines -55 to +59
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function build_suite refactored with the following changes:

suite.resource.variables._items = list(variables.values())

# Detect RPA
Expand Down
12 changes: 5 additions & 7 deletions src/robotkernel/completion_finders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

def complete_libraries(needle: str,) -> List[str]:
"""Complete library names."""
matches = []

for lib in list(STDLIBS) + list(get_root_modules()):
if needle in lib.lower():
matches.append(lib)

return matches
return [
lib
for lib in list(STDLIBS) + list(get_root_modules())
if needle in lib.lower()
]
Comment on lines -10 to +14
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function complete_libraries refactored with the following changes:

2 changes: 1 addition & 1 deletion src/robotkernel/executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def execute_ipywidget(
arguments,
values,
):
header = rpa and "Tasks" or "Test Cases"
header = "Tasks" if rpa else "Test Cases"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function execute_python.execute_ipywidget refactored with the following changes:

code += f"""\

*** {header} ***
Expand Down
15 changes: 7 additions & 8 deletions src/robotkernel/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,7 @@ def end_test(self, name, attributes):


def clear_drivers(drivers, type_):
remained = []
for driver in drivers:
if driver.get("type") != type_:
remained.append(driver)
remained = [driver for driver in drivers if driver.get("type") != type_]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function clear_drivers refactored with the following changes:

drivers.clear()
drivers.extend(remained)

Expand All @@ -189,10 +186,12 @@ def get_webdrivers(cache, type_):
conn = cache._connections[idx]
if conn in cache._closed:
continue
aliases = []
for alias, idx_ in cache._aliases.items():
if (idx + 1) == idx_:
aliases.append(alias)
aliases = [
alias
for alias, idx_ in cache._aliases.items()
if (idx + 1) == idx_
]

Comment on lines -192 to +194
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_webdrivers refactored with the following changes:

drivers.append(
dict(
instance=conn,
Expand Down
14 changes: 6 additions & 8 deletions src/robotkernel/monkeypatches.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def read(self, ipynbfile, rawdata):

for cell in notebook.cells:
# Skip non-code cells
if not cell.cell_type == "code":
if cell.cell_type != "code":
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function exec_code_into_module.NotebookReader.NotebookReader.read refactored with the following changes:

continue

# Execute %%python module magics
Expand All @@ -60,21 +60,19 @@ def read(self, ipynbfile, rawdata):

if HAS_RF32_PARSER:
return data
else:
robotfile = BytesIO(data.encode("UTF-8"))
return RobotReader().read(robotfile, rawdata, ipynbfile.name)
robotfile = BytesIO(data.encode("UTF-8"))
return RobotReader().read(robotfile, rawdata, ipynbfile.name)

return NotebookReader()


def _get_ipynb_file(old):
def _get_file(self, source, accept_text):
path = self._get_path(source, accept_text)
if path and os.path.splitext(path)[1].lower() == ".ipynb":
file = StringIO(NotebookReader().read(path, ""))
return file, os.path.basename(path), True
else:
if not path or os.path.splitext(path)[1].lower() != ".ipynb":
return old(self, source, accept_text)
file = StringIO(NotebookReader().read(path, ""))
return file, os.path.basename(path), True
Comment on lines -73 to +75
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function exec_code_into_module.NotebookReader.NotebookReader.read._get_ipynb_file._get_file refactored with the following changes:


return _get_file

Expand Down
22 changes: 8 additions & 14 deletions src/robotkernel/selectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,10 @@ def get_selenium_needle_from_user(driver):
def get_selenium_css_selector_completions(needle, driver):
needle = needle[4:]
unresolved = []
results = []
matches = []
if not needle:
needle = get_selenium_needle_from_user(driver)
if needle:
results = driver.find_elements_by_css_selector(needle)
results = driver.find_elements_by_css_selector(needle) if needle else []
Comment on lines -399 to +402
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_selenium_css_selector_completions refactored with the following changes:

for result in visible_or_all(results):
id_ = result.get_attribute("id")
if " " in needle: # always include simmer result for complex needles
Expand All @@ -425,10 +423,8 @@ def get_selenium_css_selector_completions(needle, driver):
def get_selenium_tag_selector_completions(needle, driver):
needle = needle[4:]
unresolved = []
results = []
matches = []
if needle:
results = driver.find_elements_by_css_selector(needle)
results = driver.find_elements_by_css_selector(needle) if needle else []
Comment on lines -428 to +427
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_selenium_tag_selector_completions refactored with the following changes:

for result in visible_or_all(results):
id_ = result.get_attribute("id")
if id_:
Expand All @@ -446,24 +442,22 @@ def get_selenium_tag_selector_completions(needle, driver):

def get_selenium_link_selector_completions(needle, driver):
needle = needle[5:]
matches = []
if needle:
results = driver.find_elements_by_partial_link_text(needle)
else:
results = driver.find_elements_by_xpath("//a")
for result in visible_or_all(results):
if result.text:
matches.append((f"link:{result.text}", result))
return matches
return [
(f"link:{result.text}", result)
for result in visible_or_all(results)
if result.text
]
Comment on lines -449 to +453
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_selenium_link_selector_completions refactored with the following changes:



def get_selenium_xpath_selector_completions(needle, driver):
needle = needle[6:]
results = []
matches = []
unresolved = []
if needle:
results = driver.find_elements_by_xpath(needle)
results = driver.find_elements_by_xpath(needle) if needle else []
Comment on lines -462 to +460
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function get_selenium_xpath_selector_completions refactored with the following changes:

for result in visible_or_all(results):
id_ = result.get_attribute("id")
if id_:
Expand Down
9 changes: 4 additions & 5 deletions src/robotkernel/selectors_white.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ def take_screenshot(as_bytes=False):
screenshot = Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb)
graphics = Graphics.FromImage(screenshot)
graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size)
if as_bytes:
fp = MemoryStream()
screenshot.Save(fp, ImageFormat.Png)
return bytes(bytearray(fp.ToArray()))
else:
if not as_bytes:
return screenshot
fp = MemoryStream()
screenshot.Save(fp, ImageFormat.Png)
return bytes(bytearray(fp.ToArray()))
Comment on lines -47 to +51
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PickSnipTool.take_screenshot refactored with the following changes:


@staticmethod
def pick(snip=False):
Expand Down
44 changes: 20 additions & 24 deletions src/robotkernel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ def lunr_builder(ref, fields):

def readable_keyword(s):
"""Return keyword with only the first letter in title case."""
if s and not s.startswith("*") and not s.startswith("["):
if s.count("."):
library, name = s.rsplit(".", 1)
return library + "." + name[0].title() + name[1:].lower()
else:
return s[0].title() + s[1:].lower()
else:
if not s or s.startswith("*") or s.startswith("["):
return s
if not s.count("."):
return s[0].title() + s[1:].lower()
library, name = s.rsplit(".", 1)
return library + "." + name[0].title() + name[1:].lower()
Comment on lines -87 to +92
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function readable_keyword refactored with the following changes:



def detect_robot_context(code, cursor_pos):
Expand All @@ -101,18 +99,17 @@ def detect_robot_context(code, cursor_pos):
context_parts = code.rsplit("***", 2)
if len(context_parts) != 3:
return "__root__"
context_name = context_parts[1].strip().lower()
if context_name == "settings":
return "__settings__"
elif line.lstrip() == line:
return "__root__"
elif context_name in ["tasks", "test cases"]:
return "__tasks__"
elif context_name == "keywords":
return "__keywords__"
else:
context_name = context_parts[1].strip().lower()
if context_name == "settings":
return "__settings__"
elif line.lstrip() == line:
return "__root__"
elif context_name in ["tasks", "test cases"]:
return "__tasks__"
elif context_name == "keywords":
return "__keywords__"
else:
return "__root__"
return "__root__"
Comment on lines +102 to +112
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function detect_robot_context refactored with the following changes:



NAME_REGEXP = re.compile("`(.+?)`")
Expand Down Expand Up @@ -233,12 +230,11 @@ def to_mime_and_metadata(obj) -> (dict, dict): # noqa: C901
try:
if isinstance(obj, str):
return {"text/html": f"<pre>{to_html(obj)}</pre>".replace("\\n", "\n")}, {}
else:
data, metadata = JSON(data=obj, expanded=True)._repr_json_()
return (
{"application/json": data, "text/html": f"<pre>{to_html(obj)}</pre>"},
metadata,
)
data, metadata = JSON(data=obj, expanded=True)._repr_json_()
return (
{"application/json": data, "text/html": f"<pre>{to_html(obj)}</pre>"},
metadata,
)
Comment on lines -236 to +237
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function to_mime_and_metadata refactored with the following changes:

except (TypeError, JSONDecodeError):
pass
try:
Expand Down