I'm trying to create config.py in my libraries because I have in every script below code:
config = ""
try:
config = configparser.ConfigParser()
if (configFile is None) or (configFile == ""):
configReadArrayLength = len(config.read(pathABSofScript + 'config.ini'))
else:
configReadArrayLength = len(config.read(configFile))
if configReadArrayLength == 0:
print("Script could not find configuration file.")
exit(1)
except Exception as ex:
print("An exception of typ {0} occurred in config parsing function. Arguments:\n{1!r} exiting."
.format(type(ex).__name__, ex.args))
exit(1)
def get_config_value(value):
try:
return config['environment'][value]
except KeyError:
return None
except Exception as config_value_exception:
standard_logger_error(type(config_value_exception).__name__, config_value_exception.args)
And I don't want do duplicate code. So the steps which I have followed:
0) Structure looks like this:
/
scripts/
somescript.py
lib/
config.py
1) In my somescript.py I have put:
sys.path.insert(0, /home/user/some/path/lib')
import config
2) config.py content:
import configparser
config = ""
def create_config_instance(path, file_name):
try:
global config
config = configparser.ConfigParser()
configReadArrayLength = len(config.read(path + file_name))
if configReadArrayLength == 0:
raise IOError("Cannot read config file")
except Exception as ex:
raise Exception("An exception of typ {0} occurred in config parsing function. Arguments:\n{1!r} exiting.".format(type(ex).__name__, ex.args))
def get_config_value(value):
try:
global config
section = config['DEFAULT']['section_to_use']
# return config[section][value]
return "ELO"
except KeyError:
raise KeyError
except Exception as config_value_exception:
# return str(type(config_value_exception).__name__, config_value_exception.args)
raise Exception(config_value_exception)
3) Content of somescript.py
#!/usr/bin/python3.6
import unittest
import os
import sys
import platform
sys.path.insert(0, '/scripts/lib')
import logger
import config
pathABSofScript = ""
if platform.system() == "Linux":
pathABSofScript = str(os.path.realpath(__file__).rsplit('/', 1)[0]) + "/"
else:
print("Unknown linux")
exit(1)
# I'm creating instance of config.py object and passing there location of a file and file name
config_instance = config.create_config_instance(pathABSofScript, "config.ini")
# I'm trying to get value from config.ini
pathDBfile = config_instance.get_config_value("pathDBfile")
print(pathDBfile)
class TestDatabase(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
if __name__ == '__main__':
unittest.main()
And I'm facing this error:
File "./somescript.py", line 21, in <module>
pathDBfile = config_instance.get_config_value("pathDBfile")
AttributeError: 'NoneType' object has no attribute 'get_config_value'
Can someone give me a tip to resolve my problem?
Related
How to import my database connection file into another file and create a connection in that file? For example, I have a main file in the name **"Database_main.py"**In my main file I have a function like. establish_connection, change _passkey . Now I have to import the main file(Database_main.py) things in my second file(database_connection.py). But I got a error message "TypeError: establish_connection_general() missing 1 required positional argument: 'self'. How to sucessfully import my main file and make a connection.
Database_main.py
from PyQt5.QtWidgets import QWidget,QMessageBox,QApplication
from PyQt5.QtGui import QIcon
import sqleet
import os
import sys
file_path_general = r"d:\project database\assist"
db_name_general = (os.path.join(file_path_general,'database_general.db'))
passkey_general = "1234"
new_passkey_general = "1234"
show_errormsg = "show"
class Database_connection(QWidget):
def __init__(self):
super(). __init__()
self.establish_connection_general()
# self.cahnge_passkey()
def establish_connection_general(self):
try:
connection_general = sqleet.connect(db_name_general,key = passkey_general)
print("open scueffully")
except Exception as e:
if show_errormsg == "show":
self.handle_error(e)
else:
pass
def cahnge_passkey(self):
try:
general_connection = sqleet.connect(db_name_general,key = passkey_general)
general_connection.change_key(new_passkey_general)
print("new pass key sucessfully changed")
general_connection.close()
except Exception as e:
if show_errormsg == "show":
self.handle_error(e)
else:
pass
def handle_error(self,error):
exc_type, exc_value, exc_traceback = sys.exc_info()
filenamewithpath = exc_traceback.tb_frame.f_code.co_filename
head,tail = os.path.split((filenamewithpath))
lineno = exc_traceback.tb_lineno
name = exc_traceback.tb_frame.f_code.co_name
type = exc_type.__name__
message = exc_value
nl = '\n'
kk = f'File Name : {tail[:-3]}{nl}'\
f'Line No. : {lineno}{nl}'\
f'Type : {type}{nl}'\
f'Name : {name}{nl}'
self.msg = QMessageBox()
self.msg.setFixedSize(1600,400)
self.msg.setWindowTitle(" Error/Bugs Information")
self.msg.setWindowIcon(QIcon('icon\close006.png'))
fd = " "
self.msg.setText(f'{type} - {lineno}{fd}')
self.msg.setIcon(QMessageBox.Information)
self.msg.setStandardButtons(QMessageBox.Ok)
self.msg.setDefaultButton(QMessageBox.Ok)
self.msg.setInformativeText("")
self.msg.setDetailedText(kk)
self.msg.show()
def main():
app = QApplication(sys.argv)
ex = Database_connection()
app.setStyle("Fusion")
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Database_Connection file
import sys
import os
from makeeasy_database_connection import Database_connection
db = Database_connection.establish_connection_general()
db()
Result
Traceback (most recent call last):
File "D:/project database/assist/database_connection.py", line 6, in <module>
db = Database_connection.establish_connection_general()
TypeError: establish_connection_general() missing 1 required positional argument: 'self'
How to import properly and use my code ?
Like to catch SNMPerror, if error occur, do something.
Python code to fetch Network OS type via snmphandler package. It works, but if the SNMP is not query the request, it will timeout. I would like to catch that and print "No SNMP Active"
from nelsnmp.snmp import SnmpHandler
from nelsnmp.errors import ArgumentError, SnmpError
def findnetOS(host):
dev = SnmpHandler(host=host, version='2c', community='public')
hostinfo = HostInfo(dev)
osversion = hostinfo.get_version()
try:
os = hostinfo.os
except ArgumentError as e:
if e.__str__() == "No valid SNMP version defined":
os = None
return os
else:
return os
ostype = findnetOS(host)
if ostype != None:
ostype = "cisco_"+ostype
print ostype
else:
print "No SNMP Active"
exit(-1)
Expected output should be print "No SNMP Active" when error occurs and print ostype when it runs thru without errors.
Error I get:
File "/usr/lib/python2.7/site-packages/nelsnmp/snmp.py", line 210, in _raise_error
raise ErrorType(error_data)
nelsnmp.errors.SnmpError: No SNMP response received before timeout
Thank you for the view
Do the same thing you're doing for the ArgumentError
from nelsnmp.snmp import SnmpHandler
from nelsnmp.errors import ArgumentError, SnmpError
def findnetOS(host):
try:
dev = SnmpHandler(host=host, version='2c', community='public')
hostinfo = HostInfo(dev)
osversion = hostinfo.get_version()
except SnmpError as e:
# handle your error here
pass
try:
os = hostinfo.os
except ArgumentError as e:
if e.__str__() == "No valid SNMP version defined":
os = None
return os
else:
return os
ostype = findnetOS(host)
if ostype != None:
ostype = "cisco_"+ostype
print ostype
else:
print "No SNMP Active"
exit(-1)
I want to execute a Python script that closes all the tickets of JIRA once my branch is merged with master. Can any one please help me how to solve the problem?
from __future__ import with_statement
from jira import JIRA, JIRAError
from requests.exceptions import ConnectionError
import cProfile
import logging
import sys
import os
import shutil
import logging.handlers
jiraEnabled = True
dashes = "---------------------------------------------------------------------"
import contextlib
import subprocess
import re
import collections
import getpass
import traceback
import pprint
import pdb
import stat
import cookielib
import subprocess
import urllib2
import ConfigParser
import string
def main():
global username, password, loglevel, jiraCheckEnabled, url, allowed_states, check_assignee, check_state, disabled_on_branches
configure_logging(loglevel)
config_file = get_config_file("config.ini")
error_code = handle_pre_receive()
if error_code != 0:
logging.error(“Hook failed please try later\n”)
return error_code
# Performs the git "pre-receive" hook
def handle_pre_receive():
line = sys.stdin.read()
try:
(old_commit_id, new_commit_id, ref) = line.strip().split()
except ValueError:
logging.error("\n%s", dashes)
return -1
if new_commit_id == "0000000000000000000000000000000000000000":
logging.debug("Branch was deleted, going to skip commit")
return 0
if disabled_on_branch(git_get_branchname_from_ref(ref)):
return 0
commit_id_array = git_get_array_of_commit_ids(old_commit_id, new_commit_id)
if commit_id_array == None or len(commit_id_array)==0:
if old_commit_id == "0000000000000000000000000000000000000000":
logging.debug("Branch was created, going to skip commit processing")
return 0
logging.error("No new commits found!")
return -1
if jiraEnabled:
try:
jira = JIRA(url,basic_auth=(username,password))
except ConnectionError, e:
logging.error("Failed to connect to JIRA")
return 0
except JIRAError, e:
logging.error("JIRA has rejected connection” )
return 0;
else:
jira = None
def get_shell_cmd_output(cmd):
try:
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
return proc.stdout.read().rstrip('\n')
except KeyboardInterrupt:
logging.info("... interrupted")
except Exception, e:
logging.error("Failed trying to execute '%s'", cmd)
def disabled_on_branch(current_branchname):
logging.debug("Test if '%s' is disabled...", current_branchname)
if disabled_on_branches == None or string.strip(disabled_on_branches) == "":
logging.debug("All branches enabled")
return False
branchlist = string.split(disabled_on_branches, ',')
for branch in branchlist:
branch = string.strip(branch)
if current_branchname == branch:
logging.debug("Current branch '%s' is disabled", current_branchname)
return True
logging.debug("Current branch '%s' is enabled", current_branchname)
return False
def git_get_curr_branchname():
buf = get_shell_cmd_output("git branch --no-color")
# buf is a multiline output, each line containing a branch name
# the line that starts with a "*" contains the current branch name
m = re.search("^\* .*$", buf, re.MULTILINE)
if m == None:
return None
return buf[m.start()+2 : m.end()]
def git_get_branchname_from_ref(ref):
# "refs/heads/<branchname>"
if string.find(ref, "refs/heads") != 0:
logging.error("Invalid ref '%s'", ref)
exit -1
return string.strip(ref[len("refs/heads/"):])
def git_get_commit_msg(commit_id):
return get_shell_cmd_output("git rev-list --pretty --max-count=1 " + commit_id)
#----------------------------------------------------------------------------
# python script entry point. Dispatches main()
if __name__ == "__main__":
cProfile.run('main()')
exit(0)
handle_pre_receive method checks if the branch is still enabled or not. If branch is disabled we have to close all the JIRA tickets related to that branch.
I am running a simple python script to log the accessed url using squid url_rewriter_program.
However every time it runs, rewriter crashes with broken pipe error at sys.stdout.flush().
Please suggest a specific solution.
python code is:
import sys
import os
import io
line = sys.stdin.readline()
fo=open("/home/linux/Desktop/foo1.txt","a")
fo.write(line)
fo.close()
sys.stdout.write("\n")
sys.stdout.flush()
This is a squid redirector file, written on python, can you get, or compare with your script:
Redirector:
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
#-----------------------------------------------------------------------------
# Name: redirector_master.py
# Purpose: SiSCont checker cuote
#
# Author: Ernesto Licea Martin
#
# Created: 2011/11/24
# RCS-ID: $Id: redirector_master.py $
# Copyright: (c) 2011
# Licence: GPL
#-----------------------------------------------------------------------------
import sys, string, libSiSCont
__name__= "redirector_master"
query="SELECT accounts_proxyquotatype.name, accounts_proxyaccount.proxy_quota, accounts_proxyaccount.proxy_quota_extra, accounts_proxyaccount.proxy_active, accounts_proxyaccounttype.name FROM accounts_proxyaccount INNER JOIN accounts_proxyaccounttype INNER JOIN accounts_proxyquotatype ON (accounts_proxyaccount.proxy_quota_type_id = accounts_proxyquotatype.id) AND (accounts_proxyaccount.proxy_account_type_id = accounts_proxyaccounttype.id) WHERE accounts_proxyaccount.proxy_username=%s"
class RedirMaster:
def __init__(self):
obj = libSiSCont.ParceConf()
obj.parcecfgfile()
self.__listModules = obj.getModList()
self.__redirDicc = obj.getRedirectURL()
self.__penalURL = obj.getPenalizedURL()
self.__confDicc = obj.getConfParam()
self.__dbDicc = obj.getDBDicc()
self.__proxyDicc = obj.getProxyCacheParam()
self.__dbParam = []
def getProxyTypes(self):
db=libSiSCont.connectDB(dbDicc=self.__dbDicc)
c=db.cursor()
c.execute("SELECT accounts_proxy")
def run(self):
modules=[]
for mod in self.__listModules:
try:
m=__import__(mod)
modules.append(m)
except Exception, e:
libSiSCont.reportLogs("%s.run" %__name__, 'Unable to load redirector module %s; the error was: %s' % (mod,str(e)))
if len(modules) == 0:
libSiSCont.reportLogs("%s.run" %__name__, 'No usable redirector module found; switching to trasparent behavour')
while 1:
try:
data_redir=raw_input()
data_redir=data_redir.split()
url,ip_host,user,method,urlgroup = data_redir[0:5]
ip=ip_host.split("/")[0]
host_name=ip_host.split("/")[1]
uri = url
mode=""
#Don't check cache access
if string.find(url,"cache_object") == 0:
sys.stdout.write("%s%s\n" %(mode,uri))
sys.stdout.flush()
continue
db=libSiSCont.connectDB(dbDicc=self.__dbDicc)
c=db.cursor()
c.execute(query,user)
cuote_type,cuote, ext_cuote, active, acc_type = c.fetchall()[0]
self.__dbParam=[cuote_type,int(cuote), int(ext_cuote), active, acc_type]
for module in modules:
try:
uri = module.redir(url = url, ip = ip, host_name = host_name, user = user, method = method, urlgroup = urlgroup, redirDicc = self.__redirDicc, penalURL = self.__penalURL, confDicc = self.__confDicc, proxyDicc = self.__proxyDicc, dbParam = self.__dbParam)
except Exception, e:
libSiSCont.reportLogs("%s.run" %__name__, 'Error while running module: %s -- The error was: %s' % (module,str(e)))
if uri != url:
mode = "301:"
break
sys.stdout.write("%s%s\n" %(mode,uri))
sys.stdout.flush()
except Exception, e:
if not string.find('%s' % e,'EOF') >= 0:
sys.stdout.write('%s\n' % uri)
sys.stdout.flush()
libSiSCont.reportLogs("%s.run" %__name__, '%s: data received from parent: %s' % (str(e),string.join(data_redir)))
else:
sys.exit()
obj=RedirMaster()
obj.run()
Helper:
#!/usr/bin/env python
import sys, syslog, libSiSCont, string,crypt
__name__ = "Helper"
query = "SELECT accounts_proxyaccount.proxy_username FROM accounts_proxyaccount WHERE accounts_proxyaccount.proxy_username=%s AND accounts_proxyaccount.proxy_password=%s"
class BasicAuth:
def __init__(self):
obj = libSiSCont.ParceConf()
obj.parcecfgfile()
self.__dbDicc = obj.getDBDicc()
def run(self):
while 1:
try:
user_pass = string.split(raw_input())
user = user_pass[0].strip("\n")
passwd = user_pass[1].strip("\n")
crypt_passwd = crypt.crypt(passwd,user)
db = libSiSCont.connectDB(self.__dbDicc)
c = db.cursor()
c.execute(query,(user,crypt_passwd))
if c.fetchone() == None:
libSiSCont.reportLogs('%s.run' %__name__,'User Authentication Fail, user = %s password= %s, Access Denied' %(user,passwd) )
sys.stdout.write("ERR\n")
sys.stdout.flush()
else:
libSiSCont.reportLogs('%s.run' %__name__, 'User Authentication Success, user = %s, Access Granted' %user)
sys.stdout.write("OK\n")
sys.stdout.flush()
except Exception, e:
if not string.find("%s" %e, "EOF") >= 0:
sys.stdout.write("ERR\n")
sys.stdout.flush()
libSiSCont.reportLogs('%s.run' %__name__, 'Authenticator error, user will navigate without authentication: %s' %str(e))
else:
sys.exit()
obj = BasicAuth()
obj.run()
I hope help you ;-)
While I was writing simple message-based fileserver and client, I got the idea about checking fileserver status, but don't know how to realize this: just try to connect and disconnect from server (and how disconnect immediately, when server is not running, if using this way?) or maybe twisted/autobahn have some things, which help to get server status without creating "full connection"?
a) fileserver.py
import os
import sys
import json
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol, listenWS
CONFIG_TEMPLATE = ''
CONFIG_DATA = {}
class MessageBasedServerProtocol(WebSocketServerProtocol):
"""
Message-based WebSockets server
Template contains some parts as string:
[USER_ID:OPERATION_NAME:FILE_ID] - 15 symbols for USER_ID,
10 symbols for OPERATION_NAME,
25 symbols for FILE_ID
other - some data
"""
def __init__(self):
path = CONFIG_DATA['path']
base_dir = CONFIG_DATA['base_dir']
# prepare to working with files...
if os.path.exists(path) and os.path.isdir(path):
os.chdir(path)
if not os.path.exists(base_dir) or not os.path.isdir(base_dir):
os.mkdir(base_dir)
os.chdir(base_dir)
else:
os.makedir(path)
os.chdir(path)
os.mkdir(base_dir)
os.chdir(base_dir)
# init some things
self.fullpath = path + '/' + base_dir
def __checkUserCatalog(self, user_id):
# prepare to working with files...
os.chdir(self.fullpath)
if not os.path.exists(user_id) or not os.path.isdir(user_id):
os.mkdir(user_id)
os.chdir(user_id)
else:
os.chdir(self.fullpath + '/' + user_id)
def onOpen(self):
print "[USER] User with %s connected" % (self.transport.getPeer())
def connectionLost(self, reason):
print '[USER] Lost connection from %s' % (self.transport.getPeer())
def onMessage(self, payload, isBinary):
"""
Processing request from user and send response
"""
user_id, cmd, file_id = payload[:54].replace('[', '').replace(']','').split(':')
data = payload[54:]
operation = "UNK" # WRT - Write, REA -> Read, DEL -> Delete, UNK -> Unknown
status = "C" # C -> Complete, E -> Error in operation
commentary = 'Succesfull!'
# write file into user storage
if cmd == 'WRITE_FILE':
self.__checkUserCatalog(user_id)
operation = "WRT"
try:
f = open(file_id, "wb")
f.write(data)
except IOError, argument:
status = "E"
commentary = argument
except Exception, argument:
status = "E"
commentary = argument
raise Exception(argument)
finally:
f.close()
# read some file
elif cmd == 'READU_FILE':
self.__checkUserCatalog(user_id)
operation = "REA"
try:
f = open(file_id, "rb")
commentary = f.read()
except IOError, argument:
status = "E"
commentary = argument
except Exception, argument:
status = "E"
commentary = argument
raise Exception(argument)
finally:
f.close()
# delete file from storage (and in main server, in parallel delete from DB)
elif cmd == 'DELET_FILE':
self.__checkUserCatalog(user_id)
operation = "DEL"
try:
os.remove(file_id)
except IOError, argument:
status = "E"
commentary = argument
except Exception, argument:
status = "E"
commentary = argument
raise Exception(argument)
self.sendMessage('[%s][%s]%s' % (operation, status, commentary), isBinary=True)
if __name__ == '__main__':
if len(sys.argv) < 2:
print "using python fileserver_client.py [PATH_TO_config.json_FILE]"
else:
# read config file
CONFIG_TEMPLATE = sys.argv[1]
with open(CONFIG_TEMPLATE, "r") as f:
CONFIG_DATA = json.load(f)
# create server
factory = WebSocketServerFactory("ws://localhost:9000")
factory.protocol = MessageBasedServerProtocol
listenWS(factory)
reactor.run()
b) client.py
import json
import sys
import commands
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS
CONFIG_TEMPLATE = ''
CONFIG_DATA = {}
class MessageBasedClientProtocol(WebSocketClientProtocol):
"""
Message-based WebSockets client
Template contains some parts as string:
[USER_ID:OPERATION_NAME:FILE_ID] - 15 symbols for USER_ID,
10 symbols for OPERATION_NAME,
25 symbols for FILE_ID
other - some data
"""
def onOpen(self):
user_id = CONFIG_DATA['user']
operation_name = CONFIG_DATA['cmd']
file_id = CONFIG_DATA['file_id']
src_file = CONFIG_DATA['src_file']
data = '[' + str(user_id) + ':' + str(operation_name) + ':' + str(file_id) + ']'
if operation_name == 'WRITE_FILE':
with open(src_file, "r") as f:
info = f.read()
data += str(info)
self.sendMessage(data, isBinary=True)
def onMessage(self, payload, isBinary):
cmd = payload[1:4]
result_cmd = payload[6]
if cmd in ('WRT', 'DEL'):
print payload
elif cmd == 'REA':
if result_cmd == 'C':
try:
data = payload[8:]
f = open(CONFIG_DATA['src_file'], "wb")
f.write(data)
except IOError, e:
print e
except Exception, e:
raise Exception(e)
finally:
print payload[:8] + "Successfully!"
f.close()
else:
print payload
reactor.stop()
if __name__ == '__main__':
if len(sys.argv) < 2:
print "using python fileserver_client.py [PATH_TO_config.json_FILE]"
else:
# read config file
CONFIG_TEMPLATE = sys.argv[1]
with open(CONFIG_TEMPLATE, "r") as f:
CONFIG_DATA = json.load(f)
# connection to server
factory = WebSocketClientFactory("ws://localhost:9000")
factory.protocol = MessageBasedClientProtocol
connectWS(factory)
reactor.run()
Find solution this issue: using callLater or deferLater for disconnect from server, if can't connect, but when all was 'OK', just take server status, which he says.
import sys
from twisted.internet.task import deferLater
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS
CONFIG_IP = ''
CONFIG_PORT = 9000
def isOffline(status):
print status
class StatusCheckerProtocol(WebSocketClientProtocol):
def __init__(self):
self.operation_name = "STATUS_SRV"
self.user_id = 'u00000000000000'
self.file_id = "000000000000000000000.log"
def onOpen(self):
data = '[' + str(self.user_id) + ':' + str(self.operation_name) + ':' + str(self.file_id) + ']'
self.sendMessage(data, isBinary=True)
def onMessage(self, payload, isBinary):
cmd = payload[1:4]
result_cmd = payload[6]
data = payload[8:]
print data
reactor.stop()
if __name__ == '__main__':
if len(sys.argv) < 3:
print "using python statuschecker.py [IP] [PORT]"
else:
# read preferences
CONFIG_IP = sys.argv[1]
CONFIG_PORT = int(sys.argv[2])
server_addr = "ws://%s:%d" % (CONFIG_IP, CONFIG_PORT)
# connection to server
factory = WebSocketClientFactory(server_addr)
factory.protocol = StatusCheckerProtocol
connectWS(factory)
# create special Deffered, which disconnect us from some server, if can't connect within 3 seconds
d = deferLater(reactor, 3, isOffline, 'OFFLINE')
d.addCallback(lambda ignored: reactor.stop())
# run all system...
reactor.run()