File: cookie2dat.py

package info (click to toggle)
fortune-zh 2.98.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,320 kB
  • sloc: python: 146; sh: 37; makefile: 27
file content (36 lines) | stat: -rwxr-xr-x 982 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/python3
'''
Convert "cookie" files into "dat" files for fortune.
This script requires python3 >= 3.6
Copyright (C) 2018 Mo Zhou <cdluminate@gmail.com>
'''
from typing import *
import glob
import os


def cookie2dat(src: str) -> None:
    '''
    Read "cookie" and do preprocessing. :src: refers to source file path.
    '''
    dest = src.replace('.cookie', '.dat')
    with open(src, 'r') as fsrc, open(dest, 'w') as fdest:
        contents = fsrc.readlines()
        while contents:
            if contents[0].startswith('#'):
                contents.pop(0)
            else:
                break
        if contents:
            fdest.writelines(contents)
            print(f'Converted {src} -> {dest}')
        else:
            print(f'{src} is empty. skipped')


if __name__ == '__main__':

    cookies = glob.glob('./**/**.cookie', recursive=True)
    print(f'{__file__}: Found {len(cookies)} cookie files')
    for cookie in cookies:
        cookie2dat(cookie)