Skip to content
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

Allow @ symbol in filename root_path for Kqlmagic initialization #101

Merged
merged 1 commit into from
Apr 8, 2024

Conversation

neelaryan
Copy link

Pull Request Description

This PR fixes the bug reported in Issue #100


Future Release Comment

Before this fix, Kqlmagic initialization failed in restricted environments where new folder creation at user root (/home/user@domain) level is not allowed. The reason for this fail was, while trying to create Kqlmagic initialization folder, the root_path variable omitted @ symbol from path, which resulted in a non-existent path that needed to be created. Since the @ symbol often only existed in the user's home directory, it was not possible to create a new folder at user root level, leading to PermissionError and failure to initialize Kqlmagic.

Example:
If the following Kqlmagic initialization folder needs to be created: kqlmagic/temp_files/<kernel-id> at the root_path /home/user@domain then the expected outcome should be a new folder /home/user@domain/kqlmagic/temp_files/<kernel-id>. However, the function

showfiles_folder_Full_name = adjust_path(f"{root_path}/{folder_name}") # dont remove spaces from root directory

would return the following file path /home/userdomain/kqlmagic/temp_files/<kernel-id> to be created.

Tracing this adjust_path function call -->

def adjust_path(_path:str)->str:
path = adjust_path_to_uri(_path)
path = os.path.normpath(path)
return path

def adjust_path_to_uri(_path:str)->str:
path_obj = convert_to_common_path_obj(_path)
return path_obj.get("prefix") + path_obj.get("path")

def convert_to_common_path_obj(_path:str)->Dict[str,str]:
prefix = ""
path = _path.replace("\\", "/")
if path.startswith("file:"):
path = path[5:]
if path.startswith("///"):
path = path[3:]
elif path.startswith("//"):
pass
elif path.startswith("/"):
path = path[1:]
parts = path.split(":")
if len(parts) > 1:
prefix = parts[0] + ":"
path = ":".join(parts[1:])
if path.startswith("//"):
prefix += "//"
path = path[2:]
elif path.startswith("//"):
prefix = "//"
path = path[2:]
parts = path.split("/")
# parts = [get_valid_name(part) for part in parts] if not allow_spaces else [get_valid_filename_with_spaces(part) for part in parts]
parts = [get_valid_filename_with_spaces(part) for part in parts]
path = "/".join(parts)
return {"prefix": prefix, "path": path}

def get_valid_filename_with_spaces(name:str)->str:
"""
Remove leading and trailing spaces; convert other spaces to
underscores; and remove anything that is not an alphanumeric, dash,
underscore, or dot.
"""
# name = str(name).strip().replace(' ', '_')
name = str(name).strip()
return re.sub(r'(?u)[^-\w. ]', '', name)

we arrive at the Regex substitution line, where it removes the @ symbol as well. The original intent derived from comments is to

  • remove leading and trailing spaces
  • convert other spaces to underscores
  • remove anything that is not an alphanumeric, dash, underscore, or dot.

The patch modifies the Regex substitution to include the @ symbol and thereby solving the reported bug.

Breaking Changes:

  • None

Features:

  • None

Fixes:

  • Updated azure/Kqlmagic/my_utils.py

@mbnshtck mbnshtck merged commit ed0cac5 into microsoft:master Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants