Python implements sublime3's less compilation plug in example

  • 2020-04-02 13:39:54
  • OfStack

Using the http://tool.oschina.net/less interface, send the request for remote compiling.
Save the compiled less in a CSS file with the same suffix.
For the first time using python, the code is also pieced together. Threads are needed to make asynchronous requests, but not...


import sublime, sublime_plugin
import urllib
import json
class exampleCommand(sublime_plugin.TextCommand):
 def run(self, edit):
  file_name=self.view.file_name();
  if file_name.find('.less') == -1:
   print('only .less file can compile to css!!');
   return;
  file_object_from = open(file_name);
  all_the_text = file_object_from.read();
  url = "http://tool.oschina.net/action/less/less_compile";
  data =  all_the_text.encode(encoding='UTF8');
  headers = {'User-Agent':'sublime_plugin'};
  req = urllib.request.Request(url,data,headers);
  response = urllib.request.urlopen(req);
  the_page = response.read();
  css=json.loads(the_page.decode("utf8"))['css'];
  file_object_to = open(self.view.file_name().replace('.less', '.css'), 'w')
  file_object_to.write(css);
  file_object_from.close();
  file_object_to.close();
  print(css);


Related articles: