Skip to content

Commit 029a799

Browse files
author
Jirapongse Phuriphanvichai
authored
Add files via upload
1 parent faaadab commit 029a799

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

OpenPermID/OpenPermID.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,37 @@ class OpenPermID(object):
1717
__timeout__ = 30
1818
__response__ = None
1919
__quota__ = []
20-
MAX_LOG_SIZE = 10000000
21-
20+
__max_log_size__ = 10000000
21+
__backupCount__ = 10
22+
__log_file_name__ = "openpermid"
23+
__log_path__ = None
24+
__log_format__ = "%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s \n"
2225

2326
def __init__(self):
2427
self.__access_token__=""
2528
self.log_path = None
2629
self.log_level = logging.NOTSET
2730
self.logger = logging.getLogger('openpermid')
2831

32+
def set_log_format(self, format):
33+
self.__log_format__ = format
34+
35+
def set_log_max_size(self, size):
36+
self.__max_log_size__ = size
37+
38+
def set_log_path(self, log_path):
39+
if os.access(log_path, os.W_OK):
40+
self.__log_path__ = log_path
41+
return True
42+
else:
43+
return False
44+
45+
def set_log_file_name(self, name):
46+
self.__log_file_name__ = name
47+
48+
def set_log_backup_count(self, count):
49+
self.__backupCount__ = count
50+
2951
def set_lookup_url(self, url):
3052
self.__lookupurl__ = url
3153

@@ -44,12 +66,7 @@ def set_calais_url(self, url):
4466
def set_timeout(self, timeout):
4567
self.__timeout__ = timeout
4668

47-
def set_log_path(self, log_path):
48-
if os.access(log_path, os.W_OK):
49-
self.log_path = log_path
50-
return True
51-
else:
52-
return False
69+
5370
def get_response(self):
5471
return self.__response__
5572

@@ -58,24 +75,20 @@ def get_usage(self):
5875
return pd.DataFrame([{'Time':'None', 'Quota Daily':'None', 'Quota Used':'None'}])
5976

6077
return pd.DataFrame(self.__quota__)
61-
#def get_quota_daily(self):
62-
# return self.__quotaDaily__
63-
64-
#def get_quota_used(self):
65-
# return self.__quotaUsed__
6678

6779
def set_log_level(self, log_level):
6880
if log_level > logging.NOTSET:
69-
__formatter = logging.Formatter("%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s \n")
70-
__filename = 'openpermid.{}.log'.format(datetime.now().strftime('%Y%m%d.%H-%M-%S'))
71-
72-
if self.log_path is not None:
73-
if not os.path.isdir(self.log_path):
74-
os.makedirs(self.log_path)
75-
__filename = os.path.join(self.log_path, __filename)
81+
__formatter = logging.Formatter(self.__log_format__)
82+
__filename = '{0}_{1}.{2}.log'.format(self.__log_file_name__, os.getpid(), datetime.now().strftime('%Y%m%d.%H-%M-%S'))
83+
84+
if self.__log_path__ is not None:
85+
if not os.path.isdir(self.__log_path__):
86+
os.makedirs(self.__log_path__)
87+
__filename = os.path.join(self.__log_path__, __filename)
7688

77-
__handler = logging.handlers.RotatingFileHandler(__filename, mode='a', maxBytes=self.MAX_LOG_SIZE,
78-
backupCount=10, encoding='utf-8')
89+
__handler = logging.handlers.RotatingFileHandler(__filename, mode='a', maxBytes=self.__max_log_size__,
90+
backupCount=self.__backupCount__, encoding='utf-8')
91+
7992
__handler.setFormatter(__formatter)
8093
self.logger.addHandler(__handler)
8194

@@ -213,9 +226,9 @@ def matchFile(self,
213226
self.logger.error('%s doesn\'t exist.', filename)
214227
return None,"File doesn't exist."
215228
_headers={}
216-
if(dataType not in ['Organization','Person','Instruments','Quotes']):
229+
if(dataType not in ['Organization','Person','Instrument','Quotation']):
217230
self.logger.error('Invalid dataType for match: %s', dataType)
218-
return None,"The valid dataTypes are 'Organization','Person','Instruments',and 'Quotes'."
231+
return None,"The valid dataTypes are 'Organization','Person','Instrument',and 'Quotation'."
219232

220233
if 1 < numberOfMatchesPerRecord > 5:
221234
self.logger.error('Invalid numberOfMatchesPerRecord for match: %s', numberOfMatchesPerRecord)
@@ -251,9 +264,9 @@ def match(self,
251264
_headers={}
252265

253266

254-
if(dataType not in ['Organization','Person','Instruments','Quotes']):
267+
if(dataType not in ['Organization','Person','Instrument','Quotation']):
255268
self.logger.error('Invalid dataType for match: %s', dataType)
256-
return None,"The valid dataTypes are 'Organization','Person','Instruments',and 'Quotes'."
269+
return None,"The valid dataTypes are 'Organization','Person','Instrument',and 'Quotation'."
257270

258271
if 1 < numberOfMatchesPerRecord > 5:
259272
self.logger.error('Invalid numberOfMatchesPerRecord for match: %s', numberOfMatchesPerRecord)
@@ -343,11 +356,10 @@ def search(self,
343356
if(format!='dataframe'):
344357
return resp, None
345358

346-
#Format the dataframe and return the dataframe to the caller
347-
#1. Remove '@context' from the json-ld
359+
348360
jsonObj=json.loads(resp)
349361

350-
#2. Format the dataframe according to the orient parameter
362+
351363
if(entityType=='all'):
352364
dfDict={}
353365
for (attribute, value) in jsonObj['result'].items():
@@ -421,9 +433,9 @@ def calais(self,
421433
_headers={}
422434

423435

424-
if(language not in ['English','Chinese','French','German','Japanese','Spanish']):
425-
self.logger.error('Invalid language for calais: %s', language)
426-
return None,"The valid languages are 'English','Chinese','French','German','Japanese', and 'Spanish'."
436+
#if(language not in ['English','Chinese','French','German','Japanese','Spanish']):
437+
# self.logger.error('Invalid language for calais: %s', language)
438+
# return None,"The valid languages are 'English','Chinese','French','German','Japanese', and 'Spanish'."
427439

428440
if(contentType not in ['raw','html','xml','pdf']):
429441
#if(contentType not in ['raw','html','xml']):

0 commit comments

Comments
 (0)