Mod: few changes with imports

This commit is contained in:
L-Nafaryus 2021-08-11 12:46:35 +05:00
parent 4dd74591fc
commit 4efb13954c
No known key found for this signature in database
GPG Key ID: C76D8DCD2727DBB7
9 changed files with 79 additions and 87 deletions

View File

@ -10,4 +10,4 @@ __version__ = "1.1.0"
__author__ = __maintainer = "George Kusayko"
__email__ = "gkusayko@gmail.com"
from anisotropy.core.main import main
#from anisotropy.core.main import main

View File

@ -15,12 +15,12 @@ from anisotropy.core.utils import struct, deepupdate
# Environment variables and config
##
env = { "ROOT": os.path.abspath(".") }
env.update(dict(
env.update(
BUILD = os.path.join(env["ROOT"], "build"),
LOG = os.path.join(env["ROOT"], "logs"),
DEFAULT_CONFIG = os.path.join(env["ROOT"], "anisotropy/config/default.toml"),
CONFIG = os.path.join(env["ROOT"], "conf/config.toml")
))
)
env["db_path"] = env["BUILD"]
env["salome_port"] = 2810
@ -44,60 +44,20 @@ env["salome_port"] = 2810
###
# Logger
##
from anisotropy.core.utils import setupLogger
logger_env = env.get("logger", {})
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;21m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = logger_env.get("format", "[ %(asctime)s ] [ %(levelname)s ] %(message)s")
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
logger = logging.getLogger(logger_env.get("name", "anisotropy"))
logger.setLevel(logging.INFO)
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
sh.setFormatter(CustomFormatter())
fh = logging.FileHandler(os.path.join(env["LOG"], logger_env.get("name", "anisotropy")))
fh.setLevel(logging.DEBUG)
fh.setFormatter(CustomFormatter())
logger.addHandler(sh)
logger.addHandler(fh)
setupLogger(logger, logging.INFO)
peeweeLogger = logging.getLogger("peewee")
peeweeLogger.setLevel(logging.INFO)
def timer(func):
def inner(*args, **kwargs):
start = time.monotonic()
ret = func(*args, **kwargs)
elapsed = time.monotonic() - start
return ret, elapsed
return inner
from anisotropy.core.utils import timer
from anisotropy import __version__
from anisotropy import salomepl
from anisotropy import openfoam
from anisotropy.samples import Simple, FaceCentered, BodyCentered
from math import sqrt
from peewee import JOIN

View File

@ -2,7 +2,48 @@ import logging
from multiprocessing import Queue, Process, cpu_count
import socket
import copy
import time
from types import FunctionType
import os
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;21m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
format = "[ %(asctime)s ] [ %(levelname)s ] %(message)s"
FORMATS = {
logging.DEBUG: grey + format + reset,
logging.INFO: grey + format + reset,
logging.WARNING: yellow + format + reset,
logging.ERROR: red + format + reset,
logging.CRITICAL: bold_red + format + reset
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
def setupLogger(logger, level: int):
logger.setLevel(level)
sh = logging.StreamHandler()
sh.setLevel(level)
sh.setFormatter(CustomFormatter())
fh = logging.FileHandler(os.path.join("logs", logger.name))
fh.setLevel(level)
fh.setFormatter(CustomFormatter())
logger.addHandler(sh)
logger.addHandler(fh)
return logger
class struct:
def __init__(self, *args, **kwargs):
@ -42,7 +83,6 @@ class struct:
def __repr__(self):
return str(self)
import copy
def deepupdate(target, src):
for k, v in src.items():
@ -64,44 +104,25 @@ def deepupdate(target, src):
else:
target[k] = copy.copy(v)
class Logger:
def __init__(self, name, logpath):
logging.basicConfig(
level = logging.INFO,
format = "%(levelname)s: %(message)s",
handlers = [
logging.StreamHandler(),
logging.FileHandler(logpath)
]
)
self.logger = logging.getLogger(name)
self.warnings = 0
self.errors = 0
self.criticals = 0
self.exceptions = 0
def info(self, *args):
self.logger.info(*args)
def timer(func: FunctionType) -> (tuple, float):
"""(Decorator) Returns output of inner function and execution time
def warning(self, *args):
self.warnings += 1
self.logger.warning(*args)
:param func: inner function
:type: FunctionType
def error(self, *args):
self.errors += 1
self.logger.error(*args)
:return: output, elapsed time
:rtype: tuple(tuple, float)
"""
def inner(*args, **kwargs):
start = time.monotonic()
ret = func(*args, **kwargs)
elapsed = time.monotonic() - start
def critical(self, *args):
self.criticals += 1
self.logger.critical(*args)
return ret, elapsed
def exception(self, *args):
self.exceptions += 1
self.logger.exception(*args)
def fancyline(self):
self.logger.info("-" * 80)
return inner

View File

@ -0,0 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from anisotropy.samples.simple import Simple
from anisotropy.samples.bodyCentered import BodyCentered
from anisotropy.samples.faceCentered import FaceCentered

View File

@ -1,4 +1,5 @@
from math import pi, sqrt
from anisotropy.salomepl import geometry
class BodyCentered(object):
def __init__(self, **kwargs):
@ -13,9 +14,8 @@ class BodyCentered(object):
def build(self):
import salomepl
geompy = salomepl.geometry.getGeom()
geompy = geometry.getGeom()
###
# Pore Cell

View File

@ -1,4 +1,5 @@
from math import pi, sqrt
from anisotropy.salomepl import geometry
class FaceCentered(object):
def __init__(self, **kwargs):
@ -13,9 +14,8 @@ class FaceCentered(object):
def build(self):
import salomepl
geompy = salomepl.geometry.getGeom()
geompy = geometry.getGeom()
###
# Pore Cell

View File

@ -1,4 +1,5 @@
from math import pi, sqrt
from anisotropy.salomepl import geometry
class Simple(object):
def __init__(self, **kwargs):
@ -13,9 +14,8 @@ class Simple(object):
def build(self):
import salomepl
geompy = salomepl.geometry.getGeom()
geompy = geometry.getGeom()
###
# Pore Cell

0
tests/__init__.py Normal file
View File

View File

@ -26,6 +26,7 @@ class TestAnisotropy(unittest.TestCase):
except Exception as e:
passed = False
print(e)
self.assertTrue(passed)
@ -35,6 +36,10 @@ class TestAnisotropy(unittest.TestCase):
self.assertEqual(self.model.params["structure"]["type"], "simple")
def tearDown(self):
#os.removedirs(self.model.env["BUILD"])
#os.removedirs(self.model.env["LOG"])
pass
if __name__ == "__main__":
unittest.main()