Last active
December 11, 2015 11:48
-
-
Save TomAugspurger/4596126 to your computer and use it in GitHub Desktop.
Processing papers for entry into repo
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
| import os | |
| import re | |
| import sys | |
| import subprocess | |
| import pathlib | |
| class Parser(object): | |
| def __init__(self, path, | |
| repo=pathlib.PosixPath('/Users/tom/Economics/Papers')): | |
| self.repo = repo | |
| self.path = self.path_parse(path) | |
| self.exists = self.check_existance(self.path) | |
| self.is_full = self.check_full(path) | |
| self.check_type(self.path) | |
| self.added = [] | |
| def path_parse(self, path): | |
| """Ensures a common point of entry to the functions. | |
| Returns a pathlib.PosixPath object | |
| """ | |
| if not isinstance(path, pathlib.PosixPath): | |
| path = pathlib.PosixPath(path) | |
| return path | |
| else: | |
| return path | |
| def check_existance(self, path): | |
| if not path.exists(): | |
| raise OSError('The supplied path does not exist.') | |
| else: | |
| return True | |
| def check_type(self, path): | |
| if path.is_dir(): | |
| self.is_dir = True | |
| self.is_file = False | |
| else: | |
| self.is_file = True | |
| self.is_dir = False | |
| def check_full(self, path): | |
| if path.parent().as_posix() in path.as_posix(): | |
| return True | |
| def parser(self, f): | |
| """The parsing logic to find authors and paper name from a file. | |
| f is a full path. | |
| """ | |
| file_name = f.parts[-1] | |
| self.file_name = file_name | |
| r = re.compile(r' \([\d-]{0,4}\)') | |
| sep_authors = re.compile(r' & |, | and') | |
| all_authors, paper = re.split(r, file_name) | |
| paper = paper.lstrip(' - ') | |
| authors = re.split(sep_authors, all_authors) | |
| authors = [author.strip('& ' or 'and ') for author in authors] | |
| self.authors, self.paper = authors, paper | |
| return (authors, paper) | |
| def make_dir(self, authors): | |
| repo = self.repo | |
| for author in authors: | |
| try: | |
| os.mkdir(repo[author].as_posix()) | |
| except OSError: | |
| pass | |
| def copy_and_link(self, authors, f, replace=True): | |
| repo = self.repo | |
| file_name = f.parts[-1] | |
| for author in authors: | |
| if author == authors[0]: | |
| try: | |
| subprocess.call(["cp", f.as_posix(), | |
| repo[author].as_posix()]) | |
| success = True | |
| except: | |
| success = False | |
| else: | |
| subprocess.call(["ln", "-s", | |
| repo[authors[0]][file_name].as_posix(), | |
| repo[author].as_posix()]) | |
| success = True | |
| if replace and author == authors[0] and success: | |
| try: | |
| f.unlink() | |
| subprocess.call(["ln", "-s", | |
| repo[authors[0]][file_name].as_posix(), | |
| f.parts[:-1].as_posix()]) | |
| except: | |
| raise OSError | |
| def main(self, f): | |
| authors, paper = self.parser(f) | |
| self.make_dir(authors) | |
| self.copy_and_link(authors, f) | |
| def run(self): | |
| if self.exists and self.is_full: | |
| if self.is_dir: | |
| for f in self.path: | |
| if f.parts[-1][0] == '.' or f.is_symlink(): | |
| pass | |
| else: | |
| self.main(f) | |
| self.added.append(f) | |
| else: | |
| self.main(self.path) | |
| self.added.append(self.path) | |
| print self.added | |
| if __name__ == "__main__": | |
| p = pathlib.PosixPath(sys.argv[1]) | |
| try: | |
| repo = pathlib.PosixPath(sys.argv[2]) | |
| except: | |
| repo = pathlib.PosixPath('/Users/tom/Economics/Papers') | |
| obj = Parser(p, repo) | |
| obj.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment