Skip to content

Commit

Permalink
Work in progress aligning nxsugar-py to nxsugar-go
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerzr committed Sep 20, 2016
1 parent d9961c8 commit 48af722
Show file tree
Hide file tree
Showing 6 changed files with 954 additions and 151 deletions.
99 changes: 99 additions & 0 deletions nxsugarpy/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# nxsugarpy, a Python library for building nexus services with python
# Copyright (C) 2016 by the nxsugarpy team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

# client errors
ErrParse = -32700
ErrInvalidRequest = -32600
ErrInternal = -32603
ErrInvalidParams = -32602
ErrMethodNotFound = -32601
ErrTtlExpired = -32011
ErrPermissionDenied = -32010
ErrConnClosed = -32007
ErrLockNotOwned = -32006
ErrUserExists = -32005
ErrInvalidUser = -32004
ErrInvalidPipe = -32003
ErrInvalidTask = -32002
ErrCancel = -32001
ErrTimeout = -32000
# nxpy errors
ErrUnknownError = -32098
ErrNotSupported = -32099
# nxsugar errors
ErrTestingMethodNotProvided = -20000
ErrPactNotDefined = -20001

#
ErrStr = {
# client errors
ErrParse: "Parse error",
ErrInvalidRequest: "Invalid request",
ErrMethodNotFound: "Method not found",
ErrInvalidParams: "Invalid params",
ErrInternal: "Internal error",
ErrTimeout: "Timeout",
ErrCancel: "Cancel",
ErrInvalidTask: "Invalid task",
ErrInvalidPipe: "Invalid pipe",
ErrInvalidUser: "Invalid user",
ErrUserExists: "User already exists",
ErrPermissionDenied: "Permission denied",
ErrTtlExpired: "TTL expired",
ErrLockNotOwned: "Lock not owned",
ErrConnClosed: "Connection is closed",
# nxpy errors
ErrUnknownError: "Unknown error",
ErrNotSupported: "Not supported",
# nxsugar errors
ErrTestingMethodNotProvided: "Testing method not provided",
ErrPactNotDefined: "Pact not defined for provided input",
}

def newJsonRpcErr(code, message, data):
return formatAsJsonRpcErr({"code": code, "message": message, "data": data})

def formatAsJsonRpcErr(err):
if not isinstance(err, dict):
return {"code": 0, "message": "", "data": None}
code = 0
if "code" in err:
code = err["code"]
message = ""
if "message" in err:
message = err["message"]
data = None
if "data" in err:
data = err["data"]
return {"code": code, "message": message, "data": data}

def errToStr(err):
code = 0
if "code" in err:
code = err["code"]
message = ""
if "message" in err:
message = err["message"]
return "[{0}] {1}".format(code, message)

def isNexusErrCode(err, code):
formatAsJsonRpcErr(err)
return err["code"] == code
99 changes: 99 additions & 0 deletions nxsugarpy/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# nxsugarpy, a Python library for building nexus services with python
# Copyright (C) 2016 by the nxsugarpy team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import threading

def secondsToStr(secs):
out = ""
if secs < 1:
return "{0:.4f}s".format(secs)
elif secs < 60:
return "{0:.0f}s".format(secs)
elif secs < 3600:
mins = secs/60
rsecs = secs%60
out = "{0:.0f}m".format(mins)
if rsecs > 0:
out = "{0}{1:.0f}s".format(out, rsecs)
else:
hours = secs / 3600
rsecs = secs % 3600
out = "{0:.0f}h".format(hours)
if rsecs > 0:
rmins = rsecs / 60
rsecs = rsecs % 60
out = "{0}{1:.0f}m".format(out, rmins)
if rsecs > 0:
out = "{0}{1:.0f}s".format(out, rsecs)
return out


class Stats(object):
def __init__(self):
self._lock = threading.Lock()
self.taskPullsDone = 0
self.taskPullTimeouts = 0
self.tasksPulled = 0
self.tasksPanic = 0
self.tasksServed = 0
self.tasksMethodNotFound = 0
self.tasksRunning = 0
self.threadsUsed = 0

def addTaskPullsDone(self, n):
self._lock.acquire()
self.taskPullsDone += n
self._lock.release()

def addTaskPullsTimeouts(self, n):
self._lock.acquire()
self.taskPullTimeouts += n
self._lock.release()

def addTasksPulled(self, n):
self._lock.acquire()
self.tasksPulled += n
self._lock.release()

def addTaskPanic(self, n):
self._lock.acquire()
self.tasksPanic += n
self._lock.release()

def addTasksServed(self, n):
self._lock.acquire()
self.tasksServed += n
self._lock.release()

def addTasksMethodNotFound(self, n):
self._lock.acquire()
self.tasksMethodNotFound += n
self._lock.release()

def addTasksRunning(self, n):
self._lock.acquire()
self.tasksRunning += n
self._lock.release()

def addThreadsUsed(self, n):
self._lock.acquire()
self.threadsUsed += n
self._lock.release()
28 changes: 28 additions & 0 deletions nxsugarpy/info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# nxsugarpy, a Python library for building nexus services with python
# Copyright (C) 2016 by the nxsugarpy team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time

wanIps = "1.2.3.4"
lanIps = ["10.0.0.1", "172.16.0.1"]
user = "root"
directory = "/my/dir"
started = time.time()
93 changes: 93 additions & 0 deletions nxsugarpy/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# nxsugarpy, a Python library for building nexus services with python
# Copyright (C) 2016 by the nxsugarpy team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from __future__ import print_function
import sys
import strict_rfc3339
import json

PanicLevel = "panic"
FatalLevel = "fatal"
ErrorLevel = "error"
WarnLevel = "warn"
InfoLevel = "info"
DebugLevel = "debug"

_jsonEnabled = False
_level = 0

def _eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)

def setJSONOutput(enabled):
global _jsonEnabled
if enabled:
_jsonEnabled = True
else:
_jsonEnabled = False

def setLogLevel(level):
global _level
_level = _getLogLevelNum(level)

def _getLogLevelNum(level):
if level == PanicLevel:
return 5
elif level == FatalLevel:
return 4
elif level == ErrorLevel:
return 3
elif level == WarnLevel:
return 2
elif level == InfoLevel:
return 1
else:
return 0

def getLogLevel():
if _level == 5:
return PanicLevel
elif _level == 4:
return FatalLevel
elif _level == 3:
return ErrorLevel
elif _level == 2:
return WarnLevel
elif _level == 1:
return InfoLevel
else:
return DebugLevel

def log(level, path, message, *args, **kwargs):
logWithFields(level, path, {}, message, *args, **kwargs)

def logWithFields(level, path, fields, message, *args, **kwargs):
if _level <= _getLogLevelNum(level):
level = level[:4].upper()
try:
msg = message.format(*args, **kwargs)
except:
msg = "<invalid format> msg[ {0} ] args[ {1} ] kwargs[ {2} ]".format(message, args, kwargs)
if _jsonEnabled:
jsonData = json.dumps(fields)
print('''{"time": "{time}", "level": "{level}", "path": "{path}", "msg": "{message}", "data": {data}}'''.format(time=strict_rfc3339.now_to_rfc3339_utcoffset(), level=level, path=path, message=msg, data=jsonData), file=sys.stderr)
else:
print('''[{time}] [{level}] [{path}] {message}'''.format(time=strict_rfc3339.now_to_rfc3339_utcoffset(), level=level, path=path, message=msg), file=sys.stderr)
Loading

0 comments on commit 48af722

Please sign in to comment.