Example #1
0
def main():
     parser = argparse.ArgumentParser()
     parser.add_argument('--socket', metavar='SOCKET', type=str,
                         help='the local socket to bind to')
     parser.add_argument('--infile', metavar='FILE', type=str,
                         help='read from this file instead of stdin')
     parser.add_argument('--outfile', metavar='FILE', type=str,
                         help='write to this file instead of stdout')
     args = parser.parse_args()

     if args.socket:
          ok = scipherd.call_daemon(args.socket, False,
                                    args.infile, args.outfile)
          if ok:
               sys.exit(0)
          else:
               sys.exit(-1)

     # search until blank line:
     header = ""
     header_lines = []
     for line in sys.stdin:
          line = line.rstrip()
          # we hit a blank line, and we have at least one line already
          if not line and len(header_lines) > 0:
               break
          header_lines.append(line)
          header = " ".join(header_lines)

     (conf_name, mask, version, ls_len) = decode_conf_name(header)
     common = cfp_common.CfpCommon.get_common_for_version(version)
     if common is None:
          sys.stderr.write("Unrecognized version: %s\n" % version)
          sys.exit(-1)

     body_text = ""
     for line in sys.stdin:
          body_text += line

     header_grammar = load_and_norm_grammar(common.header_cfg_filename())
     body_grammar = load_and_norm_grammar(common.body_cfg_filename())
     space_before = re.compile('([%s])' %
                               common.chars_to_remove_a_space_before())
     space_after = re.compile('([%s])' % common.chars_to_remove_a_space_after())
     state = DecodeState(common, conf_name, mask, header_grammar, body_grammar,
                         {}, space_before, space_after, Done())

     print decode(header, body_text, state, ls_len),
Example #2
0
def main():
     parser = argparse.ArgumentParser()
     parser.add_argument('--seed', metavar='S', type=int,
                         help='the random number generator seed')
     parser.add_argument('--socket', metavar='SOCKET', type=str,
                         help='the local socket to bind to')
     parser.add_argument('--infile', metavar='FILE', type=str,
                         help='read from this file instead of stdin')
     parser.add_argument('--outfile', metavar='FILE', type=str,
                         help='write to this file instead of stdout')
     parser.add_argument('--website', metavar='W', type=str,
                         help='a website link to include, if any '
                         '(must start with "http://")')
     args = parser.parse_args()

     if args.socket:
          ok = scipherd.call_daemon(args.socket, True,
                                    args.infile, args.outfile)
          if ok:
               sys.exit(0)
          else:
               sys.exit(-1)

     if args.seed:
          seed = args.seed
     else:
          seed = random.randint(0, 2**32)
     random.seed(seed)
     sys.stderr.write("Random seed: %d\n" % seed)

     input_text = ""
     for line in sys.stdin:
          input_text += line.decode('utf-8')

     if len(input_text) > 2**20:
          print "Input text must be smaller than 1MB."
          sys.exit(-1)

     common = cfp_common.CfpCommon.get_latest_common()
     space_before = re.compile('\s([%s])' %
                               common.chars_to_remove_a_space_before())
     space_after = re.compile('([%s])\s' %
                              common.chars_to_remove_a_space_after())

     last_or_nots = common.choose_last_or_nots()
     if args.website:
          if args.website.find("http://") != 0:
               sys.stderr.write("Bad website: %s\n" % args.website)
               sys.exit(-1)
          last_or_nots[nltk.Nonterminal("SUBMIT_CLOSING")] = True

     # load grammars
     #print "1) %s" % time.time()
     header_grammar = nltk.data.load("file:%s" % common.header_cfg_filename(),
                                     'cfg')
     body_grammar = nltk.data.load("file:%s" % common.body_cfg_filename(),
                                   'cfg')
     #print "2) %s" % time.time()
     state = EncodeState(input_text, Bitstring(), common, header_grammar,
                         body_grammar, {}, space_before, space_after,
                         last_or_nots, LastTime())
     (header, body) = do_encode(state, args.website)
     print header
     print ""
     print body