Skip to content

Commit 8e38f29

Browse files
committed
initial refactor
1 parent 7b579c1 commit 8e38f29

22 files changed

Lines changed: 494 additions & 331 deletions

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.2
1+
4.0

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# Netsim Wrapper
22

3+
## TODO: need to update badges
4+
35
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache2-yellow.svg)](https://opensource.org/licenses/Apache-2.0)
46
[![Version: 3.1.2](https://img.shields.io/badge/Version-3.1.2-parrotgreen.svg)](https://github.com/NSO-developer/netsim-wrapper)
57
[![Downloads](https://pepy.tech/badge/netsim-wrapper)](https://pepy.tech/project/netsim-wrapper)
68
[![Downloads](https://pepy.tech/badge/netsim-wrapper/week)](https://pepy.tech/project/netsim-wrapper/week)
79

810
ncs-netsim is a great tool, but it lack of following features which are developed as part of netsim-wrapper
911

12+
## TODO: changing options
13+
1014
- netsim-wrapper features
1115
- delete-devices \<device-names>
1216
- create-network-from [ yaml | json ] \<filename>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

netsim_wrapper/common/__init__.py

Whitespace-only changes.

netsim_wrapper/common/log.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Application logging operations
3+
"""
4+
5+
import logging
6+
7+
from .singleton import SingletonMeta
8+
9+
10+
class Logger(metaclas=SingletonMeta):
11+
"""
12+
Logger with singleton metaclass
13+
"""
14+
format = '%(levelname)-8s | %(asctime)s | %(module)-8s:%(lineno)-4d | %(message)s'
15+
log = logging.getLogger()
16+
17+
def __init__(self, level=logging.INFO) -> None:
18+
self.level = level
19+
self.log = self.get_log
20+
21+
@property
22+
def get_log(self):
23+
"""
24+
Fetching logger with logging level
25+
"""
26+
logging.basicConfig(level=self.level, format=format)
27+
return logging.getLogger()
28+

netsim_wrapper/common/singleton.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Singleton Metaclass
3+
"""
4+
5+
class SingletonMeta(type):
6+
"""
7+
Singleton Metaclass
8+
"""
9+
10+
_instance = None
11+
12+
def __call__(cls, *args, **kwargs):
13+
if not cls._instance:
14+
cls._instance = super().__call__(*args, **kwargs)
15+
return cls._instance
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,70 @@
11
"""
2-
The module is for system operations exit, unlink and setting logger
2+
The module is for system operations exit, unlink
33
"""
44

5-
import logging
6-
75
from pathlib import Path
86
from sys import exit as sys_exit
97
import subprocess
108

11-
format = '%(levelname)-8s | %(asctime)s | %(module)-8s:%(lineno)-4d | %(message)s'
12-
13-
14-
def get_log(level):
15-
logging.basicConfig(level=level, format=format)
16-
return logging.getLogger()
9+
from .log import Logger
1710

1811

1912
class System:
20-
def __init__(self, path='.', level=logging.INFO) -> None:
13+
def __init__(self) -> None:
2114
super(System, self).__init__()
22-
self.log = get_log(level)
23-
self.log.debug(f"base class: {__class__.__name__} initialize")
24-
self.path = Path(path).absolute()
15+
self.log = Logger().log
16+
self.log.debug("class: {} initialize".format(__class__.__name__))
2517

2618
@property
2719
def exit(self):
20+
self.log.debug('exit method called')
2821
sys_exit()
2922

3023
def delete(self, fpath):
24+
"""
25+
Delete file if exist
26+
"""
3127
path = Path(fpath).absolute()
3228
if path.exists():
3329
path.unlink()
30+
self.log.debug('deleted file: {}'.format(fpath))
3431

3532
def is_file(self, fpath):
33+
"""
34+
Given path is it a file
35+
"""
3636
path = Path(fpath).absolute()
3737
return path.is_file()
3838

3939
def is_dir(self, fpath):
40+
"""
41+
Given path is it a folder
42+
"""
4043
path = Path(fpath).absolute()
4144
return path.is_dir()
4245

4346
def call(self, cmd):
47+
"""
48+
System subprocess call
49+
"""
4450
try:
51+
self.log.debug("call cmd: {}".format(' '.join(cmd)))
4552
subprocess.call(cmd, shell=True)
4653
except EnvironmentError as e:
4754
self.log.error("failed to run command: {}".format(cmd))
4855
self.log.error(e)
4956

5057
def run(self, cmd, error=True):
58+
"""
59+
System subprocess open and communicate
60+
"""
61+
self.log.debug("popen cmd: {}".format(' '.join(cmd)))
5162
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
5263
out, err = p.communicate()
5364
out, err = out.decode('utf-8'), err.decode('utf-8')
5465
if err == '' or 'env.sh' in err:
5566
self.log.debug("`{}` ran successfully".format(' '.join(cmd)))
67+
self.log.debug("out: {}".format(out))
5668
return out
5769
if error:
5870
self.log.error("an error occured while running command `{}`".format(' '.join(cmd)))

0 commit comments

Comments
 (0)