Пример #1
0
 def _on_create_buf(self, data):
     if data['encoding'] == 'base64':
         data['buf'] = base64.b64decode(data['buf'])
     self.bufs[data['id']] = data
     self.paths_to_ids[data['path']] = data['id']
     view = self.get_view(data['id'])
     if view:
         self.save_view(view)
     else:
         utils.save_buf(data)
Пример #2
0
 def _on_create_buf(self, data):
     if data['encoding'] == 'base64':
         data['buf'] = base64.b64decode(data['buf'])
     self.bufs[data['id']] = data
     self.paths_to_ids[data['path']] = data['id']
     view = self.get_view(data['id'])
     if view:
         self.save_view(view)
     else:
         utils.save_buf(data)
 def _on_saved(self, data):
     buf_id = data['id']
     buf = self.bufs.get(buf_id)
     if not buf:
         return
     if G.MIRRORED_SAVES:
         view = self.get_view(data['id'])
         if view:
             self.save_view(view)
         elif 'buf' in buf:
             utils.save_buf(buf)
     username = self.get_username_by_id(data['user_id'])
     msg.log('%s saved buffer %s' % (username, buf['path']))
Пример #4
0
 def _on_saved(self, data):
     buf_id = data['id']
     buf = self.bufs.get(buf_id)
     if not buf:
         return
     if G.MIRRORED_SAVES:
         view = self.get_view(data['id'])
         if view:
             self.save_view(view)
         elif 'buf' in buf:
             utils.save_buf(buf)
     username = self.get_username_by_id(data['user_id'])
     msg.log('%s saved buffer %s' % (username, buf['path']))
Пример #5
0
 def _on_saved(self, data):
     buf_id = data['id']
     buf = self.bufs.get(buf_id)
     if not buf:
         return
     on_view_load = self.on_load.get(buf_id)
     if on_view_load:
         del on_view_load['patch']
     view = self.get_view(data['id'])
     if view:
         self.save_view(view)
     elif 'buf' in buf:
         utils.save_buf(buf)
     username = self.get_username_by_id(data['user_id'])
     msg.log('%s saved buffer %s' % (username, buf['path']))
Пример #6
0
 def _on_saved(self, data):
     buf_id = data['id']
     buf = self.bufs.get(buf_id)
     if not buf:
         return
     on_view_load = self.on_load.get(buf_id)
     if on_view_load:
         del on_view_load['patch']
     view = self.get_view(data['id'])
     if view:
         self.save_view(view)
     elif 'buf' in buf:
         utils.save_buf(buf)
     username = self.get_username_by_id(data['user_id'])
     msg.log('%s saved buffer %s' % (username, buf['path']))
    def _on_get_buf(self, data):
        buf_id = data['id']
        buf = self.bufs.get(buf_id)
        if not buf:
            return msg.warn('no buf found: %s.  Hopefully you didn\'t need that' % data)
        timeout_id = buf.get('timeout_id')
        if timeout_id:
            utils.cancel_timeout(timeout_id)

        if data['encoding'] == 'base64':
            data['buf'] = base64.b64decode(data['buf'])

        self.bufs[buf_id] = data

        save = False
        if buf_id in self.save_on_get_bufs:
            self.save_on_get_bufs.remove(buf_id)
            save = True

        view = self.get_view(buf_id)
        if not view:
            msg.debug('No view for buf %s. Saving to disk.' % buf_id)
            return utils.save_buf(data)

        view.update(data)
        if save:
            view.save()
Пример #8
0
    def _on_get_buf(self, data):
        buf_id = data['id']
        buf = self.bufs.get(buf_id)
        if not buf:
            return msg.warn('no buf found: ', data,
                            '. Hopefully you didn\'t need that.')
        timeout_id = buf.get('timeout_id')
        if timeout_id:
            utils.cancel_timeout(timeout_id)

        if data['encoding'] == 'base64':
            data['buf'] = base64.b64decode(data['buf'])

        self.bufs[buf_id] = data

        save = False
        if buf_id in self.save_on_get_bufs:
            self.save_on_get_bufs.remove(buf_id)
            save = True

        view = self.get_view(buf_id)
        if not view:
            msg.debug('No view for buf ', buf_id, '. Saving to disk.')
            return utils.save_buf(data)

        view.update(data)
        if save:
            view.save()
Пример #9
0
 def _on_rename_buf(self, data):
     del self.paths_to_ids[data['old_path']]
     self.paths_to_ids[data['path']] = data['id']
     new = utils.get_full_path(data['path'])
     old = utils.get_full_path(data['old_path'])
     new_dir = os.path.split(new)[0]
     if new_dir:
         utils.mkdir(new_dir)
     view = self.get_view(data['id'])
     if view:
         view.rename(new)
     else:
         try:
             os.rename(old, new)
         except Exception as e:
             msg.debug('Error moving ', old, 'to', new, str_e(e))
             utils.save_buf(self.bufs[data.id])
     self.bufs[data['id']]['path'] = data['path']
Пример #10
0
 def _on_rename_buf(self, data):
     del self.paths_to_ids[data['old_path']]
     self.paths_to_ids[data['path']] = data['id']
     new = utils.get_full_path(data['path'])
     old = utils.get_full_path(data['old_path'])
     new_dir = os.path.split(new)[0]
     if new_dir:
         utils.mkdir(new_dir)
     view = self.get_view(data['id'])
     if view:
         view.rename(new)
     else:
         try:
             os.rename(old, new)
         except Exception as e:
             msg.debug('Error moving ', old, 'to', new, str_e(e))
             utils.save_buf(self.bufs[data.id])
     self.bufs[data['id']]['path'] = data['path']
Пример #11
0
    def _on_create_buf(self, data):
        if data['encoding'] == 'base64':
            data['buf'] = base64.b64decode(data['buf'])
        self.bufs[data['id']] = data
        self.paths_to_ids[data['path']] = data['id']
        abs_path = utils.get_full_path(data['path'])

        self.to_emacs('create_buf', {
            'full_path': utils.get_full_path(data['path']),
            'path': data['path'],
            'username': data.get('username', ''),
        })

        if abs_path not in self.emacs_handler.emacs_bufs:
            utils.save_buf(data)
            return
        text = self.emacs_handler.emacs_bufs.get(abs_path)[0]
        if text == data['buf']:
            return
        self.emacs_handler.bufs_changed.append(data['id'])
 def _on_create_buf(self, data):
     if data['encoding'] == 'base64':
         data['buf'] = base64.b64decode(data['buf'])
     self.bufs[data['id']] = data
     self.paths_to_ids[data['path']] = data['id']
     utils.save_buf(data)
    def _on_patch(self, data):
        buf_id = data['id']
        buf = self.bufs[buf_id]
        if 'buf' not in buf:
            msg.debug('buf %s not populated yet. not patching' % buf['path'])
            return

        if buf['encoding'] == 'base64':
            # TODO apply binary patches
            return self.get_buf(buf_id, None)

        if len(data['patch']) == 0:
            msg.debug('wtf? no patches to apply. server is being stupid')
            return

        msg.debug('patch is', data['patch'])
        dmp_patches = DMP.patch_fromText(data['patch'])
        # TODO: run this in a separate thread
        old_text = buf['buf']

        view = self.get_view(buf_id)
        if view and not view.is_loading():
            view_text = view.get_text()
            if old_text == view_text:
                buf['forced_patch'] = False
            elif not buf.get('forced_patch'):
                patch = utils.FlooPatch(view_text, buf)
                # Update the current copy of the buffer
                buf['buf'] = patch.current
                buf['md5'] = hashlib.md5(patch.current.encode('utf-8')).hexdigest()
                buf['forced_patch'] = True
                msg.debug('forcing patch for %s' % buf['path'])
                self.send(patch.to_json())
                old_text = view_text
            else:
                msg.debug('forced patch is true. not sending another patch for buf %s' % buf['path'])
        md5_before = hashlib.md5(old_text.encode('utf-8')).hexdigest()
        if md5_before != data['md5_before']:
            msg.warn('starting md5s don\'t match for %s. this is dangerous!' % buf['path'])

        t = DMP.patch_apply(dmp_patches, old_text)

        clean_patch = True
        for applied_patch in t[1]:
            if not applied_patch:
                clean_patch = False
                break

        if G.DEBUG:
            if len(t[0]) == 0:
                try:
                    msg.debug('OMG EMPTY!')
                    msg.debug('Starting data:', buf['buf'])
                    msg.debug('Patch:', data['patch'])
                except Exception as e:
                    print(e)

            if '\x01' in t[0]:
                msg.debug('FOUND CRAZY BYTE IN BUFFER')
                msg.debug('Starting data:', buf['buf'])
                msg.debug('Patch:', data['patch'])

        timeout_id = buf.get('timeout_id')
        if timeout_id:
            utils.cancel_timeout(timeout_id)
            del buf['timeout_id']

        if not clean_patch:
            msg.log('Couldn\'t patch %s cleanly.' % buf['path'])
            return self.get_buf(buf_id, view)

        cur_hash = hashlib.md5(t[0].encode('utf-8')).hexdigest()
        if cur_hash != data['md5_after']:
            buf['timeout_id'] = utils.set_timeout(self.get_buf, 2000, buf_id, view)

        buf['buf'] = t[0]
        buf['md5'] = cur_hash

        if not view:
            msg.debug('No view. Saving buffer %s' % buf_id)
            utils.save_buf(buf)
            return

        view.apply_patches(buf, t, data['username'])
Пример #14
0
 def _on_create_buf(self, data):
     if data['encoding'] == 'base64':
         data['buf'] = base64.b64decode(data['buf'])
     self.bufs[data['id']] = data
     self.paths_to_ids[data['path']] = data['id']
     utils.save_buf(data)
Пример #15
0
    def _on_patch(self, data):
        buf_id = data['id']
        buf = self.bufs[buf_id]
        if 'buf' not in buf:
            msg.debug('buf %s not populated yet. not patching' % buf['path'])
            return

        if buf['encoding'] == 'base64':
            # TODO apply binary patches
            return self.get_buf(buf_id, None)

        if len(data['patch']) == 0:
            msg.debug('wtf? no patches to apply. server is being stupid')
            return

        msg.debug('patch is', data['patch'])
        dmp_patches = DMP.patch_fromText(data['patch'])
        # TODO: run this in a separate thread
        old_text = buf['buf']

        view = self.get_view(buf_id)
        if view and not view.is_loading():
            view_text = view.get_text()
            if old_text == view_text:
                buf['forced_patch'] = False
            elif not buf.get('forced_patch'):
                patch = utils.FlooPatch(view_text, buf)
                # Update the current copy of the buffer
                buf['buf'] = patch.current
                buf['md5'] = hashlib.md5(patch.current.encode('utf-8')).hexdigest()
                buf['forced_patch'] = True
                msg.debug('forcing patch for %s' % buf['path'])
                self.send(patch.to_json())
                old_text = view_text
            else:
                msg.debug('forced patch is true. not sending another patch for buf %s' % buf['path'])
        md5_before = hashlib.md5(old_text.encode('utf-8')).hexdigest()
        if md5_before != data['md5_before']:
            msg.warn('starting md5s don\'t match for %s. this is dangerous!' % buf['path'])

        t = DMP.patch_apply(dmp_patches, old_text)

        clean_patch = True
        for applied_patch in t[1]:
            if not applied_patch:
                clean_patch = False
                break

        if G.DEBUG:
            if len(t[0]) == 0:
                try:
                    msg.debug('OMG EMPTY!')
                    msg.debug('Starting data:', buf['buf'])
                    msg.debug('Patch:', data['patch'])
                except Exception as e:
                    print(e)

            if '\x01' in t[0]:
                msg.debug('FOUND CRAZY BYTE IN BUFFER')
                msg.debug('Starting data:', buf['buf'])
                msg.debug('Patch:', data['patch'])

        timeout_id = buf.get('timeout_id')
        if timeout_id:
            utils.cancel_timeout(timeout_id)
            del buf['timeout_id']

        if not clean_patch:
            msg.log('Couldn\'t patch %s cleanly.' % buf['path'])
            return self.get_buf(buf_id, view)

        cur_hash = hashlib.md5(t[0].encode('utf-8')).hexdigest()
        if cur_hash != data['md5_after']:
            buf['timeout_id'] = utils.set_timeout(self.get_buf, 2000, buf_id, view)

        buf['buf'] = t[0]
        buf['md5'] = cur_hash

        if not view:
            utils.save_buf(buf)
            return

        view.apply_patches(buf, t, data['username'])