.. _fbf.utils.name: name ~~~~ .. automodule:: fbf.utils.name :show-inheritance: :members: :undoc-members: CODE ---- :: # fbf/utils/name.py # # """ name related helper functions. google requirements on file names: - It must contain only letters, numbers, _, +, /, $, ., and -. - It must be less than 256 chars. - It must not contain "/./", "/../", or "//". - It must not end in "/". - All spaces must be in the middle of a directory or file name. """ .. _fbf.utils.name_fbf_imports: fbf imports -------------- :: from fbf.lib.datadir import getdatadir from fbf.utils.generic import toenc, fromenc from fbf.lib.errors import NameNotSet .. _fbf.utils.name_basic_imports: basic imports ---------------- :: import string import os import re import logging .. _fbf.utils.name_defines_: defines ---------- :: allowednamechars = string.ascii_letters + string.digits + "-." .. _fbf.utils.name_slugify_function_taken_from_django_(not_used_now): slugify function taken from django (not used now) ---------------------------------------------------- :: def slugify(value): """ Normalizes string, converts to lowercase, removes non-alpha characters, and converts spaces to hyphens. """ import unicodedata value = unicodedata.normalize('NFKD', value) value = str(re.sub('[^\w\s-]', '', value).strip()) return re.sub('[-\s]+', '-', value) .. _fbf.utils.name_stripname_function: stripname function --------------------- :: def stripname(namein, allowed=""): """ strip all not allowed chars from name. """ if not namein: raise NameNotSet(namein) n = namein res = [] allow = allowednamechars + allowed for c in n: if ord(c) < 31: continue elif c in allow: res.append(c) else: res.append("_" + str(ord(c))) return ''.join(res) .. _fbf.utils.name_reversename_function: reversename function ----------------------- :: reversere = re.compile("_(\d\d)") def reversename(namein, allowed=""): """ strip all not allowed chars from name. """ if not namein: raise NameNotSet(namein) n = namein for t in re.findall(reversere, namein): n = n.replace("_"+t, chr(int(t))) return n def stripdatadir(fname): res = [] ddir = getdatadir() for i in fname.split(os.sep): if i in ddir: continue res.append(i) return os.sep.join(res) .. _fbf.utils.name_testname_function: testname function -------------------- :: def testname(name): """ test if name is correct. """ for c in name: if c not in allowednamechars or ord(c) < 31: return False return True def oldname(name): from fbf.lib.datadir import getdatadir if name.startswith("-"): name[0] = "+" name = name.replace("@", "+") if os.path.exists(getdatadir() + os.sep + name): return name name = name.replace("-", "#") name = prevchan.replace("+", "@") if os.path.exists(getdatadir() + os.sep + name): return name return "" .. _fbf.utils.name_old_fbfbot_functions: old fbfbot functions ----------------------- :: oldallowednamechars = string.ascii_letters + string.digits + '_+/$.-' .. _fbf.utils.name_oldstripname_function: oldstripname function ------------------------ :: def oldstripname(namein, allowed=""): """ strip all not allowed chars from name. """ if not namein: raise NameNotSet(namein) n = namein.replace(os.sep, '+') n = n.replace("/", '+') n = n.replace("@", '+') n = n.replace("#", '-') n = n.replace("!", '.') res = [] allow = oldallowednamechars + allowed for c in n: if ord(c) < 31: continue elif c in allow: res.append(c) else: res.append("-" + str(ord(c))) return ''.join(res)