def delete_buf(self, path, unlink=False): if not utils.is_shared(path): msg.error('Skipping deleting ', path, ' because it is not in shared path ', G.PROJECT_PATH, '.') return if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): # TODO: rexamine this assumption # Don't care about hidden stuff dirnames[:] = [d for d in dirnames if d[0] != '.'] for f in filenames: f_path = os.path.join(dirpath, f) if f[0] == '.': msg.log('Not deleting buf for hidden file ', f_path) else: self.delete_buf(f_path, unlink) return buf_to_delete = self.get_buf_by_path(path) if buf_to_delete is None: msg.error(path, ' is not in this workspace') return msg.log('deleting buffer ', utils.to_rel_path(path)) event = { 'name': 'delete_buf', 'id': buf_to_delete['id'], 'unlink': unlink, } self.send(event)
def summon(self, subl_view): if 'highlight' not in G.PERMS: return buf = get_buf(subl_view) if buf: msg.debug('summoning selection in subl_view ', buf['path'], ', buf id ', buf['id']) c = [[x.a, x.b] for x in subl_view.sel()] if self.joined_workspace: self.send({ 'id': buf['id'], 'name': 'highlight', 'ranges': c, 'summon': True, 'following': False, }) return path = subl_view.file_name() if not utils.is_shared(path): sublime.error_message( 'Can\'t summon because %s is not in shared path %s.' % (path, G.PROJECT_PATH)) return share = sublime.ok_cancel_dialog( 'This file isn\'t shared. Would you like to share it?', 'Share') if share: sel = [[x.a, x.b] for x in subl_view.sel()] self.create_buf_cbs[utils.to_rel_path( path)] = lambda buf_id: send_summon(buf_id, sel) self.upload(path)
def delete_buf(self, path, unlink=False): if not utils.is_shared(path): msg.error('Skipping deleting ', path, ' because it is not in shared path ', G.PROJECT_PATH, '.') return if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): # TODO: rexamine this assumption # Don't care about hidden stuff dirnames[:] = [d for d in dirnames if d[0] != '.'] for f in filenames: f_path = os.path.join(dirpath, f) if f[0] == '.': msg.log('Not deleting buf for hidden file ', f_path) else: self.delete_buf(f_path, unlink) return buf_to_delete = self.get_buf_by_path(path) if buf_to_delete is None: msg.error(path, ' is not in this workspace') return msg.log('deleting buffer ', utils.to_rel_path(path)) event = { 'name': 'delete_buf', 'id': buf_to_delete['id'], 'unlink': unlink, } def done(d): self._on_delete_buf(event) self.send(event, done)
def delete_buf(self, path): if not utils.is_shared(path): msg.error( 'Skipping deleting %s because it is not in shared path %s.' % (path, G.PROJECT_PATH)) return if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): # TODO: rexamine this assumption # Don't care about hidden stuff dirnames[:] = [d for d in dirnames if d[0] != '.'] for f in filenames: f_path = os.path.join(dirpath, f) if f[0] == '.': msg.log('Not deleting buf for hidden file %s' % f_path) else: self.delete_buf(f_path) return buf_to_delete = self.get_buf_by_path(path) if buf_to_delete is None: msg.error('%s is not in this workspace' % path) return msg.log('deleting buffer ', utils.to_rel_path(path)) event = { 'name': 'delete_buf', 'id': buf_to_delete['id'], } G.AGENT.send(event)
def summon(self, subl_view): if 'highlight' not in G.PERMS: return buf = get_buf(subl_view) if buf: msg.debug('summoning selection in subl_view ', buf['path'], ', buf id ', buf['id']) c = [[x.a, x.b] for x in subl_view.sel()] if self.joined_workspace: self.send({ 'id': buf['id'], 'name': 'highlight', 'ranges': c, 'ping': True, 'summon': True, 'following': False, }) return path = subl_view.file_name() if not utils.is_shared(path): sublime.error_message('Can\'t summon because %s is not in shared path %s.' % (path, G.PROJECT_PATH)) return share = sublime.ok_cancel_dialog('This file isn\'t shared. Would you like to share it?', 'Share') if share: sel = [[x.a, x.b] for x in subl_view.sel()] self.create_buf_cbs[utils.to_rel_path(path)] = lambda buf_id: send_summon(buf_id, sel) self.upload(path)
def on_pre_save(self, view): if not G.AGENT or not G.AGENT.is_ready(): return p = view.name() if view.file_name(): p = utils.to_rel_path(view.file_name()) self.between_save_events[view.buffer_id()] = p
def on_post_window_command(self, window, command, *args, **kwargs): agent = args[-1] if command == 'delete_file': # User probably deleted a file. Stat and delete. files = args[0]['files'] for f in files: buf = agent.get_buf_by_path(f) if not buf: continue if os.path.exists(f): continue agent.send({ 'name': 'delete_buf', 'id': buf['id'], }) return if command == 'delete_folder': dirs = args[0]['dirs'] for d in dirs: # Delete folder prompt just closed. Check if folder exists if os.path.isdir(d): continue rel_path = utils.to_rel_path(d) if not rel_path: msg.error('Can not delete %s from workspace', d) continue for buf_id, buf in G.AGENT.bufs.items(): if buf['path'].startswith(rel_path): agent.send({ 'name': 'delete_buf', 'id': buf_id, })
def delete_buf(self, path): """deletes a path""" if not path: return path = utils.get_full_path(path) if not self.is_shared(path): msg.error('Skipping deleting %s because it is not in shared path %s.' % (path, G.PROJECT_PATH)) return if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): # Don't care about hidden stuff dirnames[:] = [d for d in dirnames if d[0] != '.'] for f in filenames: f_path = os.path.join(dirpath, f) if f[0] == '.': msg.log('Not deleting buf for hidden file %s' % f_path) else: self.delete_buf(f_path) return buf_to_delete = None rel_path = utils.to_rel_path(path) buf_to_delete = self.get_buf_by_path(rel_path) if buf_to_delete is None: msg.error('%s is not in this workspace' % path) return msg.log('deleting buffer ', rel_path) event = { 'name': 'delete_buf', 'id': buf_to_delete['id'], } self.agent.put(event)
def delete_buf(path): if not utils.is_shared(path): msg.error('Skipping deleting %s because it is not in shared path %s.' % (path, G.PROJECT_PATH)) return if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): # TODO: rexamine this assumption # Don't care about hidden stuff dirnames[:] = [d for d in dirnames if d[0] != '.'] for f in filenames: f_path = os.path.join(dirpath, f) if f[0] == '.': msg.log('Not deleting buf for hidden file %s' % f_path) else: Listener.delete_buf(f_path) return buf_to_delete = get_buf_by_path(path) if buf_to_delete is None: msg.error('%s is not in this workspace' % path) return msg.log('deleting buffer ', utils.to_rel_path(path)) event = { 'name': 'delete_buf', 'id': buf_to_delete['id'], } G.AGENT.put(event)
def _on_rename_buf(self, req): old_path = utils.to_rel_path(req["old_path"]) buf = self.get_buf_by_path(old_path) if not buf: msg.debug("No buffer for path %s" % req["path"]) return path = utils.to_rel_path(req["path"]) if not utils.is_shared(path): msg.log("New path %s is not shared. Discarding rename event." % path) return buf_id = buf["id"] self.send_to_floobits({"name": "rename_buf", "id": buf["id"], "path": path}) # KANS: is this right? old code... old_path = self.agent.bufs[buf_id]["path"] del self.agent.paths_to_ids[old_path] self.agent.paths_to_ids[path] = buf_id self.agent.bufs[buf_id]["path"] = path
def is_shared(self, p): if not self.agent.is_ready(): msg.debug('agent is not ready. %s is not shared' % p) return False p = utils.unfuck_path(p) # TODO: tokenize on path seps and then look for .. if utils.to_rel_path(p).find("../") == 0: return False return True
def get_view(buf_id): buf = BUFS.get(buf_id) if buf is None: return None for view in G.WORKSPACE_WINDOW.views(): if not view.file_name(): continue if buf['path'] == utils.to_rel_path(view.file_name()): return view return None
def get_view_text_by_path(self, path): for v in G.WORKSPACE_WINDOW.views(): if not v.file_name(): continue try: rel_path = utils.to_rel_path(v.file_name()) except ValueError: continue if path == rel_path: return get_text(v)
def on_post_save(self, view, agent): view_buf_id = view.buffer_id() def cleanup(): i = self.between_save_events[view_buf_id] i[0] -= 1 if view.is_scratch(): return i = self.between_save_events[view_buf_id] if agent.ignored_saves[view_buf_id] > 0: agent.ignored_saves[view_buf_id] -= 1 return cleanup() old_name = i[1] i = self.between_save_events[view_buf_id] if i[0] > 1: return cleanup() old_name = i[1] event = None buf = get_buf(view) try: name = utils.to_rel_path(view.file_name()) except ValueError: name = view.file_name() is_shared = utils.is_shared(view.file_name()) if buf is None: if not is_shared: return cleanup() if G.IGNORE and G.IGNORE.is_ignored(view.file_name(), log=True): msg.log(view.file_name(), ' is ignored. Not creating buffer.') return cleanup() msg.log('Creating new buffer ', name, ' ', view.file_name()) event = {'name': 'create_buf', 'buf': get_text(view), 'path': name} elif name != old_name: if is_shared: msg.log('renamed buffer ', old_name, ' to ', name) event = {'name': 'rename_buf', 'id': buf['id'], 'path': name} else: msg.log('deleting buffer from shared: ', name) event = { 'name': 'delete_buf', 'id': buf['id'], } if event: agent.send(event) if is_shared and buf: agent.views_changed.append(('saved', view, buf)) cleanup()
def upload(path): try: with open(path, 'rb') as buf_fd: buf = buf_fd.read() encoding = 'utf8' rel_path = utils.to_rel_path(path) existing_buf = get_buf_by_path(path) if existing_buf: buf_md5 = hashlib.md5(buf).hexdigest() if existing_buf['md5'] == buf_md5: msg.debug( '%s already exists and has the same md5. Skipping.' % path) return msg.log('setting buffer ', rel_path) existing_buf['buf'] = buf existing_buf['md5'] = buf_md5 try: buf = buf.decode('utf-8') except Exception: buf = base64.b64encode(buf).decode('utf-8') encoding = 'base64' existing_buf['encoding'] = encoding G.AGENT.put({ 'name': 'set_buf', 'id': existing_buf['id'], 'buf': buf, 'md5': buf_md5, 'encoding': encoding, }) return try: buf = buf.decode('utf-8') except Exception: buf = base64.b64encode(buf).decode('utf-8') encoding = 'base64' msg.log('creating buffer ', rel_path) event = { 'name': 'create_buf', 'buf': buf, 'path': rel_path, 'encoding': encoding, } G.AGENT.put(event) except (IOError, OSError): msg.error('Failed to open %s.' % path) except Exception as e: msg.error('Failed to create buffer %s: %s' % (path, unicode(e)))
def on_pre_save(self, view, agent): if view.is_scratch(): return p = view.name() if view.file_name(): try: p = utils.to_rel_path(view.file_name()) except ValueError: p = view.file_name() i = self.between_save_events[view.buffer_id()] i[0] += 1 i[1] = p
def on_pre_save(self, view, agent): if view == G.CHAT_VIEW or view.file_name() == G.CHAT_VIEW_PATH: return p = view.name() if view.file_name(): try: p = utils.to_rel_path(view.file_name()) except ValueError: p = view.file_name() i = self.between_save_events[view.buffer_id()] i[0] += 1 i[1] = p
def _on_rename_buf(self, req): old_path = utils.to_rel_path(req['old_path']) buf = self.get_buf_by_path(old_path) if not buf: msg.debug('No buffer for path %s' % req['path']) return path = utils.to_rel_path(req['path']) if not utils.is_shared(path): msg.log('New path %s is not shared. Discarding rename event.' % path) return buf_id = buf['id'] self.send_to_floobits({ 'name': 'rename_buf', 'id': buf['id'], 'path': path, }) # KANS: is this right? old code... old_path = self.agent.bufs[buf_id]['path'] del self.agent.paths_to_ids[old_path] self.agent.paths_to_ids[path] = buf_id self.agent.bufs[buf_id]['path'] = path
def upload(path): try: with open(path, 'rb') as buf_fd: buf = buf_fd.read() encoding = 'utf8' rel_path = utils.to_rel_path(path) existing_buf = get_buf_by_path(path) if existing_buf: buf_md5 = hashlib.md5(buf).hexdigest() if existing_buf['md5'] == buf_md5: msg.log('%s already exists and has the same md5. Skipping.' % path) return msg.log('setting buffer ', rel_path) existing_buf['buf'] = buf existing_buf['md5'] = buf_md5 try: buf = buf.decode('utf-8') except Exception: buf = base64.b64encode(buf).decode('utf-8') encoding = 'base64' existing_buf['encoding'] = encoding G.AGENT.put({ 'name': 'set_buf', 'id': existing_buf['id'], 'buf': buf, 'md5': buf_md5, 'encoding': encoding, }) return try: buf = buf.decode('utf-8') except Exception: buf = base64.b64encode(buf).decode('utf-8') encoding = 'base64' msg.log('creating buffer ', rel_path) event = { 'name': 'create_buf', 'buf': buf, 'path': rel_path, 'encoding': encoding, } G.AGENT.put(event) except (IOError, OSError): msg.error('Failed to open %s.' % path) except Exception as e: msg.error('Failed to create buffer %s: %s' % (path, unicode(e)))
def get_view(self, buf_id): buf = self.bufs.get(buf_id) if not buf: return for v in G.WORKSPACE_WINDOW.views(): if not v.file_name(): continue try: rel_path = utils.to_rel_path(v.file_name()) except ValueError: continue if buf["path"] == rel_path: return View(v, buf)
def rename_buf(self, buf_id, new_path): new_path = utils.to_rel_path(new_path) if not utils.is_shared(new_path): msg.log('New path %s is not shared. Discarding rename event.' % new_path) return self.agent.put({ 'name': 'rename_buf', 'id': buf_id, 'path': new_path, }) old_path = self.FLOO_BUFS[buf_id]['path'] del self.FLOO_PATHS_TO_BUFS[old_path] self.FLOO_PATHS_TO_BUFS[new_path] = buf_id self.FLOO_BUFS[buf_id]['path'] = new_path
def get_view(self, buf_id): buf = self.bufs.get(buf_id) if not buf: return for v in G.WORKSPACE_WINDOW.views(): if not v.file_name(): continue try: rel_path = utils.to_rel_path(v.file_name()) except ValueError: continue if buf['path'] == rel_path: return View(v, buf)
def summon(view): buf = get_buf(view) if buf: msg.debug('summoning selection in view %s, buf id %s' % (buf['path'], buf['id'])) Listener.selection_changed.append((view, buf, True)) else: path = view.file_name() if not utils.is_shared(path): sublime.error_message('Can\'t summon because %s is not in shared path %s.' % (path, G.PROJECT_PATH)) return share = sublime.ok_cancel_dialog('This file isn\'t shared. Would you like to share it?', 'Share') if share: sel = [[x.a, x.b] for x in view.sel()] CREATE_BUF_CBS[utils.to_rel_path(path)] = lambda buf_id: send_summon(buf_id, sel) Listener.create_buf(path)
def summon(self, view): buf = get_buf(view) if buf: msg.debug('summoning selection in view %s, buf id %s' % (buf['path'], buf['id'])) self.selection_changed.append((view, buf, True)) else: path = view.file_name() if not utils.is_shared(path): editor.error_message('Can\'t summon because %s is not in shared path %s.' % (path, G.PROJECT_PATH)) return share = editor.ok_cancel_dialog('This file isn\'t shared. Would you like to share it?', 'Share') if share: sel = [[x.a, x.b] for x in view.sel()] self.create_buf_cbs[utils.to_rel_path(path)] = lambda buf_id: send_summon(buf_id, sel) self.upload(path)
def summon(self, view): buf = get_buf(view) if buf: msg.debug("summoning selection in view %s, buf id %s" % (buf["path"], buf["id"])) self.selection_changed.append((view, buf, True)) else: path = view.file_name() if not utils.is_shared(path): sublime.error_message("Can't summon because %s is not in shared path %s." % (path, G.PROJECT_PATH)) return share = sublime.ok_cancel_dialog("This file isn't shared. Would you like to share it?", "Share") if share: sel = [[x.a, x.b] for x in view.sel()] self.create_buf_cbs[utils.to_rel_path(path)] = lambda buf_id: send_summon(buf_id, sel) self.upload(path)
def on_post_save(self, view): if not G.AGENT or not G.AGENT.is_ready(): return def cleanup(): del self.between_save_events[view.buffer_id()] if view == G.CHAT_VIEW or view.file_name() == G.CHAT_VIEW_PATH: return cleanup() event = None buf = get_buf(view) name = utils.to_rel_path(view.file_name()) is_shared = utils.is_shared(view.file_name()) old_name = self.between_save_events[view.buffer_id()] if buf is None: if is_shared: msg.log('new buffer ', name, view.file_name()) event = { 'name': 'create_buf', 'buf': get_text(view), 'path': name } elif name != old_name: if is_shared: msg.log('renamed buffer {0} to {1}'.format(old_name, name)) event = { 'name': 'rename_buf', 'id': buf['id'], 'path': name } else: msg.log('deleting buffer from shared: {0}'.format(name)) event = { 'name': 'delete_buf', 'id': buf['id'], } if event: G.AGENT.put(event) if is_shared and buf: G.AGENT.put({'name': 'saved', 'id': buf['id']}) cleanup()
def on_post_save(self, view): if not G.AGENT or not G.AGENT.is_ready(): return def cleanup(): del self.between_save_events[view.buffer_id()] if view == G.CHAT_VIEW or view.file_name() == G.CHAT_VIEW_PATH: return cleanup() event = None buf = get_buf(view) name = utils.to_rel_path(view.file_name()) is_shared = utils.is_shared(view.file_name()) old_name = self.between_save_events[view.buffer_id()] if buf is None: if is_shared: msg.log('new buffer ', name, view.file_name()) event = { 'name': 'create_buf', 'buf': get_text(view), 'path': name } elif name != old_name: if is_shared: msg.log('renamed buffer {0} to {1}'.format(old_name, name)) event = {'name': 'rename_buf', 'id': buf['id'], 'path': name} else: msg.log('deleting buffer from shared: {0}'.format(name)) event = { 'name': 'delete_buf', 'id': buf['id'], } if event: G.AGENT.put(event) if is_shared and buf: G.AGENT.put({'name': 'saved', 'id': buf['id']}) cleanup()
def delete_buf(self, path): """deletes a path""" if not path: return path = utils.get_full_path(path) if not utils.is_shared(path): msg.error('Skipping deleting %s because it is not in shared path %s.' % (path, G.PROJECT_PATH)) return if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): # Don't care about hidden stuff dirnames[:] = [d for d in dirnames if d[0] != '.'] for f in filenames: f_path = os.path.join(dirpath, f) if f[0] == '.': msg.log('Not deleting buf for hidden file %s' % f_path) else: self.delete_buf(f_path) return buf_to_delete = None rel_path = utils.to_rel_path(path) for buf_id, buf in self.FLOO_BUFS.items(): if rel_path == buf['path']: buf_to_delete = buf break if buf_to_delete is None: msg.error('%s is not in this workspace' % path) return msg.log('deleting buffer ', rel_path) event = { 'name': 'delete_buf', 'id': buf_to_delete['id'], } self.agent.put(event)
def delete_buf(self, path): if not utils.is_shared(path): msg.error("Skipping deleting %s because it is not in shared path %s." % (path, G.PROJECT_PATH)) return if os.path.isdir(path): for dirpath, dirnames, filenames in os.walk(path): # TODO: rexamine this assumption # Don't care about hidden stuff dirnames[:] = [d for d in dirnames if d[0] != "."] for f in filenames: f_path = os.path.join(dirpath, f) if f[0] == ".": msg.log("Not deleting buf for hidden file %s" % f_path) else: self.delete_buf(f_path) return buf_to_delete = self.get_buf_by_path(path) if buf_to_delete is None: msg.error("%s is not in this workspace" % path) return msg.log("deleting buffer ", utils.to_rel_path(path)) event = {"name": "delete_buf", "id": buf_to_delete["id"]} self.send(event)
def on_post_save(self, view, agent): view_buf_id = view.buffer_id() def cleanup(): i = self.between_save_events[view_buf_id] i[0] -= 1 if view.is_scratch(): return i = self.between_save_events[view_buf_id] if agent.ignored_saves[view_buf_id] > 0: agent.ignored_saves[view_buf_id] -= 1 return cleanup() old_name = i[1] i = self.between_save_events[view_buf_id] if i[0] > 1: return cleanup() old_name = i[1] event = None buf = get_buf(view) try: name = utils.to_rel_path(view.file_name()) except ValueError: name = view.file_name() is_shared = utils.is_shared(view.file_name()) if buf is None: if not is_shared: return cleanup() if G.IGNORE and G.IGNORE.is_ignored(view.file_name(), log=True): msg.log(view.file_name(), ' is ignored. Not creating buffer.') return cleanup() msg.log('Creating new buffer ', name, view.file_name()) event = { 'name': 'create_buf', 'buf': get_text(view), 'path': name } elif name != old_name: if is_shared: msg.log('renamed buffer ', old_name, ' to ', name) event = { 'name': 'rename_buf', 'id': buf['id'], 'path': name } else: msg.log('deleting buffer from shared: ', name) event = { 'name': 'delete_buf', 'id': buf['id'], } if event: agent.send(event) if is_shared and buf: agent.views_changed.append(('saved', view, buf)) cleanup()
def create_buf(self, path, ig=None, force=False): if G.SPARSE_MODE and not force: msg.debug("Skipping %s because user enabled sparse mode." % path) return if not utils.is_shared(path): msg.error('Skipping adding %s because it is not in shared path %s.' % (path, G.PROJECT_PATH)) return if os.path.islink(path): msg.error('Skipping adding %s because it is a symlink.' % path) return ignored = ig and ig.is_ignored(path) if ignored: msg.log('Not creating buf: %s' % (ignored)) return msg.debug('create_buf: path is %s' % path) if os.path.isdir(path): if ig is None: try: ig = ignore.build_ignores(path) except Exception as e: msg.error('Error adding %s: %s' % (path, unicode(e))) return try: paths = os.listdir(path) except Exception as e: msg.error('Error listing path %s: %s' % (path, unicode(e))) return for p in paths: p_path = os.path.join(path, p) if p[0] == '.': if p not in ignore.HIDDEN_WHITELIST: msg.log('Not creating buf for hidden path %s' % p_path) continue ignored = ig.is_ignored(p_path) if ignored: msg.log('Not creating buf: %s' % (ignored)) continue try: s = os.lstat(p_path) except Exception as e: msg.error('Error lstat()ing path %s: %s' % (path, unicode(e))) continue if stat.S_ISDIR(s.st_mode): child_ig = ignore.Ignore(ig, p_path) utils.set_timeout(self.create_buf, 0, p_path, child_ig) elif stat.S_ISREG(s.st_mode): utils.set_timeout(self.create_buf, 0, p_path, ig) return try: buf_fd = open(path, 'rb') buf = buf_fd.read() encoding = 'utf8' rel_path = utils.to_rel_path(path) existing_buf = self.get_buf_by_path(path) if existing_buf and existing_buf['md5'] == hashlib.md5(buf).hexdigest(): msg.debug('%s already exists and has the same md5. Skipping creating.' % path) return try: buf = buf.decode('utf-8') except Exception: buf = base64.b64encode(buf).decode('utf-8') encoding = 'base64' msg.log('creating buffer ', rel_path) event = { 'name': 'create_buf', 'buf': buf, 'path': rel_path, 'encoding': encoding, } self.agent.put(event) except (IOError, OSError): msg.error('Failed to open %s.' % path) except Exception as e: msg.error('Failed to create buffer %s: %s' % (path, unicode(e)))
def get_buf_by_path(path): p = utils.to_rel_path(path) buf_id = PATHS_TO_IDS.get(p) if buf_id: return BUFS.get(buf_id)
def create_buf(self, path, ig=None, text=None): if not utils.is_shared(path): msg.error('Skipping adding %s because it is not in shared path %s.' % (path, G.PROJECT_PATH)) return if os.path.islink(path): msg.error('Skipping adding %s because it is a symlink.' % path) return ignored = ig and ig.is_ignored(path) if ignored: msg.log('Not creating buf: %s' % (ignored)) return msg.debug('create_buf: path is %s' % path) if os.path.isdir(path): if ig is None: try: ig = ignore.build_ignores(path) except Exception as e: msg.error('Error adding %s: %s' % (path, unicode(e))) return try: paths = os.listdir(path) except Exception as e: msg.error('Error listing path %s: %s' % (path, unicode(e))) return for p in paths: p_path = os.path.join(path, p) if p[0] == '.': if p not in ignore.HIDDEN_WHITELIST: msg.log('Not creating buf for hidden path %s' % p_path) continue ignored = ig.is_ignored(p_path) if ignored: msg.log('Not creating buf: %s' % (ignored)) continue try: s = os.lstat(p_path) except Exception as e: msg.error('Error lstat()ing path %s: %s' % (path, unicode(e))) continue if stat.S_ISDIR(s.st_mode): child_ig = ignore.Ignore(ig, p_path) utils.set_timeout(self.create_buf, 0, p_path, child_ig) elif stat.S_ISREG(s.st_mode): utils.set_timeout(self.create_buf, 0, p_path, ig) return try: encoding = 'utf8' if text: buf_md5 = hashlib.md5(text).hexdigest() else: buf_fd = open(path, 'rb') text = buf_fd.read() buf_fd.close() buf_md5 = hashlib.md5(text).hexdigest() try: text = text.decode('utf-8') except Exception: text = base64.b64encode(text).decode('utf-8') encoding = 'base64' rel_path = utils.to_rel_path(path) existing_buf = self.get_buf_by_path(path) if existing_buf: if existing_buf['md5'] == buf_md5: msg.debug('%s already exists and has the same md5. Skipping creating.' % path) return msg.log('setting buffer ', rel_path) self.agent.put({ 'name': 'set_buf', 'id': existing_buf['id'], 'buf': text, 'encoding': encoding, 'md5': buf_md5, }) return msg.log('creating buffer ', rel_path) event = { 'name': 'create_buf', 'buf': text, 'path': rel_path, 'encoding': encoding, 'md5': buf_md5, } self.agent.put(event) except (IOError, OSError): msg.error('Failed to open %s.' % path) except Exception as e: msg.error('Failed to create buffer %s: %s' % (path, unicode(e)))
def get_buf_by_path(self, path): rel_path = utils.to_rel_path(path) buf_id = self.FLOO_PATHS_TO_BUFS.get(rel_path) if buf_id: return self.FLOO_BUFS.get(buf_id)
def on_post_save(self, view, agent): view_buf_id = view.buffer_id() def cleanup(): i = self.between_save_events[view_buf_id] i[0] -= 1 if view == G.CHAT_VIEW or view.file_name() == G.CHAT_VIEW_PATH: return i = self.between_save_events[view_buf_id] if agent.ignored_saves[view_buf_id] > 0: agent.ignored_saves[view_buf_id] -= 1 return cleanup() old_name = i[1] i = self.between_save_events[view_buf_id] if i[0] > 1: return cleanup() old_name = i[1] event = None buf = get_buf(view) try: name = utils.to_rel_path(view.file_name()) except ValueError: name = view.file_name() is_shared = utils.is_shared(view.file_name()) if buf is None: if not is_shared: return cleanup() if ignore.is_ignored(view.file_name()): msg.log('%s is ignored. Not creating buffer.' % view.file_name()) return cleanup() msg.log('Creating new buffer ', name, view.file_name()) event = { 'name': 'create_buf', 'buf': get_text(view), 'path': name } elif name != old_name: if is_shared: msg.log('renamed buffer {0} to {1}'.format(old_name, name)) event = { 'name': 'rename_buf', 'id': buf['id'], 'path': name } else: msg.log('deleting buffer from shared: {0}'.format(name)) event = { 'name': 'delete_buf', 'id': buf['id'], } if event: agent.send(event) if is_shared and buf: agent.send({'name': 'saved', 'id': buf['id']}) cleanup()
def get_vim_buf_by_path(self, p): for vim_buf in vim.buffers: if vim_buf.name and p == utils.to_rel_path(vim_buf.name): return vim_buf return None
def get_buf_by_path(self, path): rel_path = utils.to_rel_path(path) for buf_id, buf in self.FLOO_BUFS.iteritems(): if rel_path == buf['path']: return buf return None