Last active
September 25, 2015 02:39
-
-
Save cjo20/aca2af0e94ce8c63a943 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os | |
import argparse | |
import argcomplete | |
import textwrap | |
class Base (object): | |
always_complete_options = True | |
def __init__ (self, args): | |
self.inputs = args | |
@classmethod | |
def _get_description (klass): | |
return klass.__doc__.split("\n\n")[0] | |
@classmethod | |
def _get_epilog (klass): | |
return "\n\n".join(klass.__doc__.split("\n\n")[1:]) | |
def prep_parser (self): | |
epilog = textwrap.dedent(self._get_epilog( )) | |
description = self._get_description( ) | |
self.parser = argparse.ArgumentParser( | |
description=description | |
, epilog=epilog | |
, formatter_class=argparse.RawDescriptionHelpFormatter) | |
def configure_parser (self, parser): | |
pass | |
def prolog (self): | |
pass | |
def epilog (self): | |
pass | |
def __call__ (self): | |
self.prep_parser( ) | |
self.configure_parser(self.parser) | |
argcomplete.autocomplete(self.parser, always_complete_options=self.always_complete_options); | |
self.args = self.parser.parse_args( ) | |
self.prolog( ) | |
self.run(self.args) | |
self.epilog( ) | |
def run (self, args): | |
print self.inputs | |
print args | |
from openaps import config | |
class ConfigApp (Base): | |
def read_config (self): | |
cfg_file = os.environ.get('OPENAPS_CONFIG', 'openaps.ini') | |
if not os.path.exists(cfg_file): | |
print "Not an openaps environment, run: openaps init" | |
sys.exit(1) | |
self.config = config.Config.Read(cfg_file) | |
self.config.add_secret("serial") | |
def prolog (self): | |
self.read_config( ) | |
def epilog (self): | |
self.create_git_commit( ) | |
def git_repo (self): | |
from git import Repo | |
self.repo = getattr(self, 'repo', Repo(os.getcwd( ))) | |
return self.repo | |
def create_git_commit (self): | |
self.git_repo( ) | |
if self.repo.is_dirty( ) or self.repo.index.diff(None): | |
git = self.repo.git | |
msg = """{0:s} {1:s} | |
TODO: better change descriptions | |
{2:s} | |
""".format(self.parser.prog, ' '.join(sys.argv[1:]), ' '.join(sys.argv)) | |
git.commit('-avm', msg) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ConfigParser import SafeConfigParser | |
import re | |
import os | |
class Config (SafeConfigParser): | |
OPTCRE = re.compile( | |
r'\s?(?P<option>[^:=\s][^:=]*)' # very permissive! | |
r'\s*(?P<vi>[:=])\s*' # any number of space/tab, | |
# followed by separator | |
# (either : or =), followed | |
# by any # space/tab | |
r'(?P<value>.*)$' # everything up to eol | |
) | |
def save (self): | |
publicConfig = Config( ) | |
privateConfig = Config( ) | |
if len(self.secrets): | |
for section in self.sections(): | |
publicConfig.add_section(section) | |
for k,v in self.items(section): | |
if k in self.secrets: | |
if not privateConfig.has_section(section): | |
privateConfig.add_section(section) | |
privateConfig.set(section, k, v) | |
else: | |
publicConfig.set(section, k, v) | |
publicConfig.do_save('openaps.ini') | |
privateConfig.do_save('secret.ini') | |
else: | |
self.do_save('openaps.ini') | |
os.remove('secret.ini') | |
def add_secret(self, key): | |
self.secrets.append(key) | |
def do_save(self, filename): | |
with open(filename, 'wb') as configfile: | |
self.write(configfile) | |
def add_device (self, device): | |
section = device.section_name( ) | |
self.add_section(section) | |
for k, v in device.items( ): | |
self.set(section, k, v) | |
def remove_device (self, device): | |
section = device.section_name( ) | |
self.remove_section(section) | |
@classmethod | |
def Read (klass, name=None, defaults=['openaps.ini', '~/.openaps.ini', '/etc/openaps/openaps.ini']): | |
config = Config( ) | |
config.secrets = [] | |
if name is not None: | |
config.read([name, 'secret.ini']) | |
else: | |
config.read(defaults) | |
return config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment