def cache_tiddlers(package_name): """ Stores instance tiddlers in the package. reads store_contents from <package>.instance tiddler files are stored in <package>/resources/store """ instance_module = __import__('%s.instance' % package_name, None, None, ['instance']) store_contents = instance_module.store_contents target_store = Store('tiddlywebplugins.pkgstore', {'package': package_name, 'read_only': False}, {}) sources = {} for bag, uris in store_contents.items(): sources[bag] = [] for uri in uris: if uri.endswith('.recipe'): urls = recipe_to_urls(uri) sources[bag].extend(urls) else: sources[bag].append(uri) for bag_name, uris in sources.items(): bag = Bag(bag_name) target_store.put(bag) for uri in uris: std_error_message('retrieving %s' % uri) tiddler = url_to_tiddler(uri) tiddler.bag = bag.name target_store.put(tiddler)
def from_list(uris): """ generates collection of TiddlerS from a list of URIs supports .tiddler, .tid, .js and .recipe files """ sources = [] for uri in uris: if uri.endswith(".recipe"): urls = recipe_to_urls(uri) sources.extend(urls) else: sources.append(uri) return [url_to_tiddler(uri) for uri in sources]
def test_parse_recipe_results(): urls = recipe_to_urls(SAMPLE) assert "http://svn.tiddlywiki.org/Trunk/core/shadows/ColorPalette.tiddler" in urls assert "http://svn.tiddlywiki.org/Trunk/core/shadows/ViewTemplate.tiddler" in urls filenames = [os.path.basename(url) for url in urls] assert len(filenames) == 9 assert "aplugin.js" in filenames assert "bplugin.js" in filenames assert "Welcome.tid" in filenames assert "Greetings.tiddler" in filenames assert "Empty.tiddler" in filenames assert "Hello.tid" in filenames assert "hole.js" in filenames assert "ColorPalette.tiddler" in filenames assert "ViewTemplate.tiddler" in filenames
def test_parse_recipe_results(): urls = recipe_to_urls(SAMPLE) assert 'https://raw.github.com/TiddlyWiki/tiddlywiki/master/shadows/ColorPalette.tid' in urls assert 'https://raw.github.com/TiddlyWiki/tiddlywiki/master/shadows/ViewTemplate.tid' in urls # split is required for optional type filenames = [os.path.basename(url.split(' ', 1)[0]) for url in urls] assert len(filenames) == 10 assert 'aplugin.js' in filenames assert 'bplugin.js' in filenames assert 'Welcome.tid' in filenames assert 'Greetings.tiddler' in filenames assert 'Empty.tiddler' in filenames assert 'Hello.tid' in filenames assert 'hole.js' in filenames assert 'ColorPalette.tid' in filenames assert 'ViewTemplate.tid' in filenames assert 'normalize.css' in filenames
def cache_tiddlers(package_name): """ creates local cache of instance tiddlers to be included in distribution reads store_contents from <package>.instance tiddler files are stored in <package>/resources/<bag> a complete index is stored in <package>/resources """ instance_module = __import__("%s.instance" % package_name, None, None, ["instance"]) # XXX: unnecessarily convoluted and constraining!? store_contents = instance_module.store_contents package_path = os.path.join(*package_name.split(".")) sources = {} for bag, uris in store_contents.items(): sources[bag] = [] for uri in uris: if uri.endswith(".recipe"): urls = recipe_to_urls(uri) sources[bag].extend(urls) else: sources[bag].append(uri) metas = [] for uri in sources[bag]: metas.append("%s.meta" % uri) sources[bag].extend(metas) resources_path = os.path.join(package_path, "resources") try: os.mkdir(resources_path) except OSError: # directory exists pass for bag, uris in sources.items(): bag_path = os.path.join(resources_path, bag) try: os.mkdir(bag_path) except OSError: # directory exists pass for uri in uris: filepath = os.path.join(bag_path, os.path.basename(uri)) std_error_message("retrieving %s" % uri) try: # XXX: duplication of tiddlywebplugins.twimport._get_url_handle try: content = urlopen(uri).read() except (URLError, OSError): scheme, netloc, path, params, query, fragment = urlparse.urlparse(uri) path = quote(path) uri = urlparse.urlunparse((scheme, netloc, path, params, query, fragment)) content = urlopen(uri).read() try: content = unicode(content, "utf-8") write_utf8_file(filepath, content) except UnicodeDecodeError: # assume binary f = open(filepath, "wb") f.write(content) f.close() except (URLError, OSError): if uri.endswith(".meta"): std_error_message("no meta file found for %s" % uri[:-5]) else: raise tiddler_index = "tiddlers.index" tiddler_paths = [] for base_dir, dirs, files in os.walk(resources_path): bag = os.path.basename(base_dir) if bag in store_contents: filepaths = (os.path.join(bag, filename) for filename in files if not filename.endswith(".meta") and not filename == tiddler_index) tiddler_paths.extend(filepaths) filepath = "/".join([resources_path, tiddler_index]) std_error_message("creating %s" % filepath) write_utf8_file(filepath, "\n".join(tiddler_paths))
def test_parse_recipe_path_or_url(): urls_path = recipe_to_urls(SAMPLE) urls_url = recipe_to_urls(URL_SAMPLE) assert urls_path == urls_url