Commit 751a92ae authored by Raphael Defosseux's avatar Raphael Defosseux

Formatting to PEP8 new python files

Signed-off-by: default avatarRaphael Defosseux <raphael.defosseux@eurecom.fr>
parent 190ff5df
# How to contribute in the OAI new python framework. #
The [PEP 8 style guide](https://www.python.org/dev/peps/pep-0008/) is authoritative.
## Documentation ##
* Document all public functions and *keep those docs up to date* when you make changes.
* We use Google style docstrings in our codebase
Example:
```python
def foo(arg1: str) -> int:
"""Returns the length of arg1.
Args:
arg1 (str): string to calculate the length of
Returns: the length of the provided parameter
"""
return len(arg1)
```
## Formatters ##
* We recommend to use `autopep8`, `isort` and `add-trailing-comma` as they conform to pep8.
* We do not recommend other formatters such as `black`, as it diverges from pep8 on basic things like line length, etc.
### Installation of `isort` ###
See for details at [isort](https://pypi.org/project/isort/)
```bash
pip install isort
```
Usage:
```bash
isort oai-ci-test-main.py
```
### Installation of `autopep8` ###
See for details at [autopep8](https://pypi.org/project/autopep8/)
```bash
pip install --upgrade autopep8
```
Usage:
```bash
autopep8 --select W191,W291,W292,W293,W391,E2,E3 -r --in-place oai-ci-test-main.py
```
### Installation of `add-trailing-comma` ###
See for details at [add-trailing-comma](https://pypi.org/project/add-trailing-comma/)
```bash
pip install add-trailing-comma
```
Usage:
```bash
add-trailing-comma --py35-plus --exit-zero-even-if-changed oai-ci-test-main.py
```
### Linter ###
Formatting does not mean your code is pep8-compliant.
See for details at [flake8](https://pypi.org/project/flake8/)
```bash
pip install flake8
```
Usage:
You shall be in this folder so it can use `setup.cfg` file as configuration file.
```bash
cd ci-scripts/python_v2
flake8 oai-ci-test-main.py
```
You shall have no error message.
"""
Licensed to the OpenAirInterface (OAI) Software Alliance.
This code is licensed to OSA under one or more contributor license agreements.
See the NOTICE file distributed with this work for additional information
regarding copyright ownership.
The OpenAirInterface Software Alliance licenses this file to You under
the OAI Public License, Version 1.1 (the "License"); you may not use this file
except in compliance with the License.
You may obtain a copy of the License at
http://www.openairinterface.org/?page_id=698
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
For more information about the OpenAirInterface (OAI) Software Alliance:
contact@openairinterface.org
"""
class CN: class CN:
def __init__(self,infra,config,deployment): """Object class to support any Core Network operations."""
self.Deployment=deployment
self.Infra=infra def __init__(self, infra, config, deployment):
for k,v in config.items(): self.Deployment = deployment
setattr(self,k,v) self.Infra = infra
\ No newline at end of file for key, val in config.items():
setattr(self, key, val)
"""
Licensed to the OpenAirInterface (OAI) Software Alliance.
This code is licensed to OSA under one or more contributor license agreements.
See the NOTICE file distributed with this work for additional information
regarding copyright ownership.
The OpenAirInterface Software Alliance licenses this file to You under
the OAI Public License, Version 1.1 (the "License"); you may not use this file
except in compliance with the License.
You may obtain a copy of the License at
http://www.openairinterface.org/?page_id=698
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
For more information about the OpenAirInterface (OAI) Software Alliance:
contact@openairinterface.org
"""
class NodeB: class NodeB:
def __init__(self,infra,config,deployment): """Object class to support any RAN (eNB/gNB) operations."""
self.Deployment=deployment
self.Infra=infra def __init__(self, infra, config, deployment):
for k,v in config.items(): self.Deployment = deployment
setattr(self,k,v) self.Infra = infra
\ No newline at end of file for key, val in config.items():
setattr(self, key, val)
"""
Licensed to the OpenAirInterface (OAI) Software Alliance.
This code is licensed to OSA under one or more contributor license agreements.
See the NOTICE file distributed with this work for additional information
regarding copyright ownership.
The OpenAirInterface Software Alliance licenses this file to You under
the OAI Public License, Version 1.1 (the "License"); you may not use this file
except in compliance with the License.
You may obtain a copy of the License at
http://www.openairinterface.org/?page_id=698
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
For more information about the OpenAirInterface (OAI) Software Alliance:
contact@openairinterface.org
"""
class UE: class UE:
def __init__(self,infra,config,deployment): """Object class to support any UE operations."""
self.Deployment=deployment
self.Infra=infra def __init__(self, infra, config, deployment):
for k,v in config.items(): self.Deployment = deployment
setattr(self,k,v) self.Infra = infra
\ No newline at end of file for key, val in config.items():
setattr(self, key, val)
#loading xml action list from yaml
import yaml
import cls_ue
import cls_ran
import cls_cn
#from yaml description of test infrastructure, return infrastructure data model
def GetTestInfrastructure(filename):
with open(filename,'r') as f:
infra = yaml.load(f)
return infra
#from yaml description of testcase, return configuration data model
def GetTestConfig(filename):
with open(filename,'r') as f:
test_config = yaml.load(f)
return test_config
def GetTestObjects(key,infrastructure,test_cfg):
part=test_cfg['config'][key]['key'] #identify the relevant infra part from test config naming ex : RAN_0
elements=test_cfg['config'][key][key].keys() #retrieve the effective elements under test
#create dict of Objects under test
OBJ={}
for n in elements:
deployment = test_cfg['config'][key][key][n]['Deploy']
obj_part=infrastructure[part][n] #retrieve the infra part of the element under test only
if key=='RAN':
OBJ[n]=cls_ran.NodeB(infrastructure,obj_part,deployment)
elif key=='CN':
OBJ[n]=cls_cn.CN(infrastructure,obj_part,deployment)
elif key=='UE':
OBJ[n]=cls_ue.UE(infrastructure,obj_part,deployment)
else:
pass
return OBJ #dictionary of objects under test
if __name__ == "__main__":
testbench='testinfra-as-code.yaml'
test='test-example.yaml'
infrastructure=GetTestInfrastructure(testbench)
test_cfg=GetTestConfig(test)
RAN=GetTestObjects('RAN',infrastructure,test_cfg)
CN=GetTestObjects('CN',infrastructure,test_cfg)
UEs=GetTestObjects('UE',infrastructure,test_cfg)
"""
Licensed to the OpenAirInterface (OAI) Software Alliance.
This code is licensed to OSA under one or more contributor license agreements.
See the NOTICE file distributed with this work for additional information
regarding copyright ownership.
The OpenAirInterface Software Alliance licenses this file to You under
the OAI Public License, Version 1.1 (the "License"); you may not use this file
except in compliance with the License.
You may obtain a copy of the License at
http://www.openairinterface.org/?page_id=698
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
For more information about the OpenAirInterface (OAI) Software Alliance:
contact@openairinterface.org
"""
import yaml
import cls_cn
import cls_ran
import cls_ue
def get_test_infrastructure(filename):
"""
Load the test infrastructure.
Args:
filename: yaml description file of test infrastructure
Returns:
infra: infrastructure data model
"""
with open(filename, 'r') as infra_yml:
infra = yaml.safe_load(infra_yml)
return infra
def get_test_config(filename):
"""
Load the test configuration data model.
Args:
filename: yaml description file of testcase
Returns:
test_config: test configuration data model
"""
with open(filename, 'r') as test_yml:
test_config = yaml.safe_load(test_yml)
return test_config
def get_test_objects(key, infrastructure, test_cfg):
"""
Load the test objects.
Args:
key: relevant keys to select
infrastructure: infrastructure data model
test_cfg: test configuration data model
Returns:
dict_obj: dictionary of objects under test
"""
# identify the relevant infra part from test config naming ex : RAN_0
part = test_cfg['config'][key]['key']
# retrieve the effective elements under test
elements = test_cfg['config'][key][key].keys()
# create dict of Objects under test
dict_obj = {}
for elt in elements:
deployment = test_cfg['config'][key][key][elt]['Deploy']
# retrieve the infra part of the element under test only
obj_part = infrastructure[part][elt]
if key == 'RAN':
dict_obj[elt] = cls_ran.NodeB(infrastructure, obj_part, deployment)
elif key == 'CN':
dict_obj[elt] = cls_cn.CN(infrastructure, obj_part, deployment)
elif key == 'UE':
dict_obj[elt] = cls_ue.UE(infrastructure, obj_part, deployment)
else:
pass
return dict_obj
if __name__ == '__main__':
testbench = 'testinfra-as-code.yaml'
test = 'test-example.yaml'
infrastructure = get_test_infrastructure(testbench)
test_cfg = get_test_config(test)
RAN = get_test_objects('RAN', infrastructure, test_cfg)
CN = get_test_objects('CN', infrastructure, test_cfg)
UEs = get_test_objects('UE', infrastructure, test_cfg)
[flake8]
max-complexity = 20
strictness = short
ignore =
WPS102, # Found incorrect module name pattern
WPS110, # Found wrong variable name
WPS210, # Found too many local variables
WPS219, # Found too deep access level
WPS306, # Found class without a base class
WPS420, # Found wrong keyword
# pydocstyle warnings
D107, # Missing docstring in __init_
D2, # White space formatting for doc strings
D400, # First line should end with a period
[isort]
# Is the same as 80 in flake8:
line_length = 80
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 29 09:38:02 2021
@author: hardy
"""
class NodeB:
def __init__(self,infra,config,deployment):
self.deployment=deployment
self.infra=infra
for k,v in config.items():
setattr(self,k,v)
class UE:
def __init__(self,infra,config):
self.infra=infra
for k,v in config.items():
setattr(self,k,v)
#loading xml action list from yaml
import yaml
yaml_file='testinfra-as-code.yaml'
with open(yaml_file,'r') as f:
infra = yaml.load(f)
yaml_file='test-example.yaml'
with open(yaml_file,'r') as f:
test = yaml.load(f)
#RAN spec from test
ran_key=test['config']['RAN']['key']
nodes_key=test['config']['RAN']['Nodes'].keys()
#create dict of RAN objects RAN under Test
RAN={}
for n in nodes_key:
deployment = test['config']['RAN']['Nodes'][n]['Deploy']
config=infra[ran_key][n]
RAN[n]=NodeB(infra,config,deployment)
#UE spec from test
ue_key=test['config']['UE']['key']
allues_key=test['config']['UE']['UEs'].keys()
#create dict of UE objects UE under Test
UEs={}
for u in allues_key:
config=infra[ue_key][u]
UEs[u]=UE(infra,config)
File mode changed from 100755 to 100644
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment