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 装饰器装饰类中的方法实例

基于Python 装饰器装饰类中的方法实例

title: Python 装饰器装饰类中的方法 comments: true date: 2017-04-17 20:44:31 tags: ['Python', 'Decorate'...

python实现提取百度搜索结果的方法

本文实例讲述了python实现提取百度搜索结果的方法。分享给大家供大家参考。具体实现方法如下: # coding=utf8 import urllib2 import string...

TensorFlow实现AutoEncoder自编码器

TensorFlow实现AutoEncoder自编码器

一、概述 AutoEncoder大致是一个将数据的高维特征进行压缩降维编码,再经过相反的解码过程的一种学习方法。学习过程中通过解码得到的最终结果与原数据进行比较,通过修正权重偏置参数降低...

Python中的类学习笔记

Python中的类学习笔记

Python使用中面向对象的语言,支持继承、多态; 定义一个Person类: 复制代码 代码如下: >>> class Person: ... def sayHello...

Python获取apk文件URL地址实例

工作中经常需要提取apk文件的特定URL地址,如是想到用Python脚本进行自动处理。需要用到的Python基础知识如下:os.walk()函数声明:os.walk(top,topdow...