summaryrefslogtreecommitdiff
path: root/portato/session.py
blob: 636e00a816b5b6ef0f2df7298ba9dcbb7ae81e57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
#
# File: portato/session.py
# This file is part of the Portato-Project, a graphical portage-frontend.
#
# Copyright (C) 2007 René 'Necoro' Neumann
# This is free software.  You may redistribute copies of it under the terms of
# the GNU General Public License version 2.
# There is NO WARRANTY, to the extent permitted by law.
#
# Written by René 'Necoro' Neumann <necoro@necoro.net>

from __future__ import absolute_import, with_statement

import os, os.path

from .config_parser import ConfigParser
from .constants import SESSION_DIR
from .helper import _,debug, info

class Session (object):
	"""
	A small class allowing to save certain states of a program.
	This class works in a quite abstract manner, as it works with handlers, which
	define what options to use out of the config file and how to apply them to the program.

	Note: This class currently does not work with boolean config options. If you
	want to define boolean values, use 0 and 1. This is future proof.
	"""

	# the current session format version
	VERSION = 1

	def __init__ (self, file):
		"""
		Initialize a session with a certain file inside L{SESSION_DIR}.

		@param file: the file in L{SESSION_DIR}, where the options will be saved.
		"""

		self._cfg = None
		self._handlers = []

		if not (os.path.exists(SESSION_DIR) and os.path.isdir(SESSION_DIR)):
			os.mkdir(SESSION_DIR)
		self._cfg = ConfigParser(os.path.join(SESSION_DIR, file))
		info(_("Loading session from '%s'.") % self._cfg.file)
		try:
			self._cfg.parse()
		except IOError, e:
			if e.errno == 2: pass
			else: raise

		# add version check
		self.add_handler(([("version", "session")], self.check_version, lambda: self.VERSION))

	def add_handler (self, (options, load_fn, save_fn)):
		"""
		Adds a handler to this session. A handler is a three-tuple consisting of:
			- a list of (key,section) values
			- a function getting number of option arguments and applying them to the program
			- a function returning the number of option return values - getting them out of the program
		"""
		self._handlers.append((options, load_fn, save_fn))

	def load (self):
		"""
		Loads and applies all values of the session.
		"""
		for options, lfn, sfn in self._handlers:
			try:
				loaded = [self._cfg.get(*x) for x in options]
			except KeyError: # does not exist -> ignore
				debug("No values for %s.", options)
			else:
				debug("Loading %s with values %s.", options, loaded)
				lfn(*loaded)

	def save (self):
		"""
		Saves all options into the file.
		"""

		for options, lfn, sfn in self._handlers:
			vals = sfn()
			
			# map into list if necessairy
			if not hasattr(vals, "__iter__"):
				vals = [vals]
			debug("Saving %s with values %s", options, vals)

			for value, (option, section) in zip(vals, options):
				self._cfg.add_section(section)
				self._cfg.add(option, str(value), section = section, with_blankline = False)
		
		self._cfg.write()

	def check_version (self, vers):
		pass # do nothing atm
/td>ui-commit: add support for 'commit-filter' optionLars Hjemli4-0/+17 This new option specifies a filter which is executed on the commit message, i.e. the commit message is written to the filters STDIN and the filters STDOUT is included verbatim as the commit message. This can be used to implement commit linking by creating a simple shell script in e.g. /usr/bin/cgit-commit-filter.sh like this: #/bin/sh sed -re 's|\b([0-9a-fA-F]{6,40})\b|<a href="./?id=\1">\1</a>|g' Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-31ui-tree: add support for source-filter optionLars Hjemli4-4/+25 This new option is used to specify an external command which will be executed when displaying blob content in the tree view. Blob content will be written to STDIN of the filter and STDOUT from the filter will be included verbatim in the html output from cgit. The file name of the blob will be passed as the only argument to the filter command. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-31ui-snapshot: use cgit_{open|close}_filter() to execute compressorsLars Hjemli1-28/+7 This simplifies the code in ui-snapshot.c and makes the test-suite verify the new filter-functions. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-31Add generic filter/plugin infrastructureLars Hjemli3-0/+62 The functions cgit_open_filter() and cgit_close_filter() can be used to execute filters on the output stream from cgit. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-25Add support for mime type registration and lookupLars Hjemli4-5/+45 This patch makes it possible to register mappings from filename extension to mime type in cgitrc and use this mapping when returning blob content in `plain` view. The reason for adding this mapping to cgitrc (as opposed to parsing something like /etc/mime.types) is to allow quick lookup of a limited number of filename extensions (/etc/mime-types on my machine currently contains over 700 entries). NB: A nice addition to this patch would be to parse /etc/mime.types when `plain` view is requested for a file with an extension for which there is no mapping registered in cgitrc. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-25cgit.h: keep config flags sortedLars Hjemli1-2/+2 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-25cgitrc.5.txt: document 'embedded' and 'noheader'Lars Hjemli1-0/+9 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-25Add support for 'noheader' optionLars Hjemli3-7/+16 This option can be used to disable the standard cgit page header, which might be useful in combination with the 'embedded' option. Suggested-by: Mark Constable <markc@renta.net> Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-25cgitrc.5.txt: document 'head-include'Lars Hjemli1-0/+4 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-25ui-blob: return 'application/octet-stream' for binary blobsLars Hjemli1-1/+7 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-07-25ui-plain: Return 'application/octet-stream' for binary files.Remko Tronçon1-1/+4 Signed-off-by: Remko Tronçon <git@el-tramo.be> Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-06-11use cgit_httpscheme() for atom feedDiego Ongaro2-3/+6 2009-06-11add cgit_httpscheme() -> http:// or https://Diego Ongaro2-0/+12 2009-06-07Return http statuscode 404 on unknown branchLars Hjemli3-0/+6 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-06-07Add head-include configuration option.Mark Lodato3-1/+6 This patch adds an option to the configuration file, "head-include", which works just like "header" or "footer", except the content is put into the HTML's <head> tag. 2009-03-15CGIT 0.8.2.1v0.8.2.1Lars Hjemli1-1/+1 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-03-15Fix doc-related glitches in Makefile and .gitignoreLars Hjemli2-1/+6 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-03-15ui-snapshot: avoid segfault when no filename is specifiedLars Hjemli1-6/+17 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-03-15fix segfault when displaying empty blobsEric Wong1-5/+8 When size is zero, subtracting one from it turns it into ULONG_MAX which causes an out-of-bounds access on buf. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-02-19Add support for HEAD requestsLars Hjemli2-0/+7 This is a quick 'n dirty hack which makes cgit honor HEAD requests. Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-02-19Add support for ETag in 'plain' viewLars Hjemli4-0/+5 When downloading a blob identified by its path, the client might want to know if the blob has been modified since a previous download of the same path. To this end, an ETag containing the blob SHA1 seems to be ideal. Todo: add support for HEAD requests... Suggested-by: Owen Taylor <otaylor@redhat.com> Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-02-12ui-tree: escape ascii-text properly in hexdump viewLars Hjemli1-4/+9 Signed-off-by: Lars Hjemli <hjemli@gmail.com> 2009-02-12Makefile: add doc-related targetsLars Hjemli1-2/+17 Signed-off-by: Lars Hjemli <hjemli@gmail.com>