Difficult-Rocket/bin/configs.py

101 lines
2.7 KiB
Python
Raw Normal View History

2020-12-31 23:13:11 +08:00
"""
writen by shenjackyuanjie
mail: 3695888@qq.com
"""
2021-02-02 16:45:20 +08:00
# import re
2021-02-06 16:41:54 +08:00
import os
2021-01-26 08:06:36 +08:00
import time
2021-01-27 10:32:25 +08:00
import json5
2021-01-26 08:06:36 +08:00
import decimal
2021-02-06 16:41:54 +08:00
import logging
2021-02-04 19:05:09 +08:00
try:
import tools
except ModuleNotFoundError:
from bin import tools
2021-01-25 19:23:16 +08:00
2021-02-06 16:41:54 +08:00
# logger
configs_logger = logging.getLogger('configs')
2021-02-07 13:50:19 +08:00
def _BasicNumber(int_num=0, float_num=1, unit1=None, unit2=None) -> list:
2021-01-25 19:23:16 +08:00
if unit1 is None:
unit1 = []
2021-01-26 08:06:36 +08:00
if unit2 is None:
unit2 = []
2021-02-04 19:05:09 +08:00
if tools.is_decimal(float_num): # is decimal class?
2021-01-27 10:32:25 +08:00
return [int_num, float_num, unit1, unit2] # is just return
2021-01-26 08:06:36 +08:00
else:
2021-01-27 10:32:25 +08:00
return [int_num, decimal.Decimal(str(float_num)), unit1, unit2] # no create a decimal class
2020-12-31 23:13:11 +08:00
2021-02-07 13:50:19 +08:00
def BasicNumber(int_num=0, float_num=1, unit1=None, unit2=None, num=1) -> list:
2021-02-02 16:45:20 +08:00
numbers = []
if num > 1:
for x in range(0, num, 1):
2021-02-07 13:50:19 +08:00
numbers.append(_BasicNumber(int_num, float_num, unit1, unit2))
2021-02-02 16:45:20 +08:00
elif num == 1:
2021-02-07 13:50:19 +08:00
return _BasicNumber(int_num, float_num, unit1, unit2)
2021-02-02 16:45:20 +08:00
else: # num < 1
raise TypeError('you should give me a num with >= 1!')
return numbers
2021-02-07 13:50:19 +08:00
class BasicNumberClass:
def __init__(self, int_num=0, float_num=1, unit1=None, unit2=None):
self.int = int_num
self.float = decimal.Decimal(str(float_num))
if unit1:
self.units1 = unit1
else:
self.units1 = []
if unit2:
self.units2 = unit2
else:
self.units2 = []
def __str__(self):
return None
def __add__(self, other):
pass
2021-02-03 22:04:34 +08:00
def basic_poi(poi_type=None) -> list:
if poi_type is None:
2021-02-07 13:50:19 +08:00
return BasicNumber(unit1='m', num=2)
2021-02-06 16:41:54 +08:00
if poi_type == 'chunk':
2021-02-07 13:50:19 +08:00
return [BasicNumber(unit1='chunk', num=2), BasicNumber(unit1='m', num=2)]
2021-02-02 16:45:20 +08:00
2021-01-25 19:23:16 +08:00
def basic_force() -> list:
2021-02-07 13:50:19 +08:00
return BasicNumber(unit1='N', num=2)
2021-01-01 11:02:44 +08:00
2021-01-27 10:32:25 +08:00
def configs(name, option=None) -> dict:
with open(name, 'r') as file:
data = json5.load(file)
if option:
try:
data = data[option]
2021-02-06 16:41:54 +08:00
except IndexError as exp:
2021-02-07 13:50:19 +08:00
log = 'can\'t find stack named %s in file %s' % (option, name)
configs_logger.exception(log)
raise IndexError(log)
2021-02-02 16:45:20 +08:00
return data
2021-01-25 19:23:16 +08:00
2021-02-06 16:41:54 +08:00
def name_handler(name: str, configs=None) -> str:
2021-01-26 08:06:36 +08:00
if configs is None:
return name
for need_replace in configs:
2021-02-06 16:41:54 +08:00
replace = configs[need_replace]
if need_replace == '{date}': # special replaces
2021-01-26 08:06:36 +08:00
replace = time.strftime(configs[need_replace], time.gmtime(time.time()))
2021-02-06 16:41:54 +08:00
elif need_replace == '{time.time}':
replace = time.time()
elif need_replace == '{dir}':
replace = os.getcwd()
name.replace(need_replace, replace)