Created
January 11, 2014 21:32
-
-
Save aquinzi/8377186 to your computer and use it in GitHub Desktop.
Delete duplicated nikola meta data in translated posts (must be in nikola's translated paths)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Delete duplicated nikola meta data in translated posts (must be in nikola's translated paths) | |
| import os, sys, codecs | |
| import shutil | |
| # recursive, paths from working dir | |
| FROM_PATH = "" | |
| OUTPUT_PATH = None #if none, replace file | |
| def file_open_save(path, mode, text=False): | |
| if sys.version_info[0] == 3: | |
| if not mode == 'w': | |
| return open(path, mode, encoding='utf-8') | |
| else: | |
| try: | |
| # make dirs in path tree from path + filename | |
| os.makedirs(os.path.dirname(path)) | |
| except OSError: | |
| pass | |
| with open(path, mode, encoding='utf-8') as outputFile: | |
| outputFile.write(text) | |
| else: | |
| if not mode == 'w': | |
| return codecs.open(path, mode, encoding='utf-8') | |
| else: | |
| try: | |
| # make dirs in path tree from path + filename | |
| os.makedirs(os.path.dirname(path)) | |
| except OSError: | |
| pass | |
| with codecs.open(path, mode, encoding='utf-8') as outputFile: | |
| outputFile.write(text) | |
| def find_output_path(path): | |
| if OUTPUT_PATH: | |
| return os.path.join(OUTPUT_PATH, os.path.relpath(path, FROM_PATH)) | |
| else: | |
| return path | |
| if not os.path.exists(FROM_PATH) and not os.path.isdir(FROM_PATH): | |
| print (" Something wrong") | |
| sys.exit(0) | |
| if OUTPUT_PATH and (not os.path.exists(OUTPUT_PATH) or not os.path.isdir(OUTPUT_PATH)): | |
| print (" Output path must be an existing folder") | |
| sys.exit(0) | |
| print (" Starting..") | |
| # full paths | |
| FROM_PATH = os.path.abspath(FROM_PATH) | |
| if OUTPUT_PATH: | |
| OUTPUT_PATH = os.path.abspath(OUTPUT_PATH) | |
| # get filepaths | |
| files_list = list() | |
| for root, subFolders, files in os.walk(FROM_PATH): | |
| for filename in files: | |
| filePath = os.path.join(root, filename) | |
| if os.path.exists(filePath): | |
| files_list.append(filePath) | |
| if len(files_list) == 0: | |
| print (" No files found") | |
| sys.exit(0) | |
| # "root" filename | |
| pieces = files_list[0].split(".") | |
| prev_file = pieces[0] | |
| prev_file_path = files_list[0] | |
| new_path = find_output_path(prev_file_path) | |
| if OUTPUT_PATH: | |
| shutil.copy2(prev_file_path, new_path) | |
| for this_path in files_list[1:]: | |
| if this_path.split(".")[0] == prev_file: | |
| tmp_prev = os.path.relpath(prev_file_path, FROM_PATH) | |
| tmp_this = os.path.relpath(this_path, FROM_PATH) | |
| print (" Translation found: " + tmp_prev + " -> " + tmp_this) | |
| # find default lang meta, that's only what we need | |
| meta_main = list() | |
| with file_open_save(prev_file_path, 'r') as the_text: | |
| for line in the_text: | |
| if line and not line.isspace(): | |
| meta_main.append(line) | |
| else: | |
| break | |
| # modify translated post | |
| with file_open_save(this_path, 'r') as the_text: | |
| file_translaton = the_text.readlines() | |
| meta_trans = list() | |
| for l_number, line in enumerate(file_translaton): | |
| if line and not line.isspace(): | |
| if line not in meta_main: | |
| meta_trans.append(line) | |
| else: | |
| file_new = meta_trans + file_translaton[l_number:] | |
| file_new = "".join(file_new) | |
| break | |
| #save | |
| new_path = find_output_path(this_path) | |
| file_open_save(new_path, 'w', file_new) | |
| else: | |
| prev_file = this_path.split(".")[0] | |
| prev_file_path = this_path | |
| if OUTPUT_PATH: | |
| new_path = find_output_path(this_path) | |
| shutil.copy2(this_path, new_path) | |
| print (" Finished.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment