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

相关文章

Django跨域请求CSRF的方法示例

web跨域请求 1.为什么要有跨域限制 举个例子: 1.用户登录了自己的银行页面 http://mybank.com,http://mybank.com向用户的cookie中添加用户...

TensorFlow实现Batch Normalization

TensorFlow实现Batch Normalization

一、BN(Batch Normalization)算法 1. 对数据进行归一化处理的重要性 神经网络学习过程的本质就是学习数据分布,在训练数据与测试数据分布不同情况下,模型的泛化能力就大...

浅谈python迭代器

1、yield,将函数变为 generator (生成器) 例如:斐波那契数列 def fib(num): a, b, c = 1, 0, 1     while a <...

在Linux系统上通过uWSGI配置Nginx+Python环境的教程

1.安装ubuntu有uwsgi的ppa: add-apt-repository ppa:stevecrozz/ppa apt-get update apt-get instal...

python中异常报错处理方法汇总

python中异常报错处理方法汇总

首先异常是什么,异常白话解释就是不正常,程序里面一般是指程序员输入的格式不规范,或者需求的参数类型不对应,不全等等。 Python中异常是指程序中的例外,违例情况。异常机制是指程序出现错...