Skip to content

Commit

Permalink
update cup to 3.2.24
Browse files Browse the repository at this point in the history
enhance 3.2.24
  • Loading branch information
mythmgn committed Aug 25, 2020
1 parent 0999f02 commit a761766
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 28 deletions.
66 changes: 66 additions & 0 deletions build_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*
# Copyright: [CUP] - See LICENSE for details.
# Authors: Guannan Ma (@mythmgn),
"""
:description:
decorators related module
"""
import os
import sys
import argparse

_NOW_PATH = os.path.dirname(os.path.abspath(__file__)) + '/'
_TOP_PATH = os.path.abspath(_NOW_PATH + '/../')

# sys.path.insert(0, _NOW_PATH)
sys.path.insert(0, _TOP_PATH)

from cup import version
from cup.shell import oper


class DocGenerator(object):
"""
doc generator for cup
"""
def __init__(self, opts):
"""
doc generator by invoking sphinx
"""
self._kv_opts = opts

def build_rst(self, github=True):
"""build rst for cup"""
exclude = ''
if github:
exclude = ' cup/thirdp cup/bidu '
else:
exclude = ' cup/thirdp'
cmd = 'sphinx-apidoc {0}'
for key in self._kv_opts:
cmd = '{0} {1} {2}'.format(cmd, key, self._kv_opts[key])
print cmd
shell = oper.ShellExec()
shell.run(cmd, timeout=600)


if __name__ == '__main__':
kvs = {
'-F': ' ',
'-o': '{0}/cup'.format(_NOW_PATH),
'--doc-version': version.VERSION,
'--doc-author': version.AUTHOR,
'--ext-todo': ' ',
'--module-first': ' ',
}
gen = DocGenerator(kvs)
gen.build_rst()

helpinfo = "generate cup html docs"
parser = argparse.ArgumentParser(description=helpinfo)
helpinfo = "conf file of sphinx"
parser.add_argument('-c', '--conf', type=str, help=helpinfo)
parser.parse_args()

# vi:set tw=0 ts=4 sw=4 nowrap fdm=indent
8 changes: 4 additions & 4 deletions cup/services/threadpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def add_1job(self, func, *args, **kwargs):
:param func:
function that will be scheduled by the thread pool
:param \*args:
:param args:
args that the [func] needs
:param \*\*kw:
:param kw:
kwargs that [func] needs
"""
self.add_1job_with_callback(None, func, *args, **kwargs)
Expand All @@ -185,10 +185,10 @@ def add_1job_with_callback(self, result_callback, func, *args, **kwargs):
:param func:
same to func for add_1job
:param \*args:
:param args:
args for [func]
:param \*\*kwargs:
:param kwargs:
kwargs for [func]
"""
if self._joined:
Expand Down
80 changes: 57 additions & 23 deletions cup/util/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import time
import copy
import json
import codecs
import shutil
import warnings
import functools
from xml.dom import minidom

Expand All @@ -25,6 +27,13 @@
]


# pylint: disable=R0913
# to be compatible with py3 open
def _open_codecs(filename, mode='r', encoding=None):
"""open_codecs for py2 in order to support encoding"""
return codecs.open(filename=filename, mode=mode, encoding=encoding)


class CConf(object):
"""
Depreciated class. Please do not use it. Use python configparser instead.
Expand Down Expand Up @@ -658,15 +667,17 @@ def _handle_group_keys(


# pylint: disable=R0912, R0915
def get_dict(self, ignore_error=False):
def get_dict(self, ignore_error=False, encoding='utf8'):
"""
get conf_dict which you can use to access conf info.
:param ignore_error:
True, CUP will parse the conf file without catching exceptions
:param encoding:
utf8 by default, you can change it to your encoding
"""
comments = []
self._get_input_lines(ignore_error)
self._get_input_lines(ignore_error, encoding)
conf_layer_stack = [self._dict]
num = 0
length = len(self._lines)
Expand Down Expand Up @@ -816,16 +827,21 @@ def _handle_include_syntx(self, line, ignore_error):
)

# Read in the file content, with format check
def _get_input_lines(self, ignore_error): # pylint: disable=R0912,R0915
# pylint: disable=R0912,R0915
def _get_input_lines(self, ignore_error, encoding):
"""
read conf lines
"""
fhandle = None
try:
fhanle = open(self._file, 'r')
if platforms.is_py2():
fhandle = _open_codecs(self._file, 'r', encoding=encoding)
else:
fhandle = open(self._file, 'r', encoding=encoding)
except IOError as error:
cup.log.error('open file failed:%s, err:%s' % (self._file, error))
raise IOError(str(error))
for line in fhanle.readlines():
raise IOError(error)
for line in fhandle.readlines():
line = line.strip()
# if it's a blank line or a line with comments only
if line == '':
Expand Down Expand Up @@ -902,7 +918,7 @@ def _get_input_lines(self, ignore_error): # pylint: disable=R0912,R0915
raise ValueFormatError(line)
value = tmp_value
self._lines.append((key, value))
fhanle.close()
fhandle.close()


class Dict2Configure(object):
Expand All @@ -919,6 +935,7 @@ def __init__(self, conf_dict, separator=':'):
self._level = 0
self._str = ''
self._separator = separator
self._encoding = 'utf8'

def _get_field_value_sep(self):
return self._separator
Expand Down Expand Up @@ -948,12 +965,22 @@ def _get_write_string(self):
self._get_confstring(self._dict)
return self._str

def write_conf(self, conf_file):
def write_conf(self, conf_file, encoding='utf8'):
"""
write the conf into of the dict into a conf_file
:param conf_file:
the file which will be override
:param encoding:
'utf8' by default, specify yours if needed
"""
with open(conf_file, 'w') as fhandle:
fhandle.write(self._get_write_string())
fhandle = None
if platforms.is_py2():
fhandle = _open_codecs(conf_file, 'w', encoding=encoding)
else:
fhandle = open(conf_file, 'w', encoding=encoding)
fhandle.write(self._get_write_string())
fhandle.close()

# pylint: disable=R0911
@classmethod
Expand Down Expand Up @@ -1057,7 +1084,8 @@ def _get_confstring(self, _dict):
for comment in item[1]:
self._str += self._get_indents() + comment
self._appendline(
self._get_arrayflag() + str(key), item[0]
'{0}{1}'.format(self._get_arrayflag(), key),
item[0]
)
elif isinstance(value, dict):
self._addlevel(key)
Expand All @@ -1071,22 +1099,22 @@ def _get_confstring_ex(self, _dict):
pass

def _appendline(self, key, value):
self._str += (
self._get_indents() + str(key) +
self._get_field_value_sep()+str(value)+self._get_linesep()
self._str = '{0}{1}{2}{3}{4}{5}'.format(
self._str, self._get_indents(), key, self._get_field_value_sep(),
self._encoding, self._get_linesep()
)

def _addlevel(self, key):
self._str += (
self._get_indents() + '[' + self._get_levelsep() + str(key) + ']'
+ self._get_linesep()
self._str = '{0}{1}[{2}{3}]{4}'.format(
self._str, self._get_indents(), self._get_levelsep(), key,
self._get_linesep()
)
self._level += 1

def _add_arraylevel(self, key):
self._str += (
self._get_indents() + '[' + self._get_arraylevel_sep() +
str(key) + ']' + self._get_linesep()
self._str = '{0}{1}[{2}{3}]{4}'.format(
self._str, self._get_indents(), self._get_arraylevel_sep(),
key, self._get_linesep()
)
self._level += 1

Expand Down Expand Up @@ -1252,7 +1280,7 @@ def _write_to_conf(self, new_confdict):
configuration_node.appendChild(new_pro)
return dom.toprettyxml(newl='\n')

def write_conf(self, kvs):
def write_conf(self, kvs, encoding='utf8'):
"""
update config items with a dict kvs. Refer to the example above.
Expand All @@ -1264,10 +1292,16 @@ def write_conf(self, kvs):
......
}
"""
self._encoding = encoding
self._load_items()
str_xml = self._write_to_conf(kvs)
with open(self._xmlpath, 'w') as fhandle:
fhandle.write(str_xml)
fhandle = None
if platforms.is_py2():
fhandle = _open_codecs(self._xmlpath, 'w', encoding=encoding)
else:
fhandle = open(self._xmlpath, 'w', encoding=encoding)
fhandle.write(str_xml)
fhandle.close()
self._confdict = kvs


Expand Down
2 changes: 1 addition & 1 deletion cup/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
]


VERSION = '3.2.22'
VERSION = '3.2.24'
AUTHOR = 'CUP-DEV Team'

# vi:set tw=0 ts=4 sw=4 nowrap fdm=indent

0 comments on commit a761766

Please sign in to comment.