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

style: shorter generated py enums names #6496

Merged
merged 4 commits into from
Apr 10, 2023
Merged

style: shorter generated py enums names #6496

merged 4 commits into from
Apr 10, 2023

Conversation

hamidzr
Copy link
Contributor

@hamidzr hamidzr commented Apr 6, 2023

we'd ideally address this at proto level
but this is a quick move in that direction

Description

Test Plan

Commentary (optional)

these are getting out of hand https://github.com/determined-ai/determined-ee/blob/ec954a08b715ad85e9ca5abbb134f31500287ec6/e2e_tests/tests/cluster/test_rbac_misc.py#L105-L108

used the following to strip the prefixes

import argparse
import json
import os
import re

def find_enum_keys(json_obj):
    enum_keys = []
    if isinstance(json_obj, dict):
        for key, value in json_obj.items():
            if key == "enum" and isinstance(value, list):
                enum_keys.append(value)
            else:
                enum_keys.extend(find_enum_keys(value))
    elif isinstance(json_obj, list):
        for item in json_obj:
            enum_keys.extend(find_enum_keys(item))
    return enum_keys

def find_common_prefix(strs):
    if not strs:
        return ""
    prefix = strs[0]
    for s in strs[1:]:
        i = 0
        while i < len(prefix) and i < len(s) and prefix[i] == s[i]:
            i += 1
        prefix = prefix[:i]
    return prefix

def remove_prefix_in_file(file_path, prefix):
    with open(file_path, "r") as f:
        contents = f.read()
    regex = r"(?<=\.)" + re.escape(prefix)
    contents = re.sub(regex, "", contents)
    with open(file_path, "w") as f:
        f.write(contents)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("spec_file", help="Path to OpenAPI spec file")
    args = parser.parse_args()

    with open(args.spec_file, "r") as f:
        openapi_spec = json.load(f)

    enum_keys = find_enum_keys(openapi_spec)
    prefixes = set()
    for key in enum_keys:
        common_prefix = find_common_prefix(key)
        prefixes.add(common_prefix)
    for prefix in prefixes:
        for dirpath, dirnames, filenames in os.walk("."):
            for filename in filenames:
                if filename.endswith(".py"):
                    file_path = os.path.join(dirpath, filename)
                    remove_prefix_in_file(file_path, prefix)

Checklist

  • Changes have been manually QA'd
  • User-facing API changes need the "User-facing API Change" label.
  • Release notes should be added as a separate file under docs/release-notes/.
    See Release Note for details.
  • Licenses should be included for new code which was copied and/or modified from any external code.

Ticket

@cla-bot cla-bot bot added the cla-signed label Apr 6, 2023
@netlify
Copy link

netlify bot commented Apr 6, 2023

Deploy Preview for determined-ui ready!

Name Link
🔨 Latest commit d397c39
🔍 Latest deploy log https://app.netlify.com/sites/determined-ui/deploys/64342967cfface0008f55155
😎 Deploy Preview https://deploy-preview-6496--determined-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Member

@rb-determined-ai rb-determined-ai left a comment

Choose a reason for hiding this comment

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

  • affects only internal APIs, so changes don't affect users
  • is better

Approved!

Make the relevant changes in the python code, of course.

bindings/generate_bindings_py.py Outdated Show resolved Hide resolved
@hamidzr
Copy link
Contributor Author

hamidzr commented Apr 7, 2023

I'll update and land after the release is out. this can make potential release cherry picks harder than necessary

@hamidzr hamidzr self-assigned this Apr 7, 2023
@hamidzr hamidzr changed the title feat: shorter generated py enums names style: shorter generated py enums names Apr 7, 2023
we'd probably want to address this at proto level
but this is a quick move in that direction
@hamidzr hamidzr marked this pull request as ready for review April 10, 2023 15:21
@hamidzr hamidzr enabled auto-merge (squash) April 10, 2023 15:34
@hamidzr hamidzr merged commit 588306f into master Apr 10, 2023
@hamidzr hamidzr deleted the shorter-enums branch April 10, 2023 15:54
jgongd pushed a commit to jgongd/determined that referenced this pull request Apr 18, 2023
@tayritenour tayritenour mentioned this pull request Apr 28, 2023
4 tasks
@dannysauer dannysauer added this to the 0.21.2 milestone Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants