Skip to content
Snippets Groups Projects
Commit 6cf48371 authored by ptangu01's avatar ptangu01
Browse files

format code style pep8 but not on func name yet

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/trunk@1361 b32b6428-25c9-4566-ad07-03861ab6144f
parent 7fe7c623
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
#
# Copyright 2014, Jérôme Colin, Jérôme Kerdreux, Philippe Tanguy, Telecom Bretagne.
#
#
# Copyright 2014, Jérôme Colin, Jérôme Kerdreux, Philippe Tanguy,
# Telecom Bretagne.
#
# This file is part of xAAL.
#
#
# xAAL 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.
#
#
# xAAL 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 xAAL. If not, see <http://www.gnu.org/licenses/>.
#
......@@ -22,6 +23,11 @@
import os
import uuid
import pysodium
import logging
import logging.config
from . import config
import sys
......@@ -30,19 +36,20 @@ if (sys.version_info.major > 2):
else:
from ConfigParser import RawConfigParser as ConfigParser
import pysodium
import logging
import logging.config
def getConfigFile(name,confDir=config.CONF_DIR):
def getConfigFile(name, confDir=config.CONF_DIR):
filename = '%s.conf' % name
if os.path.isdir(confDir):
return os.path.join(confDir,filename)
return os.path.join(confDir, filename)
else:
print("Please verify your configuration filename or configuration directory: - The default directory must exist and be in your user path and in a directory called .xaal - And/Or verify your configuration filename")
print("Please verify your configuration filename or\
configuration directory:\
- The default directory must exist and be in\
your user path and in a directory called .xaal\
- And/Or verify your configuration filename")
sys.exit(0)
def loadConfigFile(filename):
if os.path.exists(filename):
cfg = ConfigParser()
......@@ -50,32 +57,37 @@ def loadConfigFile(filename):
return cfg
return None
def getConfigFileAddr(appName):
configFile = getConfigFile(appName)
cfg = loadConfigFile(configFile)
if cfg == None:
cfg = newDefaultConfigFile(configFile,appName)
addr = cfg.get(appName,'xaaladdr')
if cfg is None:
cfg = newDefaultConfigFile(configFile, appName)
addr = cfg.get(appName, 'xaaladdr')
return addr
def configParser():
cfg = ConfigParser()
return cfg
def newDefaultConfigFile(filename,appName):
def newDefaultConfigFile(filename, appName):
cfg = ConfigParser()
cfg.add_section(appName)
cfg.set(appName,'xaaladdr',getRandomUUID())
f = open(filename,'w')
cfg.set(appName, 'xaaladdr', getRandomUUID())
f = open(filename, 'w')
cfg.write(f)
return cfg
def getRandomUUID():
return uuid.uuid1().__str__()
def getLogger(name,level,filename=None):
def getLogger(name, level, filename=None):
"""
It creates logger if doesn't exist.
- Default handler is in the console with DEBUG level.
......@@ -85,23 +97,26 @@ def getLogger(name,level,filename=None):
logger = logging.getLogger(name)
# define handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
console_handler.setFormatter(formatter)
logger.root.addHandler(console_handler)
if filename:
file_handler = logging.handlers.RotatingFileHandler(filename,'a',10000,1,'utf8')
file_handler = logging.handlers.RotatingFileHandler(
filename, 'a', 10000, 1, 'utf8')
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
logger.root.addHandler(file_handler)
# Level manage by the root logger
logger.root.setLevel(level)
return logger
def pass2key(passphrase):
"""Generate key from passphrase using libsodium
crypto_pwhash_scryptsalsa208sha256 func
......@@ -109,10 +124,11 @@ def pass2key(passphrase):
opslimit: crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE
memlimit: crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
"""
KEY_BYTES=32
salt='\00'*32 #bytearray(pysodium.crypto_pwhash_scryptsalsa208sha256_SALTBYTES)
opslimit=pysodium.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE
memlimit=pysodium.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
key=pysodium.crypto_pwhash_scryptsalsa208sha256(KEY_BYTES,passphrase,salt,opslimit,memlimit)
KEY_BYTES = 32
# bytearray(pysodium.crypto_pwhash_scryptsalsa208sha256_SALTBYTES)
salt = '\00' * 32
opslimit = pysodium.crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE
memlimit = pysodium.crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE
key = pysodium.crypto_pwhash_scryptsalsa208sha256(
KEY_BYTES, passphrase, salt, opslimit, memlimit)
return key
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment