def check(this_file, target, title, titledict): """Check if a link's target is like it should be. Return an error message string or "ok". """ if target.startswith(('http://', 'https://')): # We don't need this currently, but checking these links could # be added later. return "ok" path = posixpath.join(posixpath.dirname(this_file), target) path = posixpath.normpath(path) real_path = common.slashfix(path) if not os.path.exists(real_path): return "doesn't exist" if target.endswith('/'): # A directory. if not os.path.isdir(real_path): return "not a directory" else: # A file. if not os.path.isfile(real_path): return "not a file" if title is not None and title not in titledict[path]: return "no title named %s" % title return "ok"
def check(filepath, target): """Check if a link's target is like it should be. Return an error message string or "ok". """ if target.startswith(('http://', 'https://')): # We don't need this currently, but checking these links could # be added later. return "ok" if '#' in target: where = target.index('#') if where == 0: # It's a link to a title in the same file, we need to skip it. return "ok" target = target[:where] path = posixpath.join(posixpath.dirname(filepath), target) realpath = common.slashfix(path) if not os.path.exists(realpath): return "doesn't exist" if target.endswith('/'): # A directory. if os.path.isdir(realpath): return "ok" return "not a directory" else: # A file. if os.path.isfile(realpath): return "ok" return "not a file"
def strip(file): real_file = common.slashfix(file) lines = [] with open(real_file, 'r') as f: for line in f: lines.append(fix(line.rstrip('\n'))) with common.backup(real_file): with open(real_file, 'w') as f: for line in lines: print(line, file=f)
def mkdir_slashfix_open(filename, mode): """Like common.slashfix_open(), but make directories as needed.""" real_filename = common.slashfix(filename) directory = os.path.dirname(real_filename) os.makedirs(directory, exist_ok=True) return open(real_filename, mode)