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

yipeiwu_com5年前Python基础

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

相关文章

Python 支付整合开发包的实现

轻量级支付方式整合集成,实现支付与业务完全剥离,快速简单完成支付模块的开发 特性 屏蔽支付方式之间接入API和数据结构的差异,统一API和数据结构 支持支付类型横向扩展 统...

解决python写入mysql中datetime类型遇到的问题

刚开始使用python,还不太熟练,遇到一个datetime数据类型的问题: 在mysql数据库中,有一个datetime类型的字段用于存储记录的日期时间值。python程序中有对应的一...

python对矩阵进行转置的2种处理方法

方法一 :使用常规的思路 def transpose(M): # 初始化转置后的矩阵 result = [] # 获取转置前的行和列 row, col = shap...

Python实现树莓派WiFi断线自动重连的实例代码

实现 WiFi 断线自动重连。原理是用 Python 监测网络是否断线,如果断线则重启网络服务。 1.Python 代码 autowifi.py,放在 /home/pi 目录下: #...

解决pytorch GPU 计算过程中出现内存耗尽的问题

Pytorch GPU运算过程中会出现:“cuda runtime error(2): out of memory”这样的错误。通常,这种错误是由于在循环中使用全局变量当做累加器,且累加...