IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40949


Ignore:
Timestamp:
Oct 18, 2019, 10:35:56 AM (7 years ago)
Author:
fairlamb
Message:

changed the structure so that a full schema must be given, also allowed a full schema to exist

Location:
branches/ipp-259_genericise_backups/tools/backups
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/ipp-259_genericise_backups/tools/backups/backup.py

    r40945 r40949  
    1010import backups.utils.errors as errs
    1111import backups.utils.subprocess_utils as sub_utils
    12 from backups.utils.config_parse_helper import ConfigSchemaSection, ConfigSchemaLine
     12from backups.utils.config_parse_helper import ConfigSchema, ConfigSchemaSection, ConfigSchemaLine, ParsedConfig
    1313
    1414DEFAULT_CONFIG_FILE = path.join(path.dirname(__file__), 'atlassian_backups.config')
     15
     16DEFAULT_SCHEMA_SECTION = ConfigSchemaSection('DEFAULT',
     17    [ ConfigSchemaLine('backup_paths', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
     18    , ConfigSchemaLine('source_machine', True, str)
     19    , ConfigSchemaLine('verbose', False, bool)
     20    , ConfigSchemaLine('make_dirs', False, bool)
     21    ]
     22)
     23COPY_SCHEMA_SECTION = ConfigSchemaSection('COPY',
     24    [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
     25    , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
     26    ]
     27)
     28TAR_SCHEMA_SECTION = ConfigSchemaSection('TAR',
     29    [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
     30    , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
     31    , ConfigSchemaLine('target_filename', True, str)
     32    , ConfigSchemaLine('prefix_date', True, bool)
     33    ]
     34)
     35RSYNC_SCHEMA_SECTION = ConfigSchemaSection('RSYNC',
     36    [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
     37    , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
     38    ]
     39)
     40
     41MYSQLDUMP_SCHEMA_SECTION = ConfigSchemaSection('MYSQLDUMP',
     42    [ ConfigSchemaLine('user', True, str)
     43    , ConfigSchemaLine('password', True, str)
     44    , ConfigSchemaLine('db_name', True, str)
     45    , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
     46    , ConfigSchemaLine('target_filename', True, str)
     47    , ConfigSchemaLine('prefix_date', True, bool)
     48    ]
     49)
     50
     51
     52def get_backup_schema() -> ConfigSchema:
     53    backup_schema = ConfigSchema(None)
     54    backup_schema.add_section(DEFAULT_SCHEMA_SECTION)
     55    backup_schema.add_section(COPY_SCHEMA_SECTION)
     56    backup_schema.add_section(TAR_SCHEMA_SECTION)
     57    backup_schema.add_section(RSYNC_SCHEMA_SECTION)
     58    backup_schema.add_section(MYSQLDUMP_SCHEMA_SECTION)
     59    return backup_schema
    1560
    1661
     
    2065    """
    2166
    22     def __init__(self, config_file=None):
     67    def __init__(self, config_file=None, schema: ConfigSchema=None):
    2368        if config_file is None:
    2469            raise errs.ValidationError("ValidationError: Backup class must be given a config")
    25         self.load_config(config_file)
    26 
    27     def load_config(self, config_class_or_filepath):
     70        if schema is None:
     71            schema = get_backup_schema()
     72        print(schema)
     73        self.load_config(config_file, schema)
     74
     75    def load_config(self, config_class_or_filepath, schema):
    2876        """Accepts a filepath to a config file, or """
    2977
     
    3886        config.read(config_class_or_filepath)
    3987
    40         self.read_default_section(config)
    41         if self._has_copy_section(config):
    42             self.read_copy_config_section(config)
    43         if self._has_tar_section(config):
    44             self.read_tar_config_section(config)
    45         if self._has_rsync_section(config):
    46             self.read_rsync_config_section(config)
    47         if self._has_mysqldump_section(config):
    48             self.read_mysqldump_config_section(config)
    49 
    50     def _has_copy_section(self, config_parser: ConfigParser) -> bool:
    51         section = 'COPY'
    52         self.run_copy = config_parser.has_section(section)
    53         return self.run_copy
    54 
    55     def _has_tar_section(self, config_parser: ConfigParser) -> bool:
    56         section = 'TAR'
    57         self.run_tar = config_parser.has_section(section)
    58         return self.run_tar
    59 
    60     def _has_rsync_section(self, config_parser: ConfigParser) -> bool:
    61         section = 'RSYNC'
    62         self.run_rsync = config_parser.has_section(section)
    63         return self.run_rsync
    64 
    65     def _has_mysqldump_section(self, config_parser: ConfigParser) -> bool:
    66         section = 'MYSQLDUMP'
    67         self.run_mysqldump = config_parser.has_section(section)
    68         return self.run_mysqldump
    69 
    70     DEFAULT_SCHEMA = ConfigSchemaSection('DEFAULT',
    71         [ ConfigSchemaLine('backup_paths', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
    72         , ConfigSchemaLine('source_machine', True, str)
    73         , ConfigSchemaLine('verbose', False, bool)
    74         , ConfigSchemaLine('make_dirs', False, bool)
    75         ]
    76     )
    77 
    78     def read_default_section(self, config: ConfigParser, schema: ConfigSchemaSection=DEFAULT_SCHEMA):
    79         self.default_dict = cfg_help.parse_config(config, schema)
     88        self.read_config_and_schema(config, schema)
     89        # self.read_copy_config_section(config, schema)
     90        # self.read_tar_config_section(config, schema)
     91        # self.read_rsync_config_section(config, schema)
     92        # self.read_mysqldump_config_section(config, schema)
     93
     94    def read_config_and_schema(self, config: ConfigParser, schema: ConfigSchema):
     95        parsed_config = ParsedConfig(config, schema)
     96        print(parsed_config)
     97        self.default_dict = parsed_config.get_section('DEFAULT')
    8098        self.hostname_is_valid()
    8199        self.maybe_make_backup_dirs()
    82100
    83     COPY_SCHEMA = ConfigSchemaSection('COPY',
    84         [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
    85         , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
    86         ]
    87     )
    88 
    89     def read_copy_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=COPY_SCHEMA):
    90         self.copy_dict = cfg_help.parse_config(config, schema)
    91 
    92     TAR_SCHEMA = ConfigSchemaSection('TAR',
    93         [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
    94         , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
    95         , ConfigSchemaLine('target_filename', True, str)
    96         , ConfigSchemaLine('prefix_date', True, bool)
    97         ]
    98     )
    99 
    100     def read_tar_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=TAR_SCHEMA):
    101         self.tar_dict = cfg_help.parse_config(config, schema)
    102 
    103     RSYNC_SCHEMA = ConfigSchemaSection('RSYNC',
    104         [ ConfigSchemaLine('sources', True, list, cfg_help.SpecialSchemaProcessing.commma_separated)
    105         , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
    106         ]
    107     )
    108 
    109     def read_rsync_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=RSYNC_SCHEMA):
    110         self.rsync_dict = cfg_help.parse_config(config, schema)
    111 
    112     MYSQLDUMP_SCHEMA = ConfigSchemaSection('MYSQLDUMP',
    113         [ ConfigSchemaLine('user', True, str)
    114         , ConfigSchemaLine('password', True, str)
    115         , ConfigSchemaLine('db_name', True, str)
    116         , ConfigSchemaLine('additional_args', False, list, cfg_help.SpecialSchemaProcessing.space_separated)
    117         , ConfigSchemaLine('target_filename', True, str)
    118         , ConfigSchemaLine('prefix_date', True, bool)
    119         ]
    120     )
    121 
    122     def read_mysqldump_config_section(self, config: ConfigParser, schema: ConfigSchemaSection=MYSQLDUMP_SCHEMA):
    123         self.mysqldump_dict = cfg_help.parse_config(config, schema)
     101        section = 'COPY'
     102        self.should_run_copy = parsed_config.has_section(section)
     103        if self.should_run_copy:
     104            self.copy_dict = parsed_config.get_section(section)
     105
     106        section = 'TAR'
     107        self.should_run_tar = parsed_config.has_section(section)
     108        if self.should_run_tar:
     109            self.tar_dict = parsed_config.get_section(section)
     110
     111        section = 'RSYNC'
     112        self.should_run_rsync = parsed_config.has_section(section)
     113        if self.should_run_rsync:
     114            self.rsync_dict = parsed_config.get_section(section)
     115
     116        section = 'MYSQLDUMP'
     117        self.should_run_mysqldump = parsed_config.has_section(section)
     118        if self.should_run_mysqldump:
     119            self.mysqldump_dict = parsed_config.get_section(section)
     120
     121    def run_backup_processes(self):
     122
     123        if self.should_run_copy:
     124            self._run_copy()
     125        if self.should_run_tar:
     126            self._run_tar()
     127        if self.should_run_rsync:
     128            self._run_rsync()
     129        if self.should_run_mysqldump:
     130            self._run_mysqldump()
    124131
    125132    def hostname_is_valid(self) -> bool:
     
    146153                    os.makedirs(backup_path)
    147154
    148     def run_backup(self):
    149             self._run_copy()
    150             self._run_tar()
    151             self._run_rsync()
    152             self._run_mysqldump()
    153 
    154155    def _run_copy(self):
    155         if self.run_copy is None or False:
     156        if self.should_run_copy is None or False:
    156157            return 0
    157158        self.target_backup_paths_ok(raise_error=True)
     
    177178
    178179    def _run_tar(self):
    179         if self.run_tar is None or False:
     180        if self.should_run_tar is None or False:
    180181            return 0
    181182
     
    203204
    204205    def _run_rsync(self):
    205         if self.run_rsync is None or False:
     206        if self.should_run_rsync is None or False:
    206207            return 0
    207208
  • branches/ipp-259_genericise_backups/tools/backups/backup_test.py

    r40945 r40949  
    88import backups.utils.errors as errs
    99from backups.backup import Backup
    10 
     10from backups.utils.config_parse_helper import ConfigSchema
    1111
    1212TEST_DEFAULT_CONFIG_FILE = os.path.join(os.path.dirname(__file__), 'testing/backup_test.config')
     
    9292    def test_loading_only_defaults(self, tmpdir):
    9393        config = make_default_config(tmpdir, ['bck1', 'bck2'], False)
    94         backy = Backup(config_file=config)
     94        schema = ConfigSchema(None)
     95        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     96        backy = Backup(config, schema)
    9597
    9698        assert backy.default_dict['backup_paths'] == \
     
    117119    def test_nothing_should_be_set_to_run_if_not_present(self, tmpdir):
    118120        config = make_default_config(tmpdir, ['bck1', 'bck2'], False)
    119         backy = Backup(config_file=config)
    120 
    121         assert backy.run_copy is False
    122         assert backy.run_tar is False
    123         assert backy.run_rsync is False
    124         assert backy.run_mysqldump is False
     121        schema = ConfigSchema(None)
     122        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     123        backy = Backup(config, schema)
     124
     125        assert backy.should_run_copy is False
     126        assert backy.should_run_tar is False
     127        assert backy.should_run_rsync is False
     128        assert backy.should_run_mysqldump is False
    125129
    126130    def test_hostname_assertion_fails(self, tmpdir):
    127131        config = make_default_config(tmpdir, ['bck1', 'bck2'])
    128         backy = Backup(config_file=config)
     132        schema = ConfigSchema(None)
     133        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     134        backy = Backup(config, schema)
    129135
    130136        backy.default_dict['source_machine'] = 'not_a_real_hostname'
     
    133139    def test_hostname_assertion_passes(self, tmpdir):
    134140        config = make_default_config(tmpdir, ['bck1', 'bck2'])
    135         backy = Backup(config_file=config)
     141        schema = ConfigSchema(None)
     142        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     143        backy = Backup(config, schema)
    136144
    137145        actual_hostname = socket.gethostname()
     
    168176            , 'verbose'        : 'True'
    169177            }
     178        schema = ConfigSchema(None)
     179        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
    170180        bck1_path   = os.path.join(tmpdir, 'bck1')
    171181        bck1b_path  = os.path.join(tmpdir, 'bck1/bobby')
     
    177187        assert not os.path.exists(bck2_path)
    178188        # backup dirs not created automatically
    179         Backup(config)
     189        Backup(config, schema)
    180190        assert not os.path.exists(bck1_path)
    181191        assert not os.path.exists(bck1b_path)
     
    190200            , 'make_dirs'      : 'True'
    191201            }
    192         backy = Backup(config)
     202        backy = Backup(config, schema)
    193203        assert backy.default_dict['make_dirs'] is True
    194204        assert os.path.exists(bck1_path)
     
    204214        source = os.path.join(tmpdir, "and_specific_file.jpeg")
    205215        config['COPY'] = {'sources' : f"{source}"}
     216        schema = ConfigSchema(None)
     217        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     218        schema.add_section(bckup.COPY_SCHEMA_SECTION)
    206219
    207220        # make file to be copied
    208221        Path(source).touch()
    209222
    210         backy = Backup(config)
     223        backy = Backup(config, schema)
     224        assert backy.should_run_copy is True
    211225
    212226        expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg')
     
    226240        , 'additional_args' : "-v --preserve -r"
    227241        }
     242        schema = ConfigSchema(None)
     243        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     244        schema.add_section(bckup.COPY_SCHEMA_SECTION)
    228245
    229246        os.makedirs(source1)
    230247        Path(source2).touch()
    231248
    232         backy = Backup(config)
     249        backy = Backup(config, schema)
    233250
    234251        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     
    253270        , 'additional_args' : '-r'
    254271        }
     272        schema = ConfigSchema(None)
     273        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     274        schema.add_section(bckup.COPY_SCHEMA_SECTION)
    255275
    256276        assert not os.path.exists(source)
    257         backy = Backup(config)
     277        backy = Backup(config, schema)
    258278        with pytest.raises(errs.ValidationError):
    259279            backy._run_copy()
     
    271291        Path(source).touch()
    272292        config['COPY'] = {'sources' : f"{source}"}
     293        schema = ConfigSchema(None)
     294        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     295        schema.add_section(bckup.COPY_SCHEMA_SECTION)
    273296
    274297        assert not os.path.exists(backup_does_not_exist)
    275         backy = Backup(config)
     298        backy = Backup(config, schema)
    276299        with pytest.raises(errs.ValidationError):
    277300            backy._run_copy()
     
    284307        }
    285308        os.makedirs(source)
     309        schema = ConfigSchema(None)
     310        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     311        schema.add_section(bckup.COPY_SCHEMA_SECTION)
    286312
    287313        with pytest.raises(errs.SubprocessError):
    288             backy = Backup(config)
     314            backy = Backup(config, schema)
    289315            backy._run_copy()
    290316
     
    293319        , 'additional_args' : '-r'
    294320        }
    295         backy = Backup(config)
     321        backy = Backup(config, schema)
    296322
    297323        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     
    320346        }
    321347
    322         backy = Backup(config)
     348        schema = ConfigSchema(None)
     349        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     350        schema.add_section(bckup.TAR_SCHEMA_SECTION)
     351
     352        backy = Backup(config, schema)
    323353
    324354        expected_tar1 = os.path.join(tmpdir, 'bck1', 'cool_tar.tar')
     
    344374        }
    345375
    346         backy = Backup(config)
     376        schema = ConfigSchema(None)
     377        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     378        schema.add_section(bckup.TAR_SCHEMA_SECTION)
     379
     380        backy = Backup(config, schema)
     381        assert backy.should_run_tar is True
    347382
    348383        date = bckup.get_date()
     
    363398        source = os.path.join(tmpdir, "and_specific_file.jpeg")
    364399        config['RSYNC'] = {'sources' : f"{source}"}
    365 
    366400        # make file to be copied
    367401        Path(source).touch()
    368402
    369         backy = Backup(config)
     403        schema = ConfigSchema(None)
     404        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     405        schema.add_section(bckup.RSYNC_SCHEMA_SECTION)
     406
     407        backy = Backup(config, schema)
    370408
    371409        expected1file = os.path.join(tmpdir, 'bck1', 'and_specific_file.jpeg')
     
    389427        Path(source2).touch()
    390428
    391         backy = Backup(config)
     429        schema = ConfigSchema(None)
     430        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     431        schema.add_section(bckup.RSYNC_SCHEMA_SECTION)
     432
     433        backy = Backup(config, schema)
    392434
    393435        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
     
    413455        }
    414456
     457        schema = ConfigSchema(None)
     458        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     459        schema.add_section(bckup.RSYNC_SCHEMA_SECTION)
     460
    415461        assert not os.path.exists(source)
    416         backy = Backup(config)
     462        backy = Backup(config, schema)
    417463        with pytest.raises(errs.ValidationError):
    418464            backy._run_rsync()
     
    431477        config['RSYNC'] = {'sources' : f"{source}"}
    432478
     479        schema = ConfigSchema(None)
     480        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     481        schema.add_section(bckup.RSYNC_SCHEMA_SECTION)
     482
    433483        assert not os.path.exists(backup_does_not_exist)
    434         backy = Backup(config)
     484        backy = Backup(config, schema)
    435485        with pytest.raises(errs.ValidationError):
    436486            backy._run_rsync()
     
    444494        os.makedirs(source)
    445495
    446         backy = Backup(config)
     496        schema = ConfigSchema(None)
     497        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     498        schema.add_section(bckup.RSYNC_SCHEMA_SECTION)
     499
     500        backy = Backup(config, schema)
     501
    447502        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
    448503        expected2dir  = os.path.join(tmpdir, 'bck2', 'some_dir')
     
    458513        }
    459514
    460         backy = Backup(config)
     515        backy = Backup(config, schema)
    461516        expected1dir  = os.path.join(tmpdir, 'bck1', 'some_dir')
    462517        expected2dir  = os.path.join(tmpdir, 'bck2', 'some_dir')
     
    491546        }
    492547
    493         backy = Backup(config)
     548        schema = ConfigSchema(None)
     549        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     550        schema.add_section(bckup.MYSQLDUMP_SCHEMA_SECTION)
     551
     552        backy = Backup(config, schema)
     553        assert backy.should_run_mysqldump is True
    494554
    495555        date = bckup.get_date()
     
    515575        }
    516576
    517         backy = Backup(config)
     577        schema = ConfigSchema(None)
     578        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     579        schema.add_section(bckup.MYSQLDUMP_SCHEMA_SECTION)
     580
     581        backy = Backup(config, schema)
    518582        with pytest.raises(errs.SubprocessError):
    519583            backy._run_mysqldump()
  • branches/ipp-259_genericise_backups/tools/backups/confluence_backup.py

    r40944 r40949  
    44import os.path as path
    55
     6import backups.backup as bck
    67import backups.utils.subprocess_utils as sub_utils
    78import backups.utils.errors as errs
    89from backups.backup import Backup
     10from backups.utils.config_parse_helper import ConfigSchema
    911
    1012EXPECTED_CONFIG_FILE = path.join(path.dirname(__file__), './confluence_backup.config')
     13
     14CONFLUENCE_SCHEMA = bck.get_backup_schema()
     15
     16
     17def make_confluence_schema():
     18    schema = ConfigSchema(None)
     19    schema.add_section(bck.DEFAULT_SCHEMA_SECTION)
     20    schema.add_section(bck.COPY_SCHEMA_SECTION)
     21    schema.add_section(bck.TAR_SCHEMA_SECTION)
     22    schema.add_section(bck.MYSQLDUMP_SCHEMA_SECTION)
     23    return schema
    1124
    1225
    1326class ConfluenceBackup(Backup):
    1427
    15     def __init__(self, config_file=EXPECTED_CONFIG_FILE):
    16         self.load_config(config_file)
     28    def __init__(self, config_file=EXPECTED_CONFIG_FILE, schema=None):
     29        if schema is None:
     30            schema = make_confluence_schema()
     31        self.load_config(config_file, schema)
    1732
    1833    @staticmethod
     
    2540
    2641    def _run_copy(self, verbose=False):
    27         if self.run_copy is None or False:
     42        if self.should_run_copy is None or False:
    2843            return 0
    2944
  • branches/ipp-259_genericise_backups/tools/backups/confluence_backup_test.py

    r40944 r40949  
    66from pathlib import Path
    77
     8import backups.backup as bckup
    89import backups.confluence_backup as jbck_mod
    910import backups.utils.errors as errs
    1011from backups.confluence_backup import ConfluenceBackup
    1112from backups.utils.errors import ValidationError
    12 
     13from backups.utils.config_parse_helper import ConfigSchema
    1314
    1415CONF_TEST_CONFIG = os.path.join(os.path.dirname(__file__), './testing/confluence_test.config')
     
    146147            }
    147148
    148         jb = ConfluenceBackup(config_file=config)
     149        schema = ConfigSchema(None)
     150        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     151        schema.add_section(bckup.COPY_SCHEMA_SECTION)
     152        schema.add_section(bckup.TAR_SCHEMA_SECTION)
     153
     154        jb = ConfluenceBackup(config, schema)
    149155
    150156        assert jb.default_dict == \
  • branches/ipp-259_genericise_backups/tools/backups/jira_backup.py

    r40944 r40949  
    44import os.path as path
    55
     6import backups.backup as bck
    67import backups.utils.subprocess_utils as sub_utils
    78import backups.utils.errors as errs
    89from backups.backup import Backup
     10from backups.utils.config_parse_helper import ConfigSchema
    911
    1012EXPECTED_CONFIG_FILE = path.join(path.dirname(__file__), './jira_backup.config')
    1113
    1214
     15def make_jira_schema():
     16    schema = ConfigSchema(None)
     17    schema.add_section(bck.DEFAULT_SCHEMA_SECTION)
     18    schema.add_section(bck.COPY_SCHEMA_SECTION)
     19    schema.add_section(bck.TAR_SCHEMA_SECTION)
     20    schema.add_section(bck.MYSQLDUMP_SCHEMA_SECTION)
     21    return schema
     22
     23
    1324class JiraBackup(Backup):
    1425
    15     def __init__(self, config_file=EXPECTED_CONFIG_FILE):
    16         self.load_config(config_file)
     26    def __init__(self, config_file=EXPECTED_CONFIG_FILE, schema=None):
     27        if schema is None:
     28            schema = make_jira_schema()
     29        self.load_config(config_file, schema)
    1730
    1831    @staticmethod
     
    2538
    2639    def _run_copy(self, verbose=False):
    27         if self.run_copy is None or False:
     40        if self.should_run_copy is None or False:
    2841            return 0
    2942
  • branches/ipp-259_genericise_backups/tools/backups/jira_backup_test.py

    r40944 r40949  
    66from pathlib import Path
    77
     8import backups.backup as bckup
    89import backups.jira_backup as jbck_mod
    910import backups.utils.errors as errs
    1011from backups.jira_backup import JiraBackup
    1112from backups.utils.errors import ValidationError
    12 
     13from backups.utils.config_parse_helper import ConfigSchema
    1314
    1415JIRA_TEST_CONFIG = os.path.join(os.path.dirname(__file__), './testing/jira_test.config')
     
    146147            }
    147148
    148         jb = JiraBackup(config_file=config)
     149        schema = ConfigSchema(None)
     150        schema.add_section(bckup.DEFAULT_SCHEMA_SECTION)
     151        schema.add_section(bckup.COPY_SCHEMA_SECTION)
     152        schema.add_section(bckup.TAR_SCHEMA_SECTION)
     153
     154        jb = JiraBackup(config, schema)
    149155
    150156        assert jb.default_dict == \
     
    212218        assert not os.path.exists(expected_backup_2_copy)
    213219
     220        assert jb.should_run_copy is True
    214221        return_value = jb._run_copy()
    215222        assert return_value == 0
  • branches/ipp-259_genericise_backups/tools/backups/utils/config_parse_helper.py

    r40945 r40949  
    4141    def __init__(self, section_name, options: [ConfigSchemaLine]):
    4242        self.section_name = section_name
     43        self.options = {}
    4344        self._process_options(options)
    4445
    4546    def _process_options(self, options):
    46         self.options = {}
    4747        if options is None:
    4848            return
    4949        if type(options) is not list:
    50             raise ValidationError("ValidationError: ConfigSchema options must be '[ConfigSchemaLine]' or 'None'")
     50            raise ValidationError(f"ValidationError: {self.__class__.__name__} options must be '[ConfigSchemaLine]' or 'None'")
    5151        if len(options) == 0:
    5252            return
     
    6262    def get_schema_line(self, key: str) -> ConfigSchemaLine:
    6363        if not self.key_in_options(key):
    64             raise KeyError(f"KeyError: '{key}' is not present in schema")
     64            raise KeyError(f"KeyError: '{key}' is not present in schema section")
    6565        return self.options[key]
    6666
    6767    def __str__(self):
    6868        options_strs = [ o.__str__() for o in self.options]
    69         return (f'{self.__class__.__name__} with {options_strs}')
     69        return (f'{self.__class__.__name__} named "{self.section_name}" with {options_strs}')
     70
     71
     72class ConfigSchema():
     73
     74    def __init__(self, sections: [ConfigSchemaSection]):
     75        self.sections = {}
     76        self._process_sections(sections)
     77
     78    def _process_sections(self, sections):
     79        if sections is None:
     80            return
     81        if type(sections) is not list:
     82            raise ValidationError(f"ValidationError: {self.__class__.__name__} sections must be '[ConfigSchemaSection]' or 'None'")
     83        if len(sections) == 0:
     84            return
     85        for sec in sections:
     86            self.add_section(sec)
     87
     88    def add_section(self, section: ConfigSchemaSection):
     89        self.sections[section.section_name] = section
     90
     91    def section_exists(self, section_name: str) -> bool:
     92        return section_name in self.sections.keys()
     93
     94    def get_schema_section(self, section_name: str) -> ConfigSchemaSection:
     95        if not self.section_exists(section_name):
     96            raise KeyError(f"KeyError: '{section_name}' is not present in schema")
     97        return self.sections[section_name]
     98
     99    def __str__(self):
     100        section_strs = [ o.__str__() for o in self.sections]
     101        return (f'{self.__class__.__name__} with {section_strs}')
    70102
    71103
     
    76108
    77109
    78 def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:
     110def _parse_config_section(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:
    79111    """For a given config it will extract all values defined by the schema
    80112and return them as a dictionary of the keys and the assoicated value as the
     
    87119        csl = schema.get_schema_line(csl_key)
    88120
     121        # Parses each line
    89122        if config.has_option(section, csl_key):
    90123            if csl.expected_type is bool:
     
    115148
    116149    return parsed_items
     150
     151
     152# def parse_config(config: ConfigParser(), schema: ConfigSchemaSection) -> dict:
     153#     return _parse_config_section(config, schema)
     154
     155def _parse_config(config: ConfigParser(), schema: ConfigSchema) -> dict:
     156    parsed_items = {}
     157    print(config)
     158    for c in config:
     159        print(c)
     160
     161    for css_name in schema.sections:
     162        print(css_name)
     163        css = schema.get_schema_section(css_name)
     164        print(css)
     165
     166        # if config.has_section(css_name):
     167        parsed_section = _parse_config_section(config, css)
     168        parsed_items[css_name] = parsed_section
     169        # else:
     170        #     raise ConfigError(config, f"ConfigError: Expected section '{css_name}' from schema not present in config")
     171
     172    return parsed_items
     173
     174
     175class ParsedConfig():
     176
     177    def __init__(self, config: ConfigParser, schema: ConfigSchema):
     178        self._section_dict = _parse_config(config, schema)
     179
     180    def has_section(self, section_name):
     181        return section_name in self._section_dict
     182
     183    def get_section(self, section_name):
     184        if self.has_section(section_name):
     185            return self._section_dict[section_name]
     186        return None
  • branches/ipp-259_genericise_backups/tools/backups/utils/config_parse_helper_test.py

    r40945 r40949  
    156156
    157157        with pytest.raises(ConfigError):
    158             helper.parse_config(config, schema)
     158            helper._parse_config_section(config, schema)
    159159
    160160    def test_schema_section_with_only_optional_values_is_ok(self):
     
    163163        schema = helper.ConfigSchemaSection('DEFAULT', [csl1, csl2] )
    164164        config = self.make_simple_config()
    165         result = helper.parse_config(config, schema)
     165        result = helper._parse_config_section(config, schema)
    166166        assert result == {}
    167167
     
    170170        schema = helper.ConfigSchemaSection('DEFAULT', [csl1] )
    171171        config = self.make_simple_config()
    172         result = helper.parse_config(config, schema)
     172        result = helper._parse_config_section(config, schema)
    173173        assert result == { 'inty' : 3}
    174174
     
    177177        schema = helper.ConfigSchemaSection('DEFAULT', [csl1] )
    178178        config = self.make_simple_config()
    179         result = helper.parse_config(config, schema)
     179        result = helper._parse_config_section(config, schema)
    180180        assert result == { 'floaty' : 3.141 }
    181181
     
    184184        schema = helper.ConfigSchemaSection('DEFAULT', [csl1] )
    185185        config = self.make_simple_config()
    186         result = helper.parse_config(config, schema)
     186        result = helper._parse_config_section(config, schema)
    187187        assert result == { 'mr_bool' : True }
    188188
     
    191191        schema = helper.ConfigSchemaSection('DEFAULT', [csl1] )
    192192        config = self.make_simple_config()
    193         result = helper.parse_config(config, schema)
     193        result = helper._parse_config_section(config, schema)
    194194        assert result == { 'stringer' : 'bell' }
    195195
     
    199199        schema = helper.ConfigSchemaSection('DEFAULT', [csl1] )
    200200        config = self.make_simple_config()
    201         result = helper.parse_config(config, schema)
     201        result = helper._parse_config_section(config, schema)
    202202        assert result == { 'listo_comma' : [ "i"
    203203                                           , "am"
     
    212212        schema = helper.ConfigSchemaSection('DEFAULT', [csl1] )
    213213        config = self.make_simple_config()
    214         result = helper.parse_config(config, schema)
     214        result = helper._parse_config_section(config, schema)
    215215        assert result == { 'listo_space' : [ "yo"
    216216                                           , "it's"
     
    232232
    233233        schema = helper.ConfigSchemaSection('DEFAULT', [csl_float])
    234         result = helper.parse_config(config, schema)
     234        result = helper._parse_config_section(config, schema)
    235235        assert result == { 'floaty' : 3.141 }
    236236        assert type(result['floaty']) is float
     
    238238        schema = helper.ConfigSchemaSection('DEFAULT', [csl_int])
    239239        with pytest.raises(ValueError):
    240             helper.parse_config(config, schema)
     240            helper._parse_config_section(config, schema)
    241241
    242242        schema = helper.ConfigSchemaSection('DEFAULT', [csl_bool])
    243243        with pytest.raises(ValueError):
    244             helper.parse_config(config, schema)
     244            helper._parse_config_section(config, schema)
    245245
    246246        schema = helper.ConfigSchemaSection('DEFAULT', [csl_str])
    247         result = helper.parse_config(config, schema)
     247        result = helper._parse_config_section(config, schema)
    248248        assert result == { 'floaty' : '3.141' }
    249249        assert type(result['floaty']) is str
    250250
    251251        schema = helper.ConfigSchemaSection('DEFAULT', [csl_list])
    252         result = helper.parse_config(config, schema)
     252        result = helper._parse_config_section(config, schema)
    253253        assert result == { 'floaty' : ['3.141'] }
    254254        assert type(result['floaty']) is list
     
    259259        config = self.make_simple_config()
    260260        with pytest.raises(ConfigError):
    261             helper.parse_config(config, schema)
     261            helper._parse_config_section(config, schema)
    262262
    263263    def test_all_special_processing_options_are_handled(self):
     
    269269            schema = helper.ConfigSchemaSection('DEFAULT', [csl])
    270270
    271             result = helper.parse_config(config, schema)
     271            result = helper._parse_config_section(config, schema)
    272272            assert type(result['enum_checker']) is list
    273273
     
    285285        schema = helper.ConfigSchemaSection('FULL_TEST', [csl1, csl2, csl3, csl4, csl5, csl6])
    286286
    287         result = helper.parse_config(config, schema)
     287        result = helper._parse_config_section(config, schema)
    288288        assert result == \
    289289        { 'inty'        : 3
Note: See TracChangeset for help on using the changeset viewer.