Skip to content

Commit

Permalink
fix commands
Browse files Browse the repository at this point in the history
  • Loading branch information
MrLYC committed Mar 24, 2021
1 parent fa46220 commit 30a0e82
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 60 deletions.
23 changes: 13 additions & 10 deletions paas2/esb/apps/sdk_management/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@

import os
import shutil
import string

from apps.sdk_management.constants import API_COMPONENT_TMPL, API_PY_TMPL, COLLECTIONS_PY_TMPL
from django.conf import settings
from jinja2 import Template

from apps.sdk_management.constants import COLLECTIONS_PY_TMPL, API_PY_TMPL, API_COMPONENT_TMPL


class SDKGenerator(object):
def __init__(self, channels, target_dir):
Expand All @@ -33,7 +31,7 @@ def generate_sdk_files(self):

def get_available_channels(self, channels):
new_channels = {}
for system_name, sub_channels in channels.iteritems():
for system_name, sub_channels in channels.items():
new_sub_channels = [
channel for channel in sub_channels if channel["suggest_method"] and not channel["no_sdk"]
]
Expand Down Expand Up @@ -101,15 +99,17 @@ def get_api_file_content(self, system_name, channels):
description=channel["component_label"].encode("utf-8"),
)
)

apis_str = "".join(apis)
return Template(API_PY_TMPL).render(
system_name_smart=self.smart_system_name(system_name),
system_name=system_name,
apis="".join(apis).decode("utf-8"),
apis=apis_str.decode("utf-8") if hasattr(apis_str, "decode") else apis_str,
)

def smart_system_name(self, system_name):
if "_" in system_name:
system_name = "".join(string.capitalize(word) for word in system_name.split("_"))
system_name = "".join(word.capitalize() for word in system_name.split("_"))
return system_name

def write_content_to_file(self, content, file_path):
Expand All @@ -134,10 +134,13 @@ def group_channels_by_api_ver(self, channels):
for path, channel in channels_v2.items():
if path in channels_v1:
if channels_v1[path]["suggest_method"] != channel["suggest_method"]:
print "channel method different: v1=%s, v2=%s, path=%s" % (
channels_v1[path]["suggest_method"],
channel["suggest_method"],
path,
print(
"channel method different: v1=%s, v2=%s, path=%s"
% (
channels_v1[path]["suggest_method"],
channel["suggest_method"],
path,
)
)
channels_v1_v2.append(channel)
channels_v1.pop(path)
Expand Down
16 changes: 10 additions & 6 deletions paas2/esb/common/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
specific language governing permissions and limitations under the License.
"""

import re
import json
import re

from django import forms
from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.encoding import force_unicode, smart_unicode
from django.forms import Field
from django.forms.utils import ErrorDict
from django.utils.encoding import force_text, smart_text

from common.base_utils import FancyDict, str_bool
from common.errors import CommonAPIError
Expand All @@ -31,7 +31,7 @@ def get_error_prompt(form):
content = []
fields = list(form.fields.keys())
for k, v in sorted(list(form.errors.items()), key=lambda x: fields.index(x[0]) if x[0] in fields else -1):
_msg = force_unicode(v[0])
_msg = force_text(v[0])
b_field = form._safe_get_field(k)
# Get the default error messages
messages = {}
Expand All @@ -43,7 +43,7 @@ def get_error_prompt(form):
content.append(u"%s [%s] %s" % (b_field.label, b_field.name, _msg))
else:
content.append(u"%s" % _msg)
return force_unicode(content[0])
return force_text(content[0])


class BaseComponentForm(forms.Form):
Expand Down Expand Up @@ -121,7 +121,11 @@ def get_cleaned_data_when_exist(self, keys=[]):
keys = keys or list(self.fields.keys())
if isinstance(keys, dict):
return dict(
[(key_dst, self.cleaned_data[key_src]) for key_src, key_dst in list(keys.items()) if key_src in self.data]
[
(key_dst, self.cleaned_data[key_src])
for key_src, key_dst in list(keys.items())
if key_src in self.data
]
)
else:
return dict([(key, self.cleaned_data[key]) for key in keys if key in self.data])
Expand Down Expand Up @@ -149,7 +153,7 @@ def to_python_unicode(self, value):
"Returns a Unicode object."
if value in validators.EMPTY_VALUES:
return ""
return smart_unicode(value)
return smart_text(value)

def to_python(self, value):
# 如果传入的数据类型本身就是list( 比如用json loads过来的数据结构来校验),直接返回
Expand Down
15 changes: 5 additions & 10 deletions paas2/esb/esb/management/commands/add_compperm_for_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,15 @@
"""
from __future__ import print_function

from optparse import make_option

from django.core.management.base import BaseCommand, CommandError

from esb.bkcore.models import ESBChannel, AppComponentPerm
from esb.bkcore.models import AppComponentPerm, ESBChannel


class Command(BaseCommand):

option_list = BaseCommand.option_list + (
make_option("--app_code", action="store", dest="app_code"),
make_option("--system_name", action="store", dest="system_name"),
make_option("--component_name", action="store", dest="component_name"),
)
def add_arguments(self, parser):
parser.add_argument("--app_code", action="store", dest="app_code")
parser.add_argument("--system_name", action="store", dest="system_name")
parser.add_argument("--component_name", action="store", dest="component_name")

def handle(self, *args, **options):
app_code = options["app_code"]
Expand Down
7 changes: 2 additions & 5 deletions paas2/esb/esb/management/commands/check_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from __future__ import print_function

import json
from optparse import make_option

from django.core.management.base import BaseCommand

Expand All @@ -22,10 +21,8 @@


class Command(BaseCommand):

option_list = BaseCommand.option_list + (
make_option("--service", action="store", dest="service", help="Service name"),
)
def add_arguments(self, parser):
parser.add_argument("--service", action="store", dest="service", help="Service name")

def handle(self, *args, **options):
self.check_job_ssl()
Expand Down
7 changes: 2 additions & 5 deletions paas2/esb/esb/management/commands/sync_api_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import json
import logging
from optparse import make_option

from django.core.management.base import BaseCommand
from esb.bkcore.models import ComponentAPIDoc, ESBChannel
Expand All @@ -26,10 +25,8 @@


class Command(BaseCommand):

option_list = BaseCommand.option_list + (
make_option("--all", action="store_true", dest="all", default=False, help="update all api docs"),
)
def add_arguments(self, parser):
parser.add_argument("--all", action="store_true", dest="all", default=False, help="update all api docs")

def handle(self, *args, **options):
self.update_api_docs(is_update_all_api_doc=options["all"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
specific language governing permissions and limitations under the License.
"""

from past.builtins import basestring
import json
import logging
from optparse import make_option

from common.constants import API_TYPE_Q
from django.core.management.base import BaseCommand
from esb.bkcore.models import ComponentSystem, ESBBuffetComponent, ESBChannel, SystemDocCategory
from esb.management.utils import conf_tools
from past.builtins import basestring

logger = logging.getLogger(__name__)

Expand All @@ -28,10 +27,8 @@


class Command(BaseCommand):

option_list = BaseCommand.option_list + (
make_option("--force", action="store_true", dest="force", help="Force data update to db"),
)
def add_arguments(self, parser):
parser.add_argument("--force", action="store_true", dest="force", help="Force data update to db")

def handle(self, *args, **options):
self.force = options["force"]
Expand Down
22 changes: 8 additions & 14 deletions paas2/esb/esb/management/commands/update_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,26 @@
specific language governing permissions and limitations under the License.
"""

from builtins import map
from builtins import object
import re
import os
import copy
import json
from optparse import make_option
import logging
import os
import re
from builtins import map, object

from common.base_utils import read_file
from django.conf import settings
from django.core.management.base import BaseCommand

from common.base_utils import read_file

import logging

logger = logging.getLogger(__name__)

BASE_DIR = settings.BASE_DIR


class Command(BaseCommand):

option_list = BaseCommand.option_list + (
make_option("--force", action="store_true", dest="force", help="Force update locale file"),
make_option("--parse", action="store_true", dest="parse", help="only parse and print translation info"),
)
def add_arguments(self, parser):
parser.add_argument("--force", action="store_true", dest="force", help="Force update locale file")
parser.add_argument("--parse", action="store_true", dest="parse", help="only parse and print translation info")

def handle(self, *args, **options):
self.force = options["force"]
Expand Down
6 changes: 3 additions & 3 deletions paas2/esb/esb/outgoing.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from common.log import logger, logger_api
from django.conf import settings
from django.utils import timezone
from django.utils.encoding import smart_str
from django.utils.encoding import smart_bytes
from past.builtins import basestring
from requests.exceptions import ReadTimeout, SSLError

Expand Down Expand Up @@ -409,7 +409,7 @@ def request(
params = json.dumps(params)
datetime_end = timezone.now()
msecs_cost = (datetime_end - datetime_start).total_seconds() * 1000
exception_name = smart_str(r.request_exception) if r.request_exception else None
exception_name = smart_bytes(r.request_exception) if r.request_exception else None

try:
api_log = {
Expand Down Expand Up @@ -553,7 +553,7 @@ def request(
request_params = str(request_params)
datetime_end = timezone.now()
msecs_cost = (datetime_end - datetime_start).total_seconds() * 1000
exception_name = smart_str(request_exception) if request_exception else None
exception_name = smart_bytes(request_exception) if request_exception else None

# Log to logstash, Use type="pyls-comp-api"
try:
Expand Down
7 changes: 6 additions & 1 deletion paas2/esb/esb/utils/func_ctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import json
import re
from builtins import object
from builtins import bytes, object

from cachetools import TTLCache, cached
from common.constants import CACHE_MAXSIZE, CacheTimeLevel, FunctionControllerCodeEnum
Expand Down Expand Up @@ -64,6 +64,11 @@ def get_jwt_key(cls):

@classmethod
def save_jwt_key(cls, private_key, public_key):
if isinstance(private_key, bytes):
private_key = private_key.decode("utf-8")

if isinstance(public_key, bytes):
public_key = public_key.decode("utf-8")

FunctionController.objects.get_or_create(
func_code=FunctionControllerCodeEnum.JWT_KEY.value,
Expand Down

0 comments on commit 30a0e82

Please sign in to comment.