summary refs log blame commit diff stats
path: root/doc/pydoc/ranger.ext.html
blob: 3b33e70a349f5a966b4cd92f2e07091392bfa1f5 (plain) (tree)



















                                                                                                                                                                                                             
                                                               


                                                                                   
                                                               

                                                                           
                                                 
                                                                                           
                                                                           


                                                                 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: package ranger.ext</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body bgcolor="#f0f0f8">

<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial">&nbsp;<br><big><big><strong><a href="ranger.html"><font color="#ffffff">ranger</font></a>.ext</strong></big></big></font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/hut/work/ranger/ranger/ext/__init__.py">/home/hut/work/ranger/ranger/ext/__init__.py</a></font></td></tr></table>
    <p><tt>This&nbsp;package&nbsp;includes&nbsp;extensions&nbsp;with&nbsp;broader&nbsp;usability</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#aa55cc">
<td colspan=3 valign=bottom>&nbsp;<br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Package Contents</strong></big></font></td></tr>
    
<tr><td bgcolor="#aa55cc"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="ranger.ext.accumulator.html">accumulator</a><br>
<a href="ranger.ext.command_parser.html">command_parser</a><br>
<a href="ranger.ext.curses_interrupt_handler.html">curses_interrupt_handler</a><br>
</td><td width="25%" valign=top><a href="ranger.ext.debug.html">debug</a><br>
<a href="ranger.ext.get_all_modules.html">get_all_modules</a><br>
<a href="ranger.ext.human_readable.html">human_readable</a><br>
</td><td width="25%" valign=top><a href="ranger.ext.move.html">move</a><br>
<a href="ranger.ext.openstruct.html">openstruct</a><br>
<a href="ranger.ext.relpath.html">relpath</a><br>
</td><td width="25%" valign=top><a href="ranger.ext.shell_escape.html">shell_escape</a><br>
<a href="ranger.ext.shutil_generatorized.html">shutil_generatorized</a><br>
<a href="ranger.ext.waitpid_no_intr.html">waitpid_no_intr</a><br>
</td></tr></table></td></tr></table>
</body></html>
class="n">s.write("]") of akRange: storeAny(s, skipRange(a), stored) of akEnum: s.write(getEnumField(a).escapeJson) of akPtr, akRef: var x = a.getPointer if isNil(x): s.write("null") elif stored.containsOrIncl(x.ptrToInt): # already stored, so we simply write out the pointer as an int: s.write($x.ptrToInt) else: # else as a [value, key] pair: # (reversed order for convenient x[0] access!) s.write("[") s.write($x.ptrToInt) s.write(", ") storeAny(s, a[], stored) s.write("]") of akProc, akPointer, akCString: s.write($a.getPointer.ptrToInt) of akString: var x = getString(a) if IsNil(x): s.write("null") else: s.write(escapeJson(x)) of akInt..akInt64: s.write($getBiggestInt(a)) of akFloat..akFloat128: s.write($getBiggestFloat(a)) proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) = case a.kind of akNone: assert false of akBool: case p.kind of jsonFalse: setBiggestInt(a, 0) of jsonTrue: setBiggestInt(a, 1) else: raiseParseErr(p, "'true' or 'false' expected for a bool") next(p) of akChar: if p.kind == jsonString: var x = p.str if x.len == 1: setBiggestInt(a, ord(x[0])) next(p) return raiseParseErr(p, "string of length 1 expected for a char") of akEnum: if p.kind == jsonString: setBiggestInt(a, getEnumOrdinal(a, p.str)) next(p) return raiseParseErr(p, "string expected for an enum") of akArray: if p.kind != jsonArrayStart: raiseParseErr(p, "'[' expected for an array") next(p) var i = 0 while p.kind != jsonArrayEnd and p.kind != jsonEof: loadAny(p, a[i], t) inc(i) if p.kind == jsonArrayEnd: next(p) else: raiseParseErr(p, "']' end of array expected") of akSequence: case p.kind of jsonNull: setPointer(a, nil) next(p) of jsonArrayStart: next(p) invokeNewSeq(a, 0) var i = 0 while p.kind != jsonArrayEnd and p.kind != jsonEof: extendSeq(a) loadAny(p, a[i], t) inc(i) if p.kind == jsonArrayEnd: next(p) else: raiseParseErr(p, "") else: raiseParseErr(p, "'[' expected for a seq") of akObject, akPureObject, akTuple: if a.kind == akObject: setObjectRuntimeType(a) if p.kind != jsonObjectStart: raiseParseErr(p, "'{' expected for an object") next(p) while p.kind != jsonObjectEnd and p.kind != jsonEof: if p.kind != jsonString: raiseParseErr(p, "string expected for a field name") var fieldName = p.str next(p) loadAny(p, a[fieldName], t) if p.kind == jsonObjectEnd: next(p) else: raiseParseErr(p, "'}' end of object expected") of akSet: if p.kind != jsonArrayStart: raiseParseErr(p, "'[' expected for a set") next(p) while p.kind != jsonArrayEnd and p.kind != jsonEof: if p.kind != jsonInt: raiseParseErr(p, "int expected for a set") inclSetElement(a, p.getInt.int) next(p) if p.kind == jsonArrayEnd: next(p) else: raiseParseErr(p, "']' end of array expected") of akPtr, akRef: case p.kind of jsonNull: setPointer(a, nil) next(p) of jsonInt: setPointer(a, t[p.getInt]) next(p) of jsonArrayStart: next(p) if a.kind == akRef: invokeNew(a) else: setPointer(a, alloc0(a.baseTypeSize)) if p.kind == jsonInt: t[p.getInt] = getPointer(a) next(p) else: raiseParseErr(p, "index for ref type expected") loadAny(p, a[], t) if p.kind == jsonArrayEnd: next(p) else: raiseParseErr(p, "']' end of ref-address pair expected") else: raiseParseErr(p, "int for pointer type expected") of akProc, akPointer, akCString: case p.kind of jsonNull: setPointer(a, nil) next(p) of jsonInt: setPointer(a, cast[pointer](p.getInt.int)) next(p) else: raiseParseErr(p, "int for pointer type expected") of akString: case p.kind of jsonNull: setPointer(a, nil) next(p) of jsonString: setString(a, p.str) next(p) else: raiseParseErr(p, "string expected") of akInt..akInt64: if p.kind == jsonInt: setBiggestInt(a, getInt(p)) next(p) return raiseParseErr(p, "int expected") of akFloat..akFloat128: if p.kind == jsonFloat: setBiggestFloat(a, getFloat(p)) next(p) return raiseParseErr(p, "float expected") of akRange: loadAny(p, a.skipRange, t) proc loadAny(s: PStream, a: TAny, t: var TTable[biggestInt, pointer]) = var p: TJsonParser open(p, s, "unknown file") next(p) loadAny(p, a, t) close(p) proc load*[T](s: PStream, data: var T) = ## loads `data` from the stream `s`. Raises `EIO` in case of an error. var tab = initTable[biggestInt, pointer]() loadAny(s, toAny(data), tab) proc store*[T](s: PStream, data: T) = ## stores `data` into the stream `s`. Raises `EIO` in case of an error. var stored = initIntSet() var d: T shallowCopy(d, data) storeAny(s, toAny(d), stored) proc `$$`*[T](x: T): string = ## returns a string representation of `x`. var stored = initIntSet() var d: T shallowCopy(d, x) var s = newStringStream() storeAny(s, toAny(d), stored) result = s.data proc to*[T](data: string): T = ## reads data and transforms it to a ``T``. var tab = initTable[biggestInt, pointer]() loadAny(newStringStream(data), toAny(result), tab) when isMainModule: template testit(x: expr) = echo($$to[type(x)]($$x)) var x: array[0..4, array[0..4, string]] = [ ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"]] testit(x) var test2: tuple[name: string, s: int] = ("tuple test", 56) testit(test2) type TE = enum blah, blah2 TestObj = object test, asd: int case test2: TE of blah: help: string else: nil PNode = ref TNode TNode = object next, prev: PNode data: string proc buildList(): PNode = new(result) new(result.next) new(result.prev) result.data = "middle" result.next.data = "next" result.prev.data = "prev" result.next.next = result.prev result.next.prev = result result.prev.next = result result.prev.prev = result.next var test3: TestObj test3.test = 42 test3.test2 = blah testit(test3) var test4: ref tuple[a, b: string] new(test4) test4.a = "ref string test: A" test4.b = "ref string test: B" testit(test4) var test5 = @[(0,1),(2,3),(4,5)] testit(test5) var test6: set[char] = {'A'..'Z', '_'} testit(test6) var test7 = buildList() echo($$test7) testit(test7) type TA = object TB = object of TA f: int var a: ref TA b: ref TB new(b) a = b echo($$a[]) # produces "{}", not "{f: 0}"