def sanitize_foldername(name, limit=True): """ Return foldername with dodgy chars converted to safe ones Remove any leading and trailing dot and space characters """ if not name: return name FL_ILLEGAL = CH_ILLEGAL + ':\x92"' FL_LEGAL = CH_LEGAL + "-''" uFL_ILLEGAL = FL_ILLEGAL.decode('cp1252') uFL_LEGAL = FL_LEGAL.decode('cp1252') if isinstance(name, unicode): illegal = uFL_ILLEGAL legal = uFL_LEGAL else: illegal = FL_ILLEGAL legal = FL_LEGAL if cfg.sanitize_safe(): # Remove all bad Windows chars too illegal += r'\/<>?*|":' legal += r'++{}!@#`;' repl = cfg.replace_illegal() lst = [] for ch in name.strip(): if ch in illegal: if repl: ch = legal[illegal.find(ch)] lst.append(ch) else: lst.append(ch) name = ''.join(lst) name = name.strip() if sabnzbd.WIN32 or cfg.sanitize_safe(): name = replace_win_devices(name) maxlen = cfg.folder_max_length() if limit and len(name) > maxlen: # Folders can't end on a dot in Windows name = name[:maxlen].strip('.') # And finally, make sure it doesn't end in a dot if name != '.' and name != '..': name = name.rstrip('.') if not name: name = 'unknown' return name
def sanitize_foldername(name, limit=True): """ Return foldername with dodgy chars converted to safe ones Remove any leading and trailing dot and space characters """ if not name: return name FL_ILLEGAL = CH_ILLEGAL + ':\x92"' FL_LEGAL = CH_LEGAL + "-''" uFL_ILLEGAL = FL_ILLEGAL.decode('cp1252') uFL_LEGAL = FL_LEGAL.decode('cp1252') if isinstance(name, unicode): illegal = uFL_ILLEGAL legal = uFL_LEGAL else: illegal = FL_ILLEGAL legal = FL_LEGAL if cfg.sanitize_safe(): # Remove all bad Windows chars too illegal += r'\/<>?*|":' legal += r'++{}!@#`;' repl = cfg.replace_illegal() lst = [] for ch in name.strip(): if ch in illegal: if repl: ch = legal[illegal.find(ch)] lst.append(ch) else: lst.append(ch) name = ''.join(lst) name = name.strip() if sabnzbd.WIN32 or cfg.sanitize_safe(): name = replace_win_devices(name) maxlen = cfg.folder_max_length() if limit and len(name) > maxlen: name = name[:maxlen] # And finally, make sure it doesn't end in a dot if name != '.' and name != '..': name = name.rstrip('.') if not name: name = 'unknown' return name
def sanitize_foldername(name): """ Return foldername with dodgy chars converted to safe ones Remove any leading and trailing dot and space characters """ if not name: return name if isinstance(name, unicode): illegal = uFL_ILLEGAL legal = uFL_LEGAL else: illegal = FL_ILLEGAL legal = FL_LEGAL if cfg.sanitize_safe(): # Remove all bad Windows chars too illegal += r'\/<>?*|"' legal += r'++{}!@#`' repl = cfg.replace_illegal() lst = [] for ch in name.strip(): if ch in illegal: if repl: ch = legal[illegal.find(ch)] lst.append(ch) else: lst.append(ch) name = ''.join(lst) name = name.strip('. ') if not name: name = 'unknown' maxlen = cfg.folder_max_length() if len(name) > maxlen: name = name[:maxlen] return name