~aw/rhythmbox-swedishradio/trunk

« back to all changes in this revision

Viewing changes to plugins/vim.py

  • Committer: duanedesign
  • Date: 2010-06-09 13:26:34 UTC
  • Revision ID: duanedesign@gmail.com-20100609132634-l5no42pskpm92kf7
new merge from stipple-devs/+junk/stipple-sensitive with a few small bug fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
# -*- coding: utf-8 -*-
3
 
 
4
 
"""DocString
5
 
 
6
 
DocString Paragraph
7
 
 
8
 
"""
9
 
__version__ = "$Revision$"
 
3
#
 
4
# packagesync.py - sync installed packages across several computers.
 
5
#
 
6
# Copyright 2010 Duane Hinnen, Marcos Vanetta
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify it
 
9
# under the terms of the GNU General Public License version 3, as published
 
10
# by the Free Software Foundation.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
14
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
15
# PURPOSE.  See the GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License along
 
18
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
#
10
20
 
11
21
import os
12
 
import commands
13
 
import tarfile
14
 
from desktopcouch.records.server import CouchDatabase
15
 
from desktopcouch.records.record import Record as CouchRecord
16
22
from core.stipple import PluginBase
17
 
 
18
 
database = CouchDatabase("packages", create=True)
19
 
databaserecord = ""
20
 
RECORD_TYPE = 'http://www.okiebuntu.homelinux.com/packagesync'
21
 
VIM_CONFIG = os.path.expanduser("~") + "/.vimrc"
22
23
HOME_DIR = os.path.expanduser("~")
 
24
VIM_CONFIG = HOME_DIR + "/.vimrc"
23
25
VIM_DIR = HOME_DIR + "/.vim"
24
 
 
25
26
class Plugin(PluginBase):
26
 
    def __init__ (self):
27
 
        """ Function doc """
 
27
    """
 
28
    It's blah
 
29
    """
 
30
    def __init__(self):
28
31
        self.name = "Vim"
29
 
        self.icon = None
30
 
        #self.vimDir = self.homePath + "/.vim"
 
32
        self.prog_name = "vim"
 
33
        self.record_name = self.name.lower()    
31
34
    
32
35
    def sync(self):
33
 
        ''' get simple list of installed apps to search for config files '''
34
 
        search_string = commands.getoutput("dpkg --get-selections '*'")
35
 
        if search_string.find("vim") != -1:
36
 
            ''' write the vim config to a couchdb '''
37
 
            with open(VIM_CONFIG, 'r') as vim:
38
 
                vr = vim.read()
39
 
            print vr ##debug
40
 
 
41
 
 
42
 
            record_four = CouchRecord({  "_id"       :  "vim"   ,  
43
 
                                        "profile"   :  "user001" ,
44
 
                                        "type"      :  "dotfile" ,
45
 
                                        "file"      :  vr        ,
46
 
                                     },
47
 
                                     record_type = RECORD_TYPE)
48
 
            if os.path.exists(VIM_DIR):
49
 
                tar = tarfile.open(HOME_DIR + "/vim.tar", "w")
50
 
                tar.add(VIM_DIR)
51
 
                tar.close()
52
 
                record_four.attach(open(HOME_DIR + "/vim.tar", "r"), "vim.tar", "tarfile/tar")
53
 
                os.remove(HOME_DIR + "/vim.tar")
54
 
            databaserecord = database.put_record(record_four)
55
 
            vim.close()
56
 
        else:
57
 
            print "Bash it's not even installed SUCKER!"
58
 
            return
59
 
        
 
36
        """It must be installed"""
 
37
        with open(VIM_CONFIG, "r") as vim:
 
38
            vr = vim.read()
 
39
        vim.close()
 
40
        self.saveDotFile(vr)
 
41
        self.attachDirectory(VIM_DIR)
 
42
        return True
 
43
    
60
44
    def restore(self):
61
 
        map_js2 = 'function(doc){if (doc._id == "vim"){emit(null,doc.file);}}'
62
 
        database.add_view("vimconfig", map_js2, None, "getfile")
63
 
        result = database.execute_view("vimconfig", "getfile")
64
 
        
65
 
        for row in result:
66
 
            config_iter = row.value
67
 
 
68
 
        vim_file = open(os.path.expanduser("~") + "/.vimrc", 'w')
69
 
        vim_file.write(config_iter)
70
 
        vim_file.close()
71
 
        rec = database.get_record("vim")
72
 
        if len(rec.list_attachments()) > 0:
73
 
            tar = open(os.path.expanduser("~") + "/vim.tar", 'w')
74
 
            tar.write(rec.attachment_data(rec.list_attachments()[0])[0])
75
 
            tar.close()
76
 
            tar = tarfile.open(HOME_DIR + "/vim.tar", "r")
77
 
            tar.extractall("/")
78
 
            tar.close()
79
 
            os.remove(HOME_DIR + "/vim.tar")
80
 
            
81
 
            
82
 
            
 
45
        cad = self.recoverDotFile()
 
46
        if cad:
 
47
            with open(VIM_CONFIG, "w") as vim:
 
48
                vim.write(cad)
 
49
            vim.close()    
 
50
        self.recoverDirectory(VIM_DIR)
 
51
        return True