@classmethod<EOL><INDENT>def HWProcess(cls, proc: HWProcess, ctx: ResourceContext) -> None:<DEDENT>

seen = ctx.seen<EOL>for stm in proc.statements:<EOL><INDENT>encl = stm._enclosed_for<EOL>full_ev_dep = stm._is_completly_event_dependent<EOL>now_ev_dep = stm._now_is_event_dependent<EOL>ev_dep = full_ev_dep or now_ev_dep<EOL>out_mux_dim = count_mux_inputs_for_outputs(stm)<EOL>for o in stm._outputs:<EOL><INDENT>if o in seen:<EOL><INDENT>continue<EOL><DEDENT>i = out_mux_dim[o]<EOL>if isinstance(o._dtype, HArray):<EOL><INDENT>assert i == <NUM_LIT:1>, (o, i, "<STR_LIT>")<EOL>for a in walk_assignments(stm, o):<EOL><INDENT>assert len(a.indexes) == <NUM_LIT:1>, "<STR_LIT>"<EOL>addr = a.indexes[<NUM_LIT:0>]<EOL><DEDENT>ctx.registerRAM_write_port(o, addr, ev_dep)<EOL><DEDENT>elif ev_dep:<EOL><INDENT>ctx.registerFF(o)<EOL>if i > <NUM_LIT:1>:<EOL><INDENT>ctx.registerMUX(stm, o, i)<EOL><DEDENT><DEDENT>elif o not in encl:<EOL><INDENT>ctx.registerLatch(o)<EOL>if i > <NUM_LIT:1>:<EOL><INDENT>ctx.registerMUX(stm, o, i)<EOL><DEDENT><DEDENT>elif i > <NUM_LIT:1>:<EOL><INDENT>ctx.registerMUX(stm, o, i)<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>if isinstance(stm, SwitchContainer):<EOL><INDENT>caseEqs = set([stm.switchOn._eq(c[<NUM_LIT:0>]) for c in stm.cases])<EOL>inputs = chain(<EOL>[sig for sig in stm._inputs if sig not in caseEqs], [stm.switchOn])<EOL><DEDENT>else:<EOL><INDENT>inputs = stm._inputs<EOL><DEDENT>for i in inputs:<EOL><INDENT>if not i.hidden or i in seen:<EOL><INDENT>continue<EOL><DEDENT>cls.HWProcess_operators(i, ctx, ev_dep)<EOL><DEDENT><DEDENT>


@classmethod<EOL><INDENT>def Entity(cls, ent: Entity, ctx: ResourceContext) -> None:<DEDENT>

return<EOL>

Entity is just header, we do not need to inspect it for resources


def getBaseContext(self) -> ResourceContext:

return self.context<EOL>

Return context for collecting of resource informatins
prepared on this instance


def __init__(self, width, items,<EOL>rwSync: int, rSync: int, wSync: int, rSync_wAsync: int,<EOL>rwAsync: int, rAsync: int, wAsync: int, rAsync_wSync: int):

self.width = width<EOL>self.items = items<EOL>self.rwSync = rwSync<EOL>self.rSync = rSync<EOL>self.wSync = wSync<EOL>self.rSync_wAsync = rSync_wAsync<EOL>self.rwAsync = rwAsync<EOL>self.rAsync = rAsync<EOL>self.wAsync = wAsync<EOL>self.rAsync_wSync = rAsync_wSync<EOL>

:param width: widtho of word in RAM/ROM
:param items: number of words in RAM/ROM
:param rwSync: count of read + write sychronous ports
:param rSync: count of read only sychronous ports
:param wSync: count of write only sychronous ports
:param rSync_wAsync: count of synchronous read + asynchronous write ports
:param rwAsync: count of read + write asychronous ports
:param rAsync: count of read only asychronous ports
:param wAsync: count of write only asychronous ports
:param rAsync_wSync: count of asynchronous read + synchronous write ports


def registerMUX(self, stm: Union[HdlStatement, Operator], sig: RtlSignal,<EOL>inputs_cnt: int):

assert inputs_cnt > <NUM_LIT:1><EOL>res = self.resources<EOL>w = sig._dtype.bit_length()<EOL>k = (ResourceMUX, w, inputs_cnt)<EOL>res[k] = res.get(k, <NUM_LIT:0>) + <NUM_LIT:1><EOL>self.resource_for_object[(stm, sig)] = k<EOL>

mux record is in format (self.MUX, n, m)
where n is number of bits of this mux
and m is number of possible inputs


ff_to_remove = <NUM_LIT:0><EOL>res = self.resources<EOL>for m, addrDict in self.memories.items():<EOL><INDENT>rwSyncPorts, rSyncPorts, wSyncPorts = <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0><EOL>rwAsyncPorts, rAsyncPorts, wAsyncPorts = <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0><EOL>rSync_wAsyncPorts, rAsync_wSyncPorts = <NUM_LIT:0>, <NUM_LIT:0><EOL>for _, (rSync, wSync, rAsync, wAsync) in addrDict.items():<EOL><INDENT>if rSync:<EOL><INDENT>ff_to_remove += rSync * m._dtype.elmType.bit_length()<EOL><DEDENT>rwSync = min(rSync, wSync)<EOL>rSync -= rwSync<EOL>wSync -= rwSync<EOL>rwAsync = min(rAsync, wAsync)<EOL>rAsync -= rwAsync<EOL>wAsync -= rwAsync<EOL>rSync_wAsync = min(rSync, wAsync)<EOL>rSync -= rSync_wAsync<EOL>wAsync -= rSync_wAsync<EOL>rAsync_wSync = min(rAsync, wSync)<EOL>rAsync -= rAsync_wSync<EOL>wSync -= rAsync_wSync<EOL>rwSyncPorts += rwSync<EOL>rSyncPorts += rSync<EOL>wSyncPorts += wSync<EOL>rwAsyncPorts += rwAsync<EOL>rAsyncPorts += rAsync<EOL>wAsyncPorts += wAsync<EOL>rSync_wAsyncPorts += rSync_wAsync<EOL>rAsync_wSyncPorts += rAsync_wSync<EOL><DEDENT>k = ResourceRAM(m._dtype.elmType.bit_length(),<EOL>int(m._dtype.size),<EOL>rwSyncPorts, rSyncPorts, wSyncPorts,<EOL>rSync_wAsyncPorts,<EOL>rwAsyncPorts, rAsyncPorts, wAsyncPorts,<EOL>rAsync_wSyncPorts)<EOL>res[k] = res.get(k, <NUM_LIT:0>) + <NUM_LIT:1><EOL><DEDENT>self.memories.clear()<EOL>if ff_to_remove:<EOL><INDENT>ff_cnt = res[ResourceFF]<EOL>ff_cnt -= ff_to_remove<EOL>if ff_cnt:<EOL><INDENT>res[ResourceFF] = ff_cnt<EOL><DEDENT>else:<EOL><INDENT>del res[ResourceFF]<EOL><DEDENT><DEDENT>


def __init__(self, topUnit: Unit, name: str=None,<EOL>extraVhdlFiles: List[str]=[],<EOL>extraVerilogFiles: List[str]=[],<EOL>serializer=VhdlSerializer,<EOL>targetPlatform=DummyPlatform()):

assert not topUnit._wasSynthetised()<EOL>if not name:<EOL><INDENT>name = topUnit._getDefaultName()<EOL><DEDENT>super(IpPackager, self).__init__(<EOL>topUnit, name, extraVhdlFiles, extraVerilogFiles)<EOL>self.serializer = serializer<EOL>self.targetPlatform = targetPlatform<EOL>

:param topObj: Unit instance of top component
:param name: optional name of top
:param extraVhdlFiles: list of extra vhdl file names for files
    which should be distributed in this IP-core
:param extraVerilogFiles: same as extraVhdlFiles just for Verilog
:param serializer: serializer which specifies target HDL language
:param targetPlatform: specifies properties of target platform, like available resources, vendor, etc.


@internal<EOL><INDENT>def toHdlConversion(self, top, topName: str, saveTo: str) -> List[str]:<DEDENT>

return toRtl(top,<EOL>saveTo=saveTo,<EOL>name=topName,<EOL>serializer=self.serializer,<EOL>targetPlatform=self.targetPlatform)<EOL>

:param top: object which is represenation of design
:param topName: name which should be used for ipcore
:param saveTo: path of directory where generated files should be stored

:return: list of file namens in correct compile order


@internal<EOL><INDENT>def serializeType(self, hdlType: HdlType) -> str:<DEDENT>

def createTmpVar(suggestedName, dtype):<EOL><INDENT>raise NotImplementedError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % (hdlType))<EOL><DEDENT>return VhdlSerializer.HdlType(hdlType, VhdlSerializer.getBaseContext())<EOL>


@internal<EOL><INDENT>def getVectorFromType(self, dtype) -> Union[bool, None, Tuple[int, int]]:<DEDENT>

if dtype == BIT:<EOL><INDENT>return False<EOL><DEDENT>elif isinstance(dtype, Bits):<EOL><INDENT>return [evalParam(dtype.width) - <NUM_LIT:1>, hInt(<NUM_LIT:0>)]<EOL><DEDENT>


@internal<EOL><INDENT>def getInterfaceType(self, intf: Interface) -> HdlType:<DEDENT>

return intf._dtype<EOL>


@internal<EOL><INDENT>def getInterfaceLogicalName(self, intf: Interface):<DEDENT>

return getSignalName(intf)<EOL>


@internal<EOL><INDENT>def getInterfacePhysicalName(self, intf: Interface):<DEDENT>

return intf._sigInside.name<EOL>


return thisIntf._direction<EOL>


@internal<EOL><INDENT>def getExprVal(self, val, do_eval=False):<DEDENT>

ctx = VhdlSerializer.getBaseContext()<EOL>def createTmpVar(suggestedName, dtype):<EOL><INDENT>raise NotImplementedError(<EOL>"<STR_LIT>",<EOL>val)<EOL><DEDENT>ctx.createTmpVarFn = createTmpVar<EOL>if do_eval:<EOL><INDENT>val = val.staticEval()<EOL><DEDENT>val = VivadoTclExpressionSerializer.asHdl(val, ctx)<EOL>return val<EOL>


@internal<EOL><INDENT>def getTypeWidth(self, dtype: HdlType, do_eval=False) -> Tuple[int, Union[int, RtlSignal], bool]:<DEDENT>

width = dtype.width<EOL>if isinstance(width, int):<EOL><INDENT>widthStr = str(width)<EOL><DEDENT>else:<EOL><INDENT>widthStr = self.getExprVal(width, do_eval=do_eval)<EOL><DEDENT>return width, widthStr, False<EOL>


@internal<EOL><INDENT>def getObjDebugName(self, obj: Union[Interface, Unit, Param]) -> str:<DEDENT>

return obj._getFullName()<EOL>


@internal<EOL><INDENT>def serialzeValueToTCL(self, val, do_eval=False) -> Tuple[str, str, bool]:<DEDENT>

if isinstance(val, int):<EOL><INDENT>val = hInt(val)<EOL><DEDENT>if do_eval:<EOL><INDENT>val = val.staticEval()<EOL><DEDENT>if isinstance(val, RtlSignalBase):<EOL><INDENT>ctx = VivadoTclExpressionSerializer.getBaseContext()<EOL>tclVal = VivadoTclExpressionSerializer.asHdl(val, ctx)<EOL>tclValVal = VivadoTclExpressionSerializer.asHdl(<EOL>val.staticEval())<EOL>return tclVal, tclValVal, False<EOL><DEDENT>else:<EOL><INDENT>tclVal = VivadoTclExpressionSerializer.asHdl(val, None)<EOL>return tclVal, tclVal, True<EOL><DEDENT>


@classmethod<EOL><INDENT>def HWProcess(cls, proc, ctx):<DEDENT>

body = proc.statements<EOL>childCtx = ctx.withIndent()<EOL>statemets = [cls.asHdl(s, childCtx) for s in body]<EOL>proc.name = ctx.scope.checkedName(proc.name, proc)<EOL>return cls.methodTmpl.render(<EOL>indent=getIndent(ctx.indent),<EOL>name=proc.name,<EOL>statements=statemets<EOL>)<EOL>

Serialize HWProcess instance

:param scope: name scope to prevent name collisions


@classmethod<EOL><INDENT>def Architecture_var(cls, v, serializerVars, extraTypes,<EOL>extraTypes_serialized, ctx, childCtx):<DEDENT>

v.name = ctx.scope.checkedName(v.name, v)<EOL>serializedVar = cls.SignalItem(v, childCtx, declaration=True)<EOL>serializerVars.append(serializedVar)<EOL>

:return: list of extra discovered processes


if signalItem._const orarr_any(signalItem.drivers,<EOL>lambda d: isinstance(d, HdlStatement)<EOL>and d._now_is_event_dependent):<EOL><INDENT>return SIGNAL_TYPE.REG<EOL><DEDENT>else:<EOL><INDENT>return SIGNAL_TYPE.WIRE<EOL><DEDENT>


body = proc.statements<EOL>extraVars = []<EOL>extraVarsSerialized = []<EOL>hasToBeVhdlProcess = extraVars orarr_any(body,<EOL>lambda x: isinstance(x,<EOL>(IfContainer,<EOL>SwitchContainer,<EOL>WhileContainer,<EOL>WaitStm)) or<EOL>(isinstance(x, Assignment) and<EOL>x.indexes))<EOL>anyIsEventDependnt = arr_any(<EOL>proc.sensitivityList, lambda s: isinstance(s, Operator))<EOL>sensitivityList = sorted(<EOL>map(lambda s: cls.sensitivityListItem(s, ctx,<EOL>anyIsEventDependnt),<EOL>proc.sensitivityList))<EOL>if hasToBeVhdlProcess:<EOL><INDENT>childCtx = ctx.withIndent()<EOL><DEDENT>else:<EOL><INDENT>childCtx = ctx<EOL><DEDENT>def createTmpVarFn(suggestedName, dtype):<EOL><INDENT>s = SignalItem(None, dtype, virtualOnly=True)<EOL>s.name = childCtx.scope.checkedName(suggestedName, s)<EOL>s.hidden = False<EOL>serializedS = cls.SignalItem(s, childCtx, declaration=True)<EOL>extraVars.append(s)<EOL>extraVarsSerialized.append(serializedS)<EOL>return s<EOL><DEDENT>childCtx.createTmpVarFn = createTmpVarFn<EOL>statemets = [cls.asHdl(s, childCtx) for s in body]<EOL>if hasToBeVhdlProcess:<EOL><INDENT>proc.name = ctx.scope.checkedName(proc.name, proc)<EOL><DEDENT>extraVarsInit = []<EOL>for s in extraVars:<EOL><INDENT>a = Assignment(s.defVal, s, virtualOnly=True)<EOL>extraVarsInit.append(cls.Assignment(a, childCtx))<EOL><DEDENT>return cls.processTmpl.render(<EOL>indent=getIndent(ctx.indent),<EOL>name=proc.name,<EOL>hasToBeVhdlProcess=hasToBeVhdlProcess,<EOL>extraVars=extraVarsSerialized,<EOL>sensitivityList="<STR_LIT>".join(sensitivityList),<EOL>statements=extraVarsInit + statemets<EOL>)<EOL>


processes = []<EOL>signals = []<EOL>for e in rom.endpoints:<EOL><INDENT>assert isinstance(e, Operator) and e.operator == AllOps.INDEX, e<EOL>me, index = e.operands<EOL>assert me is rom<EOL>romValSig = rom.ctx.sig(rom.name, dtype=e.result._dtype)<EOL>signals.append(romValSig)<EOL>romValSig.hidden = False<EOL>cases = [(toHVal(i), [romValSig(v), ])<EOL>for i, v in enumerate(rom.defVal.val)]<EOL>statements = [SwitchContainer(index, cases), ]<EOL>for (_, (stm, )) in cases:<EOL><INDENT>stm.parentStm = statements[<NUM_LIT:0>] <EOL><DEDENT>p = HWProcess(rom.name, statements, {index, },<EOL>{index, }, {romValSig, })<EOL>processes.append(p)<EOL>def replaceOrigRomIndexExpr(x):<EOL><INDENT>if x is e.result:<EOL><INDENT>return romValSig<EOL><DEDENT>else:<EOL><INDENT>return x<EOL><DEDENT><DEDENT>for _e in e.result.endpoints:<EOL><INDENT>_e.operands = tuple(map(replaceOrigRomIndexExpr, _e.operands))<EOL>e.result = romValSig<EOL><DEDENT><DEDENT>return processes, signals<EOL>

Due to verilog restrictions it is not posible to use array constants
and rom memories has to be hardcoded as process


t = v._dtype<EOL>if isinstance(t, HArray) and v.defVal.vldMask:<EOL><INDENT>if v.drivers:<EOL><INDENT>raise SerializerException("<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>eProcs, eVars = cls.hardcodeRomIntoProcess(v)<EOL>for _v in eVars:<EOL><INDENT>_procs = cls.Architecture_var(_v, serializerVars, extraTypes,<EOL>extraTypes_serialized, ctx,<EOL>childCtx)<EOL>eProcs.extend(_procs)<EOL><DEDENT>return eProcs<EOL><DEDENT>v.name = ctx.scope.checkedName(v.name, v)<EOL>serializedVar = cls.SignalItem(v, childCtx, declaration=True)<EOL>serializerVars.append(serializedVar)<EOL>return []<EOL>


driver_cnt = len(signalItem.drivers)<EOL>if signalItem._const or driver_cnt > <NUM_LIT:1> orarr_any(signalItem.drivers, _isEventDependentDriver):<EOL><INDENT>return SIGNAL_TYPE.REG<EOL><DEDENT>else:<EOL><INDENT>if driver_cnt == <NUM_LIT:1>:<EOL><INDENT>d = signalItem.drivers[<NUM_LIT:0>]<EOL>if not isinstance(d, (Assignment, PortItem)):<EOL><INDENT>return SIGNAL_TYPE.REG<EOL><DEDENT><DEDENT>return SIGNAL_TYPE.WIRE<EOL><DEDENT>


maxId = <NUM_LIT:0><EOL>if isinstance(stm, Assignment):<EOL><INDENT>return stm._instId<EOL><DEDENT>elif isinstance(stm, WaitStm):<EOL><INDENT>return maxId<EOL><DEDENT>else:<EOL><INDENT>for _stm in stm._iter_stms():<EOL><INDENT>maxId = max(maxId, getMaxStmIdForStm(_stm))<EOL><DEDENT>return maxId<EOL><DEDENT>

Get maximum _instId from all assigments in statement


maxId = <NUM_LIT:0><EOL>for stm in proc.statements:<EOL><INDENT>maxId = max(maxId, getMaxStmIdForStm(stm))<EOL><DEDENT>return maxId<EOL>

get max statement id,
used for sorting of processes in architecture


@classmethod<EOL><INDENT>def Value(cls, val, ctx: SerializerCtx):<DEDENT>

t = val._dtype<EOL>if isinstance(val, RtlSignalBase):<EOL><INDENT>return cls.SignalItem(val, ctx)<EOL><DEDENT>c = cls.Value_try_extract_as_const(val, ctx)<EOL>if c:<EOL><INDENT>return c<EOL><DEDENT>if isinstance(t, Slice):<EOL><INDENT>return cls.Slice_valAsHdl(t, val, ctx)<EOL><DEDENT>elif isinstance(t, HArray):<EOL><INDENT>return cls.HArrayValAsHdl(t, val, ctx)<EOL><DEDENT>elif isinstance(t, Bits):<EOL><INDENT>return cls.Bits_valAsHdl(t, val, ctx)<EOL><DEDENT>elif isinstance(t, HBool):<EOL><INDENT>return cls.Bool_valAsHdl(t, val, ctx)<EOL><DEDENT>elif isinstance(t, HEnum):<EOL><INDENT>return cls.HEnumValAsHdl(t, val, ctx)<EOL><DEDENT>elif isinstance(t, Integer):<EOL><INDENT>return cls.Integer_valAsHdl(t, val, ctx)<EOL><DEDENT>elif isinstance(t, String):<EOL><INDENT>return cls.String_valAsHdl(t, val, ctx)<EOL><DEDENT>else:<EOL><INDENT>raise SerializerException(<EOL>"<STR_LIT>"<EOL>% (val))<EOL><DEDENT>

:param dst: is signal connected with value
:param val: value object, can be instance of Signal or Value


ctx = copy(self)<EOL>ctx.indent += indent<EOL>return ctx<EOL>

Create copy of this context with increased indent


try:<EOL><INDENT>return self._cache[val]<EOL><DEDENT>except KeyError:<EOL><INDENT>if isinstance(val.val, int):<EOL><INDENT>name = "<STR_LIT>" % val.val<EOL><DEDENT>else:<EOL><INDENT>name = "<STR_LIT>"<EOL><DEDENT>c = self.nameCheckFn(name, val)<EOL>self._cache[val] = c<EOL>return c<EOL><DEDENT>

Get constant name for value
name of constant is reused if same value was used before


try:<EOL><INDENT>return _indentCache[indentNum]<EOL><DEDENT>except KeyError:<EOL><INDENT>i = "<STR_LIT>".join([_indent for _ in range(indentNum)])<EOL>_indentCache[indentNum] = i<EOL>return i<EOL><DEDENT>


s = NameScope(False)<EOL>s.setLevel(<NUM_LIT:1>)<EOL>s[<NUM_LIT:0>].update(cls._keywords_dict)<EOL>return s<EOL>


@classmethod<EOL><INDENT>def asHdl(cls, obj, ctx: SerializerCtx):<DEDENT>

if isinstance(obj, RtlSignalBase):<EOL><INDENT>return cls.SignalItem(obj, ctx)<EOL><DEDENT>elif isinstance(obj, Value):<EOL><INDENT>return cls.Value(obj, ctx)<EOL><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>serFn = getattr(cls, obj.__class__.__name__)<EOL><DEDENT>except AttributeError:<EOL><INDENT>raise SerializerException("<STR_LIT>", obj)<EOL><DEDENT>return serFn(obj, ctx)<EOL><DEDENT>

Convert object to HDL string

:param obj: object to serialize
:param ctx: SerializerCtx instance


@classmethod<EOL><INDENT>def Entity(cls, ent: Entity, ctx: SerializerCtx):<DEDENT>

ent.name = ctx.scope.checkedName(ent.name, ent, isGlobal=True)<EOL>return "<STR_LIT>"<EOL>

Entity is just forward declaration of Architecture, it is not used
in most HDL languages as there is no recursion in hierarchy


@classmethod<EOL><INDENT>def serializationDecision(cls, obj, serializedClasses,<EOL>serializedConfiguredUnits):<DEDENT>

isDeclaration = isinstance(obj, Entity)<EOL>isDefinition = isinstance(obj, Architecture)<EOL>if isDeclaration:<EOL><INDENT>unit = obj.origin<EOL><DEDENT>elif isDefinition:<EOL><INDENT>unit = obj.entity.origin<EOL><DEDENT>else:<EOL><INDENT>return True<EOL><DEDENT>assert isinstance(unit, Unit)<EOL>sd = unit._serializeDecision<EOL>if sd is None:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>prevPriv = serializedClasses.get(unit.__class__, None)<EOL>seriazlize, nextPriv = sd(unit, obj, isDeclaration, prevPriv)<EOL>serializedClasses[unit.__class__] = nextPriv<EOL>return seriazlize<EOL><DEDENT>

Decide if this unit should be serialized or not eventually fix name
to fit same already serialized unit

:param obj: object to serialize
:param serializedClasses: dict {unitCls : unitobj}
:param serializedConfiguredUnits: (unitCls, paramsValues) : unitObj
    where paramsValues are named tuple name:value


@classmethod<EOL><INDENT>def HdlType(cls, typ: HdlType, ctx: SerializerCtx, declaration=False):<DEDENT>

if isinstance(typ, Bits):<EOL><INDENT>sFn = cls.HdlType_bits<EOL><DEDENT>elif isinstance(typ, HEnum):<EOL><INDENT>sFn = cls.HdlType_enum<EOL><DEDENT>elif isinstance(typ, HArray):<EOL><INDENT>sFn = cls.HdlType_array<EOL><DEDENT>elif isinstance(typ, Integer):<EOL><INDENT>sFn = cls.HdlType_int<EOL><DEDENT>elif isinstance(typ, HBool):<EOL><INDENT>sFn = cls.HdlType_bool<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>% (typ.name))<EOL><DEDENT>return sFn(typ, ctx, declaration=declaration)<EOL>


@classmethod<EOL><INDENT>def IfContainer(cls, ifc: IfContainer, ctx: SerializerCtx):<DEDENT>

childCtx = ctx.withIndent()<EOL>def asHdl(statements):<EOL><INDENT>return [cls.asHdl(s, childCtx) for s in statements]<EOL><DEDENT>try:<EOL><INDENT>cond = cls.condAsHdl(ifc.cond, True, ctx)<EOL><DEDENT>except UnsupportedEventOpErr as e:<EOL><INDENT>cond = None<EOL><DEDENT>if cond is None:<EOL><INDENT>assert not ifc.elIfs<EOL>assert not ifc.ifFalse<EOL>stmBuff = [cls.asHdl(s, ctx) for s in ifc.ifTrue]<EOL>return "<STR_LIT:\n>".join(stmBuff)<EOL><DEDENT>elIfs = []<EOL>ifTrue = ifc.ifTrue<EOL>ifFalse = ifc.ifFalse<EOL>if ifFalse is None:<EOL><INDENT>ifFalse = []<EOL><DEDENT>for c, statements in ifc.elIfs:<EOL><INDENT>try:<EOL><INDENT>elIfs.append((cls.condAsHdl(c, True, ctx), asHdl(statements)))<EOL><DEDENT>except UnsupportedEventOpErr as e:<EOL><INDENT>if len(ifc.elIfs) == <NUM_LIT:1> and not ifFalse:<EOL><INDENT>ifFalse = statements<EOL><DEDENT>else:<EOL><INDENT>raise e<EOL><DEDENT><DEDENT><DEDENT>return cls.ifTmpl.render(<EOL>indent=getIndent(ctx.indent),<EOL>cond=cond,<EOL>ifTrue=asHdl(ifTrue),<EOL>elIfs=elIfs,<EOL>ifFalse=asHdl(ifFalse))<EOL>


while len(self) != lvl:<EOL><INDENT>if len(self) > lvl:<EOL><INDENT>self.pop()<EOL><DEDENT>else:<EOL><INDENT>self.append(NameScopeItem(len(self)))<EOL><DEDENT><DEDENT>

Trim or extend scope
lvl = 1 -> only one scope (global)


stms = []<EOL>for st in statements:<EOL><INDENT>if isinstance(st, Assignment):<EOL><INDENT>try:<EOL><INDENT>if not isinstance(st.src, RtlSignalBase):<EOL><INDENT>raise DoesNotContainsTernary()<EOL><DEDENT>d = st.src.singleDriver()<EOL>if not isinstance(d, Operator) or d.operator != AllOps.TERNARY:<EOL><INDENT>raise DoesNotContainsTernary()<EOL><DEDENT>else:<EOL><INDENT>ops = d.operands<EOL>ifc = IfContainer(ops[<NUM_LIT:0>],<EOL>[Assignment(ops[<NUM_LIT:1>], st.dst)],<EOL>[Assignment(ops[<NUM_LIT:2>], st.dst)]<EOL>)<EOL>stms.append(ifc)<EOL>continue<EOL><DEDENT><DEDENT>except (MultipleDriversErr, DoesNotContainsTernary):<EOL><INDENT>pass<EOL><DEDENT>except NoDriverErr:<EOL><INDENT>assert (hasattr(st.src, "<STR_LIT>")<EOL>and st.src._interface is not None)or st.src.defVal.vldMask, st.src<EOL><DEDENT><DEDENT>stms.append(st)<EOL><DEDENT>return stms<EOL>

Convert all ternary operators to IfContainers


body = proc.statements<EOL>extraVars = []<EOL>extraVarsSerialized = []<EOL>hasToBeVhdlProcess = arr_any(body,<EOL>lambda x: isinstance(x,<EOL>(IfContainer,<EOL>SwitchContainer,<EOL>WhileContainer,<EOL>WaitStm)))<EOL>sensitivityList = sorted(<EOL>map(lambda s: cls.sensitivityListItem(s, ctx),<EOL>proc.sensitivityList))<EOL>if hasToBeVhdlProcess:<EOL><INDENT>childCtx = ctx.withIndent()<EOL><DEDENT>else:<EOL><INDENT>childCtx = copy(ctx)<EOL><DEDENT>def createTmpVarFn(suggestedName, dtype):<EOL><INDENT>s = RtlSignal(None, None, dtype, virtualOnly=True)<EOL>s.name = ctx.scope.checkedName(suggestedName, s)<EOL>s.hidden = False<EOL>serializedS = cls.SignalItem(s, childCtx, declaration=True)<EOL>extraVars.append(s)<EOL>extraVarsSerialized.append(serializedS)<EOL>return s<EOL><DEDENT>childCtx.createTmpVarFn = createTmpVarFn<EOL>statemets = [cls.asHdl(s, childCtx) for s in body]<EOL>proc.name = ctx.scope.checkedName(proc.name, proc)<EOL>extraVarsInit = []<EOL>for s in extraVars:<EOL><INDENT>if isinstance(s.defVal, RtlSignalBase) or s.defVal.vldMask:<EOL><INDENT>a = Assignment(s.defVal, s, virtualOnly=True)<EOL>extraVarsInit.append(cls.Assignment(a, childCtx))<EOL><DEDENT>else:<EOL><INDENT>assert s.drivers, s<EOL><DEDENT>for d in s.drivers:<EOL><INDENT>extraVarsInit.append(cls.asHdl(d, childCtx))<EOL><DEDENT><DEDENT>_hasToBeVhdlProcess = hasToBeVhdlProcess<EOL>hasToBeVhdlProcess = extraVars or hasToBeVhdlProcess<EOL>if hasToBeVhdlProcess and not _hasToBeVhdlProcess:<EOL><INDENT>oneIndent = getIndent(<NUM_LIT:1>)<EOL>statemets = list(map(lambda x: oneIndent + x, statemets))<EOL><DEDENT>return cls.processTmpl.render(<EOL>indent=getIndent(ctx.indent),<EOL>name=proc.name,<EOL>hasToBeVhdlProcess=hasToBeVhdlProcess,<EOL>extraVars=extraVarsSerialized,<EOL>sensitivityList="<STR_LIT:U+002CU+0020>".join(sensitivityList),<EOL>statements=extraVarsInit + statemets<EOL>)<EOL>

Serialize HWProcess objects as VHDL

:param scope: name scope to prevent name collisions


def makeTestbenchTemplate(unit: Unit, name: str=None):

if name is None:<EOL><INDENT>name = unit._name + "<STR_LIT>"<EOL><DEDENT>entity = Entity(name)<EOL>arch = Architecture(entity)<EOL>arch.components.append(unit._entity)<EOL>arch.componentInstances.append(unit._entity)<EOL>nl = RtlNetlist()<EOL>ctx = {}<EOL>for p in unit._entity.ports:<EOL><INDENT>t = p._dtype<EOL>if isinstance(t, Bits) and not t == BIT:<EOL><INDENT>t = Bits(t.bit_length(), signed=t.signed,<EOL>forceVector=t.forceVector)<EOL><DEDENT>s = RtlSignal(nl, p.name, t, t.fromPy(<NUM_LIT:0>))<EOL>ctx[p._interface] = s<EOL>p.connectSig(s)<EOL><DEDENT>arch.variables.extend(ctx.values())<EOL>return entity, arch, ctx<EOL>

:param unit: synthesized unit
:return: (entity, arch, context) of testbench


def beforeSim(self, simulator, synthesisedUnit):

top = self.top<EOL>self.tbEnt, self.tbArch, self.tbCtx = makeTestbenchTemplate(top)<EOL>def reg(sigIntf):<EOL><INDENT>"""<STR_LIT>"""<EOL>proc = mkDriverProc(sigIntf, self.tbCtx)<EOL>self.registered[sigIntf._sigInside] = proc<EOL>self.tbArch.processes.append(proc)<EOL><DEDENT>for s in walkPhysInterfaces(top):<EOL><INDENT>if s._direction is INTF_DIRECTION.SLAVEand isinstance(s._dtype, self.supported_type_classes):<EOL><INDENT>reg(s)<EOL><DEDENT><DEDENT>

This method is called before first step of simulation.


def logChange(self, nowTime, sig, nextVal):

try:<EOL><INDENT>hwProc = self.registered[sig]<EOL><DEDENT>except KeyError:<EOL><INDENT>return<EOL><DEDENT>if hwProc.actualTime < nowTime:<EOL><INDENT>a = hwProc.actualTime<EOL>if a < <NUM_LIT:0>:<EOL><INDENT>a = <NUM_LIT:0><EOL><DEDENT>delay = int(nowTime - a)<EOL>if delay > <NUM_LIT:0>:<EOL><INDENT>hwProc.statements.append(<EOL>WaitStm(int(delay) // <NUM_LIT:1000>)<EOL>)<EOL>hwProc.actualTime = nowTime<EOL><DEDENT><DEDENT>try:<EOL><INDENT>nextVal._dtype.forceVector = hwProc.driverFor._dtype.forceVector<EOL><DEDENT>except AttributeError:<EOL><INDENT>pass<EOL><DEDENT>hwProc.statements.extend(<EOL>connect(nextVal, hwProc.driverFor)<EOL>)<EOL>

This method is called for every value change of any signal.


def logPropagation(self, simulator, signal, proceses):


def logApplyingValues(self, simulator, values):


@internal<EOL>def sensitivity(proc: HWProcess, *sensitiveTo):

for s in sensitiveTo:<EOL><INDENT>if isinstance(s, tuple):<EOL><INDENT>sen, s = s<EOL>if sen == SENSITIVITY.ANY:<EOL><INDENT>s.simSensProcs.add(proc)<EOL><DEDENT>elif sen == SENSITIVITY.RISING:<EOL><INDENT>s.simRisingSensProcs.add(proc)<EOL><DEDENT>elif sen == SENSITIVITY.FALLING:<EOL><INDENT>s.simFallingSensProcs.add(proc)<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(sen)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>s.simSensProcs.add(proc)<EOL><DEDENT><DEDENT>


_cond = True<EOL>_vld = True<EOL>for v in conds:<EOL><INDENT>val = bool(v.val)<EOL>fullVld = v.vldMask == <NUM_LIT:1><EOL>if fullVld:<EOL><INDENT>if not val:<EOL><INDENT>return False, True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return False, False<EOL><DEDENT>_cond = _cond and val<EOL>_vld = _vld and fullVld<EOL><DEDENT>return _cond, _vld<EOL>


@internal<EOL>def connectSimPort(simUnit, subSimUnit, srcName, dstName, direction):

if direction == DIRECTION.OUT:<EOL><INDENT>origPort = getattr(subSimUnit, srcName)<EOL>newPort = getattr(simUnit, dstName)<EOL>setattr(subSimUnit, srcName, newPort)<EOL><DEDENT>else:<EOL><INDENT>origPort = getattr(subSimUnit, dstName)<EOL>newPort = getattr(simUnit, srcName)<EOL>setattr(subSimUnit, dstName, newPort)<EOL><DEDENT>subSimUnit._ctx.signals.remove(origPort)<EOL>

Connect ports of simulation models by name


@internal<EOL>def mkUpdater(nextVal: Value, invalidate: bool):

def updater(currentVal):<EOL><INDENT>_nextVal = nextVal.clone()<EOL>if invalidate:<EOL><INDENT>_nextVal.vldMask = <NUM_LIT:0><EOL><DEDENT>return (valueHasChanged(currentVal, _nextVal), _nextVal)<EOL><DEDENT>return updater<EOL>

Create value updater for simulation

:param nextVal: instance of Value which will be asssiggned to signal
:param invalidate: flag which tells if value has been compromised
    and if it should be invaidated
:return: function(value) -> tuple(valueHasChangedFlag, nextVal)


@internal<EOL>def mkArrayUpdater(nextItemVal: Value, indexes: Tuple[Value],<EOL>invalidate: bool):

def updater(currentVal):<EOL><INDENT>if len(indexes) > <NUM_LIT:1>:<EOL><INDENT>raise NotImplementedError("<STR_LIT>")<EOL><DEDENT>_nextItemVal = nextItemVal.clone()<EOL>if invalidate:<EOL><INDENT>_nextItemVal.vldMask = <NUM_LIT:0><EOL><DEDENT>index = indexes[<NUM_LIT:0>]<EOL>change = valueHasChanged(currentVal._getitem__val(index), _nextItemVal)<EOL>currentVal._setitem__val(index, _nextItemVal)<EOL>return (change, currentVal)<EOL><DEDENT>return updater<EOL>

Create value updater for simulation for value of array type

:param nextVal: instance of Value which will be asssiggned to signal
:param indexes: tuple on indexes where value should be updated
    in target array

:return: function(value) -> tuple(valueHasChangedFlag, nextVal)


def assertValSequenceEqual(self, seq1, seq2, msg=None, seq_type=None):

seq1 = allValuesToInts(seq1)<EOL>self.assertSequenceEqual(seq1, seq2, msg, seq_type)<EOL>

An equality assertion for ordered sequences (like lists and tuples).
For the purposes of this function, a valid ordered sequence type is one
which can be indexed, has a length, and has an equality operator.

Args:

:param seq1: can contain instance of values or nested list of them
:param seq2: items are not converted
:param seq_type: The expected datatype of the sequences, or None if no
    datatype should be enforced.
:param msg: Optional message to use on failure instead of a list of
    differences.


randomEnProc = self.simpleRandomizationProcess(intf._ag)<EOL>self.procs.append(randomEnProc)<EOL>

Randomly disable and enable interface for testing purposes


def prepareUnit(self, unit, modelCls=None, dumpModelIn=None,<EOL>onAfterToRtl=None, targetPlatform=DummyPlatform()):

self.u, self.model, self.procs = simPrepare(<EOL>unit,<EOL>modelCls=modelCls,<EOL>targetPlatform=targetPlatform,<EOL>dumpModelIn=dumpModelIn,<EOL>onAfterToRtl=onAfterToRtl)<EOL>

Create simulation model and connect it with interfaces of original unit
and decorate it with agents and collect all simulation processes

:param unit: interface level unit which you wont prepare for simulation
:param modelCls: class of rtl simulation model to run simulation on,
    if is None rtl sim model will be generated from unit
:param dumpModelIn: folder to where put sim model files (if is None
    sim model will be constructed only in memory)
:param onAfterToRtl: callback fn(unit) which will be called unit after
    it will be synthesised to rtl


@internal<EOL>def simBitsT(width: int, signed: Union[bool, None]):

k = (width, signed)<EOL>try:<EOL><INDENT>return __simBitsTCache[k]<EOL><DEDENT>except KeyError:<EOL><INDENT>t = SimBitsT(width, signed)<EOL>__simBitsTCache[k] = t<EOL>return t<EOL><DEDENT>


proc = []<EOL>for intf in unit._interfaces:<EOL><INDENT>if not intf._isExtern:<EOL><INDENT>continue<EOL><DEDENT>intf._initSimAgent()<EOL>assert intf._ag is not None, intf<EOL>agents = [intf._ag, ]<EOL>if intf._direction == INTF_DIRECTION.MASTER:<EOL><INDENT>agProcs = list(map(lambda a: a.getMonitors(), agents))<EOL><DEDENT>elif intf._direction == INTF_DIRECTION.SLAVE:<EOL><INDENT>agProcs = list(map(lambda a: a.getDrivers(), agents))<EOL><DEDENT>else:<EOL><INDENT>raise NotImplementedError("<STR_LIT>" % (<EOL>intf._direction, intf))<EOL><DEDENT>for p in agProcs:<EOL><INDENT>proc.extend(p)<EOL><DEDENT><DEDENT>return proc<EOL>

Walk all interfaces on unit and instantiate agent for every interface.

:return: all monitor/driver functions which should be added to simulation
     as processes


res = []<EOL>append = res.append<EOL>for d in values:<EOL><INDENT>if isinstance(d, int):<EOL><INDENT>append(d)<EOL><DEDENT>else:<EOL><INDENT>append(valToInt(d))<EOL><DEDENT><DEDENT>return res<EOL>

Iterable of values to ints (nonvalid = None)


return valuesToInts(interface._ag.data)<EOL>

Convert all values which has agent collected in time >=0 to integer array.
Invalid value will be None.


def registerWriteCallback(self, callback, getEnFn) -> int:

index = len(self._writeCallbacks)<EOL>self._writeCallbacks.append(None)<EOL>self._writeCallbacksToEn.append((index, callback, getEnFn))<EOL>return index<EOL>

Register writeCallback for signal.
Registration is evaluated at the end of deltastep of simulator.

:param callback: simulation process represented by function(simulator)
    which should be called after update of this signal
:param getEnFn: function() to get initial value for enable of callback


def simUpdateVal(self, simulator, valUpdater):

dirtyFlag, newVal = valUpdater(self._oldVal)<EOL>if dirtyFlag:<EOL><INDENT>self._val = newVal<EOL>newVal.updateTime = simulator.now<EOL>log = simulator.config.logChange<EOL>if log:<EOL><INDENT>log(simulator.now, self, newVal)<EOL><DEDENT>self.simPropagateChanges(simulator)<EOL><DEDENT>

Method called by simulator to update new value for this object


@internal<EOL>def vcdTypeInfoForHType(t) -> Tuple[str, int, Callable[[RtlSignalBase, Value], str]]:

if isinstance(t, (SimBitsT, Bits, HBool)):<EOL><INDENT>return (VCD_SIG_TYPE.WIRE, t.bit_length(), vcdBitsFormatter)<EOL><DEDENT>elif isinstance(t, HEnum):<EOL><INDENT>return (VCD_SIG_TYPE.REAL, <NUM_LIT:1>, vcdEnumFormatter)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError(t)<EOL><DEDENT>


def vcdRegisterInterfaces(self, obj: Union[Interface, Unit],<EOL>parent: Optional[VcdVarWritingScope]):

if hasattr(obj, "<STR_LIT>") and obj._interfaces:<EOL><INDENT>name = obj._name<EOL>parent_ = self.vcdWriter if parent is None else parent<EOL>subScope = parent_.varScope(name)<EOL>self._obj2scope[obj] = subScope<EOL>with subScope:<EOL><INDENT>for chIntf in obj._interfaces:<EOL><INDENT>self.vcdRegisterInterfaces(chIntf, subScope)<EOL><DEDENT>if isinstance(obj, (Unit, SimModel)):<EOL><INDENT>for u in obj._units:<EOL><INDENT>self.vcdRegisterInterfaces(u, subScope)<EOL><DEDENT><DEDENT><DEDENT>return subScope<EOL><DEDENT>else:<EOL><INDENT>t = obj._dtype<EOL>if isinstance(t, self.supported_type_classes):<EOL><INDENT>tName, width, formatter = vcdTypeInfoForHType(t)<EOL>try:<EOL><INDENT>parent.addVar(obj, getSignalName(obj),<EOL>tName, width, formatter)<EOL><DEDENT>except VarAlreadyRegistered:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>

Register signals from interfaces for Interface or Unit instances


try:<EOL><INDENT>self.vcdWriter.logChange(nowTime, sig, nextVal)<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>


@internal<EOL>def isEvDependentOn(sig, process) -> bool:

if sig is None:<EOL><INDENT>return False<EOL><DEDENT>return process in sig.simFallingSensProcsor process in sig.simRisingSensProcs<EOL>

Check if hdl process has event depenency on signal


raise StopSimumulation()<EOL>return<EOL>yield<EOL>

Simulation process used to stop simulation


self._all_signals = []<EOL>for name, s in dstSignalsTuples:<EOL><INDENT>setattr(self, name, None)<EOL>self._all_signals.append([name, s])<EOL><DEDENT>

:param dstSignalsTuples: tuples (name, signal)


@internal<EOL><INDENT>def _add_process(self, proc, priority) -> None:<DEDENT>

self._events.push(self.now, priority, proc)<EOL>

Schedule process on actual time with specified priority


if not self._combUpdateDonePlaned:<EOL><INDENT>return self._scheduleCombUpdateDoneEv()<EOL><DEDENT>else:<EOL><INDENT>return self.combUpdateDoneEv<EOL><DEDENT>

Sim processes can wait on combUpdateDone by:
yield sim.waitOnCombUpdate()

Sim process is then woken up when all combinational updates
are done in this delta step


@internal<EOL><INDENT>def _addHdlProcToRun(self, trigger: SimSignal, proc) -> None:<DEDENT>

<EOL>if not self._applyValPlaned:<EOL><INDENT>self._scheduleApplyValues()<EOL><DEDENT>if isEvDependentOn(trigger, proc):<EOL><INDENT>if self.now == <NUM_LIT:0>:<EOL><INDENT>return  <EOL><DEDENT>self._seqProcsToRun.append(proc)<EOL><DEDENT>else:<EOL><INDENT>self._combProcsToRun.append(proc)<EOL><DEDENT>

Add hdl process to execution queue

:param trigger: instance of SimSignal
:param proc: python generator function representing HDL process


@internal<EOL><INDENT>def _initUnitSignals(self, unit: Unit) -> None:<DEDENT>

<EOL>for s in unit._ctx.signals:<EOL><INDENT>if s.defVal.vldMask:<EOL><INDENT>v = s.defVal.clone()<EOL>s.simUpdateVal(self, mkUpdater(v, False))<EOL><DEDENT><DEDENT>for u in unit._units:<EOL><INDENT>self._initUnitSignals(u)<EOL><DEDENT>for p in unit._processes:<EOL><INDENT>self._addHdlProcToRun(None, p)<EOL><DEDENT>for p, outputs in unit._outputs.items():<EOL><INDENT>containerNames = list(map(lambda x: x[<NUM_LIT:0>], outputs))<EOL>class SpecificIoContainer(IoContainer):<EOL><INDENT>__slots__ = containerNames<EOL><DEDENT>self._outputContainers[p] = SpecificIoContainer(outputs)<EOL><DEDENT>

* Inject default values to simulation

* Instantiate IOs for every process


@internal<EOL><INDENT>def __deleteCombUpdateDoneEv(self) -> Generator[None, None, None]:<DEDENT>

self._combUpdateDonePlaned = False<EOL>return<EOL>yield<EOL>

Callback called on combUpdateDoneEv finished


@internal<EOL><INDENT>def _scheduleCombUpdateDoneEv(self) -> Event:<DEDENT>

assert not self._combUpdateDonePlaned, self.now<EOL>cud = Event(self)<EOL>cud.process_to_wake.append(self.__deleteCombUpdateDoneEv())<EOL>self._add_process(cud, PRIORITY_AGENTS_UPDATE_DONE)<EOL>self._combUpdateDonePlaned = True<EOL>self.combUpdateDoneEv = cud<EOL>return cud<EOL>

Schedule combUpdateDoneEv event to let agents know that current
delta step is ending and values from combinational logic are stable


@internal<EOL><INDENT>def _scheduleApplyValues(self) -> None:<DEDENT>

assert not self._applyValPlaned, self.now<EOL>self._add_process(self._applyValues(), PRIORITY_APPLY_COMB)<EOL>self._applyValPlaned = True<EOL>if self._runSeqProcessesPlaned:<EOL><INDENT>return<EOL><DEDENT>assert not self._seqProcsToRun and not self._runSeqProcessesPlaned, self.now<EOL>self._add_process(self._runSeqProcesses(), PRIORITY_APPLY_SEQ)<EOL>self._runSeqProcessesPlaned = True<EOL>


@internal<EOL><INDENT>def _conflictResolveStrategy(self, newValue: set)-> Tuple[Callable[[Value], bool], bool]:<DEDENT>

invalidate = False<EOL>resLen = len(newValue)<EOL>if resLen == <NUM_LIT:3>:<EOL><INDENT>val, indexes, isEvDependent = newValue<EOL>return (mkArrayUpdater(val, indexes, invalidate), isEvDependent)<EOL><DEDENT>else:<EOL><INDENT>val, isEvDependent = newValue<EOL>return (mkUpdater(val, invalidate), isEvDependent)<EOL><DEDENT>

This functions resolves write conflicts for signal

:param actionSet: set of actions made by process


@internal<EOL><INDENT>def _runCombProcesses(self) -> None:<DEDENT>

for proc in self._combProcsToRun:<EOL><INDENT>cont = self._outputContainers[proc]<EOL>proc(self, cont)<EOL>for sigName, sig in cont._all_signals:<EOL><INDENT>newVal = getattr(cont, sigName)<EOL>if newVal is not None:<EOL><INDENT>res = self._conflictResolveStrategy(newVal)<EOL>updater, isEvDependent = res<EOL>self._valuesToApply.append(<EOL>(sig, updater, isEvDependent, proc))<EOL>setattr(cont, sigName, None)<EOL><DEDENT><DEDENT><DEDENT>self._combProcsToRun = UniqList()<EOL>


@internal<EOL><INDENT>def _runSeqProcesses(self) -> Generator[None, None, None]:<DEDENT>

updates = []<EOL>for proc in self._seqProcsToRun:<EOL><INDENT>try:<EOL><INDENT>outContainer = self._outputContainers[proc]<EOL><DEDENT>except KeyError:<EOL><INDENT>outContainer = None<EOL><DEDENT>proc(self, outContainer)<EOL>if outContainer is not None:<EOL><INDENT>updates.append(outContainer)<EOL><DEDENT><DEDENT>self._seqProcsToRun = UniqList()<EOL>self._runSeqProcessesPlaned = False<EOL>for cont in updates:<EOL><INDENT>for sigName, sig in cont._all_signals:<EOL><INDENT>newVal = getattr(cont, sigName)<EOL>if newVal is not None:<EOL><INDENT>v = self._conflictResolveStrategy(newVal)<EOL>updater, _ = v<EOL>sig.simUpdateVal(self, updater)<EOL>setattr(cont, sigName, None)<EOL><DEDENT><DEDENT><DEDENT>return<EOL>yield<EOL>

Delta step for event dependent processes


@internal<EOL><INDENT>def _applyValues(self) -> Generator[None, None, None]:<DEDENT>

va = self._valuesToApply<EOL>self._applyValPlaned = False<EOL>lav = self.config.logApplyingValues<EOL>if va and lav:<EOL><INDENT>lav(self, va)<EOL><DEDENT>self._valuesToApply = []<EOL>addSp = self._seqProcsToRun.append<EOL>for s, vUpdater, isEventDependent, comesFrom in va:<EOL><INDENT>if isEventDependent:<EOL><INDENT>addSp(comesFrom)<EOL><DEDENT>else:<EOL><INDENT>s.simUpdateVal(self, vUpdater)<EOL><DEDENT><DEDENT>self._runCombProcesses()<EOL>if self._valuesToApply and not self._applyValPlaned:<EOL><INDENT>self._scheduleApplyValues()<EOL><DEDENT>return<EOL>yield<EOL>

Perform delta step by writing stacked values to signals


try:<EOL><INDENT>v = sig._val<EOL><DEDENT>except AttributeError:<EOL><INDENT>v = sig._sigInside._val<EOL><DEDENT>return v.clone()<EOL>


def write(self, val, sig: SimSignal)-> None:

<EOL>try:<EOL><INDENT>simSensProcs = sig.simSensProcs<EOL><DEDENT>except AttributeError:<EOL><INDENT>sig = sig._sigInside<EOL>simSensProcs = sig.simSensProcs<EOL><DEDENT>t = sig._dtype<EOL>if isinstance(val, Value):<EOL><INDENT>v = val.clone()<EOL>v = v._auto_cast(t)<EOL><DEDENT>else:<EOL><INDENT>v = t.fromPy(val)<EOL><DEDENT>sig.simUpdateVal(self, lambda curentV: (<EOL>valueHasChanged(curentV, v), v))<EOL>if not self._applyValPlaned:<EOL><INDENT>if not (simSensProcs or<EOL>sig.simRisingSensProcs or<EOL>sig.simFallingSensProcs):<EOL><INDENT>self._scheduleApplyValues()<EOL><DEDENT>elif (sig._writeCallbacks or<EOL>sig._writeCallbacksToEn):<EOL><INDENT>self._scheduleApplyValues()<EOL><DEDENT><DEDENT>


assert until > self.now<EOL>events = self._events<EOL>schedule = events.push<EOL>next_event = events.pop<EOL>schedule(until, PRIORITY_URGENT, raise_StopSimulation(self))<EOL>try:<EOL><INDENT>while True:<EOL><INDENT>nextTime, priority, process = next_event()<EOL>self.now = nextTime<EOL>if isinstance(process, Event):<EOL><INDENT>process = iter(process)<EOL><DEDENT>while True:<EOL><INDENT>try:<EOL><INDENT>ev = next(process)<EOL><DEDENT>except StopIteration:<EOL><INDENT>break<EOL><DEDENT>if isinstance(ev, Wait):<EOL><INDENT>schedule(nextTime + ev.time, priority, process)<EOL>break<EOL><DEDENT>elif isinstance(ev, Event):<EOL><INDENT>ev.process_to_wake.append(process)<EOL>break<EOL><DEDENT>else:<EOL><INDENT>schedule(nextTime, priority, ev)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>except StopSimumulation:<EOL><INDENT>return<EOL><DEDENT>

Run simulation until specified time
:note: can be used to run simulation again after it ends from time when it ends


self._events.push(self.now, PRIORITY_NORMAL, proc)<EOL>

Add process to events with default priority on current time


def simUnit(self, synthesisedUnit: Unit, until: float, extraProcesses=[]):

beforeSim = self.config.beforeSim<EOL>if beforeSim is not None:<EOL><INDENT>beforeSim(self, synthesisedUnit)<EOL><DEDENT>add_proc = self.add_process<EOL>for p in extraProcesses:<EOL><INDENT>add_proc(p(self))<EOL><DEDENT>self._initUnitSignals(synthesisedUnit)<EOL>self.run(until)<EOL>


return [self.driver]<EOL>

Called before simulation to collect all drivers of interfaces
from this agent


return [self.monitor]<EOL>

Called before simulation to collect all monitors of interfaces
from this agent


raise NotImplementedError()<EOL>

Implement this method to drive your interface
in simulation/verification


raise NotImplementedError()<EOL>

Implement this method to monitor your interface
in simulation/verification


def pprintInterface(intf, prefix="<STR_LIT>", indent=<NUM_LIT:0>, file=sys.stdout):

try:<EOL><INDENT>s = intf._sig<EOL><DEDENT>except AttributeError:<EOL><INDENT>s = "<STR_LIT>"<EOL><DEDENT>if s is not "<STR_LIT>":<EOL><INDENT>s = "<STR_LIT:U+0020>" + repr(s)<EOL><DEDENT>file.write("<STR_LIT>".join([getIndent(indent), prefix, repr(intf._getFullName()),<EOL>s]))<EOL>file.write("<STR_LIT:\n>")<EOL>if isinstance(intf, HObjList):<EOL><INDENT>for i, p in enumerate(intf):<EOL><INDENT>pprintInterface(p, prefix=prefix, indent=indent + <NUM_LIT:1>, file=file)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in intf._interfaces:<EOL><INDENT>pprintInterface(i, indent=indent + <NUM_LIT:1>, file=file)<EOL><DEDENT><DEDENT>


def simPrepare(unit: Unit, modelCls: Optional[SimModel]=None,<EOL>targetPlatform=DummyPlatform(),<EOL>dumpModelIn: str=None, onAfterToRtl=None):

if modelCls is None:<EOL><INDENT>modelCls = toSimModel(<EOL>unit, targetPlatform=targetPlatform, dumpModelIn=dumpModelIn)<EOL><DEDENT>else:<EOL><INDENT>toSimModel(unit)<EOL><DEDENT>if onAfterToRtl:<EOL><INDENT>onAfterToRtl(unit, modelCls)<EOL><DEDENT>reconnectUnitSignalsToModel(unit, modelCls)<EOL>model = modelCls()<EOL>procs = autoAddAgents(unit)<EOL>return unit, model, procs<EOL>

Create simulation model and connect it with interfaces of original unit
and decorate it with agents

:param unit: interface level unit which you wont prepare for simulation
:param modelCls: class of rtl simulation model to run simulation on,
    if is None rtl sim model will be generated from unit
:param targetPlatform: target platform for this synthes
:param dumpModelIn: folder to where put sim model files
    (if is None sim model will be constructed only in memory)
:param onAfterToRtl: callback fn(unit, modelCls) which will be called
    after unit will be synthesised to rtl

:return: tuple (fully loaded unit with connected sim model,
    connected simulation model,
    simulation processes of agents
    )


def toSimModel(unit, targetPlatform=DummyPlatform(), dumpModelIn=None):

sim_code = toRtl(unit,<EOL>targetPlatform=targetPlatform,<EOL>saveTo=dumpModelIn,<EOL>serializer=SimModelSerializer)<EOL>if dumpModelIn is not None:<EOL><INDENT>d = os.path.join(os.getcwd(), dumpModelIn)<EOL>dInPath = d in sys.path<EOL>if not dInPath:<EOL><INDENT>sys.path.insert(<NUM_LIT:0>, d)<EOL><DEDENT>if unit._name in sys.modules:<EOL><INDENT>del sys.modules[unit._name]<EOL><DEDENT>simModule = importlib.import_module(unit._name)<EOL>if not dInPath:<EOL><INDENT>sys.path.remove(d)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>simModule = ModuleType('<STR_LIT>')<EOL>exec(sim_code, simModule.__dict__)<EOL><DEDENT>return simModule.__dict__[unit._name]<EOL>

Create a simulation model for unit

:param unit: interface level unit which you wont prepare for simulation
:param targetPlatform: target platform for this synthes
:param dumpModelIn: folder to where put sim model files
    (otherwise sim model will be constructed only in memory)


obj = synthesisedUnitOrIntf<EOL>subInterfaces = obj._interfaces<EOL>if subInterfaces:<EOL><INDENT>for intf in subInterfaces:<EOL><INDENT>reconnectUnitSignalsToModel(intf, modelCls)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>s = synthesisedUnitOrIntf<EOL>s._sigInside = getattr(modelCls, s._sigInside.name)<EOL><DEDENT>

Reconnect model signals to unit to run simulation with simulation model
but use original unit interfaces for communication

:param synthesisedUnitOrIntf: interface where should be signals
    replaced from signals from modelCls
:param modelCls: simulation model form where signals
    for synthesisedUnitOrIntf should be taken


def simUnitVcd(simModel, stimulFunctions, outputFile=sys.stdout,<EOL>until=<NUM_LIT:100> * Time.ns):

assert isinstance(simModel, SimModel),"<STR_LIT>" % (simModel)<EOL>if isinstance(outputFile, str):<EOL><INDENT>d = os.path.dirname(outputFile)<EOL>if d:<EOL><INDENT>os.makedirs(d, exist_ok=True)<EOL><DEDENT>with open(outputFile, '<STR_LIT:w>') as f:<EOL><INDENT>return _simUnitVcd(simModel, stimulFunctions,<EOL>f, until)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return _simUnitVcd(simModel, stimulFunctions,<EOL>outputFile, until)<EOL><DEDENT>

Syntax sugar
If outputFile is string try to open it as file

:return: hdl simulator object


@internal<EOL>def _simUnitVcd(simModel, stimulFunctions, outputFile, until):

sim = HdlSimulator()<EOL>sim.config = VcdHdlSimConfig(outputFile)<EOL>sim.simUnit(simModel, until=until, extraProcesses=stimulFunctions)<EOL>return sim<EOL>

:param unit: interface level unit to simulate
:param stimulFunctions: iterable of function(env)
    (simpy environment) which are driving the simulation
:param outputFile: file where vcd will be dumped
:param time: endtime of simulation, time units are defined in HdlSimulator
:return: hdl simulator object


def oscilate(sig, period=<NUM_LIT:10> * Time.ns, initWait=<NUM_LIT:0>):

def oscillateStimul(s):<EOL><INDENT>s.write(False, sig)<EOL>halfPeriod = period / <NUM_LIT:2><EOL>yield s.wait(initWait)<EOL>while True:<EOL><INDENT>yield s.wait(halfPeriod)<EOL>s.write(True, sig)<EOL>yield s.wait(halfPeriod)<EOL>s.write(False, sig)<EOL><DEDENT><DEDENT>return oscillateStimul<EOL>

Oscillative simulation driver for your signal
(usually used as clk generator)


def __init__(self, sig: SimSignal, fn, shouldBeEnabledFn):

assert not isinstance(fn, CallbackLoop)<EOL>self.fn = fn<EOL>self.isGenerator = inspect.isgeneratorfunction(fn)<EOL>self.shouldBeEnabledFn = shouldBeEnabledFn<EOL>self._callbackIndex = None<EOL>try:<EOL><INDENT>self.sig = sig._sigInside<EOL><DEDENT>except AttributeError:<EOL><INDENT>self.sig = sig<EOL><DEDENT>

:param sig: signal on which write callback should be used
:attention: if condFn is None callback function is always executed

:ivra fn: function/generator which is callback which should be executed
:ivar isGenerator: flag if callback function is generator
    or normal function
:ivar _callbackIndex: index of callback in write callbacks on sig,
    if is None callback was not registered yet
:ivar shouldBeEnabledFn: function() -> bool, which returns True if this
    callback loop should be enabled


self._callbackIndex = self.sig.registerWriteCallback(<EOL>self.onWriteCallback,<EOL>self.shouldBeEnabledFn)<EOL>return<EOL>yield<EOL>

Process for injecting of this callback loop into simulator


w = to - _from<EOL>return mask(w) << _from<EOL>


return (val >> bitNo) & <NUM_LIT:1><EOL>


if default is not None:<EOL><INDENT>assigTop = default<EOL><DEDENT>else:<EOL><INDENT>assigTop = []<EOL><DEDENT>for cond, statements in reversed(cases):<EOL><INDENT>assigTop = If(cond,<EOL>statements<EOL>).Else(<EOL>assigTop<EOL>)<EOL><DEDENT>return assigTop<EOL>

Generate if tree for cases like (syntax shugar for large elifs)

..code-block:: python
    if cond0:
        statements0
    elif cond1:
        statements1
    else:
        default

:param case: iterable of tuples (condition, statements)
:param default: default statements


res = None<EOL>for i in iterable:<EOL><INDENT>i = toHVal(i)<EOL>if res is None:<EOL><INDENT>res = sigOrVal._eq(i)<EOL><DEDENT>else:<EOL><INDENT>res = res | sigOrVal._eq(i)<EOL><DEDENT><DEDENT>assert res is not None, "<STR_LIT>"<EOL>return res<EOL>

Hdl convertible in operator, check if any of items
in "iterable" equals "sigOrVal"


def StaticForEach(parentUnit, items, bodyFn, name="<STR_LIT>"):

items = list(items)<EOL>itemsCnt = len(items)<EOL>if itemsCnt == <NUM_LIT:0>:<EOL><INDENT>return []<EOL><DEDENT>elif itemsCnt == <NUM_LIT:1>:<EOL><INDENT>return bodyFn(items[<NUM_LIT:0>], <NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>index = parentUnit._reg(name + "<STR_LIT>",<EOL>Bits(log2ceil(itemsCnt + <NUM_LIT:1>), signed=False),<EOL>defVal=<NUM_LIT:0>)<EOL>ackSig = parentUnit._sig(name + "<STR_LIT>")<EOL>statementLists = []<EOL>for i, (statementList, ack) in [(i, bodyFn(item, i))<EOL>for i, item in enumerate(items)]:<EOL><INDENT>statementLists.append(statementList + [(ackSig(ack)), ])<EOL><DEDENT>If(ackSig,<EOL>If(index._eq(itemsCnt - <NUM_LIT:1>),<EOL>index(<NUM_LIT:0>)<EOL>).Else(<EOL>index(index + <NUM_LIT:1>)<EOL>)<EOL>)<EOL>return Switch(index).addCases(<EOL>enumerate(statementLists)<EOL>).Default(<EOL>bodyFn(items[<NUM_LIT:0>], <NUM_LIT:0>)[<NUM_LIT:0>],<EOL>ackSig(True)<EOL>)<EOL><DEDENT>

Generate for loop for static items

:param parentUnit: unit where this code should be instantiated
:param items: items which this "for" itering on
:param bodyFn: function which fn(item, index) or fn(item)
    returns (statementList, ack).
    It's content is performed in every iteration.
    When ack is high loop will fall to next iteration


def connect(src, *destinations, exclude: set=None, fit=False):

assignemnts = []<EOL>if isinstance(src, HObjList):<EOL><INDENT>for dst in destinations:<EOL><INDENT>assert len(src) == len(dst), (src, dst)<EOL><DEDENT>_destinations = [iter(d) for d in destinations]<EOL>for _src in src:<EOL><INDENT>dsts = [next(d) for d in _destinations]<EOL>assignemnts.append(connect(_src, *dsts, exclude=exclude, fit=fit))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for dst in destinations:<EOL><INDENT>assignemnts.append(_connect(src, dst, exclude, fit))<EOL><DEDENT><DEDENT>return assignemnts<EOL>

Connect src (signals/interfaces/values) to all destinations

:param exclude: interfaces on any level on src or destinations
    which should be excluded from connection process
:param fit: auto fit source width to destination width


return sig[howMany:]._concat(sig[:howMany])<EOL>


width = sig._dtype.bit_length()<EOL>return sig[(width - howMany):]._concat(sig[:(width - howMany)])<EOL>


width = sig._dtype.bit_length()<EOL>return sig[(width - howMany):]._concat(vec(<NUM_LIT:0>, howMany))<EOL>


return vec(<NUM_LIT:0>, howMany)._concat(sig[:howMany])<EOL>


if not isinstance(x, (int, float)):<EOL><INDENT>x = int(x)<EOL><DEDENT>if x == <NUM_LIT:0> or x == <NUM_LIT:1>:<EOL><INDENT>res = <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>res = math.ceil(math.log2(x))<EOL><DEDENT>return hInt(res)<EOL>

Returns no of bits required to store x-1
for example x=8 returns 3


if not isinstance(num, int):<EOL><INDENT>num = int(num)<EOL><DEDENT>return num != <NUM_LIT:0> and ((num & (num - <NUM_LIT:1>)) == <NUM_LIT:0>)<EOL>

Check if number or constant is power of two


s = _type.bit_length()<EOL>return math.ceil(s / <NUM_LIT:8>)<EOL>


cond_sig = _intfToSig(cond)<EOL>if not isinstance(cond_sig, RtlSignalBase):<EOL><INDENT>raise IntfLvlConfErr("<STR_LIT>"<EOL>"<STR_LIT>", cond_sig)<EOL><DEDENT>super(If, self).__init__(cond_sig)<EOL>self.rank = <NUM_LIT:1><EOL>self._inputs.append(cond_sig)<EOL>cond_sig.endpoints.append(self)<EOL>ev_dep = arr_any(discoverEventDependency(cond_sig), lambda x: True)<EOL>self._is_completly_event_dependent = ev_dep<EOL>self._now_is_event_dependent = ev_dep<EOL>self._register_stements(statements, self.ifTrue)<EOL>self._get_rtl_context().statements.add(self)<EOL>

:param cond: condition in if statement
:param statements: list of statements which should be active
    if condition is met


s = self<EOL>for val, statements in tupesValStmnts:<EOL><INDENT>s = s.Case(val, statements)<EOL><DEDENT>return s<EOL>

Add multiple case statements from iterable of tuleles
(caseVal, statements)


assert self.parentStm is None<EOL>caseVal = toHVal(caseVal, self.switchOn._dtype)<EOL>assert isinstance(caseVal, Value), caseVal<EOL>assert caseVal._isFullVld(), "<STR_LIT>"<EOL>assert caseVal not in self._case_value_index, (<EOL>"<STR_LIT>", caseVal)<EOL>self.rank += <NUM_LIT:1><EOL>case = []<EOL>self._case_value_index[caseVal] = len(self.cases)<EOL>self.cases.append((caseVal, case))<EOL>cond = self.switchOn._eq(caseVal)<EOL>self._inputs.append(cond)<EOL>cond.endpoints.append(self)<EOL>self._register_stements(statements, case)<EOL>return self<EOL>


assert self.parentStm is None<EOL>self.rank += <NUM_LIT:1><EOL>self.default = []<EOL>self._register_stements(statements, self.default)<EOL>return self<EOL>


def __init__(self, parent, stateT, stateRegName="<STR_LIT>"):

if isinstance(stateT, HEnum):<EOL><INDENT>beginVal = stateT.fromPy(stateT._allValues[<NUM_LIT:0>])<EOL><DEDENT>else:<EOL><INDENT>beginVal = <NUM_LIT:0><EOL><DEDENT>self.stateReg = parent._reg(stateRegName, stateT, beginVal)<EOL>Switch.__init__(self, self.stateReg)<EOL>

:param parent: parent unit where fsm should be builded
:param stateT: enum type of state
:param stateRegName: name of register where sate is stored


def Trans(self, stateFrom, *condAndNextState):

top = []<EOL>last = True<EOL>for cAndS in reversed(condAndNextState):<EOL><INDENT>if last is True:<EOL><INDENT>last = False<EOL>try:<EOL><INDENT>condition, newvalue = cAndS<EOL><DEDENT>except TypeError:<EOL><INDENT>top = self.stateReg(cAndS)<EOL>continue<EOL><DEDENT>top = []<EOL><DEDENT>else:<EOL><INDENT>condition, newvalue = cAndS<EOL><DEDENT>top =If(condition,<EOL>self.stateReg(newvalue)<EOL>).Else(<EOL>top<EOL>)<EOL><DEDENT>if stateFrom is None:<EOL><INDENT>return Switch.Default(self, top)<EOL><DEDENT>else:<EOL><INDENT>return Switch.Case(self, stateFrom, top)<EOL><DEDENT>

:param stateFrom: apply when FSM is in this state
:param condAndNextState: tupes (condition, newState),
    last does not to have condition

:attention: transitions has priority, first has the biggest
:attention: if stateFrom is None it is evaluated as default


return fn<EOL>

Decorator which does not affect functionality but it is used as marker
which tells that this object is not interesting for users and it is only used internally


if six.PY3:<EOL><INDENT>return<EOL><DEDENT>http_client.HTTPMessage.get_content_type = http_client.HTTPMessage.gettype<EOL>http_client.HTTPMessage.get_param = http_client.HTTPMessage.getparam<EOL>

Python 2 uses a deprecated method signature and doesn't provide the
forward compatibility.
Add it.


return bool(json.loads(str_val))<EOL>

>>> xml_bool('true')
True
>>> xml_bool('false')
False


def query(self, input, params=(), **kwargs):

data = dict(<EOL>input=input,<EOL>appid=self.app_id,<EOL>)<EOL>data = itertools.chain(params, data.items(), kwargs.items())<EOL>query = urllib.parse.urlencode(tuple(data))<EOL>url = '<STR_LIT>' + query<EOL>resp = urllib.request.urlopen(url)<EOL>assert resp.headers.get_content_type() == '<STR_LIT>'<EOL>assert resp.headers.get_param('<STR_LIT>') == '<STR_LIT:utf-8>'<EOL>return Result(resp)<EOL>

Query Wolfram|Alpha using the v2.0 API

Allows for arbitrary parameters to be passed in
the query. For example, to pass assumptions:

    client.query(input='pi', assumption='*C.pi-_*NamedConstant-')

To pass multiple assumptions, pass multiple items
as params:

    params = (
        ('assumption', '*C.pi-_*NamedConstant-'),
        ('assumption', 'DateOrder_**Day.Month.Year--'),
    )
    client.query(input='pi', params=params)

For more details on Assumptions, see
https://products.wolframalpha.com/api/documentation.html#6


return map(cls, always_iterable(doc))<EOL>

Load instances from the xmltodict result. Always return
an iterable, even if the result is a singleton.


return [subpod.plaintext for subpod in self.subpod]<EOL>

The text from each subpod in this pod as a list.


return itertools.chain(self.pods, self.assumptions, self.warnings)<EOL>

The pods, assumptions, and warnings of this result.


return (<EOL>pod<EOL>for pod in self.pods<EOL>if pod.primary<EOL>or pod.title == '<STR_LIT>'<EOL>)<EOL>

The pods that hold the response to a simple, discrete query.


return {pod.title: pod.text for pod in self.pods}<EOL>

A simplified set of answer text by title.


@command(aliases='<STR_LIT>')<EOL>def wa(client, event, channel, nick, rest):

client = wolframalpha.Client(pmxbot.config['<STR_LIT>'])<EOL>res = client.query(rest)<EOL>return next(res.results).text<EOL>

A free-text query resolver by Wolfram|Alpha. Returns the first
result, if available.


def gen_keywords(*args: Union[ANSIColors, ANSIStyles], **kwargs: Union[ANSIColors, ANSIStyles]) -> tuple:

fields: tuple = tuple()<EOL>values: tuple = tuple()<EOL>for tpl in args:<EOL><INDENT>fields += tpl._fields<EOL>values += tpl<EOL><DEDENT>for prefix, tpl in kwargs.items():<EOL><INDENT>fields += tuple(map(lambda x: '<STR_LIT:_>'.join([prefix, x]), tpl._fields))<EOL>values += tpl<EOL><DEDENT>return namedtuple('<STR_LIT>', fields)(*values)<EOL>

generate single escape sequence mapping.


reducer = lambda x, y: tuple() if y == <NUM_LIT:0> else x + (y,)<EOL>return reduce(reducer, stack, tuple())<EOL>

Handle Resets in input stack.
    Breaks the input stack if a Reset operator (zero) is encountered.


def annihilate(predicate: tuple, stack: tuple) -> tuple:

extra = tuple(filter(lambda x: x not in predicate, stack))<EOL>head = reduce(lambda x, y: y if y in predicate else x, stack, None)<EOL>return extra + (head,) if head else extra<EOL>

Squash and reduce the input stack.
    Removes the elements of input that match predicate and only keeps the last
    match at the end of the stack.


def annihilator(predicate: tuple) -> Callable[[tuple], tuple]:

return partial(annihilate, predicate)<EOL>

Build a partial annihilator for given predicate.


<EOL>reducer = lambda x, y: x if y in x else x + (y,)<EOL>return reduce(reducer, stack, tuple())<EOL>

Remove duplicates from the stack in first-seen order.


def apply(funcs: tuple, stack: tuple) -> tuple:

return reduce(lambda x, y: y(x), funcs, stack)<EOL>

Apply functions to the stack, passing the resulting stack to next state.


def colorize(string: str, stack: tuple) -> str:

codes = optimize(stack)<EOL>if len(codes):<EOL><INDENT>prefix = SEQ % '<STR_LIT:;>'.join(map(str, codes))<EOL>suffix = SEQ % STYLE.reset<EOL>return prefix + string + suffix<EOL><DEDENT>else:<EOL><INDENT>return string<EOL><DEDENT>

Apply optimal ANSI escape sequences to the string.


def setup(path: Union[str, Path]) -> None:

if not isinstance(path, Path):<EOL><INDENT>path = Path(path)<EOL><DEDENT>if not path.is_dir():<EOL><INDENT>path = path.parent<EOL><DEDENT>global __base_dir__<EOL>__base_dir__ = path<EOL>container = str(pkg_dir())<EOL>if container not in sys.path:<EOL><INDENT>sys.path.insert(<NUM_LIT:0>, container)<EOL>env['<STR_LIT>'] = '<STR_LIT>'.format(container, env['<STR_LIT>'] | '<STR_LIT>')<EOL><DEDENT>

Use the supplied path to initialise the tests base dir.
    If _path is a file, its dirname is used.


index = List.range(self.depth + <NUM_LIT:1>)<EOL>g = index.fold_left(f)(lambda z, i: lambda a: a.map(z))<EOL>nested = g(self.value)<EOL>def sequence_level(z, depth, tpe):<EOL><INDENT>nesting = lambda z, i: lambda a: a.map(z).sequence(tpe)<EOL>lifter = List.range(depth).fold_left(I)(nesting)<EOL>return z // lifter<EOL><DEDENT>def sequence_type(z, data):<EOL><INDENT>return lambda a: sequence_level(a, *data).map(z)<EOL><DEDENT>h = self.all_effects.reversed.with_index.fold_left(I)(sequence_type)<EOL>return h(nested)<EOL>

**f** must return the same stack type as **self.value** has.
        Iterates over the effects, sequences the inner instance
        successively to the top and joins with the outer instance.
        Example:
        List(Right(Just(1))) => List(Right(Just(List(Right(Just(5))))))
        => List(List(Right(Just(Right(Just(5))))))
        => List(Right(Just(Right(Just(5)))))
        => List(Right(Right(Just(Just(5)))))
        => List(Right(Just(Just(5))))
        => List(Right(Just(5)))
        Note: IO works only as outermost effect, as it cannot sequence


return self.copy(self._flat_map(f))<EOL>

Calls **f** with the inner values, if present, and sequences
        the result, which is assumed to be a complete stack, one by one
        through the old stack.


def lookup(self, TC: type, G: type) -> Optional[TypeClass]:

if isinstance(G, str):<EOL><INDENT>raise ImplicitNotFound(TC, G, f'<STR_LIT>')<EOL><DEDENT>if not isinstance(G, (type, TypeVar, _GenericAlias)):<EOL><INDENT>raise ImplicitNotFound(TC, G, f'<STR_LIT>')<EOL><DEDENT>match = lambda a: self._lookup_type(TC, a)<EOL>def attach_type(tc: TypeClass) -> TypeClass:<EOL><INDENT>setattr(tc, '<STR_LIT>', G)<EOL>return tc<EOL><DEDENT>scrutinee = (<EOL>(<EOL>unbounded_typevar(TC, G)<EOL>if G.__bound__ is None else<EOL>G.__bound__<EOL>)<EOL>if isinstance(G, TypeVar) else<EOL>G<EOL>)<EOL>safe_mro = lambda t: getattr(t, '<STR_LIT>', (t,))<EOL>target = scrutinee.__origin__ if isinstance(scrutinee, _GenericAlias) else scrutinee<EOL>mro = safe_mro(target)<EOL>return next((attach_type(a) for a in map(match, mro) if a is not None), None)<EOL>

Find an instance of the type class `TC` for type `G`.
        Iterates `G`'s parent classes, looking up instances for each,
        checking whether the instance is a subclass of the target type
        class `TC`.


def fold_map(self, fa: F[A], z: B, f: Callable[[A], B], g: Callable[[Z, B], Z]=operator.add) -> Z:

mapped = Functor.fatal(type(fa)).map(fa, f)<EOL>return self.fold_left(mapped)(z)(g)<EOL>

map `f` over the traversable, then fold over the result
        using the supplied initial element `z` and operation `g`,
        defaulting to addition for the latter.


@abc.abstractmethod<EOL><INDENT>def ap(self, fa: F, f: F) -> F:<DEDENT>

...<EOL>


result = self.__cur.execute("<STR_LIT>")<EOL>if result is None:<EOL><INDENT>return []<EOL><DEDENT>table_names = [record[<NUM_LIT:0>] for record in result.fetchall()]<EOL>if include_system_table:<EOL><INDENT>return table_names<EOL><DEDENT>return [table for table in table_names if table not in SQLITE_SYSTEM_TABLES]<EOL>

:return: List of table names in the database.
:rtype: list


sqlite_master_record_list = []<EOL>result = self.__cur.execute(<EOL>"<STR_LIT>".format("<STR_LIT:U+002CU+0020>".join(self._SQLITE_MASTER_ATTR_NAME_LIST))<EOL>)<EOL>for record in result.fetchall():<EOL><INDENT>sqlite_master_record_list.append(<EOL>dict(<EOL>[<EOL>[attr_name, item]<EOL>for attr_name, item in zip(self._SQLITE_MASTER_ATTR_NAME_LIST, record)<EOL>]<EOL>)<EOL>)<EOL><DEDENT>return sqlite_master_record_list<EOL>

Get sqlite_master table information as a list of dictionaries.

:return: sqlite_master table information.
:rtype: list

:Sample Code:
    .. code:: python

        from sqliteschema import SQLiteSchemaExtractor

        print(json.dumps(SQLiteSchemaExtractor("sample.sqlite").fetch_sqlite_master(), indent=4))

:Output:
    .. code-block:: json

        [
            {
                "tbl_name": "sample_table",
                "sql": "CREATE TABLE 'sample_table' ('a' INTEGER, 'b' REAL, 'c' TEXT, 'd' REAL, 'e' TEXT)",
                "type": "table",
                "name": "sample_table",
                "rootpage": 2
            },
            {
                "tbl_name": "sample_table",
                "sql": "CREATE INDEX sample_table_a_index ON sample_table('a')",
                "type": "index",
                "name": "sample_table_a_index",
                "rootpage": 3
            }
        ]


if not LOGBOOK_INSTALLED:<EOL><INDENT>return<EOL><DEDENT>logbook.get_level_name(log_level)<EOL>if log_level == logger.level:<EOL><INDENT>return<EOL><DEDENT>if log_level == logbook.NOTSET:<EOL><INDENT>set_logger(is_enable=False)<EOL><DEDENT>else:<EOL><INDENT>set_logger(is_enable=True)<EOL><DEDENT>logger.level = log_level<EOL>try:<EOL><INDENT>import pytablewriter<EOL>pytablewriter.set_log_level(log_level)<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT>tabledata.set_log_level(log_level)<EOL>

Set logging level of this module. Using
`logbook <https://logbook.readthedocs.io/en/stable/>`__ module for logging.

:param int log_level:
    One of the log level of
    `logbook <https://logbook.readthedocs.io/en/stable/api/base.html>`__.
    Disabled logging if ``log_level`` is ``logbook.NOTSET``.
:raises LookupError: If ``log_level`` is an invalid value.


@register.simple_tag<EOL>def countdown(name, date, description='<STR_LIT>', id='<STR_LIT>', granularity='<STR_LIT>', start=None,<EOL>progressbar=False, progressbar_inversed=False, showpct=False):

end_date = dateparse.parse_datetime(date)<EOL>end = dateformat.format(end_date, '<STR_LIT>')<EOL>content = '<STR_LIT>' + name + '<STR_LIT>'<EOL>content += '<STR_LIT>' + description + '<STR_LIT>'<EOL>if progressbar:<EOL><INDENT>if not end: raise Exception('<STR_LIT>')<EOL>parsed_date = datetime.datetime.combine(<EOL>dateparse.parse_date(start), datetime.time())<EOL>start_date = dateparse.parse_datetime(start) or parsed_date<EOL>now = datetime.datetime.now()<EOL>pct = (now - start_date).total_seconds() /(end_date - start_date).total_seconds()<EOL>pct = int(pct * <NUM_LIT:100>)<EOL>if progressbar_inversed:<EOL><INDENT>pct = <NUM_LIT:100> - pct<EOL><DEDENT>bar = '<STR_LIT>'<EOL>bar += '<STR_LIT>'<EOL>bar += '<STR_LIT>'<EOL>bar += '<STR_LIT>'<EOL>bar += '<STR_LIT>'<EOL>if showpct:<EOL><INDENT>bar += '<STR_LIT>'<EOL><DEDENT>bar = bar.format(pct=pct)<EOL>content += bar<EOL><DEDENT>content += '<STR_LIT>'<EOL>attr = {<EOL>'<STR_LIT:class>': '<STR_LIT>',<EOL>'<STR_LIT>': end,<EOL>'<STR_LIT>': granularity<EOL>}<EOL>if id:<EOL><INDENT>attr['<STR_LIT:id>'] = id<EOL><DEDENT>return html.tag('<STR_LIT>', content, attr)<EOL>


output = '<STR_LIT>'<EOL>for row in rows:<EOL><INDENT>output += '<STR_LIT>'<EOL>for column in row:<EOL><INDENT>output += '<STR_LIT>'.format(s=column)<EOL><DEDENT>output += '<STR_LIT>'<EOL><DEDENT>output += '<STR_LIT>'<EOL>return output<EOL>

Output a simple table with several columns.


@register.simple_tag<EOL>def link(url, text='<STR_LIT>', classes='<STR_LIT>', target='<STR_LIT>', get="<STR_LIT>", **kwargs):

if not (url.startswith('<STR_LIT:http>') or url.startswith('<STR_LIT:/>')):<EOL><INDENT>urlargs = {}<EOL>for arg, val in kwargs.items():<EOL><INDENT>if arg[:<NUM_LIT:4>] == "<STR_LIT>":<EOL><INDENT>urlargs[arg[<NUM_LIT:4>:]] = val<EOL><DEDENT><DEDENT>url = reverse(url, kwargs=urlargs)<EOL>if get:<EOL><INDENT>url += '<STR_LIT:?>' + get<EOL><DEDENT><DEDENT>return html.tag('<STR_LIT:a>', text or url, {<EOL>'<STR_LIT:class>': classes, '<STR_LIT:target>': target, '<STR_LIT>': url})<EOL>


if not url.startswith('<STR_LIT>') and not url[:<NUM_LIT:1>] == '<STR_LIT:/>':<EOL><INDENT>url = settings.STATIC_URL + url<EOL><DEDENT>return '<STR_LIT>'.format(<EOL>src=url)<EOL>


if not url.startswith('<STR_LIT>') and not url[:<NUM_LIT:1>] == '<STR_LIT:/>':<EOL><INDENT>url = settings.STATIC_URL + url<EOL><DEDENT>return '<STR_LIT>'.format(src=url)<EOL>


@register.simple_tag<EOL>def img(url, alt='<STR_LIT>', classes='<STR_LIT>', style='<STR_LIT>'):

if not url.startswith('<STR_LIT>') and not url[:<NUM_LIT:1>] == '<STR_LIT:/>':<EOL><INDENT>url = settings.STATIC_URL + url<EOL><DEDENT>attr = {<EOL>'<STR_LIT:class>': classes,<EOL>'<STR_LIT>': alt,<EOL>'<STR_LIT>': style,<EOL>'<STR_LIT:src>': url<EOL>}<EOL>return html.tag('<STR_LIT>', '<STR_LIT>', attr)<EOL>


try:<EOL><INDENT>return valid_numeric(value) - valid_numeric(arg)<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>try:<EOL><INDENT>return value - arg<EOL><DEDENT>except Exception:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>


try:<EOL><INDENT>return valid_numeric(value) * valid_numeric(arg)<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>try:<EOL><INDENT>return value * arg<EOL><DEDENT>except Exception:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>


try:<EOL><INDENT>return valid_numeric(value) / valid_numeric(arg)<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>try:<EOL><INDENT>return value / arg<EOL><DEDENT>except Exception:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>


try:<EOL><INDENT>return abs(valid_numeric(value))<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>try:<EOL><INDENT>return abs(value)<EOL><DEDENT>except Exception:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>


try:<EOL><INDENT>return valid_numeric(value) % valid_numeric(arg)<EOL><DEDENT>except (ValueError, TypeError):<EOL><INDENT>try:<EOL><INDENT>return value % arg<EOL><DEDENT>except Exception:<EOL><INDENT>return '<STR_LIT>'<EOL><DEDENT><DEDENT>


if isinstance(obj, ModelForm):<EOL><INDENT>name = obj._meta.model._meta.verbose_name<EOL><DEDENT>elif isinstance(obj, Model):<EOL><INDENT>name = obj._meta.verbose_name<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>' + type(obj))<EOL><DEDENT>return name.capitalize() if capitalize else name<EOL>

Return the verbose name of a model.
The obj argument can be either a Model instance, or a ModelForm instance.
This allows to retrieve the verbose name of the model of a ModelForm
easily, without adding extra context vars.


return obj.user_can_edit(user) if hasattr(obj, "<STR_LIT>") else None<EOL>

If a model implements the user_can_edit method,
this filter returns the result of the method.


pass<EOL>


pass<EOL>

Hook for check or alterations before deleting.


self.object = form.save(commit=False)<EOL>response = self.pre_save(self.object)<EOL>if response:<EOL><INDENT>return response<EOL><DEDENT>self.object.save()<EOL>form.save_m2m()<EOL>self.post_save(self.object)<EOL>return HttpResponseRedirect(self.get_success_url())<EOL>


def delete(self, request, *args, **kwargs):

self.object = self.get_object()<EOL>success_url = self.get_success_url()<EOL>self.pre_delete(self.object)<EOL>self.object.delete()<EOL>self.post_delete(self.object)<EOL>return HttpResponseRedirect(success_url)<EOL>

Calls pre and post delete hooks for DelteViews.


data = super(UserViewMixin, self).get_initial()<EOL>for k in self.user_field:<EOL><INDENT>data[k] = self.request.user<EOL><DEDENT>return data<EOL>

Supply user object as initial data for the specified user_field(s).


return getattr(settings, key, default)<EOL>

Get settings from django.conf if exists,
return default value otherwise

example:

ADMIN_EMAIL = get_config('ADMIN_EMAIL', 'default@email.com')


return Group.objects.filter(name=name).first()<EOL>

Get a django.contrib.auth group model object by group name.


def user_has_group(user, group, superuser_skip=True):

if user.is_superuser and superuser_skip:<EOL><INDENT>return True<EOL><DEDENT>return user.groups.filter(name=group).exists()<EOL>

Check if a user is in a certaing group.
By default, the check is skipped for superusers.


modulepath, classname = class_path.rsplit('<STR_LIT:.>', <NUM_LIT:1>)<EOL>module = __import__(modulepath, fromlist=[classname])<EOL>return getattr(module, classname)<EOL>

Load a class by a fully qualified class_path,
eg. myapp.models.ModelName


templates = [tpl] if type(tpl) != list else tpl<EOL>tpl_instance = None<EOL>for tpl in templates:<EOL><INDENT>try:<EOL><INDENT>tpl_instance = template.loader.get_template(tpl)<EOL>break<EOL><DEDENT>except template.TemplateDoesNotExist:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if not tpl_instance:<EOL><INDENT>raise Exception('<STR_LIT>' + templates[-<NUM_LIT:1>])<EOL><DEDENT>return tpl_instance.render(template.Context(context))<EOL>

A shortcut function to render a partial template with context and return
the output.


def get_object_or_none(qs, *args, **kwargs):

try:<EOL><INDENT>return qs.get(*args, **kwargs)<EOL><DEDENT>except models.ObjectDoesNotExist:<EOL><INDENT>return None<EOL><DEDENT>

Try to retrieve a model, and return None if
it is not found.
Useful if you do not want to bother with the try/except block.


return ContentType.objects.get_for_model(cls)<EOL>

Get the content type of the class, useful for queries.


def decorator(view_func):<EOL><INDENT>@login_required(redirect_field_name=redirect_field_name,<EOL>login_url=login_url)<EOL>def _wrapped_view(request, *args, **kwargs):<EOL><INDENT>if not (request.user.is_superuser and skip_superuser):<EOL><INDENT>if request.user.groups.filter(name=group).count() == <NUM_LIT:0>:<EOL><INDENT>raise PermissionDenied<EOL><DEDENT><DEDENT>return view_func(request, *args, **kwargs)<EOL><DEDENT>return _wrapped_view<EOL><DEDENT>return decorator<EOL>

View decorator for requiring a user group.


data = load_feed(url)<EOL>feed = data['<STR_LIT>']<EOL>entries = data['<STR_LIT>']<EOL>_type = '<STR_LIT>'<EOL>country = '<STR_LIT>'<EOL>for entry in entries:<EOL><INDENT>_id = sluggify(entry['<STR_LIT:id>'])<EOL>city = entry['<STR_LIT>'][<NUM_LIT:0>]['<STR_LIT>']<EOL>landing = entry['<STR_LIT>']<EOL>start_time = dt_normalize(entry['<STR_LIT>'], local_tz=True)<EOL>title = entry['<STR_LIT:title>']<EOL>summary = entry['<STR_LIT>']<EOL>link = entry['<STR_LIT>']<EOL>ipdb.set_trace()<EOL><DEDENT>

Rip the events from a given rss feed, normalize the data and store.


emails = []<EOL>if isinstance(values, str):<EOL><INDENT>values = [values]<EOL><DEDENT>for value in values:<EOL><INDENT>matches = re_emails.findall(value)<EOL>emails.extend([match[<NUM_LIT:2>] for match in matches])<EOL><DEDENT>return emails<EOL>

Take a string or list of strings and try to extract all the emails


def get_token(client_id, client_secret, client_access_token, page=None):

payload = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': client_id,<EOL>'<STR_LIT>': client_secret,<EOL>}<EOL>if client_access_token:<EOL><INDENT>payload['<STR_LIT>'] = '<STR_LIT>'<EOL>payload['<STR_LIT>'] = client_access_token<EOL><DEDENT>response = requests.post(<EOL>'<STR_LIT>',<EOL>params=payload)<EOL>access_token = response.json()['<STR_LIT>']<EOL>return access_token<EOL>


graph = GraphAPI(client_access_token)<EOL>resp = graph.get('<STR_LIT>')<EOL>page_access_token = None<EOL>for page in resp['<STR_LIT:data>']:<EOL><INDENT>if page['<STR_LIT:id>'] == page_id:<EOL><INDENT>page_access_token = page['<STR_LIT>']<EOL>break<EOL><DEDENT><DEDENT>return GraphAPI(page_access_token)<EOL>

You can also skip the above if you get a page token:
http://stackoverflow.com/questions/8231877
and make that long-lived token as in Step 3


pass<EOL>


def update_membership(self, contact, group):

if not contact:<EOL><INDENT>log.debug('<STR_LIT>')<EOL>return None<EOL><DEDENT>_uid = contact.email[<NUM_LIT:0>].address<EOL>_gtitle = group.title.text<EOL>for contact_group in contact.group_membership_info:<EOL><INDENT>if contact_group.href == group.get_id():<EOL><INDENT>log.warn(<EOL>'<STR_LIT>'.format(_uid, _gtitle))<EOL>return contact<EOL><DEDENT><DEDENT>log.debug('<STR_LIT>'.format(_uid, _gtitle))<EOL>membership = self.api.contacts.data.GroupMembershipInfo(<EOL>href=group.id.text)<EOL>contact.group_membership_info.append(membership)<EOL>contact = self.api.update(contact)<EOL>return contact<EOL>

input: gdata ContactEntry and GroupEntry objects


def _create_msg(self, to, subject, msgHtml, msgPlain, attachments=None):

sender = self.sender<EOL>if attachments and isinstance(attachments, str):<EOL><INDENT>attachments = [attachments]<EOL><DEDENT>else:<EOL><INDENT>attachments = list(attachments or [])<EOL><DEDENT>msg = MIMEMultipart('<STR_LIT>')<EOL>msg['<STR_LIT>'] = subject<EOL>msg['<STR_LIT>'] = sender<EOL>msg['<STR_LIT>'] = to<EOL>msg.attach(MIMEText(msgPlain, '<STR_LIT>'))<EOL>msg.attach(MIMEText(msgHtml, '<STR_LIT:html>'))<EOL>for path in attachments:<EOL><INDENT>_attachment = self._prep_attachment(path)<EOL>msg.attach(_attachment)<EOL><DEDENT>raw = base64.urlsafe_b64encode(msg.as_bytes()).decode()<EOL>body = {'<STR_LIT>': raw}<EOL>return body<EOL>


if exc_info is None:<EOL><INDENT>return None<EOL><DEDENT>return traceback.format_exception(*exc_info)<EOL>

Return list of stack text lines or None.


if getattr(this, '<STR_LIT>', {}).get(LOGS_NAME, False):<EOL><INDENT>self.format(record)<EOL>this.send({<EOL>'<STR_LIT>': ADDED,<EOL>'<STR_LIT>': LOGS_NAME,<EOL>'<STR_LIT:id>': meteor_random_id('<STR_LIT>' % LOGS_NAME),<EOL>'<STR_LIT>': {<EOL>attr: {<EOL>'<STR_LIT:args>': lambda args: [repr(arg) for arg in args],<EOL>'<STR_LIT>': datetime.datetime.fromtimestamp,<EOL>'<STR_LIT>': stacklines_or_none,<EOL>}.get(<EOL>attr,<EOL>lambda val: val  <EOL>)(getattr(record, attr, None))<EOL>for attr in (<EOL>'<STR_LIT:args>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT:filename>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT:message>',<EOL>'<STR_LIT:name>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>)<EOL>},<EOL>})<EOL><DEDENT>


def set_default_forwards(app_name, operation, apps, schema_editor):

model = apps.get_model(app_name, operation.model_name)<EOL>for obj_pk in model.objects.values_list('<STR_LIT>', flat=True):<EOL><INDENT>model.objects.filter(pk=obj_pk).update(**{<EOL>operation.name: get_meteor_id(model, obj_pk),<EOL>})<EOL><DEDENT>


def set_default_reverse(app_name, operation, apps, schema_editor):

model = apps.get_model(app_name, operation.model_name)<EOL>for obj_pk in model.objects.values_list('<STR_LIT>', flat=True):<EOL><INDENT>get_meteor_id(model, obj_pk)<EOL><DEDENT>


def __init__(self, forwards=None, backwards=None):

self.truncate_forwards = forwards or []<EOL>self.truncate_backwards = backwards or []<EOL>

Accept model names which are to be migrated.


def truncate(self, app_label, schema_editor, models):

for model_name in models:<EOL><INDENT>model = '<STR_LIT>' % (app_label, model_name)<EOL>schema_editor.execute(<EOL>'<STR_LIT>' % (<EOL>model.lower(),<EOL>),<EOL>)<EOL><DEDENT>


def state_forwards(self, app_label, state):

pass<EOL>


def database_forwards(self, app_label, schema_editor, from_state, to_state):

self.truncate(app_label, schema_editor, self.truncate_forwards)<EOL>

Use schema_editor to apply any forward changes.


def database_backwards(self, app_label, schema_editor, from_state, to_state):

self.truncate(app_label, schema_editor, self.truncate_backwards)<EOL>

Use schema_editor to apply any reverse changes.


return "<STR_LIT>"<EOL>

Describe what the operation does in console output.


if not isinstance(rgt, dict):<EOL><INDENT>return rgt<EOL><DEDENT>result = deepcopy(lft)<EOL>for key, val in rgt.iteritems():<EOL><INDENT>if key in result and isinstance(result[key], dict):<EOL><INDENT>result[key] = dict_merge(result[key], val)<EOL><DEDENT>else:<EOL><INDENT>result[key] = deepcopy(val)<EOL><DEDENT><DEDENT>return result<EOL>

Recursive dict merge.

Recursively merges dict's. not just simple lft['key'] = rgt['key'], if
both lft and rgt have a key who's value is a dict then dict_merge is
called on both values and the result stored in the returned dictionary.


def read(path, default=None, encoding='<STR_LIT:utf8>'):

if not path:<EOL><INDENT>return default<EOL><DEDENT>try:<EOL><INDENT>with io.open(path, mode='<STR_LIT:r>', encoding=encoding) as contents:<EOL><INDENT>return contents.read()<EOL><DEDENT><DEDENT>except IOError:<EOL><INDENT>if default is not None:<EOL><INDENT>return default<EOL><DEDENT>raise<EOL><DEDENT>

Read encoded contents from specified path or return default.


with open(path, mode='<STR_LIT:r>') as json_file:<EOL><INDENT>return loads(json_file.read())<EOL><DEDENT>

Read JSON encoded contents from specified path.


self.runtime_config = {}<EOL>self.meteor_settings = {}<EOL>for other in [<EOL>getattr(settings, '<STR_LIT>', {}),<EOL>loads(os.environ.get('<STR_LIT>', '<STR_LIT:{}>')),<EOL>self.meteor_settings or {},<EOL>kwargs.pop('<STR_LIT>', {}),<EOL>]:<EOL><INDENT>self.meteor_settings = dict_merge(self.meteor_settings, other)<EOL><DEDENT>self.meteor_public_envs = set()<EOL>self.meteor_public_envs.update(<EOL>getattr(settings, '<STR_LIT>', []),<EOL>os.environ.get('<STR_LIT>', '<STR_LIT>').replace('<STR_LIT:U+002C>', '<STR_LIT:U+0020>').split(),<EOL>self.meteor_public_envs or [],<EOL>kwargs.pop('<STR_LIT>', []),<EOL>)<EOL>public = self.meteor_settings.setdefault('<STR_LIT>', {})<EOL>for env_name in self.meteor_public_envs:<EOL><INDENT>try:<EOL><INDENT>public[env_name] = os.environ[env_name]<EOL><DEDENT>except KeyError:<EOL><INDENT>pass  <EOL><DEDENT><DEDENT>super(MeteorView, self).__init__(**kwargs)<EOL>mimetypes.init()<EOL>self.url_map = {}<EOL>self.star_json = read_json(self.json_path)<EOL>star_format = self.star_json['<STR_LIT>']<EOL>if star_format != '<STR_LIT>':<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' % star_format,<EOL>)<EOL><DEDENT>programs = {<EOL>program['<STR_LIT:name>']: program<EOL>for program in self.star_json['<STR_LIT>']<EOL>}<EOL>server_json_path = os.path.join(<EOL>os.path.dirname(self.json_path),<EOL>os.path.dirname(programs['<STR_LIT>']['<STR_LIT:path>']),<EOL>'<STR_LIT>',<EOL>)<EOL>server_json = read_json(server_json_path)<EOL>server_format = server_json['<STR_LIT>']<EOL>if server_format != '<STR_LIT>':<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' % server_format,<EOL>)<EOL><DEDENT>self.server_load_map = {}<EOL>for item in server_json['<STR_LIT>']:<EOL><INDENT>item['<STR_LIT>'] = os.path.join(<EOL>os.path.dirname(server_json_path),<EOL>item['<STR_LIT:path>'],<EOL>)<EOL>self.server_load_map[item['<STR_LIT:path>']] = item<EOL>self.url_map[item['<STR_LIT:path>']] = (<EOL>item['<STR_LIT>'], '<STR_LIT>'<EOL>)<EOL>try:<EOL><INDENT>item['<STR_LIT>'] = os.path.join(<EOL>os.path.dirname(server_json_path),<EOL>item['<STR_LIT>'],<EOL>)<EOL>self.url_map[item['<STR_LIT>']] = (<EOL>item['<STR_LIT>'], '<STR_LIT>'<EOL>)<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>self.template_path = os.path.join(<EOL>os.path.dirname(server_json_path),<EOL>self.server_load_map[<EOL>'<STR_LIT>'<EOL>][<EOL>'<STR_LIT>'<EOL>][<EOL>'<STR_LIT>'<EOL>],<EOL>)<EOL>web_browser_json_path = os.path.join(<EOL>os.path.dirname(self.json_path),<EOL>programs['<STR_LIT>']['<STR_LIT:path>'],<EOL>)<EOL>web_browser_json = read_json(web_browser_json_path)<EOL>web_browser_format = web_browser_json['<STR_LIT>']<EOL>if web_browser_format != '<STR_LIT>':<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' % (<EOL>web_browser_format,<EOL>),<EOL>)<EOL><DEDENT>self.client_map = {}<EOL>self.internal_map = {}<EOL>for item in web_browser_json['<STR_LIT>']:<EOL><INDENT>item['<STR_LIT>'] = os.path.join(<EOL>os.path.dirname(web_browser_json_path),<EOL>item['<STR_LIT:path>'],<EOL>)<EOL>if item['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>if '<STR_LIT:?>' in item['<STR_LIT:url>']:<EOL><INDENT>item['<STR_LIT:url>'] = item['<STR_LIT:url>'].split('<STR_LIT:?>', <NUM_LIT:1>)[<NUM_LIT:0>]<EOL><DEDENT>if item['<STR_LIT:url>'].startswith('<STR_LIT:/>'):<EOL><INDENT>item['<STR_LIT:url>'] = item['<STR_LIT:url>'][<NUM_LIT:1>:]<EOL><DEDENT>self.client_map[item['<STR_LIT:url>']] = item<EOL>self.url_map[item['<STR_LIT:url>']] = (<EOL>item['<STR_LIT>'],<EOL>mimetypes.guess_type(<EOL>item['<STR_LIT>'],<EOL>)[<NUM_LIT:0>] or '<STR_LIT>',<EOL>)<EOL><DEDENT>elif item['<STR_LIT>'] == '<STR_LIT>':<EOL><INDENT>self.internal_map[item['<STR_LIT:type>']] = item<EOL><DEDENT><DEDENT>config = {<EOL>'<STR_LIT>': [<EOL>{'<STR_LIT:url>': item['<STR_LIT:path>']}<EOL>for item in web_browser_json['<STR_LIT>']<EOL>if item['<STR_LIT:type>'] == '<STR_LIT>' and item['<STR_LIT>'] == '<STR_LIT>'<EOL>],<EOL>'<STR_LIT>': [<EOL>{'<STR_LIT:url>': item['<STR_LIT:path>']}<EOL>for item in web_browser_json['<STR_LIT>']<EOL>if item['<STR_LIT:type>'] == '<STR_LIT>' and item['<STR_LIT>'] == '<STR_LIT>'<EOL>],<EOL>'<STR_LIT>': '<STR_LIT>' % (<EOL>dumps(self.runtime_config)<EOL>),<EOL>'<STR_LIT>': self.root_url_path_prefix,<EOL>'<STR_LIT>': self.bundled_js_css_prefix,<EOL>'<STR_LIT>': False,<EOL>'<STR_LIT>': None,<EOL>'<STR_LIT>': read(<EOL>self.internal_map.get('<STR_LIT>', {}).get('<STR_LIT>', None),<EOL>default=u'<STR_LIT>',<EOL>),<EOL>'<STR_LIT:body>': read(<EOL>self.internal_map.get('<STR_LIT:body>', {}).get('<STR_LIT>', None),<EOL>default=u'<STR_LIT>',<EOL>),<EOL>}<EOL>tmpl_raw = read(self.template_path, encoding='<STR_LIT:utf8>')<EOL>compiler = pybars.Compiler()<EOL>tmpl = compiler.compile(tmpl_raw)<EOL>self.html = '<STR_LIT>' % tmpl(config)<EOL>

Initialisation for Django DDP server view.

The following items populate `Meteor.settings` (later take precedence):
  1. django.conf.settings.METEOR_SETTINGS
  2. os.environ['METEOR_SETTINGS']
  3. MeteorView.meteor_settings (class attribute) or empty dict
  4. MeteorView.as_view(meteor_settings=...)

Additionally, `Meteor.settings.public` is updated with values from
environemnt variables specified from the following sources:
  1. django.conf.settings.METEOR_PUBLIC_ENVS
  2. os.environ['METEOR_PUBLIC_ENVS']
  3. MeteorView.meteor_public_envs (class attribute) or empty dict
  4. MeteorView.as_view(meteor_public_envs=...)


if path == '<STR_LIT>':<EOL><INDENT>config = {<EOL>'<STR_LIT>': request.build_absolute_uri('<STR_LIT:/>'),<EOL>'<STR_LIT>': self.meteor_settings.get('<STR_LIT>', {}),<EOL>'<STR_LIT>': request.build_absolute_uri(<EOL>'<STR_LIT>' % (<EOL>self.runtime_config.get('<STR_LIT>', '<STR_LIT>'),<EOL>),<EOL>),<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>}<EOL>if config['<STR_LIT>'].startswith('<STR_LIT>')and settings.SECURE_SSL_REDIRECT:<EOL><INDENT>config['<STR_LIT>'] = '<STR_LIT>' % (<EOL>config['<STR_LIT>'].split('<STR_LIT::>', <NUM_LIT:1>)[<NUM_LIT:1>],<EOL>)<EOL><DEDENT>config.update(self.runtime_config)<EOL>return HttpResponse(<EOL>'<STR_LIT>' % dumps(config),<EOL>content_type='<STR_LIT>',<EOL>)<EOL><DEDENT>try:<EOL><INDENT>file_path, content_type = self.url_map[path]<EOL>with open(file_path, '<STR_LIT:r>') as content:<EOL><INDENT>return HttpResponse(<EOL>content.read(),<EOL>content_type=content_type,<EOL>)<EOL><DEDENT><DEDENT>except KeyError:<EOL><INDENT>return HttpResponse(self.html)<EOL><DEDENT>

Return HTML (or other related content) for Meteor.


return [<EOL>('<STR_LIT>', '<STR_LIT:*>'),<EOL>('<STR_LIT>', '<STR_LIT:false>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>('<STR_LIT>', '<STR_LIT>'),<EOL>]<EOL>

Return list of common headers for SockJS HTTP responses.


start_response(<EOL>'<STR_LIT>',<EOL>[<EOL>('<STR_LIT:Content-Type>', '<STR_LIT>'),<EOL>] + common_headers(environ),<EOL>)<EOL>yield '<STR_LIT>'<EOL>

Dummy method that doesn't handle XHR requests.


import random<EOL>import ejson<EOL>start_response(<EOL>'<STR_LIT>',<EOL>[<EOL>('<STR_LIT:Content-Type>', '<STR_LIT>'),<EOL>] + common_headers(environ),<EOL>)<EOL>yield ejson.dumps(collections.OrderedDict([<EOL>('<STR_LIT>', True),<EOL>('<STR_LIT>', [<EOL>'<STR_LIT>',<EOL>]),<EOL>('<STR_LIT>', False),<EOL>('<STR_LIT>', random.getrandbits(<NUM_LIT:32>)),<EOL>]))<EOL>

Inform client that WebSocket service is available.


def addr(val, default_port=<NUM_LIT>, defualt_host='<STR_LIT:localhost>'):

import re<EOL>import socket<EOL>match = re.match(r'<STR_LIT>', val)<EOL>if match is None:<EOL><INDENT>raise argparse.ArgumentTypeError(<EOL>'<STR_LIT>' % val<EOL>)<EOL><DEDENT>host, port = match.group('<STR_LIT:host>', '<STR_LIT:port>')<EOL>if not host:<EOL><INDENT>host = defualt_host<EOL><DEDENT>if not port:<EOL><INDENT>port = default_port<EOL><DEDENT>elif port.isdigit():<EOL><INDENT>port = int(port)<EOL><DEDENT>else:<EOL><INDENT>port = socket.getservbyname(port)<EOL><DEDENT>return Addr(host, port)<EOL>

Convert a string of format host[:port] into Addr(host, port).

>>> addr('0:80')
Addr(host='0', port=80)

>>> addr('127.0.0.1:80')
Addr(host='127.0.0.1', port=80)

>>> addr('0.0.0.0', default_port=8000)
Addr(host='0.0.0.0', port=8000)


def serve(listen, verbosity=<NUM_LIT:1>, debug_port=<NUM_LIT:0>, **ssl_args):

launcher = DDPLauncher(debug=verbosity == <NUM_LIT:3>, verbosity=verbosity)<EOL>if debug_port:<EOL><INDENT>launcher.servers.append(<EOL>launcher.get_backdoor_server('<STR_LIT>' % debug_port)<EOL>)<EOL><DEDENT>launcher.add_web_servers(listen, **ssl_args)<EOL>sigmap = {<EOL>val: name<EOL>for name, val<EOL>in vars(signal).items()<EOL>if name.startswith('<STR_LIT>')<EOL>}<EOL>def sighandler(signum=None, frame=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>launcher.logger.info(<EOL>'<STR_LIT>',<EOL>sigmap.get(signum, signum),<EOL>frame,<EOL>)<EOL>launcher.stop()<EOL><DEDENT>for signum in [signal.SIGINT, signal.SIGQUIT]:<EOL><INDENT>gevent.signal(signum, sighandler)<EOL><DEDENT>launcher.run()<EOL>

Spawn greenlets for handling websockets and PostgreSQL calls.


parser = argparse.ArgumentParser(description=__doc__)<EOL>django = parser.add_argument_group('<STR_LIT>')<EOL>django.add_argument(<EOL>'<STR_LIT>', '<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>', type=int,<EOL>default=<NUM_LIT:1>,<EOL>)<EOL>django.add_argument(<EOL>'<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>', type=int,<EOL>default=<NUM_LIT:0>,<EOL>)<EOL>django.add_argument(<EOL>'<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>',<EOL>help="<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>",<EOL>)<EOL>http = parser.add_argument_group('<STR_LIT>')<EOL>http.add_argument(<EOL>'<STR_LIT>', metavar='<STR_LIT>', nargs='<STR_LIT:*>', type=addr,<EOL>help='<STR_LIT>',<EOL>)<EOL>ssl = parser.add_argument_group('<STR_LIT>')<EOL>ssl.add_argument('<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>',<EOL>help="<STR_LIT>",<EOL>choices=['<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>'], default='<STR_LIT:3>')<EOL>ssl.add_argument('<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>ssl.add_argument('<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>ssl.add_argument('<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>ssl.add_argument('<STR_LIT>', metavar='<STR_LIT>', dest='<STR_LIT>',<EOL>help="<STR_LIT>")<EOL>namespace = parser.parse_args()<EOL>if namespace.settings:<EOL><INDENT>os.environ['<STR_LIT>'] = namespace.settings<EOL><DEDENT>serve(<EOL>namespace.listen or [Addr('<STR_LIT:localhost>', <NUM_LIT>)],<EOL>debug_port=namespace.debug_port,<EOL>keyfile=namespace.keyfile,<EOL>certfile=namespace.certfile,<EOL>verbosity=namespace.verbosity,<EOL>)<EOL>


def __init__(self, debug=False, verbosity=<NUM_LIT:1>):

import logging<EOL>from django.apps import apps<EOL>from django.utils.module_loading import import_string<EOL>from dddp.websocket import DDPWebSocketApplication<EOL>self.verbosity = verbosity<EOL>self._stop_event = gevent.event.Event()<EOL>self._stop_event.set()  <EOL>self.logger = logging.getLogger('<STR_LIT>')<EOL>if DDPLauncher.pgworker is None:<EOL><INDENT>from django.db import connection, close_old_connections<EOL>from dddp.postgres import PostgresGreenlet<EOL>close_old_connections()<EOL>DDPLauncher.pgworker = PostgresGreenlet(connection)<EOL><DEDENT>from django.conf import settings<EOL>self.wsgi_app = None<EOL>if hasattr(settings, '<STR_LIT>'):<EOL><INDENT>self.wsgi_name = settings.WSGI_APPLICATION<EOL>try:<EOL><INDENT>self.wsgi_app = import_string(self.wsgi_name)<EOL><DEDENT>except ImportError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>if self.wsgi_app is None:<EOL><INDENT>from django.core.wsgi import get_wsgi_application<EOL>self.wsgi_app = get_wsgi_application()<EOL>self.wsgi_name = str(self.wsgi_app.__class__)<EOL><DEDENT>self.api = apps.get_app_config('<STR_LIT>').api<EOL>self.api.pgworker = DDPLauncher.pgworker<EOL>DDPWebSocketApplication.api = self.api<EOL>DDPWebSocketApplication.pgworker = self.pgworker<EOL>self.resource = geventwebsocket.Resource(<EOL>collections.OrderedDict([<EOL>(r'<STR_LIT>', DDPWebSocketApplication),<EOL>(r'<STR_LIT>', DDPWebSocketApplication),<EOL>(r'<STR_LIT>', ddpp_sockjs_xhr),<EOL>(r'<STR_LIT>', ddpp_sockjs_info),<EOL>(r'<STR_LIT>', self.wsgi_app),<EOL>]),<EOL>)<EOL>self.servers = []<EOL>self.threads = []<EOL>

Spawn greenlets for handling websockets and PostgreSQL calls.

Only one pgworker should be started for any process, and ideally only
one instance per node as well (ie: use IPC to communicate events between
processes on the same host).  This class handles the former (per
process), per host IPC is an excercise left to you, dear reader - pull
requests are of course welcome.  ;)

For the record, there's no technical reason that you can't have multiple
pgworker instances running on one host (or even one process), it's just
inefficient to send exactly the same traffic multiple times between your
web servers and your database host.


if self.verbosity >= <NUM_LIT:1>:<EOL><INDENT>print(msg, *args, **kwargs)<EOL><DEDENT>

Print formatted msg if verbosity set at 1 or above.


def add_web_servers(self, listen_addrs, debug=False, **ssl_args):

self.servers.extend(<EOL>self.get_web_server(listen_addr, debug=debug, **ssl_args)<EOL>for listen_addr in listen_addrs<EOL>)<EOL>

Add WebSocketServer for each (host, port) in listen_addrs.


def get_web_server(self, listen_addr, debug=False, **ssl_args):

return geventwebsocket.WebSocketServer(<EOL>listen_addr,<EOL>self.resource,<EOL>debug=debug,<EOL>**{key: val for key, val in ssl_args.items() if val is not None}<EOL>)<EOL>

Setup WebSocketServer on listen_addr (host, port).


def get_backdoor_server(self, listen_addr, **context):

from django.conf import settings<EOL>local_vars = {<EOL>'<STR_LIT>': self,<EOL>'<STR_LIT>': self.servers,<EOL>'<STR_LIT>': self.pgworker,<EOL>'<STR_LIT>': self.stop,<EOL>'<STR_LIT>': self.api,<EOL>'<STR_LIT>': self.resource,<EOL>'<STR_LIT>': settings,<EOL>'<STR_LIT>': self.wsgi_app,<EOL>'<STR_LIT>': self.wsgi_name,<EOL>}<EOL>local_vars.update(context)<EOL>return BackdoorServer(<EOL>listen_addr, banner='<STR_LIT>', locals=local_vars,<EOL>)<EOL>


self.logger.debug('<STR_LIT>')<EOL>self._stop_event.set()<EOL>for server in self.servers + [DDPLauncher.pgworker]:<EOL><INDENT>self.logger.debug('<STR_LIT>', server)<EOL>server.stop()<EOL><DEDENT>gevent.joinall(self.threads + [DDPLauncher.pgworker])<EOL>self.threads = []<EOL>


self.logger.debug('<STR_LIT>')<EOL>self._stop_event.clear()<EOL>self.print('<STR_LIT>')<EOL>if self.verbosity > <NUM_LIT:1>:<EOL><INDENT>for api_path in sorted(self.api.api_path_map()):<EOL><INDENT>print('<STR_LIT>' % api_path)<EOL><DEDENT><DEDENT>self.pgworker.start()<EOL>self.print('<STR_LIT>')<EOL>for server in self.servers:<EOL><INDENT>thread = gevent.spawn(server.serve_forever)<EOL>gevent.sleep()  <EOL>self.threads.append(thread)<EOL>if thread.dead:<EOL><INDENT>self.stop()<EOL>thread.get()<EOL><DEDENT>if isinstance(server, geventwebsocket.WebSocketServer):<EOL><INDENT>self.print(<EOL>'<STR_LIT>' % (<EOL>'<STR_LIT>' if server.ssl_enabled else '<STR_LIT:http>',<EOL>server.server_host,<EOL>server.server_port,<EOL>),<EOL>)<EOL><DEDENT>elif isinstance(server, gevent.backdoor.BackdoorServer):<EOL><INDENT>self.print(<EOL>'<STR_LIT>' % (<EOL>server.server_host,<EOL>server.server_port,<EOL>),<EOL>)<EOL><DEDENT><DEDENT>self.print('<STR_LIT>' % self.wsgi_name)<EOL>

Run PostgresGreenlet and web/debug servers.


self.logger.debug('<STR_LIT>')<EOL>self.start()<EOL>self._stop_event.wait()<EOL>gevent.joinall(self.threads + [DDPLauncher.pgworker])<EOL>self.threads = []<EOL>


if not settings.DATABASES:<EOL><INDENT>raise ImproperlyConfigured('<STR_LIT>')<EOL><DEDENT>for (alias, conf) in settings.DATABASES.items():<EOL><INDENT>engine = conf['<STR_LIT>']<EOL>if engine not in [<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>]:<EOL><INDENT>warnings.warn(<EOL>'<STR_LIT>' % (<EOL>alias, engine,<EOL>),<EOL>UserWarning,<EOL>)<EOL><DEDENT><DEDENT>self.api = autodiscover()<EOL>self.api.ready()<EOL>

Initialisation for django-ddp (setup lookups and signal handlers).


def iter_auth_hashes(user, purpose, minutes_valid):

now = timezone.now().replace(microsecond=<NUM_LIT:0>, second=<NUM_LIT:0>)<EOL>for minute in range(minutes_valid + <NUM_LIT:1>):<EOL><INDENT>yield hashlib.sha1(<EOL>'<STR_LIT>' % (<EOL>now - datetime.timedelta(minutes=minute),<EOL>user.password,<EOL>purpose,<EOL>user.pk,<EOL>settings.SECRET_KEY,<EOL>),<EOL>).hexdigest()<EOL><DEDENT>

Generate auth tokens tied to user and specified purpose.

The hash expires at midnight on the minute of now + minutes_valid, such
that when minutes_valid=1 you get *at least* 1 minute to use the token.


return iter_auth_hashes(user, purpose, minutes_valid=<NUM_LIT:1>).next()<EOL>

Generate a user hash for a particular purpose.


return (<EOL>timezone.now() + datetime.timedelta(minutes=minutes_valid + <NUM_LIT:1>)<EOL>).replace(second=<NUM_LIT:0>, microsecond=<NUM_LIT:0>)<EOL>

Return specific time an auth_hash will expire.


def get_user_token(user, purpose, minutes_valid):

token = '<STR_LIT>'.join(<EOL>dumps([<EOL>user.get_username(),<EOL>get_auth_hash(user, purpose),<EOL>]).encode('<STR_LIT>').split('<STR_LIT:\n>')<EOL>)<EOL>return {<EOL>'<STR_LIT:id>': get_meteor_id(user),<EOL>'<STR_LIT>': token,<EOL>'<STR_LIT>': calc_expiry_time(minutes_valid),<EOL>}<EOL>


def serialize(self, obj, *args, **kwargs):

<EOL>data = super(Users, self).serialize(obj, *args, **kwargs)<EOL>profile = data.pop('<STR_LIT>')<EOL>profile.setdefault('<STR_LIT:name>', obj.get_full_name())<EOL>fields = data['<STR_LIT>'] = {<EOL>'<STR_LIT:username>': obj.get_username(),<EOL>'<STR_LIT>': [],<EOL>'<STR_LIT>': profile,<EOL>'<STR_LIT>': sorted(self.model.get_all_permissions(obj)),<EOL>}<EOL>for sensitive in [<EOL>'<STR_LIT:password>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>]:<EOL><INDENT>profile.pop(sensitive, None)<EOL><DEDENT>try:<EOL><INDENT>fields['<STR_LIT>'] = profile.pop('<STR_LIT>')<EOL><DEDENT>except KeyError:<EOL><INDENT>date_joined = getattr(<EOL>obj, '<STR_LIT>',<EOL>lambda: getattr(obj, '<STR_LIT>', None)<EOL>)()<EOL>if date_joined:<EOL><INDENT>fields['<STR_LIT>'] = date_joined<EOL><DEDENT><DEDENT>try:<EOL><INDENT>email = profile.pop('<STR_LIT:email>')<EOL><DEDENT>except KeyError:<EOL><INDENT>email = getattr(<EOL>obj, '<STR_LIT>',<EOL>lambda: getattr(obj, '<STR_LIT:email>', None)<EOL>)()<EOL><DEDENT>if email:<EOL><INDENT>fields['<STR_LIT>'].append({'<STR_LIT:address>': email, '<STR_LIT>': True})<EOL><DEDENT>return data<EOL>

Serialize user as per Meteor accounts serialization.