# Path: lib/core/data.py

# 
# Path: lib/core/common.py
# def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, unique=False):
#     """
#     @function returns newline delimited items contained inside file
#     """
# 
#     retVal = list() if not unique else OrderedDict()
# 
#     checkFile(filename)
# 
#     try:
#         with open(filename, 'r') as f:
#             for line in (f.readlines() if unicode_ else f.xreadlines()):
#                 # xreadlines doesn't return unicode strings when codecs.open() is used
#                 if commentPrefix and line.find(commentPrefix) != -1:
#                     line = line[:line.find(commentPrefix)]
# 
#                 line = line.strip()
# 
#                 if not unicode_:
#                     try:
#                         line = str.encode(line)
#                     except UnicodeDecodeError:
#                         continue
# 
#                 if line:
#                     if lowercase:
#                         line = line.lower()
# 
#                     if unique and line in retVal:
#                         continue
# 
#                     if unique:
#                         retVal[line] = True
# 
#                     else:
#                         retVal.append(line)
# 
#     except (IOError, OSError, MemoryError), ex:
#         errMsg = "something went wrong while trying "
#         errMsg += "to read the content of file '%s' ('%s')" % (filename, ex)
#         raise ToolkitSystemException(errMsg)
# 
#     return retVal if not unique else retVal.keys()


import random
from lib.core.data import conf, th, paths, logger
from lib.core.common import getFileItems
"""
Functions to get user-agent string
edit by cdxy [i@cdxy.me]
May 9 Mon, 2016

usage:
from lib.util.useragent import *
str1 = get_random_agent()
str2 = firefox()
str3 = iphone()
str4 = google_bot()
...

tips:
init_UAlist(),get_random_agent()
these 2 methods should be called after [path-init-method] in lib.core.common
"""



def _init_UAlist(path):
    infoMsg = "loading HTTP User-Agent header(s) from "
    infoMsg += "file '%s'" % path
    logger.info(infoMsg)

    # TODO 此处 conf.RANDOM_UA 在其他地方暂时没有用到
    conf.RANDOM_UA = True
    th.UA_LIST = getFileItems(path)

    successMsg = "Total: %d" % len(th.UA_LIST)

# Path: lib/core/data.py

# 
# Path: lib/core/common.py
# def getFileItems(filename, commentPrefix='#', unicode_=True, lowercase=False, unique=False):
#     """
#     @function returns newline delimited items contained inside file
#     """
# 
#     retVal = list() if not unique else OrderedDict()
# 
#     checkFile(filename)
# 
#     try:
#         with open(filename, 'r') as f:
#             for line in (f.readlines() if unicode_ else f.xreadlines()):
#                 # xreadlines doesn't return unicode strings when codecs.open() is used
#                 if commentPrefix and line.find(commentPrefix) != -1:
#                     line = line[:line.find(commentPrefix)]
# 
#                 line = line.strip()
# 
#                 if not unicode_:
#                     try:
#                         line = str.encode(line)
#                     except UnicodeDecodeError:
#                         continue
# 
#                 if line:
#                     if lowercase:
#                         line = line.lower()
# 
#                     if unique and line in retVal:
#                         continue
# 
#                     if unique:
#                         retVal[line] = True
# 
#                     else:
#                         retVal.append(line)
# 
#     except (IOError, OSError, MemoryError), ex:
#         errMsg = "something went wrong while trying "
#         errMsg += "to read the content of file '%s' ('%s')" % (filename, ex)
#         raise ToolkitSystemException(errMsg)
# 
#     return retVal if not unique else retVal.keys()


# Path: plugin/useragent.py
import random
from lib.core.data import conf, th, paths, logger
from lib.core.common import getFileItems

"""
Functions to get user-agent string
edit by cdxy [i@cdxy.me]
May 9 Mon, 2016

usage:
from lib.util.useragent import *
str1 = get_random_agent()
str2 = firefox()
str3 = iphone()
str4 = google_bot()
...

tips:
init_UAlist(),get_random_agent()
these 2 methods should be called after [path-init-method] in lib.core.common
"""



def _init_UAlist(path):
    infoMsg = "loading HTTP User-Agent header(s) from "
    infoMsg += "file '%s'" % path
    logger.info(infoMsg)

    # TODO 此处 conf.RANDOM_UA 在其他地方暂时没有用到
    conf.RANDOM_UA = True
    th.UA_LIST = getFileItems(path)

    successMsg = "Total: %d" % len(th.UA_LIST)

    logger.info(successMsg)