Skip to content

Commit

Permalink
Add open function and re module to DSL (#917)
Browse files Browse the repository at this point in the history
* Add open function and re module to DSL
  • Loading branch information
chen3feng authored Mar 17, 2021
1 parent fd85dc7 commit a505c77
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion doc/en/dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ functions and keywords, include but not limited to:
- `exec`, `execfile` and `eval` To improve the consistency of BUILD files.
- `import` Use the built-in `blade` module instead
- `print` Use the functions in `blade.console` module instead
- `open`

Some builtin functions are restricted.
- `open` only read mode is allowed

To use some common additional functions, such as `os.path.join`, you need to use similar sub-modules in the `blade` module.
If you want to add more appropriate modules, please make an Issue.
Expand All @@ -26,6 +28,7 @@ The global Blade API module, accessed through `blade.`, includes:
- `current_target_dir()` function: The output directory where the current BUILD file is located corresponds to (relative to the root directory of the workspace)
- `config` submodule: Read blade configuration information
- `console` submodule: Output diagnostic information
- `re` submodule: The python regex library
- `path` submodule: a Restricted subset of `os.path`

### `blade.config` Submodule
Expand Down
6 changes: 5 additions & 1 deletion doc/zh_CN/dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
- `exec``execfile``eval` 提高构建文件的一致性。
- `import` 请用 `blade` 的子模块。
- `print` 请用 `blade.console` 里的函数代替。
- `open`

某些函数功能则有限制:

- `open` 只允许读模式访问

即使用 python 2 来运行 Blade,你也应当尽量采用反向移植的 python 3 的语法。

Expand All @@ -24,6 +27,7 @@
- `current_target_dir()` 函数:当前 BUILD 文件所在的目录对应于(相对于 workspace 的根目录)
- `config` 子模块:读取 blade 的配置信息
- `console` 子模块:输出诊断信息
- `re` 子模块:正则表达式
- `path` 子模块:`os.path` 的一个受限制的子集

### `blade.config` 模块
Expand Down
2 changes: 2 additions & 0 deletions src/blade/dsl_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import os
import re
import types

import blade
Expand Down Expand Up @@ -97,6 +98,7 @@ def _safe_blade_module():
module.current_target_dir = blade.current_target_dir
module.environ = os.environ
module.path = _safe_path_module()
module.re = re
module.util = _safe_util_module()
module.workspace = _safe_workspace_module()
return module
Expand Down
22 changes: 21 additions & 1 deletion src/blade/restricted.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
'exec',
'execfile',
'eval',
'open',
]


Expand All @@ -145,6 +144,24 @@ def wrapper(*args, **kwargs):
return wrapper


def _open(name, mode=None, buffering=None):
"""A Readonly open function"""
if mode is None and buffering is None:
return open(name)
if mode:
if 'w' in mode or 'a' in mode:
raise ValueError('"open" only allow readonly mode')
if buffering is None:
return open(name, mode)
return open(name, mode, buffering)


# Replace some functions to better help users know how to deal with.
_REPLACED_FUNCTIONS = {
'open': _open,
}


def _make_safe_buildins():
safe_builtins = {}

Expand All @@ -161,6 +178,9 @@ def _make_safe_buildins():
user_friend_name = name
safe_builtins[name] = _make_forbidden_wrapper(user_friend_name)

for name, func in _REPLACED_FUNCTIONS.items():
safe_builtins[name] = func

return safe_builtins


Expand Down

0 comments on commit a505c77

Please sign in to comment.