配置文件Config.py格式如下:
numNature = 9 # Integer
# NOTE!
padding = True # BOOLEAN
numProduct = None # Integer / None
prefix = "ST" # String / None
numUnit = 6 # Integer / None
paddingForNumUnicite = False # BOOLEAN
numExploitantEmetteur = None # Integer / None
numExploitantDest = None # Integer / None
useLogFileTimeStamp = False # BOOLEAN: Option to set DATE_FABRICATION from last modification date of all the log files inside XML.
#### IF ALL THREE OPTIONS ABOVE ARE SET TO None/False,THEN DATE IS RETRIEVED FROM USER SYSTEM CLOCK NO MATTER WHAT #####
useSystemDateForXMLFileName = False # Operator can choose whether to print the current system date for Xml filename
# If False, we use config, header or log time stamp values for file name instead.
# If system time is used in dateFabrication, it will print the same date,
# regardless of the choice here. Only affects DATE_FABRICATION in xml IF all
# three options above are set to false.
limitXMLSize = True # BOOLEAN: Controls whether the XML number of lines are limited.
# When False, max value does not matter.
# Default/Normal is 'True'
maxXMLSizeInEntries = 100000 # You can adjust this variable to control the maximum number of lines printed
# onto a file. The program will pre-emptively switch to new xml file
# when value exceeds maxXMLSizeInEntries subtracted by last detected line count
# in the latest log file. Only active when limitXMLsize is True.
在test.py中获取配置文件的值:
import re
import struct
import xml.etree.ElementTree as ET
import getopt
import time
import datetime
import fnmatch
import sys
from ctypes import *
import os
#from types import SimpleNamespace #Only for python 3, replace with class SimpleNamespace for python 27
#Python 27 equivalent implementation of SimpleNamespace
class SimpleNamespace:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
keys = sorted(self.__dict__)
items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
return "{}({})".format(type(self).__name__, ", ".join(items))
def __eq__(self, other):
return self.__dict__ == other.__dict__
# 读取配置文件
LotItem_dict = {}
lot_item_globals = {}
exec(compile(open(r'Config.py', "rb").read(), r'Config.py', 'exec'), lot_item_globals, LotItem_dict)
CF = SimpleNamespace(**LotItem_dict)
def main(argv):
print(CF.limitXMLSize); # 可以将配置项作为属性使用
if (CF.limitXMLSize):
print(CF.maxXMLSizeInEntries);
if __name__ == "__main__":
main(sys.argv[1:])
pass