def make_post(file_name, post, output_dir=default_out_dir): """ prepare and save the post file. :param file_name: source code file :param post: the post object :param output_dir: directory to create the post file into :return: """ # Get the file name except the file path # e.g. /dir/test/file.java --> file.java base_name = os.path.basename(file_name) # Split the file name base_name into a pair (root, ext) name = os.path.splitext(base_name)[0] code_parser = CodeParser() # get the source code as text from the input file code = code_parser.get_code(file_name) # Set the output file name into current post object post.post_file = output_dir + name with open(post.post_file, 'wb') as outf: outf.write("<b>Title: </b>" + post.title) outf.write(" <b>Source: </b><a target='_blank' href='" + post.link + "'>" + post.host_name + "</a>") outf.write(str(post.problem_text)) outf.write('\n\n') if '.java' in file_name: outf.write("<h3>Java solution</h3>") outf.write('<pre class="lang:java decode:true ">') elif '.py' in file_name: outf.write("<h3>Python solution</h3>") outf.write('<pre class="lang:python decode:true ">') outf.write(code) outf.write('</pre>')
def append_to_post(post_file, code_file): """ Method to append code to existing post-file :param post_file: :param code_file: :return: """ code_parser = CodeParser() code = code_parser.get_code(code_file) with open(post_file, 'a') as outf, open(code_file, 'r') as inf: outf.write('<br><br>') if '.java' in code_file: outf.write("<h3>Java solution</h3>") outf.write('<pre class="lang:java decode:true ">') elif '.py' in code_file: outf.write("<h3>Python solution</h3>") outf.write('<pre class="lang:python decode:true ">') outf.write(code) outf.write('</pre>')
def process_file(file_name, out_dir): """ :param file_name: fully qualified name of the file to be processed :param out_dir: Output directory to put the post in. :return: None """ code_parser = CodeParser() link = code_parser.get_page_link(file_name) if not link: print "I can't find any link in the given file" return # link = 'https://www.leetcode.com/problems/add-digits/' # initialize the post post = Post(link=link, file_name=file_name) # Object of Uniqueness checker verify_unique = VerifyUnique(config) # Check for post uniqueness for current file status = verify_unique.check_post_exists(post) if status is verify_unique.NON_UNIQUE: print 'post file already exists' elif status is verify_unique.NEW_CODE: # Old post file already exists. # Append the current code to existing post file. append_to_post(post.post_file, file_name) print "updated already existing post file for: " + file_name else: # Object to fetch the web page fetcher = Fetcher(logging.DEBUG) # Get the host and web page from the link try: host, response = fetcher.fetch(link.strip()) except Exception as e: print "Exception while fetching for link: ", link print "Exception: ", e return # print host # print response # update the post with host_name and # raw_page response values post.host_name = host post.raw_page = response # Get the appropriate problem description try: problem_desc_tag = WebParser.parse_page(post) except Exception, e: print "Exception in parsing page: ", e problem_desc_tag = None # print 'title: ' + post.title # print problem_desc_tag if not problem_desc_tag: print "could not parse the file " return # update the post object with problem description post.problem_text = problem_desc_tag # Save the post file print "making the post" try: make_post(file_name, post, out_dir) verify_unique.serialize_post(post) except Exception, e: print "could not make the post file", e return