Changeset 71 in main for trunk/plugins
- Timestamp:
- 07/06/10 14:25:22 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/plugins/gedit/openplm.py
r70 r71 26 26 import json 27 27 import urllib 28 29 # sudo easy_install poster 30 from poster.encode import multipart_encode 31 from poster.streaminghttp import StreamingHTTPRedirectHandler, StreamingHTTPHandler 32 28 33 import urllib2 29 34 import gedit, gtk … … 45 50 <menuitem name="login" action="login"/> 46 51 <menuitem name="checkout" action="checkout"/> 52 <menuitem name="checkin" action="checkin"/> 47 53 </menu> 48 54 <separator/> … … 57 63 SERVER = "http://localhost:8000/" 58 64 OPENPLM_DIR = os.path.expanduser("~/.openplm") 65 PLUGIN_DIR = os.path.join(OPENPLM_DIR, "gedit") 66 CONF_FILE = os.path.join(PLUGIN_DIR, "conf.json") 59 67 60 68 def __init__(self, plugin, window): … … 72 80 self.on_window_activate) 73 81 82 try: 83 os.makedirs(self.OPENPLM_DIR, 0700) 84 except os.error: 85 pass 86 74 87 def stop(self): 75 88 self.remove_menu() … … 97 110 self._action_group2 = gtk.ActionGroup("GeditOpenPLMPluginActions2") 98 111 self._action_group2.add_actions([ 99 ("checkout", None, _("Check out"), None, 100 _("Check out"), lambda a: self.checkout()), 112 ("checkout", None, _("Check-out"), None, 113 _("Check out"), lambda a: self.check_out_cb()), 114 ("checkin", None, _("Check-in"), None, 115 _("Check-in"), lambda a: self.check_in()), 101 116 ]) 102 117 self._action_group2.set_sensitive(False) … … 125 140 self.password = diag.get_password() 126 141 127 self.opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(), 142 self.opener = urllib2.build_opener(StreamingHTTPHandler(), 143 StreamingHTTPRedirectHandler(), 128 144 urllib2.HTTPCookieProcessor()) 129 145 data = urllib.urlencode(dict(username=self.username, … … 131 147 self.opener.open(self.SERVER + "login/", data) 132 148 self._action_group2.set_sensitive(True) 149 self.load_managed_files() 133 150 diag.destroy() 134 151 135 def check out(self):152 def check_out_cb(self): 136 153 diag = CheckOutDialog(self._window, self) 137 154 diag.run() … … 147 164 def check_out(self, doc, doc_file): 148 165 self.document = doc 149 self.get_data("api/object/%s/checkout/%s " % (doc["id"], doc_file["id"]))166 self.get_data("api/object/%s/checkout/%s/" % (doc["id"], doc_file["id"])) 150 167 f = self.opener.open(self.SERVER + "file/%s/" % doc_file["id"]) 151 rep = os.path.join(self. OPENPLM_DIR, "gedit", doc["type"],152 doc["re ference"], doc["revision"])168 rep = os.path.join(self.PLUGIN_DIR, doc["type"], doc["reference"], 169 doc["revision"]) 153 170 try: 154 171 os.makedirs(rep, 0700) … … 156 173 # directory already exists 157 174 pass 158 dst_name = os.path.join(rep, doc_file["filename"])175 dst_name = os.path.join(rep, doc_file["filename"]) 159 176 dst = open(dst_name, "wb") 160 177 shutil.copyfileobj(f, dst) 161 178 f.close() 162 179 dst.close() 180 self.add_managed_file(doc, doc_file, dst_name) 163 181 gedit.commands.load_uri(self._window, "file://" + dst_name, None, -1) 164 182 gdoc = self._window.get_active_document() 183 gdoc.set_data("openplm_doc", doc) 184 gdoc.set_data("openplm_file", doc_file) 185 gdoc.set_data("openplm_path", dst_name) 186 187 def add_managed_file(self, document, doc_file, path): 188 f = open(self.CONF_FILE, "r") 189 try: 190 data = json.load(f) 191 except ValueError: 192 data = {} 193 f.close() 194 documents = data.get("documents", {}) 195 doc = documents.get(document["id"], dict(document)) 196 files = doc.get("files", {}) 197 files[doc_file["id"]] = path 198 doc["files"] = files 199 documents["id"] = doc 200 data["documents"] = documents 201 f = open(self.CONF_FILE, "w") 202 json.dump(data, f) 203 f.close() 204 205 def get_managed_files(self): 206 with open(self.CONF_FILE, "r") as f: 207 try: 208 data = json.load(f) 209 except ValueError: 210 data = {} 211 files = [] 212 for doc in data.get("documents", {}).itervalues(): 213 files.extend((d, doc) for d in doc.get("files", {}).items()) 214 return files 215 216 def load_managed_files(self): 217 for (doc_file_id, path), doc in self.get_managed_files(): 218 gedit.commands.load_uri(self._window, "file://" + path, None, -1) 219 gdoc = self._window.get_active_document() 220 gdoc.set_data("openplm_doc", doc) 221 gdoc.set_data("openplm_file_id", doc_file_id) 222 gdoc.set_data("openplm_path", path) 223 224 def check_in(self): 225 gdoc = self._window.get_active_document() 226 doc = gdoc.get_data("openplm_doc") 227 doc_file_id = gdoc.get_data("openplm_file_id") 228 path = gdoc.get_data("openplm_path") 229 if doc and doc_file_id and path: 230 gedit.commands.save_document(self._window, gdoc) 231 # headers contains the necessary Content-Type and Content-Length 232 # datagen is a generator object that yields the encoded parameters 233 datagen, headers = multipart_encode({"filename": open(path, "rb")}) 234 # Create the Request object 235 url = self.SERVER + "api/object/%s/checkin/%s/" % (doc["id"], doc_file_id) 236 request = urllib2.Request(url, datagen, headers) 237 res = self.opener.open(request) 238 else: 239 # TODO 240 pass 165 241 166 242 class CheckOutDialog(gtk.Dialog):
Note: See TracChangeset
for help on using the changeset viewer.