fql——File Query Language

作为对语言编译的复习、练手,最近实现了一个类似SQL的小语言FQL,用SQL的语法完成对文件的查询。用python实现的,用到了PLY解析器。FQL当前版本是0.1.0,支持的feature有限:

1)文件属性:

- name:文件名

- ctime:文件创建时间

- mtime:上次修改时间

- atime:上次访问时间

- size:文件大小

2)聚集函数:

- count

- sum

- max

- min

3)用法:

- 交互式命令行解析器:

python fql.py

每一行作为一条命令执行

- 一次执行一条命令

python fql.py "select * from . where name like '%.py$'"

4)例子:

- 列出当前文件夹以及子文件夹中的文件

        > python fql.py 'select * from .'
        > -----------------------------------------------------------------------------------------------------
          | name                | ctime               | mtime               | atime               | size      |
          -----------------------------------------------------------------------------------------------------
          | accu_func.py        | 2015-01-22 16:05:07 | 2015-01-22 16:05:07 | 2015-01-23 19:53:00 | 2.21K     |
          -----------------------------------------------------------------------------------------------------
          | accu_func.pyc       | 2015-01-23 19:53:00 | 2015-01-23 19:53:00 | 2015-01-23 19:53:00 | 5.87K     |
          -----------------------------------------------------------------------------------------------------
          | fql.py              | 2015-01-23 19:24:58 | 2015-01-23 19:24:58 | 2015-01-23 19:53:00 | 1.44K     |
          -----------------------------------------------------------------------------------------------------

- 列出所有python文件的文件名及其大小

        > python fql.py 'select name, size from name like "%.py$"'
        > -----------------------------------
          | name                | size      |
          -----------------------------------
          | accu_func.py        | 2.21K     |
          -----------------------------------
          | fql.py              | 1.44K     |
          -----------------------------------

- 列出在“2015-01-23 19:50:00”之后创建的python文件

        > python fql.py 'select * from name like "%.py$" and ctime > 2015-01-23 19:50:00'
        > -----------------------------------------------------------------------------------------------------
          | name                | ctime               | mtime               | atime               | size      |
          -----------------------------------------------------------------------------------------------------
          | fql.py              | 2015-01-23 19:54:50 | 2015-01-23 19:54:49 | 2015-01-23 20:01:31 | 1.41K     |
          -----------------------------------------------------------------------------------------------------
          | parsetab.py         | 2015-01-23 19:53:00 | 2015-01-23 19:53:00 | 2015-01-23 19:54:00 | 17.87K    |
          -----------------------------------------------------------------------------------------------------

- 列出在“2015-01-23 19:50:00”之后创建的python文件,以及名为“README.md”的文件

        > python fql.py 'select * from (name like "%.py$" and ctime > 2015-01-23) or name = "README.md"'
        > -----------------------------------------------------------------------------------------------------
| name | ctime | mtime | atime | size | ----------------------------------------------------------------------------------------------------- | fql.py | 2015-01-23 19:54:50 | 2015-01-23 19:54:49 | 2015-01-23 19:57:25 | 1.41K | ----------------------------------------------------------------------------------------------------- | parsetab.py | 2015-01-23 19:53:00 | 2015-01-23 19:53:00 | 2015-01-23 19:54:00 | 17.87K | -----------------------------------------------------------------------------------------------------
          | README.md           | 2015-01-23 20:00:53 | 2015-01-23 20:00:53 | 2015-01-23 20:00:53 | 3.94K     |
          -----------------------------------------------------------------------------------------------------

- 计算所有python文件的总大小

        > python fql.py 'select sum(size) from . where name like "%.py$"'
        > -----------------------------------------
          | sum(st_size)    | 38.54K              |
          -----------------------------------------

- 列出最近创建的文件的创建时间

        > python fql.py 'select max(ctime) from .'
        > ------------------------------------------
          | max(st_ctime)    | 2015-01-23 20:07:49 |
          ------------------------------------------

5)安装与执行

依赖于ply,直接通过easy_install安装即可

easy_install ply
然后,执行python fql.py即可

目前,fql算是自己把玩的小玩具,之后会不断完善,争取成为高效、有用的工具,具体代码见 github。

你可能感兴趣的:(python,compiler,parser,ply)