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中tell()方法的使用详解

 tell()方法返回的文件内的文件读/写指针的当前位置。 语法 以下是tell()方法的语法: fileObject.tell() 参数  &nbs...

python实现大学人员管理系统

python作为一个面对对象的程序设计语言,实现一个人员管理系统有自己关于类的方法。 首先,通过定义一个人员的类对象,实现对于人员公共特性的支持,公共的特性包括:姓名,性别,出生日期等,...

python使用标准库根据进程名如何获取进程的pid详解

前言 标准库是Python的一个组成部分。这些标准库是Python为你准备好的利器,可以让编程事半功倍。特别是有时候需要获取进程的pid,但又无法使用第三方库的时候。下面话不多说了,来一...

解决python中用matplotlib画多幅图时出现图形部分重叠的问题

1.解决方法:使用函数 tight_layout() 2.具体使用方法 import matplotlib.pyplot as plt fig = plt.figure()...

跟老齐学Python之啰嗦的除法

除法啰嗦的,不仅是python。 整数除以整数 看官请在启动idle之后,练习下面的运算: >>> 2/5 0 >>> 2.0/5 0.4 >...