# encoding: utf-8
import os, io, sys
import ConfigParser
# set system defaults
defaults = """### BEGIN DEFAULTS
[system]
# The number of processor cores to use in video conversion
cores = 1
# If True, will not generate derivatives
no_deriv = False
# whether to generate lossless ffv1 versions of videos
# caution: these are extremely large!
ffv1 = False
debug = False
# Commands for binary applications. Normally these paths should be
# inserted automatically by "egaia setup"
cmd_convert =
cmd_montage =
cmd_ffmpeg =
cmd_ffprobe =
cmd_mogrify =
cmd_inkscape =
cmd_libreoffice =
cmd_identify =
cmd_wget =
cmd_wkhtmltopdf =
cmd_wkhtmltoimage =
[archive]
archive_name = Ethnographic Archive
organization =
user = Anonymous
email =
# The URL for the archive, with trailing slash. This will be used as the
# base for absolute hyperlinks.
archive_url = http://example.com/
# A prefix for RDF (should not include spaces etc.).
# This prefix will also be used as the archive short-name or acronym
# in the header for HTML output for very small devices
archive_prefix = ARCHIVE
# Log of file renames and similar reversible operations
logfile = ~/egaia/log.txt
# The default language. This will be used in HTML output.
language = en
# Location of a directory for HTML output
pub_path = ~/egaia/pub
# Location of the home page. This should be the full path to a docx file,
# which will be converted to HTML by the ``pub --home-page`` command.
home_page = ~/egaia/README-en.docx
# Whether or not to include still images from videos in HTML output.
# This is nice to have, but can be overwhelming for FTP transfer and might
# exceed shared web server inode quotas. The PDF version of video stills
# will be exported regardless of this setting.
export_stills = false
# Whether to generate html pages using embedded media from remote sites
# (YouTube, Vimeo, etc.) when given in the "remote_url" metadata field.
# For offline distribution, set this to False. If this value is True, video
# derivatives will still be copied to the public directory but download links
# will not be created.
remote_embeds = true
# When publishing videos to the Internet Archive, we have the option to
# exclude them from the index, making them unlisted. To do so, set this
# value to "true" in the global or collection-level settings.
ia_noindex = false
# The list of default metadata fields that will be included in ALL item
# and collection descriptions. The labels for any terms given here should be
# listed in the [terms] section below, along with any optional fields, which
# will be included in metadata output below the core fields. Note that core
# metadata fields will always be listed for each item, even if empty, whereas
# optional elements will be ignored unless supplied by the user.
core_metadata = DCTERMS.identifier,
DCTERMS.title,
DCTERMS.creator,
DCTERMS.type,
DCTERMS.coverage,
DCTERMS.description,
DCTERMS.publisher,
DCTERMS.source,
DCTERMS.rights,
DCTERMS.subject,
DCTERMS.date,
DCTERMS.language,
DCTERMS.tableOfContents
[terms]
# These are the terms and localized / customized labels in use in the
# archive. The keys may be used as html template variables, in urls, and as
# keys in json. Caution: changing the label for a term that is already in
# use will break metadata parsing, and may require manual updating in item
# descriptions through a global find-and-replace.
# Note that the keys are case-sensitive, but labels may be converted to
# title case or lowercase by the application depending on the context.
DCTERMS.abstract = abstract
DCTERMS.accessRights = access rights
DCTERMS.accrualMethod = accrual method
DCTERMS.accrualPeriodicity = accrual periodicity
DCTERMS.accrualPolicy = accrual policy
DCTERMS.alternative = alternative
DCTERMS.audience = audience
DCTERMS.available = available
DCTERMS.bibliographicCitation = bibliographic citation
DCTERMS.conformsTo = conforms to
DCTERMS.contributor = contributor
DCTERMS.coverage = coverage
DCTERMS.created = created
DCTERMS.creator = creator
DCTERMS.date = date
DCTERMS.dateAccepted = date accepted
DCTERMS.dateCopyrighted = date copyrighted
DCTERMS.dateSubmitted = date submitted
DCTERMS.description = description
DCTERMS.educationLevel = education level
DCTERMS.extent = extent
DCTERMS.format = format
DCTERMS.hasFormat = has format
DCTERMS.hasPart = has part
DCTERMS.hasVersion = has version
DCTERMS.identifier = identifier
DCTERMS.instructionalMethod = instructional method
DCTERMS.isFormatOf = is format of
DCTERMS.isPartOf = is part of
DCTERMS.isReferencedBy = is referenced by
DCTERMS.isReplacedBy = is replaced by
DCTERMS.isRequiredBy = is required by
DCTERMS.issued = issued
DCTERMS.isVersionOf = is version of
DCTERMS.language = language
DCTERMS.license = license
DCTERMS.mediator = mediator
DCTERMS.medium = medium
DCTERMS.modified = modified
DCTERMS.provenance = provenance
DCTERMS.publisher = publisher
DCTERMS.references = references
DCTERMS.relation = relation
DCTERMS.replaces = replaces
DCTERMS.requires = requires
DCTERMS.rights = rights
DCTERMS.rightsHolder = rights holder
DCTERMS.source = source
DCTERMS.spatial = spatial
DCTERMS.subject = subject
DCTERMS.tableOfContents = table of contents
DCTERMS.temporal = temporal
DCTERMS.title = title
DCTERMS.type = type
DCTERMS.valid = valid
# The original filename, as set by egaia on ingestion
original_filename = original filename
# Field designating a URL that represents a version of the resource that can
# be embedded in an iframe, such as a video
remote_embed_url = remote embed url
# The parent collection UUID; normally set automatically by egaia
collection = collection
# A shortcut; this designates the filename for an html document in the
# root directory that redirects to the current item
alias = alias
[prefixes]
# URIs for metadata terms in RDF output
DCTERMS = http://purl.org/dc/terms/
[template_fields]
# Variables used in html templates. These are included here in order to
# enable end-user localization or extension.
toggle = Toggle navigation
Home = Home
Collections = Collections
All_items = All items
Keywords = Keywords
Subject = Subject
Coverage_area = Coverage area
Media_type = Media type
Creator = Creator
Language = Language
Items = Items
About = About
generator = Generated by
<a href="http://mcdrc.org/egaia/html/">
egaia</a>.
last_updated = Last updated:
collection = collection
Metadata = Metadata
files = files
keyword_index = keyword index
index = index
language_index = language index
creator_index = creator index
subject_index = subject index
type_index = type index
coverage_index = coverage index
source = source
### END DEFAULTS
"""
usr_cfg = os.path.expanduser(os.path.join('~', '.config', 'egaia', 'shared.cfg'))
local_cfg = os.path.expanduser(os.path.join('~', '.config', 'egaia', 'local.cfg'))
[docs]def init(path):
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
[docs]def loadConfig():
"""Load configuration values, and return a ConfigParser object."""
import egaia_root
config = ConfigParser.RawConfigParser(allow_no_value=True)
# allow case-sensitive keys, needed for DCTERMS URIs
config.optionxform = str
# load system defaults
config.readfp(io.BytesIO(defaults))
# load any available config files
p = config.read([usr_cfg, local_cfg])
return config
[docs]def getConfig(section, key, boolean=False):
"""Get the value of a particular configuration setting"""
config = loadConfig()
if boolean:
try:
return config.getboolean(section, key)
except ValueError:
print "Not a boolean value for %s.%s" % (section, key)
return False
return config.get(section, key)
[docs]def setConfig(cfg_path, section, key, value):
"""Set the value of a particular configuration setting"""
config = ConfigParser.RawConfigParser(allow_no_value=True)
# allow case-sensitive keys, needed for DCTERMS URIs
config.optionxform = str
config.read(cfg_path)
config.set(section, key, value)
with open(cfg_path, 'wb') as cfg_file:
config.write(cfg_file)
[docs]def printConfig(section=None):
"""Print a list containing configuration values"""
config = loadConfig()
if section:
items = config.items(section)
else:
items = config.items('system') + config.items('archive')
return items
[docs]def dumpConfig(cfg_path):
"""Write the currently set configuration values to the file located at
cfg_path.
"""
if os.path.exists(cfg_path):
print "The file %s already exists!" % cfg_path
return
config = loadConfig()
try:
with open(cfg_path, 'w') as cfg_file:
config.write(cfg_file)
print "Current values written to %s." % cfg_path
except:
print "Unable to write to %s!" % cfg_path
return
[docs]def updateConfig(cfg_path, section='archive'):
"""Update the specified configuration file located at cfg_path, providing
a user prompt with the key and output prefilled with the current value.
"""
import utils
config = ConfigParser.RawConfigParser(allow_no_value=True)
# allow case-sensitive keys, needed for DCTERMS URIs
config.optionxform = str
config.read(cfg_path)
for key, value in config.items(section):
# join continuation lines
values = ' '.join(x.strip() for x in value.splitlines())
value2 = utils.rlinput('%s: ' % key, prefill=values)
config.set(section, key, value2)
with open(cfg_path, 'wb') as cfg_file:
config.write(cfg_file)
def _cli(args):
"""egaia config
Set, print, or edit variables in configuration files.
Usage:
egaia config --help
egaia config --defaults
egaia config --new [ --path=FILE ]
egaia config --update [ --section=SECTION ] [ --path=FILE ]
egaia config --get [ --section=SECTION ] [ --key=KEY ]
egaia config --set=KEY:VALUE... [ --section=SECTION ] [ --path=FILE ]
"""
if args['--defaults']:
print defaults
exit()
# Use the default user config file unless specified otherwise
if not args['--path']:
args['--path'] = usr_cfg
init(args['--path'])
if not args['--section']:
args['--section'] = 'archive'
if args['--new']:
dumpConfig(args['--path'])
return
elif args['--update']:
updateConfig(args['--path'], section=args['--section'])
return
elif args['--set']:
for i in args['--set']:
key, value = i.split(':', 1)
setConfig(args['--path'], section=args['--section'],
key=key, value=value)
return
elif args['--get']:
if args['--key']:
print getConfig(section=args['--section'], key=args['--key'])
return
items = printConfig(section=args['--section'])
for item in items:
print item[0], '=', item[1]
return