# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import absolute_import from multiprocessing import Pipe, Process from .messenger import Messenger from nark import enum import time import threading # Special signal from either side to mark the other as dead. WorkerEvents = enum(TERMINATE=0xfffffff1) class Worker(object): """ Helper class to run things in other processes. Use self.api in remote() and local() to do things. To handle events in a sub-thread, use api.event_loop() To poll once and then do other things, use api.poll(), or api.event_loop(False) Notice that for a 1-time job where all you need to do on the client side is wait for the process to complete and then continue, you can do this: class MyWorker(Worker):
def test_can_create_enum(self): a = nark.Assert() i = nark.enum("ONE", "TWO") a.not_null(i, "Enum instance returned null") a.not_equal(i.ONE, i.TWO, "Enum values are not unique")
reason = "less file updated" break # Found some? Ok, process the target dir if updated: for f in os.listdir(path): f = f.lower() if f[-5:] == ".less": fullpath = join(path, f) output_path = fullpath[:-5] + ".css" run("lessc", fullpath, output_path) log.info("css rebuild: %s" % reason) # Event keys for the less watcher LessWatcherActions = enum("PING", "PONG") class LessWatcher(Worker): """ Creates a process that watches for changes to a file and compiles on demand """ def __init__(self, path): self.updater = Less() self.path = path def remote_update(self): interval = datetime.now() - self.last_update if interval > timedelta(seconds=2): log.info("Remote timeout. Quiting less watcher...") self.api.stop() else:
# Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at: # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from abc import ABCMeta, abstractmethod import nark LOG_CONSTS = nark.enum("INFO", "WARN", "ERROR") """ Constants for the logging service """ class IWriter(object): """ Generic writer type for logs """ __metaclass__ = ABCMeta @abstractmethod def trace(self, level, msg): """ Invoked to record a message """ return @abstractmethod def traceback(self, exception): """ Print a traceback for an exception. """
from nark import enum, LogManager import pau from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine from sqlalchemy import Column, Integer, String from sqlalchemy.orm import sessionmaker from sqlalchemy import ForeignKey from sqlalchemy.orm import relationship, backref from sqlalchemy import Sequence from sqlalchemy import * from sqlalchemy.orm import * import datetime # Possible message types FlashTypes = enum(NOTICE=1, SUCCESS=2, FAILURE=3) class FlashMsg(Base): __tablename__ = 'flash_msg' id = Column(Integer, Sequence('flash_msg_id_seq'), primary_key=True) created_on = Column(DateTime, nullable=False) level = Column(Integer, nullable=False) message = Column(String, nullable=False) def __init__(self, level, message): self.level = level self.message = message self.created_on = datetime.datetime.utcnow()