Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
coco
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
coco
Commits
2b177eda
Commit
2b177eda
authored
Jan 09, 2019
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] 修改coco的conf
parent
60545fcd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
382 additions
and
1 deletion
+382
-1
.gitignore
.gitignore
+0
-1
conf.py
coco/conf.py
+382
-0
No files found.
.gitignore
View file @
2b177eda
...
...
@@ -5,7 +5,6 @@ env/
.access_key
*.log
logs/*
conf.py
host_rsa_key
sessions/*
coco.pid
...
...
coco/conf.py
0 → 100644
View file @
2b177eda
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
"""
coco.config
~~~~~~~~~~~~
the configuration related objects.
copy from flask
:copyright: (c) 2015 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
import
os
import
sys
import
types
import
errno
import
json
import
socket
import
yaml
from
werkzeug.utils
import
import_string
BASE_DIR
=
os
.
path
.
dirname
(
os
.
path
.
dirname
(
__file__
))
root_path
=
os
.
environ
.
get
(
"COCO_PATH"
)
if
not
root_path
:
root_path
=
BASE_DIR
class
ConfigAttribute
(
object
):
"""Makes an attribute forward to the config"""
def
__init__
(
self
,
name
,
get_converter
=
None
):
self
.
__name__
=
name
self
.
get_converter
=
get_converter
def
__get__
(
self
,
obj
,
type
=
None
):
if
obj
is
None
:
return
self
rv
=
obj
.
config
[
self
.
__name__
]
if
self
.
get_converter
is
not
None
:
rv
=
self
.
get_converter
(
rv
)
return
rv
def
__set__
(
self
,
obj
,
value
):
obj
.
config
[
self
.
__name__
]
=
value
class
Config
(
dict
):
"""Works exactly like a dict but provides ways to fill it from files
or special dictionaries. There are two common patterns to populate the
config.
Either you can fill the config from a config file::
app.config.from_pyfile('yourconfig.cfg')
Or alternatively you can define the configuration options in the
module that calls :meth:`from_object` or provide an import path to
a module that should be loaded. It is also possible to tell it to
use the same module and with that provide the configuration values
just before the call::
DEBUG = True
SECRET_KEY = 'development key'
app.config.from_object(__name__)
In both cases (loading from any Python file or loading from modules),
only uppercase keys are added to the config. This makes it possible to use
lowercase values in the config file for temporary values that are not added
to the config or to define the config keys in the same file that implements
the application.
Probably the most interesting way to load configurations is from an
environment variable pointing to a file::
app.config.from_envvar('YOURAPPLICATION_SETTINGS')
In this case before launching the application you have to set this
environment variable to the file you want to use. On Linux and OS X
use the export statement::
export YOURAPPLICATION_SETTINGS='/path/to/config/file'
On windows use `set` instead.
:param root_path: path to which files are read relative from. When the
config object is created by the application, this is
the application's :attr:`~flask.Flask.root_path`.
:param defaults: an optional dictionary of default values
"""
def
__init__
(
self
,
root_path
,
defaults
=
None
):
self
.
defaults
=
defaults
or
{}
self
.
root_path
=
root_path
super
(
Config
,
self
)
.
__init__
({})
def
from_envvar
(
self
,
variable_name
,
silent
=
False
):
"""Loads a configuration from an environment variable pointing to
a configuration file. This is basically just a shortcut with nicer
error messages for this line of code::
app.config.from_pyfile(os.environ['YOURAPPLICATION_SETTINGS'])
:param variable_name: name of the environment variable
:param silent: set to ``True`` if you want silent failure for missing
files.
:return: bool. ``True`` if able to load config, ``False`` otherwise.
"""
rv
=
os
.
environ
.
get
(
variable_name
)
if
not
rv
:
if
silent
:
return
False
raise
RuntimeError
(
'The environment variable
%
r is not set '
'and as such configuration could not be '
'loaded. Set this variable and make it '
'point to a configuration file'
%
variable_name
)
return
self
.
from_pyfile
(
rv
,
silent
=
silent
)
def
from_pyfile
(
self
,
filename
,
silent
=
False
):
"""Updates the values in the config from a Python file. This function
behaves as if the file was imported as module with the
:meth:`from_object` function.
:param filename: the filename of the config. This can either be an
absolute filename or a filename relative to the
root path.
:param silent: set to ``True`` if you want silent failure for missing
files.
.. versionadded:: 0.7
`silent` parameter.
"""
filename
=
os
.
path
.
join
(
self
.
root_path
,
filename
)
d
=
types
.
ModuleType
(
'config'
)
d
.
__file__
=
filename
try
:
with
open
(
filename
,
mode
=
'rb'
)
as
config_file
:
exec
(
compile
(
config_file
.
read
(),
filename
,
'exec'
),
d
.
__dict__
)
except
IOError
as
e
:
if
silent
and
e
.
errno
in
(
errno
.
ENOENT
,
errno
.
EISDIR
):
return
False
e
.
strerror
=
'Unable to load configuration file (
%
s)'
%
e
.
strerror
raise
self
.
from_object
(
d
)
return
True
def
from_object
(
self
,
obj
):
"""Updates the values from the given object. An object can be of one
of the following two types:
- a string: in this case the object with that name will be imported
- an actual object reference: that object is used directly
Objects are usually either modules or classes. :meth:`from_object`
loads only the uppercase attributes of the module/class. A ``dict``
object will not work with :meth:`from_object` because the keys of a
``dict`` are not attributes of the ``dict`` class.
Example of module-based configuration::
app.config.from_object('yourapplication.default_config')
from yourapplication import default_config
app.config.from_object(default_config)
You should not use this function to load the actual configuration but
rather configuration defaults. The actual config should be loaded
with :meth:`from_pyfile` and ideally from a location not within the
package because the package might be installed system wide.
See :ref:`config-dev-prod` for an example of class-based configuration
using :meth:`from_object`.
:param obj: an import name or object
"""
if
isinstance
(
obj
,
str
):
obj
=
import_string
(
obj
)
for
key
in
dir
(
obj
):
if
key
.
isupper
():
self
[
key
]
=
getattr
(
obj
,
key
)
def
from_json
(
self
,
filename
,
silent
=
False
):
"""Updates the values in the config from a JSON file. This function
behaves as if the JSON object was a dictionary and passed to the
:meth:`from_mapping` function.
:param filename: the filename of the JSON file. This can either be an
absolute filename or a filename relative to the
root path.
:param silent: set to ``True`` if you want silent failure for missing
files.
.. versionadded:: 0.11
"""
filename
=
os
.
path
.
join
(
self
.
root_path
,
filename
)
try
:
with
open
(
filename
)
as
json_file
:
obj
=
json
.
loads
(
json_file
.
read
())
except
IOError
as
e
:
if
silent
and
e
.
errno
in
(
errno
.
ENOENT
,
errno
.
EISDIR
):
return
False
e
.
strerror
=
'Unable to load configuration file (
%
s)'
%
e
.
strerror
raise
return
self
.
from_mapping
(
obj
)
def
from_yaml
(
self
,
filename
,
silent
=
False
):
if
self
.
root_path
:
filename
=
os
.
path
.
join
(
self
.
root_path
,
filename
)
try
:
with
open
(
filename
)
as
json_file
:
obj
=
yaml
.
load
(
json_file
)
except
IOError
as
e
:
if
silent
and
e
.
errno
in
(
errno
.
ENOENT
,
errno
.
EISDIR
):
return
False
e
.
strerror
=
'Unable to load configuration file (
%
s)'
%
e
.
strerror
raise
return
self
.
from_mapping
(
obj
)
def
from_mapping
(
self
,
*
mapping
,
**
kwargs
):
"""Updates the config like :meth:`update` ignoring items with non-upper
keys.
.. versionadded:: 0.11
"""
mappings
=
[]
if
len
(
mapping
)
==
1
:
if
hasattr
(
mapping
[
0
],
'items'
):
mappings
.
append
(
mapping
[
0
]
.
items
())
else
:
mappings
.
append
(
mapping
[
0
])
elif
len
(
mapping
)
>
1
:
raise
TypeError
(
'expected at most 1 positional argument, got
%
d'
%
len
(
mapping
)
)
mappings
.
append
(
kwargs
.
items
())
for
mapping
in
mappings
:
for
(
key
,
value
)
in
mapping
:
if
key
.
isupper
():
self
[
key
]
=
value
return
True
def
get_namespace
(
self
,
namespace
,
lowercase
=
True
,
trim_namespace
=
True
):
"""Returns a dictionary containing a subset of configuration options
that match the specified namespace/prefix. Example usage::
app.config['IMAGE_STORE_TYPE'] = 'fs'
app.config['IMAGE_STORE_PATH'] = '/var/app/images'
app.config['IMAGE_STORE_BASE_URL'] = 'http://img.website.com'
image_store_config = app.config.get_namespace('IMAGE_STORE_')
The resulting dictionary `image_store_config` would look like::
{
'types': 'fs',
'path': '/var/app/images',
'base_url': 'http://img.website.com'
}
This is often useful when configuration options map directly to
keyword arguments in functions or class constructors.
:param namespace: a configuration namespace
:param lowercase: a flag indicating if the keys of the resulting
dictionary should be lowercase
:param trim_namespace: a flag indicating if the keys of the resulting
dictionary should not include the namespace
.. versionadded:: 0.11
"""
rv
=
{}
for
k
,
v
in
self
.
items
():
if
not
k
.
startswith
(
namespace
):
continue
if
trim_namespace
:
key
=
k
[
len
(
namespace
):]
else
:
key
=
k
if
lowercase
:
key
=
key
.
lower
()
rv
[
key
]
=
v
return
rv
def
__getitem__
(
self
,
item
):
try
:
value
=
super
(
Config
,
self
)
.
__getitem__
(
item
)
except
KeyError
:
value
=
None
if
value
is
not
None
:
return
value
value
=
os
.
environ
.
get
(
item
,
None
)
if
value
is
not
None
:
return
value
return
self
.
defaults
.
get
(
item
)
def
__getattr__
(
self
,
item
):
return
self
.
__getitem__
(
item
)
def
__repr__
(
self
):
return
'<
%
s
%
s>'
%
(
self
.
__class__
.
__name__
,
dict
.
__repr__
(
self
))
access_key_path
=
os
.
path
.
abspath
(
os
.
path
.
join
(
root_path
,
'keys'
,
'.access_key'
))
defaults
=
{
'NAME'
:
socket
.
gethostname
(),
'CORE_HOST'
:
'http://127.0.0.1:8080'
,
'BOOTSTRAP_TOKEN'
:
os
.
environ
.
get
(
"BOOTSTRAP_TOKEN"
)
or
'PleaseChangeMe'
,
'ROOT_PATH'
:
root_path
,
'DEBUG'
:
True
,
'BIND_HOST'
:
'0.0.0.0'
,
'SSHD_PORT'
:
2222
,
'HTTPD_PORT'
:
5000
,
'COCO_ACCESS_KEY'
:
''
,
'ACCESS_KEY_FILE'
:
access_key_path
,
'SECRET_KEY'
:
'SDK29K03
%
MM0ksf'
,
'LOG_LEVEL'
:
'DEBUG'
,
'LOG_DIR'
:
os
.
path
.
join
(
root_path
,
'logs'
),
'SESSION_DIR'
:
os
.
path
.
join
(
root_path
,
'sessions'
),
'ASSET_LIST_SORT_BY'
:
'hostname'
,
# hostname, ip
'PASSWORD_AUTH'
:
True
,
'PUBLIC_KEY_AUTH'
:
True
,
'SSH_TIMEOUT'
:
10
,
'ALLOW_SSH_USER'
:
[],
'BLOCK_SSH_USER'
:
[],
'HEARTBEAT_INTERVAL'
:
5
,
'MAX_CONNECTIONS'
:
500
,
# Not use now
'ADMINS'
:
''
,
'COMMAND_STORAGE'
:
{
'TYPE'
:
'server'
},
# server
'REPLAY_STORAGE'
:
{
'TYPE'
:
'server'
},
'LANGUAGE_CODE'
:
'zh'
,
'SECURITY_MAX_IDLE_TIME'
:
60
,
'ASSET_LIST_PAGE_SIZE'
:
'auto'
,
'REDIS_HOST'
:
'127.0.0.1'
,
'REDIS_PORT'
:
6379
,
'REDIS_PASSWORD'
:
''
,
'REDIS_DB'
:
8
,
}
def
load_from_object
(
config
):
try
:
from
conf
import
config
as
c
config
.
from_object
(
c
)
return
True
except
ImportError
:
pass
return
False
def
load_from_yml
(
config
):
for
i
in
[
'config.yml'
,
'config.yaml'
]:
if
not
os
.
path
.
isfile
(
os
.
path
.
join
(
config
.
root_path
,
i
)):
continue
loaded
=
config
.
from_yaml
(
i
)
if
loaded
:
return
True
return
False
def
load_user_config
():
sys
.
path
.
insert
(
0
,
root_path
)
config
=
Config
(
root_path
,
defaults
)
loaded
=
load_from_object
(
config
)
if
not
loaded
:
loaded
=
load_from_yml
(
config
)
if
not
loaded
:
msg
=
"""
Error: No config file found.
You can run `cp config_example.yml config.yml`, and edit it.
"""
raise
ImportError
(
msg
)
return
config
config
=
load_user_config
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment