'robots.txt': 'User-agent: *\nDisallow: *', # Misc options 'update_du_with_read_from_disk': False, # Should get rid of this one 'search_client': 'dicoclient', 'search_tip': 'Search files or directories', 'checkbox_tip': 'Search using a regular expression', 'read_from_disk_tip': 'Read the contents of the disc, bypassing the cache', 'logo_href': 'http://localhost', 'logo_img': 'http://localhost/whatever.png', } if settings['search_client'] == 'dicoclient': try: from dicoclient import DicoClient, DicoNotConnectedError dico = DicoClient() dico.open('localhost') except: settings['search_client'] = 'locate' sep = os.path.sep du = {} du_last_read = 0 read_from_disk = '!' @route('/') @route('/:dirpath#.+#') def dircloud(dirpath='/'): global du du = read_du_file_maybe(settings['filename'])
def openfile_fallback(item, pre=True): '''Read the contents of a leaf as indicated in the openfile_fallback parameters When using dircloud to display tree-like structures that don't correspond to a disc directory and files, like simple catalogs, this funcion provides some methods to get the information of the final leaf, filename or record. Currently there are four methods: file, http(s), dict and sqlite. Syntax used is the canonical protocol, with a single printf-like %s field to indicate the record. For sqlite, as it seems that there is no standard protocol syntax, we use a dict-like one, with two extra fields to accomodate the selected column and index field. - file://%s or file://path/%s - http://hostname/%s of http://hostname/whatever?field=%s - dict://host/d:%s or dict://host/d:%s:database - sqlite://path/database.db/d:%s:table:column:key TODO: - resolve how to parameterize pre - accept unicode strings! ''' protocol = args.openfile_fallback.split(':')[0] contents = [] if protocol in ['http', 'https']: # Web browsers already know those protocols. Just redirect # and let the browser do all the job, including error handling. url = args.openfile_fallback % (item) redirect(url) elif protocol == 'file': filepath = args.openfile_fallback.replace('file://', '') filename = filepath % (item) if os.path.isfile(filename): contents.append(read_file_if_exists('.', filename)) else: contents.append('Cannot open %s' % (filename)) elif protocol == 'dict': protocol_re = "(\w+)://([\w./]+)/d:(%s):*([\w:\*]+)*" (protocol, host, what, dictionary) = re.findall(protocol_re, args.openfile_fallback)[0] if not dictionary: dictionary = '*' try: fallback = DicoClient() except: contents.append('Sorry, cannot create dict client for %s' % (args.openfile_fallback)) else: try: fallback.open(host) except: contents.append('Sorry, cannot open dict connection at %s' % (host)) else: definitions = fallback.define(dictionary, item) if 'error' in definitions: contents.append('No results found for %s at %s' % (item, args.openfile_fallback)) else: contents = [definitions['definitions'][i]['desc'] for i in range(len(definitions['definitions']))] elif protocol == 'sqlite': protocol_re = "(\w+)://([\w./]+)/d:(%s):(\w+):(\w+):(\w+)" (protocol, filename, what, table, column, key) = re.findall(protocol_re, args.openfile_fallback)[0] fallback = sqlite3.connect(filename) t = (item,) sql = 'select %(column)s from %(table)s where %(key)s=?;' % { 'column': column, 'table': table, 'key': key, } for row in fallback.execute(sql, t): contents.append(row[0]) if not contents: contents.append('No results found for %s at %s' % (item, args.openfile_fallback)) fallback.close() if pre: out = '<pre>\n%s\n</pre>' % ('\n\n'.join(contents)) else: out = '\n<p />\n'.join(contents) return out