python less编译 python实现sublime3的less编译插件示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python less编译 python实现sublime3的less编译插件示例

  2021-03-19 我要评论
想了解python实现sublime3的less编译插件示例的相关内容吗,在本文为您仔细讲解python less编译的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:sublime3,less编译,下面大家一起来学习吧。

利用http://tool.oschina.net/less 提供的接口,发送请求进行远程编译.
再将编译好的less,保存为同名后缀为css的文件中.
第一次使用python,代码也是拼拼凑凑的.需要加上线程进行异步请求,但是不会...

复制代码 代码如下:

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);

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们