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

yipeiwu_com6年前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 pickle模块用法实例分析

本文实例讲述了Python pickle模块用法。分享给大家供大家参考。具体分析如下: pickle提供了一个简单的持久化功能。可以将对象以文件的形式存放在磁盘上。 pickle.dum...

python 利用pywifi模块实现连接网络破解wifi密码实时监控网络

python 利用pywifi模块实现连接网络破解wifi密码实时监控网络,具体内容如下: import pywifi from pywifi import * import tim...

Python处理CSV与List的转换方法

1.读取CSV文件到List def readCSV2List(filePath): try: file=open(filePath,'r',encoding="gbk")#...

Window10下python3.7 安装与卸载教程图解

Window10下python3.7 安装与卸载教程图解

1.进入官网https://www.python.org/,点击Downloads下的Windows按钮,进入下载页面。 2.如下图所示,点击下载。 3.安装Python3.7.4...

使用50行Python代码从零开始实现一个AI平衡小游戏

使用50行Python代码从零开始实现一个AI平衡小游戏

  集智导读: 本文会为大家展示机器学习专家 Mike Shi 如何用 50 行 Python 代码创建一个 AI,使用增强学习技术,玩耍一个保持杆子平衡的小游戏。所用环境为...