update command for config v2 to v3
This commit is contained in:
parent
4e9ce17c71
commit
dc73c987c8
@ -3,6 +3,7 @@ import sys
|
||||
from .. import repo
|
||||
from .. import color
|
||||
from ..configs import config
|
||||
from ..__init__ import __version__
|
||||
|
||||
def parser(subparsers):
|
||||
parser = subparsers.add_parser('update', help='update the repository to the lastest format')
|
||||
@ -10,33 +11,36 @@ def parser(subparsers):
|
||||
|
||||
|
||||
def command(ui):
|
||||
rp = repo.Repository(config())
|
||||
code_version = papers.__version__
|
||||
repo_version = config().version
|
||||
code_version = __version__
|
||||
repo_version = int(config().version)
|
||||
|
||||
if repo_version == code_version:
|
||||
ui._print('You papers repository is up-to-date.')
|
||||
ui.print_('You papers repository is up-to-date.')
|
||||
sys.exit(0)
|
||||
elif repo_version <= code_version:
|
||||
ui._print('Your repository was generated with an newer version of papers.\n'
|
||||
elif repo_version > code_version:
|
||||
ui.print_('Your repository was generated with an newer version of papers.\n'
|
||||
'You should not use papers until you install the newest version.')
|
||||
sys.exit(0)
|
||||
else:
|
||||
msg = ("You should backup the paper directory {} before continuing."
|
||||
"Continue ?").format(color.dye(rp.papersdir, color.filepath))
|
||||
"Continue ?").format(color.dye(config().papers_dir, color.filepath))
|
||||
sure = ui.input_yn(question=msg, default='n')
|
||||
if not sure:
|
||||
sys.exit(0)
|
||||
|
||||
if repo_version == 1:
|
||||
rp = repo.Repository(config())
|
||||
for p in rp.all_papers():
|
||||
tags = set(p.metadata['tags'])
|
||||
tags = tags.union(p.metadata['labels'])
|
||||
p.metadata.pop('labels', [])
|
||||
rp.save_paper(p)
|
||||
repo_version = 2
|
||||
|
||||
|
||||
if repo_version == 2:
|
||||
# update config
|
||||
print 'bla'
|
||||
cfg_update = [('papers-directory', 'papers_dir'),
|
||||
('open-cmd', 'open_cmd'),
|
||||
('edit-cmd', 'edit_cmd'),
|
||||
@ -45,13 +49,12 @@ def command(ui):
|
||||
]
|
||||
for old, new in cfg_update:
|
||||
try:
|
||||
config().__setattr__('papers', new, config()._cfg.get('papers', old))
|
||||
config()._cfg.set('papers', new, config()._cfg.get('papers', old))
|
||||
config()._cfg.remove_option('papers', old)
|
||||
except Exception:
|
||||
pass
|
||||
config().save()
|
||||
repo_version = 3
|
||||
|
||||
|
||||
config().version = repo_version
|
||||
config().save()
|
||||
config().save()
|
||||
|
@ -40,9 +40,13 @@ def config(section = MAIN_SECTION):
|
||||
class Config(object):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
object.__setattr__(self, '_cfg', configparser.SafeConfigParser(DFT_CONFIG))
|
||||
object.__setattr__(self, '_section', MAIN_SECTION) # active section
|
||||
object.__setattr__(self, '_cfg', configparser.SafeConfigParser())
|
||||
|
||||
self._cfg.add_section(self._section)
|
||||
for name, value in DFT_CONFIG.items():
|
||||
self._cfg.set(self._section, name, str(value))
|
||||
|
||||
for name, value in kwargs.items():
|
||||
self.__setattr__(name, value)
|
||||
|
||||
|
@ -25,9 +25,11 @@ class TestConfig(unittest.TestCase):
|
||||
a.as_global()
|
||||
config().color = 'no'
|
||||
self.assertEqual(config().color, False)
|
||||
self.assertEqual(config('papers').color, False)
|
||||
# booleans type for new variables are memorized, but not saved.
|
||||
config().bla = True
|
||||
self.assertEqual(config().bla, True)
|
||||
self.assertEqual(config('papers').bla, True)
|
||||
|
||||
with self.assertRaises(configparser.NoOptionError):
|
||||
config()._cfg.get(configs.MAIN_SECTION, '_section')
|
||||
|
Loading…
x
Reference in New Issue
Block a user