Ejemplo n.º 1
0
def main(argv=None, logger=None):
  if(logger is None):
    logger=createLog(logname="subtitle",level=logging.INFO)

  fname=None

  startDtime=datetime.now()
  print "Start time: "+str(startDtime)#.strftime("%Y-%m-%d %H:%M:%S"))
  print
  #sub=Subtitle(logging.getLogger())
  sub=Subtitle(logger)

  try:
    opts, args=getopt.getopt(
      argv, 
      "hvf:w:t:d:p:?lm:WDc", 
      ["help", "version", "checkup" "file=","word=","type=","dir=","pickle=","limit="])
    #print opts, args
    logger.info("opts:{0};args:{1}".format(opts, args))
  except getopt.GetoptError as msg:
    print "error happened when get options!!! error:{0}".format(msg)
    usage()
    logger.error("getopt.GetoptError:{0}, exit!".format(msg))
    sys.exit(2)
  except Exception as msg:
    logger.error("error:{0}, exit!".format(msg))
    sys.exit(2)

  _is_lines_show=False
  _is_words_show=False
  sub_type = ""
  words_limit=None
  for opt, arg in opts:
    if(opt in ("-?","-h", "--help")):
      usage()
      sys.exit()
      pass
    elif(opt in ("-v", "--version")):
      version()
      sys.exit()
      pass
    elif(opt in ("-c", "--checkup")):
      sub.checkup=True
      pass
    elif(opt in ("-d", "--dir")):
      print "Sorry, -d --dir option still not offer"
      sys.exit()
      pass
    elif(opt in ("-p", "--pickle")):
      pkl=arg
      sub.setLexiconFile(pkl)
      pass
    elif(opt in ('-f',"--file")):
      fname= arg
      sub.addFile(fname)
      pass
    elif(opt == '-D'):
      logger.setLevel(logging.DEBUG)
      sub.setLogger(logger)
      pass
    elif(opt in ("-w", "--word")):
      word = arg
      sub.addWord(word)
      pass
    elif(opt in ("-t","--type")):
      sub_type = arg
      if(sub_type not in ('word', 'scan')):
        usage()
        sys.exit()
        pass
      pass
    elif(opt in ("-m","--limit")):
      words_limit= int(arg)
      #print words_limit
      _is_words_show=True
      pass
    elif(opt == '-l'):
      #show lines
      _is_lines_show=True
      pass
    elif(opt == '-W'):
      #show words
      _is_words_show=True
      pass

  """
  if(len(sys.argv)<2):
    print "need args!!"
    logger.error("need args!!sys.argv:{0}".format(sys.argv))
    return None
    pass
  """
  #print sys.argv

  #sub.addPunctuation([',','!',';','.',':','>','<'])
  #sub.addLexicon(["hello", "world"])

  if sub.lexicon_path is None:
    sub.setLexiconFile("lexicon.pickle")
  sub.loadOldData()

  sub.addFiles(args)
  #sub.addStrings("hello world, I'm wang. Please call me wang.")

  sub.parse()

  if(_is_lines_show):
    sub.lines_show()
    pass

  if(_is_words_show):
    #print words_limit
    sub.words_show(words_limit)
    pass
  sub.show()

  if(sub_type =='word'):
    sub.dumpData()

  print 
  endDtime = datetime.now()
  print "End time: "+str(endDtime)
  timedelta  = endDtime-startDtime
  print "Cost time: "+str(timedelta) 

  #getChecksum(sys.argv[1])
  pass
Ejemplo n.º 2
0
def main(argv=None, log_ger=None):
    if log_ger is None:
        log_ger = create_log(log_name="subtitle", level=logging.INFO)

    fname = None

    start_dtime = datetime.now()
    # print("Start time: "+str(start_dtime))#.strftime("%Y-%m-%d %H:%M:%S"))
    print()
    # sub=Subtitle(logging.getLogger())
    sub = Subtitle(log_ger)

    try:
        opts, args = getopt.getopt(
            argv,
            "hvf:w:t:d:e:p:s:b:?lm:WDc",
            ["help", "version", "parse",
             "checkup" "file=", "word=",
             "type=", "dir=", "pickle=",
             "limit=", "section=", "bigger="])
        # print opts, args
        log_ger.info("opts:{0};args:{1}".format(opts, args))
    except getopt.GetoptError as msg:
        print("error happened when get options!!! error:{0}".format(msg))
        usage()
        log_ger.error("getopt.GetoptError:{0}, exit!".format(msg))
        sys.exit(2)
    except Exception as msg:
        log_ger.error("error:{0}, exit!".format(msg))
        sys.exit(2)

    _is_lines_show = False
    _is_words_show = False
    sub_type = ""
    words_limit = None
    for opt, arg in opts:
        if opt in ("-?", "-h", "--help"):
            usage()
            sys.exit()
            pass
        elif opt in ("-v", "--version"):
            version()
            sys.exit()
            pass
        elif opt in ("-b", "--bigger"):
            sub.set_times_bigger(int(arg))
            pass
        elif opt in ("-c", "--checkup"):
            sub.checkup = True
            pass
        elif opt in ("-d", "--dir"):
            print("Sorry, -d --dir option still not offer")
            sys.exit()
            pass
        elif opt in ("-e", "--excel"):
            pkl = arg
            sub.set_lexicon_file(pkl)
            pass
        elif opt in ("-s", "--section"):
            if ',' in arg:
                section = arg.split(',')
                if len(section) == 2:
                    # print(section)
                    start, end = section
                    if len(start) != 0:
                        sub.set_start(int(start))
                    if len(end) != 0:
                        sub.set_end(int(end))
                    # print(start, end)
                else:
                    print("something wrong, with option -s --section:", arg)
                    sys.exit()
            else:
                print("something wrong, with option -s --section:", arg)
                sys.exit()

            pass
        elif opt in ('-f', "--file"):
            fname = arg
            sub.add_file(fname)
            pass
        elif opt in ('-p', "--parse"):
            sub.set_parse(True)
            pass

        elif opt == '-D':
            log_ger.setLevel(logging.DEBUG)
            sub.set_logger(log_ger)
            sub.set_debug(True)
            pass
        elif opt in ("-w", "--word"):
            word = arg
            sub.add_word(word)
            # 多用于测试,放弃写入
            sub.set_output(False)
            pass
        elif opt in ("-t", "--type"):
            sub_type = arg
            if sub_type not in ('save', 'scan', 'cloud'):
                usage()
                sys.exit()
                pass
            pass
        elif opt in ("-m", "--limit"):
            words_limit = int(arg)
            # print words_limit
            _is_words_show = True
            pass
        elif opt == '-l':
            # show lines
            _is_lines_show = True
            pass
        elif opt == '-W':
            # show words
            _is_words_show = True
            pass

    """
    if(len(sys.argv)<2):
      print "need args!!"
      log_ger.error("need args!!sys.argv:{0}".format(sys.argv))
      return None
      pass
    """
    # print sys.argv

    # sub.add_punctuation([',','!',';','.',':','>','<'])
    # sub.addLexicon(["hello", "world"])

    if sub.lexicon_path is None:
        sub.set_lexicon_file("lexicon/lexicon.xlsx")
    sub.load_old_data()

    sub.add_files(args)
    # sub.add_strings("hello world, I'm wang. Please call me wang.")

    sub.check_all(encode='utf-8')

    if _is_lines_show:
        sub.lines_show(words_limit)
        pass

    if _is_words_show:
        # print words_limit
        sub.words_show(words_limit)
        pass
    sub.show()

    if sub_type == 'save':
        sub.dump_data()
    elif sub_type == 'cloud':
        sub.cloud()

    print()
    end_dtime = datetime.now()
    # print("End time: "+str(end_dtime))
    timedelta = end_dtime - start_dtime
    print("Cost time: " + str(timedelta))

    # getChecksum(sys.argv[1])
    pass