Path: blob/master/elisp/emacs-for-python/rope-dist/rope/base/resources.py
1415 views
import os1import re23import rope.base.change4import rope.base.fscommands5from rope.base import exceptions678class Resource(object):9"""Represents files and folders in a project"""1011def __init__(self, project, path):12self.project = project13self._path = path1415def move(self, new_location):16"""Move resource to `new_location`"""17self._perform_change(rope.base.change.MoveResource(self, new_location),18'Moving <%s> to <%s>' % (self.path, new_location))1920def remove(self):21"""Remove resource from the project"""22self._perform_change(rope.base.change.RemoveResource(self),23'Removing <%s>' % self.path)2425def is_folder(self):26"""Return true if the resource is a folder"""2728def create(self):29"""Create this resource"""3031def exists(self):32return os.path.exists(self.real_path)3334@property35def parent(self):36parent = '/'.join(self.path.split('/')[0:-1])37return self.project.get_folder(parent)3839@property40def path(self):41"""Return the path of this resource relative to the project root4243The path is the list of parent directories separated by '/' followed44by the resource name.45"""46return self._path4748@property49def name(self):50"""Return the name of this resource"""51return self.path.split('/')[-1]5253@property54def real_path(self):55"""Return the file system path of this resource"""56return self.project._get_resource_path(self.path)5758def __eq__(self, obj):59return self.__class__ == obj.__class__ and self.path == obj.path6061def __ne__(self, obj):62return not self.__eq__(obj)6364def __hash__(self):65return hash(self.path)6667def _perform_change(self, change_, description):68changes = rope.base.change.ChangeSet(description)69changes.add_change(change_)70self.project.do(changes)717273class File(Resource):74"""Represents a file"""7576def __init__(self, project, name):77super(File, self).__init__(project, name)7879def read(self):80data = self.read_bytes()81try:82return rope.base.fscommands.file_data_to_unicode(data)83except UnicodeDecodeError, e:84raise exceptions.ModuleDecodeError(self.path, e.reason)8586def read_bytes(self):87return open(self.real_path, 'rb').read()8889def write(self, contents):90try:91if contents == self.read():92return93except IOError:94pass95self._perform_change(rope.base.change.ChangeContents(self, contents),96'Writing file <%s>' % self.path)9798def is_folder(self):99return False100101def create(self):102self.parent.create_file(self.name)103104105class Folder(Resource):106"""Represents a folder"""107108def __init__(self, project, name):109super(Folder, self).__init__(project, name)110111def is_folder(self):112return True113114def get_children(self):115"""Return the children of this folder"""116result = []117for name in os.listdir(self.real_path):118try:119child = self.get_child(name)120except exceptions.ResourceNotFoundError:121continue122if not self.project.is_ignored(child):123result.append(self.get_child(name))124return result125126def create_file(self, file_name):127self._perform_change(128rope.base.change.CreateFile(self, file_name),129'Creating file <%s>' % self._get_child_path(file_name))130return self.get_child(file_name)131132def create_folder(self, folder_name):133self._perform_change(134rope.base.change.CreateFolder(self, folder_name),135'Creating folder <%s>' % self._get_child_path(folder_name))136return self.get_child(folder_name)137138def _get_child_path(self, name):139if self.path:140return self.path + '/' + name141else:142return name143144def get_child(self, name):145return self.project.get_resource(self._get_child_path(name))146147def has_child(self, name):148try:149self.get_child(name)150return True151except exceptions.ResourceNotFoundError:152return False153154def get_files(self):155return [resource for resource in self.get_children()156if not resource.is_folder()]157158def get_folders(self):159return [resource for resource in self.get_children()160if resource.is_folder()]161162def contains(self, resource):163if self == resource:164return False165return self.path == '' or resource.path.startswith(self.path + '/')166167def create(self):168self.parent.create_folder(self.name)169170171class _ResourceMatcher(object):172173def __init__(self):174self.patterns = []175self._compiled_patterns = []176177def set_patterns(self, patterns):178"""Specify which resources to match179180`patterns` is a `list` of `str`\s that can contain ``*`` and181``?`` signs for matching resource names.182183"""184self._compiled_patterns = None185self.patterns = patterns186187def _add_pattern(self, pattern):188re_pattern = pattern.replace('.', '\\.').\189replace('*', '[^/]*').replace('?', '[^/]').\190replace('//', '/(.*/)?')191re_pattern = '^(.*/)?' + re_pattern + '(/.*)?$'192self.compiled_patterns.append(re.compile(re_pattern))193194def does_match(self, resource):195for pattern in self.compiled_patterns:196if pattern.match(resource.path):197return True198path = os.path.join(resource.project.address,199*resource.path.split('/'))200if os.path.islink(path):201return True202return False203204@property205def compiled_patterns(self):206if self._compiled_patterns is None:207self._compiled_patterns = []208for pattern in self.patterns:209self._add_pattern(pattern)210return self._compiled_patterns211212213