def _regexp(expr, item): if expr is None: return False # only search on basenames p = path_obj(item) item = p.basename() return re.search(expr, item, re.UNICODE | re.I) is not None
def get_location(self): file_loc = self.file_location if file_loc.startswith("/"): location = path_obj(file_loc) else: location = config.c.fsroot / file_loc if not location.exists(): location.makedirs() return location
def search_files(self, query, limit=20): """Scans the files for filenames that match the queries.""" match_list = [] # make the query lower case so that the match boosting # in _SearchMatch can use it query = query.lower() escaped_query = [re.escape(char) for char in query] search_re = ".*".join(escaped_query) main_search = re.compile(search_re, re.I) location = self.location files = self._search_cache.lines(retain=False) for f in files: if main_search.search(path_obj(f).basename()): match_list.append(_SearchMatch(query, f)) all_results = [str(match) for match in sorted(match_list)] return all_results[:limit]
def __init__(self, project, name): if "../" in name: raise BadValue("Relative directories are not allowed") if not name.endswith("/"): name += "/" # chop off any leading slashes while name and name.startswith("/"): name = name[1:] self.name = name self.location = project.location / name # we can only properly check directory entries as being symlinks # if they don't have the trailing slash, which we ensured is there # a couple lines ago if path_obj(self.location[:-1]).islink(): raise FSException("That path points to a symlink, and symlinks are not supported.")
def __init__(self, project, name): if "../" in name: raise BadValue("Relative directories are not allowed") if not name.endswith("/"): name += "/" # chop off any leading slashes while name and name.startswith("/"): name = name[1:] self.name = name self.location = project.location / name # we can only properly check directory entries as being symlinks # if they don't have the trailing slash, which we ensured is there # a couple lines ago if path_obj(self.location[:-1]).islink(): raise FSException( "That path points to a symlink, and symlinks are not supported." )
def install_template(self, template="template", other_vars=None): """Installs a set of template files into a new project. The template directory will be found by searching config.c.template_path to find a matching directory name. The files inside of that directory will be installed into the project, with the common root for the files chopped off. Additionally, filenames containing { will be treated as JSON Template templates and the contents of files will be treated as JSON Template templates. This means that the filenames can contain variables that are substituted when the template is installed into the user's project. The contents of the files can also have variables and small amounts of logic. These JSON Template templates automatically have the following variables: * project: the project name * username: the project owner's username * filename: the name of the file being generated You can pass in a dictionary for other_vars and those values will also be available in the templates. """ log.debug("Installing template %s for user %s as project %s", template, self.owner, self.name) if "/" in template or "." in template: raise BadValue("Template names cannot include '/' or '.'") found = False for p in config.c.template_path: source_dir = path_obj(p) / template if source_dir.isdir(): found = True break if not found: raise FSException("Unknown project template: %s" % template) if other_vars is not None: variables = LenientUndefinedDict(other_vars) else: variables = LenientUndefinedDict() variables["project"] = self.name variables["username"] = self.owner.username common_path_len = len(source_dir) + 1 for dirpath, dirnames, filenames in os.walk(source_dir): destdir = dirpath[common_path_len:] if ".svn" in destdir: continue for f in filenames: if "{" in f: dest_f = jsontemplate.expand(f, variables) else: dest_f = f if destdir: destpath = "%s/%s" % (destdir, dest_f) else: destpath = dest_f contents = open(os.path.join(dirpath, f)).read() variables["filename"] = dest_f contents = jsontemplate.expand(contents, variables) self.save_file(destpath, contents)
def install_template(self, template="template", other_vars=None): """Installs a set of template files into a new project. The template directory will be found by searching config.c.template_path to find a matching directory name. The files inside of that directory will be installed into the project, with the common root for the files chopped off. Additionally, filenames containing { will be treated as JSON Template templates and the contents of files will be treated as JSON Template templates. This means that the filenames can contain variables that are substituted when the template is installed into the user's project. The contents of the files can also have variables and small amounts of logic. These JSON Template templates automatically have the following variables: * project: the project name * username: the project owner's username * filename: the name of the file being generated You can pass in a dictionary for other_vars and those values will also be available in the templates. """ log.debug("Installing template %s for user %s as project %s", template, self.owner, self.name) if "/" in template or "." in template: raise BadValue("Template names cannot include '/' or '.'") found = False for p in config.c.template_path: source_dir = path_obj(p) / template if source_dir.isdir(): found = True break if not found: raise FSException("Unknown project template: %s" % template) if other_vars is not None: variables = LenientUndefinedDict(other_vars) else: variables = LenientUndefinedDict() variables['project'] = self.name variables['username'] = self.owner.username common_path_len = len(source_dir) + 1 for dirpath, dirnames, filenames in os.walk(source_dir): destdir = dirpath[common_path_len:] if '.svn' in destdir: continue for f in filenames: if "{" in f: dest_f = jsontemplate.expand(f, variables) else: dest_f = f if destdir: destpath = "%s/%s" % (destdir, dest_f) else: destpath = dest_f contents = open(os.path.join(dirpath, f)).read() variables['filename'] = dest_f contents = jsontemplate.expand(contents, variables) self.save_file(destpath, contents)
def install_template(self, template="template", other_vars=None): """Installs a set of template files into a new project. The template directory will be found by searching config.c.template_path to find a matching directory name. The files inside of that directory will be installed into the project, with the common root for the files chopped off. Additionally, filenames containing { will be treated as Jinja2 templates and the contents of files will be treated as Jinja2 templates. This means that the filenames can contain variables that are substituted when the template is installed into the user's project. The contents of the files can also have variables and small amounts of logic. These Jinja2 templates automatically have "project" as a variable (you might often want to use project.name). You can pass in a dictionary for other_vars and those values will also be available in the templates. """ log.debug("Installing template %s for user %s as project %s", template, self.owner, self.name) if "/" in template or "." in template: raise BadValue("Template names cannot include '/' or '.'") found = False for p in config.c.template_path: source_dir = path_obj(p) / template if source_dir.isdir(): found = True break if not found: raise FSException("Unknown project template: %s" % template) env = jinja2.Environment() if other_vars is not None: variables = other_vars else: variables = {} variables['project'] = self common_path_len = len(source_dir) + 1 for dirpath, dirnames, filenames in os.walk(source_dir): destdir = dirpath[common_path_len:] if '.svn' in destdir: continue for f in filenames: if "{" in f: temp = env.from_string(f) dest_f = temp.render(variables) else: dest_f = f if destdir: destpath = "%s/%s" % (destdir, dest_f) else: destpath = dest_f contents = open(os.path.join(dirpath, f)).read() temp = env.from_string(contents) contents = temp.render(variables) self.save_file(destpath, contents)