Skip to content

Commit

Permalink
Optimized CodeGen
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelvanstraten committed Sep 14, 2023
1 parent 268cd94 commit f7aa8b5
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 94 deletions.
8 changes: 6 additions & 2 deletions Sources/CodeGen/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

from parse import process_json_files
from parsing_types.command import Command
from git_utils import make_sure_remote_repo_is_downloaded
from utils import clean_out_directory, get_todays_date, THIS_DIR
from utils import (
make_sure_remote_repo_is_downloaded,
clean_out_directory,
get_todays_date,
THIS_DIR,
)
from swift_format import format_files
from templates import render

Expand Down
31 changes: 0 additions & 31 deletions Sources/CodeGen/git_utils.py

This file was deleted.

4 changes: 1 addition & 3 deletions Sources/CodeGen/parsing_types/argument.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os.path
from collections import Counter
from typing import List, Dict
from jinja2 import Template

from templates import render
from utils import camel_case, snake_case, THIS_DIR
from utils import camel_case, snake_case
from config import names_to_substitute, token_to_substitute, arguments_to_ignore

ARG_TYPES = {
Expand Down
9 changes: 6 additions & 3 deletions Sources/CodeGen/parsing_types/complex_argument.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from utils import pascal_case
from parsing_types.argument import Argument


class ComplexArgument(Argument):
def __init__(self, parent_name, desc, is_sub_arg = False):
def __init__(self, parent_name, desc, is_sub_arg=False):
super().__init__(desc)
self.parent_name: str = parent_name
self.type: str = pascal_case(self.name) if is_sub_arg else pascal_case(self.fullname())
self.type: str = (
pascal_case(self.name) if is_sub_arg else pascal_case(self.fullname())
)

def fullname(self):
return f"{self.parent_name} {self.name}"
return f"{self.parent_name} {self.name}"
2 changes: 1 addition & 1 deletion Sources/CodeGen/pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["**/__pycache__"],
"exclude": ["**/__pycache__", "redis", "swift-format"],
"venv": "swifty-redis-codegen-YKd2kwY0-py3.9",
"venvPath": "/Users/michaelvanstraten/Library/Caches/pypoetry/virtualenvs"
}
4 changes: 2 additions & 2 deletions Sources/CodeGen/swift_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from os.path import exists

from git_utils import make_sure_remote_repo_is_downloaded
from utils import make_sure_remote_repo_is_downloaded
from config import formating_config


Expand Down Expand Up @@ -32,7 +32,7 @@ def make_sure_swift_format_is_compiled():
compile_swift_format()


def compile_swift_format(*files: str):
def compile_swift_format():
print("Compiling swift-format...")
try:
subprocess.check_call(
Expand Down
84 changes: 49 additions & 35 deletions Sources/CodeGen/utils.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,78 @@
import glob
import os
from re import sub
import glob
import re
from datetime import date
import click
from git.repo import Repo
from git import RemoteProgress
from tqdm import tqdm

# Get the current directory of the script
THIS_DIR = os.path.dirname(os.path.abspath(__file__))


class ProgressHandler(RemoteProgress):
def __init__(self, desc):
super().__init__()
self.pbar = tqdm()
self.pbar.set_description_str(desc)

def update(self, _, cur_count, max_count=None):
self.pbar.total = max_count
self.pbar.n = cur_count
self.pbar.refresh()


def make_sure_remote_repo_is_downloaded(
name: str, remote_url: str, branch: str = "main"
) -> Repo:
repo_path = os.path.join(THIS_DIR, name)
repo = Repo.init(repo_path, mkdir=True)
try:
remote = repo.remote(name)
except:
remote = repo.create_remote(name, remote_url)
remote.pull(
branch, progress=ProgressHandler(desc=f"Downloading {name} ({remote_url})")
)
return repo


def get_optional_desc_string(desc, field, force_uppercase=False):
v = desc.get(field, None)
if v and force_uppercase:
v = v.upper()
ret = '"%s"' % v if v else "NULL"
return ret.replace("\n", "\\n")
value = desc.get(field, None)
if value and force_uppercase:
value = value.upper()
return f'"{value}"' if value else "NULL"


def camel_case(s):
s = pascal_case(s)
return "".join([s[0].lower(), s[1:]])
return pascal_case(s)[0].lower() + pascal_case(s)[1:]


def pascal_case(s):
return sub("[\_\-\/\.\:]", " ", s).title().replace(" ", "")
s = re.sub(r"[\_\-\/\.\:]", " ", s)
return "".join(s.title().split())


# https://www.w3resource.com/python-exercises/string/python-data-type-string-exercise-97.php
def snake_case(s):
return "_".join(
sub(
"([A-Z][a-z]+)",
r" \1",
sub(
"([A-Z]+)",
r" \1",
s.replace("-", " ").replace("_", " ").replace(":", " "),
),
).split()
).lower()
s = re.sub(r"[\_\-\/\.\:]", " ", s)
return "_".join(s.split()).lower()


def kebab_case(s):
return "-".join(
sub(
"([A-Z][a-z]+)",
r" \1",
sub("([A-Z]+)", r" \1", s.replace("-", " ").replace("_", " ")),
).split()
).lower()
s = re.sub(r"[\_\-\/\.\:]", " ", s)
return "-".join(s.split()).lower()


def sanitize(s):
return s.replace("-", "_").replace(":", "")


def clean_out_directory(out_dir):
print("Cleaning out directory...")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
else:
for f in glob.glob(f"{out_dir}/*"):
os.remove(f)
click.echo("Cleaning out directory...")
os.makedirs(out_dir, exist_ok=True)
for file_path in glob.glob(os.path.join(out_dir, "*")):
os.remove(file_path)


def get_todays_date():
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/acl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// acl.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// client.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/cluster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// cluster.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// command.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// config.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/containerless.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// containerless.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// function.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/latency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// latency.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/memory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// memory.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// module.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// object.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/pubsub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// pubsub.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/script.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// script.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/sentinel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// sentinel.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/slowlog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// slowlog.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/xgroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// xgroup.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftyRedis/CodeGen/Commands/xinfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// xinfo.swift
//
//
// Created by CodeGen on 08.09.23.
// Created by CodeGen on 14.09.23.
//
import Foundation
extension RedisConnection {
Expand Down

0 comments on commit f7aa8b5

Please sign in to comment.