Exemple #1
0
    def iter_read_body(self, ifile):
        '-> (case, key, value)'
        state0 = self.all_cases
        state1 = ':'
        state2 = '.'

        state = state0
        for line in ifile:
            if comment_line_rex.match(line):
                continue
            case = line[0]
            if case not in state:
                raise SaveFileException(f'bad format: case: {case!r}  not in {state!r}')

            if state == state0:
                key = safe_eval(line[1:])
                if case == self.caseDELETE:
                    state = state2
                    value = None
                else:
                    state = state1
                case0 = case
            elif state == state1:
                value = safe_eval(line[1:])
                state = state2
Exemple #2
0
 def iter_read_body(self, ifile):
     for line in ifile:
         if comment_line_rex.match(line):
             continue
         object_literal = line
         obj = safe_eval(object_literal)
         yield obj
Exemple #3
0
def eval_to(expr_str, type_name, convertor=None):
    try:
        obj = safe_eval(expr_str)
        return obj if convertor is None else convertor(obj)
    except Exception as e:
        raise ArgumentTypeError(
            '{!r} is not a {} expression; details: {!r}'.format(
                expr_str, type_name, e))
Exemple #4
0
    def iter_read_body(self, ifile):
        '-> (case, element)'
        state0 = self.all_cases
        state = state0
        for line in ifile:
            if comment_line_rex.match(line):
                continue
            case = line[0]
            if case not in state:
                raise SaveFileException(f'bad format: case: {case!r}  not in {state!r}')

            element = safe_eval(line[1:])
            yield case, element
            del case, element
Exemple #5
0
def parse_string_regex_pattern(pattern:str, simplify:bool=True
    , *
    # terminal_set_cmp/is_terminal_block_set used for debug only
    , terminal_set_cmp=None
    , is_terminal_block_set=True
    ):
    # pattern -> ISimpleRE
    tokens = iter_tokens_of_string_regex_pattern(pattern)
    py_expr_str = ''.join(stream_convertor(tokens))
    locals = {'ns': theSimpleNameSpace}
    regex = safe_eval(py_expr_str, locals=locals)
    if simplify:
        regex = regex.simplify(terminal_set_cmp=terminal_set_cmp,
                               is_terminal_block_set=is_terminal_block_set)
    return regex
Exemple #6
0
    def iter_read_body(self, ifile):
        for line in ifile:
            if comment_line_rex.match(line):
                continue
            if not line.startswith('['):
                raise SaveFileException('bad format: not startswith "["')
            if line.startswith('[]'):
                yield tuple(ast.literal_eval(line))
                continue

            lines = [line]
            for line in ifile:
                lines.append(line)
                if line.startswith(']'): #!!!!!!!!!!
                    break
            else:
                raise Exception('unexpected EOF: when reading a block tuple')
            object_literal = ''.join(lines)
            obj = safe_eval(object_literal)
            obj = tuple(obj)
            yield obj
Exemple #7
0
def read_savefile_header(ifile):
    '-> (encoding, information)'
    begin_of_header_EOL = ifile.read(len(Global.begin_of_header_EOL))
    if begin_of_header_EOL != Global.begin_of_header_EOL:
        raise SaveFileException('not a "nn_ns text Save File": not have good begin_of_header_EOL')

    encoding_decl_EOL = ifile.readline()
    if encoding_decl_EOL[-1:] != '\n':
        raise SaveFileException('unexpected EOF when reading encoding_decl_EOL')
    m = Global.encoding_decl_EOL_rex.fullmatch(encoding_decl_EOL)
    if not m:
        encoding_decl = encoding_decl_EOL[:-1]
        raise SaveFileException(f'bad format: encoding_decl: {encoding_decl!r}', encoding_decl=encoding_decl)

    nonstd_encoding_literal = m['nonstd_encoding_literal']; del m
    encoding = ast.literal_eval(nonstd_encoding_literal)
    if type(encoding) is not str:
        raise SaveFileException(f'bad format: nonstd_encoding_literal: {nonstd_encoding_literal!r}')

    lines = []
    for line in ifile:
        if line.startswith('='):
            break
        lines.append(line)
    else:
        raise SaveFileException('unexpected EOF when reading information dict')

    end_of_header_EOL = line
    if end_of_header_EOL != Global.end_of_header_EOL:
        raise SaveFileException(f'bad format: end_of_header_EOL: {end_of_header_EOL!r}')

    information_literal = ''.join(lines)
    try:
        information = safe_eval(information_literal)
    except Exception:
        raise SaveFileException(f'bad format: information: {information_literal!r}')
    if type(information) not in (dict, OrderedDict):
        raise SaveFileException(f'bad type: information: not dict/OrderedDict: {type(information)!r}')

    return encoding, information
Exemple #8
0
######################
py -m nn_ns.app.hex -i '333' '3 + 7' '2 ** 3' '2 ^ 3'
0x14d
0xa
0x8
0x1


#'''

from seed.helper.safe_eval import safe_eval

assert '0x14d' == hex(333)

def eval_then_show(expr, /, *, fout):
    print(hex(safe_eval(expr)), file=fout)

def main(args=None, /):
    import argparse
    from seed.io.may_open import may_open_stdin, may_open_stdout

    parser = argparse.ArgumentParser(
        description='python eval then hex then print'
        , epilog=''
        , formatter_class=argparse.RawDescriptionHelpFormatter
        )
    parser.add_argument('-i', '--input', type=str, nargs='*', default=[]
                        , help='input python expression')
    parser.add_argument('-o', '--output', type=str, default=None
                        , help='output file path')
    parser.add_argument('-e', '--encoding', type=str
Exemple #9
0
def main(args=None):
    import argparse, sys, os.path

    path2title = 'lambda path: "" if not path else path[-1].strip()'
    parser = argparse.ArgumentParser(
        description='convert novel text to html',
        epilog=NOTE)
    parser.add_argument('input_fname', type=str, \
                        help='text file name')
    parser.add_argument('output_fname', type=str, \
                        nargs = '?',
                        default = None,
                        help='html file name')

    parser.add_argument('-t', '--title', type=str, \
                        default = None,
                        help='title of html file')

    parser.add_argument('-e', '--encoding', type=str, \
                        default = 'utf-8',
                        help='encoding of both text/html file')

    parser.add_argument('-ie', '--iencoding', type=str, \
                        default = None,
                        help='encoding of the input text file')

    parser.add_argument('-oe', '--oencoding', type=str, \
                        default = None,
                        help='encoding of the output html file')



    parser.add_argument('-css', '--style_css_fname', type=str, \
                        default = 'style.css',
                        help='css fname')

    parser.add_argument('-s', '--styles', type=str, \
                        default = '',
                        help='inline css')

    parser.add_argument('-js', '--action_js_fname', type=str, \
                        default = 'action.js',
                        help='javascript fname')

    parser.add_argument('-j', '--jscript', type=str, \
                        default = '',
                        help='inline javascript')




    parser.add_argument('-p2t', '--path2title', type=str, \
                        default = path2title,
                        help='covert subtitle-path to htm radio label')

    parser.add_argument('-rs', '--re_seps', type=str, \
                        nargs = '+',
                        help='regular-expression list to identify subtitle-path')
    parser.add_argument('-f', '--force', action='store_true',
                        default=False,
                        help='overwrite output file if it exists')

    # args.output_fname may be None
    args = parser.parse_args(args)
    if args.title is None:
        title = args.output_fname
        if title is None:
            title = args.input_fname
        args.title = os.path.basename(title)
        del title

    if args.iencoding is None:
        args.iencoding = args.encoding

    if args.oencoding is None:
        args.oencoding = args.encoding

    args.oencoding = python_encoding2html_encoding(args.oencoding)

    args.path2title = safe_eval(args.path2title)
    args.re_seps = replace_re_seps(args.re_seps)

    novel2htm(args.input_fname, args.output_fname,
              title=args.title,
              iencoding=args.iencoding, oencoding = args.oencoding,
              re_seps=args.re_seps, path2title=args.path2title,
              style_css_fname=args.style_css_fname, styles=args.styles,
              action_js_fname=args.action_js_fname, jscript=args.jscript,
              force=args.force)
Exemple #10
0
 def read_body(self, ifile):
     object_literal = ifile.read()
     obj = safe_eval(object_literal)
     return obj
Exemple #11
0
from seed.func_tools.fmapT.fmapT__tiny import fmapT__list, fmapT__tuple
from seed.helper.safe_eval import safe_eval
from seed.helper.stable_repr import stable_repr
import re
from pathlib import Path
from sys import stdin
from ast import literal_eval

assert '0x14d' == hex(333)
assert 819 == (0x333)

if 0:

    def eval_then_show(expr, /, *, fout):
        print(safe_eval(expr), file=fout)
else:

    def eval_then_show(locals_, preprocess_fs, postprocess_fs, expr, /, *,
                       fout, to_txt, main_action):
        for f in reversed(preprocess_fs):
            expr = f(expr)

        x = main_action(expr, locals=locals_)

        for f in iter(postprocess_fs):
            x = f(x)

        print(to_txt(x), file=fout)

Exemple #12
0
def do_work(expr):
    r = safe_eval(expr)
    print(repr(r))
def txt2cjk_d(cjk_d_txt):
    cjk_d = safe_eval(cjk_d_txt, locals=dict(CharsetCharFilter=CharsetCharFilter))
    return cjk_d
Exemple #14
0
def main(args=None):
    import argparse
    from seed.io.may_open import may_open_stdin, may_open_stdout

    parser = argparse.ArgumentParser(
        description="charset_filter",
        epilog=r"""
exprs that usr can use:
    E :: encoding -> ICharFilter
    R :: Map begin end -> ICharFilter
    N :: py_regex_pattern -> ICharFilter
        #unicodedata.name()
    U :: ICharFilter
    (|), (&), (^), (-) :: ICharFilter -> ICharFilter -> ICharFilter
    where
        E(s) = EncodingCharFilter(s)
        R({1:2,3:4}) = CharsetCharFilter([(1,2),(3,4)])
        N(s) = CharNameRegexCharFilter(s)
        U = E('utf8').to_CharsetCharFilter()
        U = CharsetCharFilter([(0, 0xD800), (0xE000, 0x11_0000)])
        | & ^ -
            union, intersection, symmetric_difference, difference

==================================
py -m seed.text.charset_filter -i U
CharsetCharFilter(((0, 55296), (57344, 1114112)))
py -m seed.text.charset_filter -i 'E("utf8").to_CharsetCharFilter()'
CharsetCharFilter(((0, 55296), (57344, 1114112)))

py -m seed.text.charset_filter -i '(E("gb2312") & N("CJK")).to_CharsetCharFilter()'

py -m seed.text.charset_filter -o ~/tmp/cjk_d.txt > ~/tmp/cjk_pr.txt
py -m seed.text.charset_filter -o ~/tmp/cjk_d.txt -f > ~/tmp/cjk_pr2.txt

=========
py -m seed.text.charset_filter -i '(E("gb2312") & N("CJK")).sort_all_good_chars_by_encoding("gb2312")' > ~/tmp/gb2312.txt


py -m seed.text.charset_filter -i '(E("gbk") & N("CJK")).sort_all_good_chars_by_encoding("gbk")' > ~/tmp/gbk.txt

py -m seed.text.charset_filter -i '(E("big5") & N("CJK")).sort_all_good_chars_by_encoding("big5")' > ~/tmp/big5.txt


py -m seed.text.charset_filter -i '(E("hz") & N("CJK")).sort_all_good_chars_by_encoding("hz")' > ~/tmp/hz.txt

gb2312
gbk
hz
big5
big5hkscs
cp950
iso2022_jp_2
gb18030
utf8

gb18030-2000
python 没有 *与 ISO 10646 相应的国家标准 GB 13000 中的其它 CJK 汉字
#""",
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument(
        '-i',
        '--input',
        type=str,
        default=None,
        help='input python expr str; without this arg, handle all cjk_encodings'
    )
    parser.add_argument('-o',
                        '--output',
                        type=str,
                        default=None,
                        help='output file path')
    parser.add_argument('-e',
                        '--encoding',
                        type=str,
                        default='utf8',
                        help='input/output file encoding')
    parser.add_argument('-f',
                        '--force',
                        action='store_true',
                        default=False,
                        help='open mode for output file')

    args = parser.parse_args(args)
    encoding = args.encoding
    omode = 'wt' if args.force else 'xt'

    #may_ifname = args.input
    #with may_open_stdin(may_ifname, 'rt', encoding=encoding) as fin:
    locals = dict(E=E, R=R, N=N, U=U)
    may_s = args.input
    if may_s is None:
        r = d = _cjk()
    else:
        s = may_s
        r = safe_eval(s, locals=locals)

    may_ofname = args.output
    with may_open_stdout(may_ofname, omode, encoding=encoding) as fout:
        print(repr(r), file=fout)