Skip to main content

A package resource and symbol loading helper library.

Project description

Loads resources and symbols from a python package, whether installed as a directory, an egg, or in source form.

TL;DR

Install:

$ pip install asset

Load symbols (e.g. functions, class, or variables) from a package by name:

import asset

# load the 'mypackage.foo.myfunc' function and call it with some parameter
retval = asset.symbol('mypackage.foo.myfunc')(param='value')

Load data files from a package:

import asset

# load the file 'mypackage/templates/data.txt' into string
data = asset.string('mypackage:templates/data.txt')

# or as a file-like stream
stream = asset.stream('mypackage:templates/data.txt')
data   = stream.read()

Multiple files can be operated on at once by using globre style wildcards:

import asset

# concatenate all 'css' files into one string:
css = asset.load('mypackage:static/style/**.css').read()

# load all '.txt' files, XML-escaping the data and wrapping
# each file in an <node name="...">...</node> element.
import xml.etree.ElementTree as ET
data = ET.Element('nodes')
for item in asset.load('asset:**.txt'):
  cur = ET.SubElement(data, 'node', name=item.name)
  cur.text = item.read()
data = ET.tostring(data)

Details

TODO: add detailed docs…

Note: because asset.load() does lazy-loading, it only throws a NoSuchAsset exception when you actually attempt to use the AssetGroup! If you need an immediate error, use the peek() method. Note that it returns itself, so you can do something like:

import asset

def my_function_that_returns_an_iterable():

  return asset.load(my_spec).peek()

  # this returns exactly the same thing as the following:
  #
  #   return asset.load(my_spec)
  #
  # but throws an exception early if there are no matching assets.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asset-0.0.5b.tar.gz (18.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page