def stream(self, whitespace=''): '''This function convert the :class:`css` element into a string.''' # First we execute mixins if self.rendered: raise RuntimeError('%s already rendered' % self) self.rendered = True children = self._children self._children = OrderedDict() for tag, clist in children.items(): for c in clist: c._parent = None s = c.set_parent(self) if s: # the child (mixin) has return a string, added it. yield (None, s) data = [] for k, v in self._attributes: v = as_value(v) if v is not None: data.append('%s %s: %s;' % (whitespace, k, v)) if data: yield (self.tag, '\n'.join(data)) # Mixins and children for child_list in self._children.values(): if isinstance(child_list, list): child = child_list[0] for c in child_list[1:]: child.extend(c) for s in child.stream(whitespace): yield s else: yield None, child_list
def get_form_meta_data(bases, attrs): fields = [] inlines = [] for name, obj in list(attrs.items()): if isinstance(obj, Field): # field name priority is the name in the instance obj.name = obj.name or name fields.append((obj.name, attrs.pop(name))) elif isinstance(obj, FieldList): obj = attrs.pop(name) fields.extend(obj.fields(name + '__')) elif isinstance(obj, FormSet): obj.name = name inlines.append((name, attrs.pop(name))) fields = sorted(fields, key=lambda x: x[1].creation_counter) inlines = sorted(inlines, key=lambda x: x[1].creation_counter) # If this class is subclassing another Form, add that Form's fields. # Note that we loop over the bases in *reverse*. This is necessary in # order to preserve the correct order of fields. for base in bases[::-1]: if hasattr(base, 'base_fields'): fields = list(base.base_fields.items()) + fields return OrderedDict(fields), OrderedDict(inlines)
def __new__(cls, name, bases, attrs): rule_methods = get_roule_methods(attrs.items()) defaults = {} for key, value in list(attrs.items()): if isinstance(value, RouterParam): defaults[key] = attrs.pop(key).value no_rule = set(attrs) - set((x[0] for x in rule_methods)) base_rules = [] for base in reversed(bases): if hasattr(base, 'defaults'): params = base.defaults.copy() params.update(defaults) defaults = params if hasattr(base, 'rule_methods'): items = base.rule_methods.items() else: g = ((key, getattr(base, key)) for key in dir(base)) items = get_roule_methods(g) rules = [pair for pair in items if pair[0] not in no_rule] base_rules = base_rules + rules if base_rules: all = base_rules + rule_methods rule_methods = {} for namerule, rule in all: if namerule in rule_methods: rule = rule.override(rule_methods[namerule]) rule_methods[namerule] = rule rule_methods = sorted(rule_methods.items(), key=lambda x: x[1].order) attrs['rule_methods'] = OrderedDict(rule_methods) attrs['defaults'] = defaults return super(RouterType, cls).__new__(cls, name, bases, attrs)
def stream(self, whitespace=''): '''This function convert the :class:`css` element into a string.''' # First we execute mixins if self.rendered: raise RuntimeError('%s already rendered' % self) self.rendered = True children = self._children self._children = OrderedDict() for tag, clist in iteritems(children): for c in clist: c._parent = None s = c.set_parent(self) if s: # the child (mixin) has return a string, added it. yield (None, s) data = [] for k, v in self._attributes: v = as_value(v) if v is not None: data.append('%s %s: %s;' % (whitespace, k, v)) if data: yield (self.tag, '\n'.join(data)) # yield Mixins and children for child_list in itervalues(self._children): if isinstance(child_list, list): child = child_list[0] for c in child_list[1:]: child.extend(c) for s in child.stream(whitespace): yield s else: yield None, child_list
def apps(self): '''List of :class:`Application` for this :class:`MultiApp`. The list is lazily loaded from the :meth:`build` method.''' if self._apps is None: # Add non-default settings values to the list of cfg params self.cfg.params.update(((s.name, s.value) for s in self.cfg.settings.values() if s.value != s.default)) self.cfg.settings = {} self._apps = [] apps = OrderedDict(self.build()) if not apps: return self._apps # Load the configuration (command line and config file) self.load_config() kwargs = self._get_app_params() for App, name, callable, cfg in self._iter_app(apps): settings = self.cfg.settings new_settings = {} for key in cfg: setting = settings[key].copy() if setting.orig_name and setting.orig_name != setting.name: setting.name = setting.orig_name new_settings[setting.name] = setting cfg.settings = new_settings kwargs.update({'name': name, 'cfg': cfg, 'callable': callable}) if name == self.name: params = kwargs.copy() params['version'] = self.version else: params = kwargs app = App(**params) app() self._apps.append(app) return self._apps
def json_collection(self, request, info): columns = info.columns objs = [] formatter = self.json_format for elem in info.data: obj = self.extract_column_data(request, elem, columns, formatter) objs.append(OrderedDict(self.column_to_name(obj, info.pretty))) return Json(objs)
class TransactionStore(object): '''Transaction for a given store ''' def __init__(self, store): self._store = store self._models = OrderedDict() self.commands = [] def model(self, manager): sm = self._models.get(manager) if sm is None: sm = TransactionModel(manager) self._models[manager] = sm return sm def models(self): return self._models.values()
def json(self, request): all = [] for router in self.routers: fields = router.columns(request) all.append( OrderedDict((('model', router.manager._meta.name), ('api_url', router.path()), ('fields', fields)))) return all
def __init__(self, parent=None, name=None): self.__dict__.update({ '_reserved': { 'name': name, 'parent': parent }, '_data': OrderedDict() })
def clone(self): c = copy(self) c._parent = None c._children = OrderedDict(((name, [c.clone() for c in children]) for name, children in self._children.items())) c._attributes = copy(self._attributes) return c
def __init__(self, tag=None, vars=None, app=None): self._tag = tag self._http = None self._parent = None self._children = OrderedDict() self._attributes = [] if app: assert tag is None, 'app should be passed to the root element only' self._app = app if self._tag is None: self._css_libs = wsgi.Links(self.config('MEDIA_URL', '/media/')) self.variables = Variables() if vars is None else vars self.classes = Variables() self.classes.hover = 'hover' self.classes.active = 'active' elif not tag: raise ValueError('A tag must be defined')
def get(self, request): '''Get all routes grouped in sections. This is the only method available for the router base route.''' if request.response.content_type in JSON_CONTENT_TYPES: json = Json(as_list=True) for name, section in mapping_iterator(self.sections): json.append( OrderedDict( (('name', name), ('routes', section.json(request))))) return json.http_response(request) else: raise HttpException(status=415)
def sections(self): sec = {} unsupported = [] sections = OrderedDict() for info in COMMANDS_INFO.values(): if info.group not in sections: sections[info.group] = [] group = sections[info.group] if info.supported: group.append(info) else: unsupported.append(info) return unsupported, sections
def on_loaded(self, app, handler): templates = app.config['PAGE_TEMPLATES'] dtemplates = OrderedDict() for id, template in enumerate(app.config['PAGE_TEMPLATES'], 1): if not template.key: template.key = 'Template %s' % id dtemplates[template.key] = template app.config['PAGE_TEMPLATES'] = dtemplates models = app.models path = app.config['PAGE_EDIT_URL'] ws = WebSocket('<id>/updates', PageUpdates()) handler.middleware.extend((EditPage(path, models.page, ws), CmsRouter('<path:path>')))
def get_fields(bases, attrs): # fields = [] for name, field in list(attrs.items()): if isinstance(field, Field): fields.append((name, attrs.pop(name))) # fields = sorted(fields, key=lambda x: x[1].creation_counter) # for base in bases: if hasattr(base, '_meta'): fields = list( (name, copy(field)) for name, field in base._meta.dfields.items()) + fields # return OrderedDict(fields)
def render(self, whitespace=''): '''Render the :class:`css` component and all its children''' od = OrderedDict() for tag, data in self.stream(whitespace): if data not in od: od[data] = [] if tag: od[data].append(tag) def _(): for data, tags in od.items(): if tags: yield ',\n'.join(('%s%s' % (whitespace, t) for t in tags) ) + ' {' yield data yield whitespace + '}\n' else: yield data return '\n'.join(_())
def apps(self): '''List of :class:`Application` for this :class:`MultiApp`. The list is lazily loaded from the :meth:`build` method. ''' if self._apps is None: # Add modified settings values to the list of cfg params self.cfg.params.update(((s.name, s.value) for s in self.cfg.settings.values() if s.modified)) self.cfg.settings = {} self._apps = OrderedDict() self._apps.update(self._build()) if not self._apps: return [] # Load the configuration (command line and config file) self.load_config() kwargs = self._get_app_params() apps = self._apps self._apps = [] for App, name, callable, cfg in self._iter_app(apps): settings = self.cfg.settings new_settings = {} for key in cfg: setting = settings[key].copy() if setting.orig_name and setting.orig_name != setting.name: setting.name = setting.orig_name new_settings[setting.name] = setting cfg.settings = new_settings kwargs.update({'name': name, 'cfg': cfg, 'callable': callable}) if name == self.name: params = kwargs.copy() params['version'] = self.version else: params = kwargs self._apps.append(App(**params)) return self._apps
class Css(CssBase): """A :class:`css` element in python. .. attribute:: attributes List of css attributes for the css element. .. attribute:: children An ordered dictionary of children for this :class:`css` element. Children are either other :class:`css` elements or :class:`Mixin`. .. attribute:: parent The :class:`css` ancestor for this :class:`css` element. """ rendered = False theme = None _app = None _css_libs = None def __init__(self, tag=None, vars=None, app=None): self._tag = tag self._http = None self._parent = None self._children = OrderedDict() self._attributes = [] if app: assert tag is None, "app should be passed to the root element only" self._app = app if self._tag is None: self._css_libs = wsgi.Links(self.config("MEDIA_URL", "/media/")) self.variables = Variables() if vars is None else vars self.classes = Variables() self.classes.hover = "hover" self.classes.active = "active" elif not tag: raise ValueError("A tag must be defined") def clone(self): c = copy(self) c._parent = None c._children = OrderedDict(((name, [c.clone() for c in children]) for name, children in self._children.items())) c._attributes = copy(self._attributes) return c @property def tag(self): """The tag for this :class:`Css` element. Always defined unless this is the root instance. """ tag = self._tag if self._parent: ptag = self._parent.tag if ptag: tag = "%s%s" % (ptag, tag) if tag: return tag[1:] if tag.startswith(" ") else tag @property def code(self): """The code for this css tag.""" return self._tag or "ROOT" @property def attributes(self): """Css attributes for this element.""" return self._attributes @property def children(self): """:class:`Css` children of this element.""" return self._children @property def parent(self): return self._parent @property def root(self): if self._parent: return self._parent.root else: return self @property def app(self): return self.root._app @property def http(self): if self._parent: return self._parent.http else: if self._http is None: self._http = HttpClient(loop=asyncio.new_event_loop()) return self._http def __setitem__(self, name, value): if value is None or isinstance(value, Variables): return if isinstance(value, Mixin): raise TypeError("Cannot assign a Mixin to {0}. Use add instead.".format(name)) name = name.replace("_", "-") self._attributes.append((name, value)) def __getitem__(self, name): raise NotImplementedError("cannot get item") def config(self, name, default=None): return self.app.config.get(name, default) if self.app else default def css(self, tag, *components, **attributes): """A child :class:`Css` elements.""" if tag: elems = [Css(t) for t in alltags(tag)] else: elems = [Css(tag)] for clone, css in enumerate(elems): for name, value in attributes.items(): css[name] = value css.set_parent(self) # Loop over components to add them to self for cl in components: if not isinstance(cl, list): cl = (cl,) for c in cl: css.add(c.clone() if clone else c) return elems[0] if len(elems) == 1 else elems def media(self, *type, **query): assert len(type) <= 1 media = Media(type[0] if type else "all", query) self.add(media) return media def get_media_url(self, path): """Build the url for a media path. """ libs = self.root._css_libs if libs: path = libs.absolute_path(path) if not path.startswith("http"): path = "http:%s" % path return path else: raise RuntimeError("No css libs configured") def update(self, iterable): for name, value in mapping_iterator(iterable): self[name] = value def add(self, c): """Add a child :class:`css` or a class:`Mixin`.""" if isinstance(c, CssBase): c.set_parent(self) def add_child(self, child): clist = self._children.get(child.code) if isinstance(clist, list) and child not in clist: clist.append(child) else: self._children[child.code] = [child] def add_stream(self, stream): """Add css text to the element.""" self._children[stream] = stream def set_parent(self, parent): # Get the element if available if getattr(self, "tag", False) is None: if parent: raise ValueError("Body cannot have parent") return self assert parent is not self, "cannot set self as parent" # When switching parents, remove itself from current parent children if self._parent and self._parent is not parent: self._parent.remove(self) self._parent = parent self._parent.add_child(self) def destroy(self): """Safely this :class:`css` from the body tree.""" parent = self.parent if parent: parent.remove(self) def remove(self, child): """Safely remove *child* form this :class:`css` element.""" clist = self._children.get(child.code) if clist: try: clist.remove(child) except ValueError: pass if not clist: self._children.pop(child.code) def extend(self, elem): """Extend by adding *elem* attributes and children.""" self._attributes.extend(elem._attributes) for child_list in tuple(elem._children.values()): for child in child_list: child.set_parent(self) def stream(self, whitespace=""): """This function convert the :class:`css` element into a string.""" # First we execute mixins if self.rendered: raise RuntimeError("%s already rendered" % self) self.rendered = True children = self._children self._children = OrderedDict() for tag, clist in children.items(): for c in clist: c._parent = None s = c.set_parent(self) if s: # the child (mixin) has return a string, added it. yield (None, s) data = [] for k, v in self._attributes: v = as_value(v) if v is not None: data.append("%s %s: %s;" % (whitespace, k, v)) if data: yield (self.tag, "\n".join(data)) # Mixins and children for child_list in self._children.values(): if isinstance(child_list, list): child = child_list[0] for c in child_list[1:]: child.extend(c) for s in child.stream(whitespace): yield s else: yield None, child_list def render(self, whitespace=""): """Render the :class:`css` component and all its children""" od = OrderedDict() for tag, data in self.stream(whitespace): if data not in od: od[data] = [] if tag: od[data].append(tag) def _(): for data, tags in od.items(): if tags: yield ",\n".join(("%s%s" % (whitespace, t) for t in tags)) + " {" yield data yield whitespace + "}\n" else: yield data return "\n".join(_()) def render_all(self, media_url=None, charset="utf-8"): root = self.root if media_url: root.variables.MEDIAURL = media_url start = time.time() body = root.render() created = datetime.fromtimestamp(int(start)) nice_dt = round(time.time() - start, 2) intro = """@charset "UTF-8"; /* ------------------------------------------------------------------ Created by lux in {1} seconds Date: {0} http://quantmind.github.io/lux/ ------------------------------------------------------------------ */ """.format( created.isoformat(" "), nice_dt ) return intro + body def dump(self, theme=None, dump_variables=False): root = self.root root.theme = theme app = root.app if app: module = None # Import applications styles if available exclude = app.config["EXCLUDE_EXTENSIONS_CSS"] or () for extension in app.config["EXTENSIONS"]: if extension in exclude: continue try: module = import_module(extension) if hasattr(module, "add_css"): module.add_css(root) app.write('Imported style from "%s".' % extension) except ImportError as e: app.write_err('Cannot import style %s: "%s".' % (extension, e)) if dump_variables: data = root.variables.tojson() return json.dumps(data, indent=4) else: return root.render_all()
def tojson(self): return OrderedDict(((v.name, v.tojson()) for v in self))
def json(self): '''An ordered dictionary representation of this :class:`Column`. Used when serialising to json.''' return OrderedDict(((name, value) for name, value in zip(self._fields, self) if value is not None))
class MultiApp(Configurator): '''A :class:`MultiApp` is a tool for creating several :class:`Application` and starting them at once. It makes sure all :ref:`settings <settings>` for the applications created are available in the command line. Check the :class:`~examples.taskqueue.manage.server` class in the :ref:`taskqueue example <tutorials-taskqueue>` for an actual implementation. :class:`MultiApp` derives from :class:`Configurator` and therefore supports all its configuration utilities, :meth:`build` is the only method which must be implemented by subclasses. A minimal example usage:: import pulsar class Server(pulsar.MultiApp): def build(self): yield self.new_app(TaskQueue) yield self.new_app(WSGIserver, prefix="rpc", callable=..., ...) yield self.new_app(WSGIserver, prefix="web", callable=..., ...) ''' _apps = None def build(self): '''Virtual method, must be implemented by subclasses and return an iterable over results obtained from calls to the :meth:`new_app` method. ''' raise NotImplementedError def apps(self): '''List of :class:`Application` for this :class:`MultiApp`. The list is lazily loaded from the :meth:`build` method. ''' if self._apps is None: # Add modified settings values to the list of cfg params self.cfg.params.update(((s.name, s.value) for s in self.cfg.settings.values() if s.modified)) self.cfg.settings = {} self._apps = OrderedDict() self._apps.update(self._build()) if not self._apps: return [] # Load the configuration (command line and config file) self.load_config() kwargs = self._get_app_params() apps = self._apps self._apps = [] for App, name, callable, cfg in self._iter_app(apps): settings = self.cfg.settings new_settings = {} for key in cfg: setting = settings[key].copy() if setting.orig_name and setting.orig_name != setting.name: setting.name = setting.orig_name new_settings[setting.name] = setting cfg.settings = new_settings kwargs.update({'name': name, 'cfg': cfg, 'callable': callable}) if name == self.name: params = kwargs.copy() params['version'] = self.version else: params = kwargs self._apps.append(App(**params)) return self._apps def new_app(self, App, prefix=None, callable=None, **params): '''Invoke this method in the :meth:`build` method as many times as the number of :class:`Application` required by this :class:`MultiApp`. :param App: an :class:`Application` class. :param prefix: The prefix to use for the application, the prefix is appended to the application :ref:`config parameters <settings>` and to the application name. Each call to this methjod must use a different value of for this parameter. It can be ``None``. :param callable: optional callable (function of object) used during initialisation of *App* (the :class:`Application.callable`). :param params: additional key-valued parameters used when creating an instance of *App*. :return: a tuple used by the :meth:`apps` method. ''' params.update(self.cfg.params.copy()) params.pop('name', None) # remove the name prefix = prefix or '' if not prefix and '' in self._apps: prefix = App.name or App.__name__.lower() if not prefix: name = self.name cfg = App.create_config(params, name=name) else: name = '%s_%s' % (prefix, self.name) cfg = App.create_config(params, prefix=prefix, name=name) # Add the config entry to the multi app config if not available for k in cfg.settings: if k not in self.cfg.settings: self.cfg.settings[k] = cfg.settings[k] return new_app(prefix, (App, name, callable, cfg)) def __call__(self, actor=None): apps = [app(actor) for app in self.apps()] return multi_async(apps, loop=get_actor()._loop) # INTERNALS def _build(self): for app in self.build(): if not isinstance(app, new_app): raise ImproperlyConfigured( 'You must use new_app when building a MultiApp') yield app def _iter_app(self, app_name_callables): main = app_name_callables.pop('', None) if not main: raise ImproperlyConfigured('No main application in MultiApp') yield main for app in app_name_callables.values(): yield app def _get_app_params(self): params = self.cfg.params.copy() for key, value in self.__dict__.items(): if key.startswith('_'): continue elif key == 'console_parsed': params['parse_console'] = not value else: params[key] = value params['load_config'] = False return params
import time from functools import partial import pulsar from pulsar.utils.structures import OrderedDict from pulsar.utils.pep import to_string from .parser import CommandError COMMANDS_INFO = OrderedDict() def check_input(request, failed): if failed: raise CommandError("wrong number of arguments for '%s'" % request[0]) class command: '''Decorator for pulsar-ds server commands ''' def __init__(self, group, write=False, name=None, script=1, supported=True, subcommands=None): self.group = group self.write = write self.name = name self.script = script
def children(self): if self._children is None: self._children = OrderedDict() return self._children
def __init__(self, store): self._store = store self._models = OrderedDict() self.commands = []
def create_datagram_endpoint(event_loop, protocol_factory, local_addr, remote_addr, family, proto, flags): if not (local_addr or remote_addr): if family == socket.AF_UNSPEC: raise ValueError('unexpected address family') addr_pairs_info = (((family, proto), (None, None)),) else: # join address by (family, protocol) addr_infos = OrderedDict() for idx, addr in enumerate((local_addr, remote_addr)): if addr is not None: assert isinstance(addr, tuple) and len(addr) == 2, ( '2-tuple is expected') infos = yield event_loop.getaddrinfo( *addr, family=family, type=socket.SOCK_DGRAM, proto=proto, flags=flags) if not infos: raise OSError('getaddrinfo() returned empty list') for fam, _, pro, _, address in infos: key = (fam, pro) if key not in addr_infos: addr_infos[key] = [None, None] addr_infos[key][idx] = address # each addr has to have info for each (family, proto) pair addr_pairs_info = [ (key, addr_pair) for key, addr_pair in addr_infos.items() if not ((local_addr and addr_pair[0] is None) or (remote_addr and addr_pair[1] is None))] if not addr_pairs_info: raise ValueError('can not get address information') exceptions = [] for ((family, proto), (local_address, remote_address)) in addr_pairs_info: sock = None l_addr = None r_addr = None try: sock = socket.socket( family=family, type=socket.SOCK_DGRAM, proto=proto) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.setblocking(False) if local_addr: sock.bind(local_address) l_addr = sock.getsockname() if remote_addr: yield event_loop.sock_connect(sock, remote_address) r_addr = remote_address except OSError as exc: if sock is not None: sock.close() exceptions.append(exc) else: break else: raise exceptions[0] protocol = protocol_factory() transport = SocketDatagramTransport(event_loop, sock, protocol, r_addr, extra={'addr': l_addr}) yield transport, protocol
class Css(CssBase): '''A :class:`css` element in python. .. attribute:: attributes List of css attributes for the css element. .. attribute:: children An ordered dictionary of children for this :class:`css` element. Children are either other :class:`css` elements or :class:`Mixin`. .. attribute:: parent The :class:`css` ancestor for this :class:`css` element. ''' rendered = False _app = None _css_libs = None def __init__(self, tag=None, vars=None, app=None, known_libraries=None): self._tag = tag self._http = None self._parent = None self._children = OrderedDict() self._attributes = [] if app: assert tag is None, 'app should be passed to the root element only' self._app = app if self._tag is None: known_libraries = known_libraries or lux.media_libraries self._css_libs = wsgi.Css(self.config('MEDIA_URL', '/media/'), known_libraries=known_libraries) self.variables = Variables() if vars is None else vars self.classes = Variables() self.classes.hover = 'hover' self.classes.active = 'active' elif not tag: raise ValueError('A tag must be defined') def clone(self): c = copy(self) c._parent = None c._children = OrderedDict(((name, [c.clone() for c in children]) for name, children in self._children.items())) c._attributes = copy(self._attributes) return c @property def tag(self): '''The tag for this :class:`Css` element. Always defined unless this is the root instance. ''' return self._full_tag(self._tag) @property def code(self): '''The code for this css tag.''' return self._tag or 'ROOT' @property def attributes(self): '''Css attributes for this element.''' return self._attributes @property def children(self): ''':class:`Css` children of this element.''' return self._children @property def parent(self): return self._parent @property def root(self): if self._parent: return self._parent.root else: return self @property def app(self): return self.root._app @property def http(self): if self._parent: return self._parent.http else: if self._http is None: self._http = HttpClient(loop=asyncio.new_event_loop()) return self._http def __setitem__(self, name, value): if value is None or isinstance(value, Variables): return if isinstance(value, Mixin): raise TypeError('Cannot assign a Mixin to {0}. Use add instead.' .format(name)) name = name.replace('_', '-') self._attributes.append((name, value)) def __getitem__(self, name): raise NotImplementedError('cannot get item') def config(self, name, default=None): return self.app.config.get(name, default) if self.app else default def css(self, tag, *components, **attributes): '''A child :class:`Css` elements.''' if tag: elems = [Css(t) for t in alltags(tag)] else: elems = [Css(tag)] for clone, css in enumerate(elems): for name, value in iteritems(attributes): css[name] = value css.set_parent(self) # Loop over components to add them to self for cl in components: if not isinstance(cl, list): cl = (cl,) for c in cl: css.add(c.clone() if clone else c) return elems[0] if len(elems) == 1 else elems def get_media_url(self, path): '''Build the url for a media path. ''' libs = self.root._css_libs if libs: path = libs.absolute_path(path) if not path.startswith('http'): path = 'http:%s' % path return path else: raise RuntimeError('No css libs configured') def update(self, iterable): for name, value in mapping_iterator(iterable): self[name] = value def add(self, c): '''Add a child :class:`css` or a class:`Mixin`.''' if isinstance(c, CssBase): c.set_parent(self) def add_child(self, child): clist = self._children.get(child.code) if isinstance(clist, list) and child not in clist: clist.append(child) else: self._children[child.code] = [child] def add_stream(self, stream): '''Add css text to the element.''' self._children[stream] = stream def set_parent(self, parent): # Get the element if available if getattr(self, 'tag', False) is None: if parent: raise ValueError('Body cannot have parent') return self assert parent is not self, 'cannot set self as parent' # When switching parents, remove itself from current parent children if self._parent and self._parent is not parent: self._parent.remove(self) self._parent = parent self._parent.add_child(self) def destroy(self): '''Safely this :class:`css` from the body tree.''' parent = self.parent if parent: parent.remove(self) def remove(self, child): '''Safely remove *child* form this :class:`css` element.''' clist = self._children.get(child.code) if clist: try: clist.remove(child) except ValueError: pass if not clist: self._children.pop(child.code) def extend(self, elem): '''Extend by adding *elem* attributes and children.''' self._attributes.extend(elem._attributes) for child_list in itervalues(elem._children): for child in child_list: child.set_parent(self) def stream(self, whitespace=''): '''This function convert the :class:`css` element into a string.''' # First we execute mixins if self.rendered: raise RuntimeError('%s already rendered' % self) self.rendered = True children = self._children self._children = OrderedDict() for tag, clist in iteritems(children): for c in clist: c._parent = None s = c.set_parent(self) if s: # the child (mixin) has return a string, added it. yield (None, s) data = [] for k, v in self._attributes: v = as_value(v) if v is not None: data.append('%s %s: %s;' % (whitespace, k, v)) if data: yield (self.tag, '\n'.join(data)) # yield Mixins and children for child_list in itervalues(self._children): if isinstance(child_list, list): child = child_list[0] for c in child_list[1:]: child.extend(c) for s in child.stream(whitespace): yield s else: yield None, child_list def render(self, whitespace=''): '''Render the :class:`css` component and all its children''' od = OrderedDict() for tag, data in self.stream(whitespace): if data not in od: od[data] = [] if tag: od[data].append(tag) def _(): for data, tags in iteritems(od): if tags: yield ',\n'.join(('%s%s' % (whitespace, t) for t in tags) ) + ' {' yield data yield whitespace + '}\n' else: yield data return '\n'.join(_()) def render_all(self, media_url=None, charset='utf-8'): root = self.root if media_url: root.variables.MEDIAURL = media_url start = time.time() body = root.render() created = datetime.fromtimestamp(int(start)) nice_dt = round(time.time() - start, 2) intro = '''\ /* ------------------------------------------------------------------ ------------------------------------------------------------------ Created by lux {0} in {1} seconds. ------------------------------------------------------------------ ------------------------------------------------------------------ */ '''.format(created.isoformat(' '), nice_dt) return intro + body def dump(self, theme=None, dump_variables=False): root = self.root app = root.app if app: module = None # Import applications styles if available for extension in app.config['EXTENSIONS']: try: module = import_module(extension) if hasattr(module, 'add_css'): module.add_css(root) app.write('Imported style from "%s".' % extension) except ImportError as e: app.write_err('Cannot import style %s: "%s".' % (extension, e)) if dump_variables: data = root.variables.tojson() return json.dumps(data, indent=4) else: return root.render_all() ######################################################################## ## PRIVATE METHODS ######################################################################## def _full_tag(self, tag): if self._parent and self._parent.tag: tag = '%s%s' % (self._parent.tag, tag) if tag: return tag[1:] if tag.startswith(' ') else tag
class Css(CssBase): '''A :class:`css` element in python. .. attribute:: attributes List of css attributes for the css element. .. attribute:: children An ordered dictionary of children for this :class:`css` element. Children are either other :class:`css` elements or :class:`Mixin`. .. attribute:: parent The :class:`css` ancestor for this :class:`css` element. ''' rendered = False theme = None _app = None _css_libs = None def __init__(self, tag=None, vars=None, app=None): self._tag = tag self._http = None self._parent = None self._children = OrderedDict() self._attributes = [] if app: assert tag is None, 'app should be passed to the root element only' self._app = app if self._tag is None: self._css_libs = wsgi.Links(self.config('MEDIA_URL', '/media/')) self.variables = Variables() if vars is None else vars self.classes = Variables() self.classes.hover = 'hover' self.classes.active = 'active' elif not tag: raise ValueError('A tag must be defined') def clone(self): c = copy(self) c._parent = None c._children = OrderedDict(((name, [c.clone() for c in children]) for name, children in self._children.items())) c._attributes = copy(self._attributes) return c @property def tag(self): '''The tag for this :class:`Css` element. Always defined unless this is the root instance. ''' tag = self._tag if self._parent: ptag = self._parent.tag if ptag: tag = '%s%s' % (ptag, tag) if tag: return tag[1:] if tag.startswith(' ') else tag @property def code(self): '''The code for this css tag.''' return self._tag or 'ROOT' @property def attributes(self): '''Css attributes for this element.''' return self._attributes @property def children(self): ''':class:`Css` children of this element.''' return self._children @property def parent(self): return self._parent @property def root(self): if self._parent: return self._parent.root else: return self @property def app(self): return self.root._app @property def http(self): if self._parent: return self._parent.http else: if self._http is None: self._http = HttpClient(loop=asyncio.new_event_loop()) return self._http def __setitem__(self, name, value): if value is None or isinstance(value, Variables): return if isinstance(value, Mixin): raise TypeError('Cannot assign a Mixin to {0}. Use add instead.' .format(name)) name = name.replace('_', '-') self._attributes.append((name, value)) def __getitem__(self, name): raise NotImplementedError('cannot get item') def config(self, name, default=None): return self.app.config.get(name, default) if self.app else default def css(self, tag, *components, **attributes): '''A child :class:`Css` elements.''' if tag: elems = [Css(t) for t in alltags(tag)] else: elems = [Css(tag)] for clone, css in enumerate(elems): for name, value in attributes.items(): css[name] = value css.set_parent(self) # Loop over components to add them to self for cl in components: if not isinstance(cl, list): cl = (cl,) for c in cl: css.add(c.clone() if clone else c) return elems[0] if len(elems) == 1 else elems def media(self, *type, **query): assert len(type) <= 1 media = Media(type[0] if type else 'all', query) self.add(media) return media def get_media_url(self, path): '''Build the url for a media path. ''' libs = self.root._css_libs if libs: path = libs.absolute_path(path) if not path.startswith('http'): path = 'http:%s' % path return path else: raise RuntimeError('No css libs configured') def update(self, iterable): for name, value in mapping_iterator(iterable): self[name] = value def add(self, c): '''Add a child :class:`css` or a class:`Mixin`.''' if isinstance(c, CssBase): c.set_parent(self) def add_child(self, child): clist = self._children.get(child.code) if isinstance(clist, list) and child not in clist: clist.append(child) else: self._children[child.code] = [child] def add_stream(self, stream): '''Add css text to the element.''' self._children[stream] = stream def set_parent(self, parent): # Get the element if available if getattr(self, 'tag', False) is None: if parent: raise ValueError('Body cannot have parent') return self assert parent is not self, 'cannot set self as parent' # When switching parents, remove itself from current parent children if self._parent and self._parent is not parent: self._parent.remove(self) self._parent = parent self._parent.add_child(self) def destroy(self): '''Safely this :class:`css` from the body tree.''' parent = self.parent if parent: parent.remove(self) def remove(self, child): '''Safely remove *child* form this :class:`css` element.''' clist = self._children.get(child.code) if clist: try: clist.remove(child) except ValueError: pass if not clist: self._children.pop(child.code) def extend(self, elem): '''Extend by adding *elem* attributes and children.''' self._attributes.extend(elem._attributes) for child_list in tuple(elem._children.values()): for child in child_list: child.set_parent(self) def stream(self, whitespace=''): '''This function convert the :class:`css` element into a string.''' # First we execute mixins if self.rendered: raise RuntimeError('%s already rendered' % self) self.rendered = True children = self._children self._children = OrderedDict() for tag, clist in children.items(): for c in clist: c._parent = None s = c.set_parent(self) if s: # the child (mixin) has return a string, added it. yield (None, s) data = [] for k, v in self._attributes: v = as_value(v) if v is not None: data.append('%s %s: %s;' % (whitespace, k, v)) if data: yield (self.tag, '\n'.join(data)) # Mixins and children for child_list in self._children.values(): if isinstance(child_list, list): child = child_list[0] for c in child_list[1:]: child.extend(c) for s in child.stream(whitespace): yield s else: yield None, child_list def render(self, whitespace=''): '''Render the :class:`css` component and all its children''' od = OrderedDict() for tag, data in self.stream(whitespace): if data not in od: od[data] = [] if tag: od[data].append(tag) def _(): for data, tags in od.items(): if tags: yield ',\n'.join(('%s%s' % (whitespace, t) for t in tags) ) + ' {' yield data yield whitespace + '}\n' else: yield data return '\n'.join(_()) def render_all(self, media_url=None, charset='utf-8'): root = self.root if media_url: root.variables.MEDIAURL = media_url start = time.time() body = root.render() created = datetime.fromtimestamp(int(start)) nice_dt = round(time.time() - start, 2) intro = '''@charset "UTF-8"; /* ------------------------------------------------------------------ Created by lux in {1} seconds Date: {0} http://quantmind.github.io/lux/ ------------------------------------------------------------------ */ '''.format(created.isoformat(' '), nice_dt) return intro + body def dump(self, theme=None, dump_variables=False): root = self.root root.theme = theme app = root.app if app: module = None # Import applications styles if available exclude = app.config['EXCLUDE_EXTENSIONS_CSS'] or () for extension in app.config['EXTENSIONS']: if extension in exclude: continue try: module = import_module(extension) if hasattr(module, 'add_css'): module.add_css(root) app.write('Imported style from "%s".' % extension) except ImportError as e: app.write_err('Cannot import style %s: "%s".' % (extension, e)) if dump_variables: data = root.variables.tojson() return json.dumps(data, indent=4) else: return root.render_all()
_EPOLLIN = 0x001 _EPOLLPRI = 0x002 _EPOLLOUT = 0x004 _EPOLLERR = 0x008 _EPOLLHUP = 0x010 _EPOLLRDHUP = 0x2000 _EPOLLONESHOT = (1 << 30) _EPOLLET = (1 << 31) # Events map the epoll events READ = _EPOLLIN WRITE = _EPOLLOUT ERROR = _EPOLLERR | _EPOLLHUP | _EPOLLRDHUP _select = select.select POLLERS = OrderedDict() __all__ = ['Poller'] class Poller(object): '''The Poller interface''' def __init__(self): self._handlers = {} def handlers(self, fd): '''Return the handlers for file descriptor ``fd``.''' return self._handlers[fd] def install_waker(self, event_loop):
def json_object(self, request, info): '''Render ``instance`` as ``JSON``.''' obj = self.extract_column_data(request, info.instance, info.columns, self.json_format) return Json(OrderedDict(self.column_to_name(obj, info.pretty)))