Example #1
0
    def test_data_in(self):
        """Verify a sample file is loaded."""
        path = os.path.join(FILES, 'mine-in.yml')

        data = Data()
        yorm.sync(data, path)

        assert data.config.applications
        for application in data.config.applications:
            if application.name == 'slack':
                break
        else:
            assert False
Example #2
0
    def test_function(self, tmpdir):
        """Verify extended attribute types dump/parse correctly."""
        tmpdir.chdir()
        _sample = SampleExtended()
        attrs = {'text': Markdown}
        sample = yorm.sync(_sample, "tmp/directory/sample.yml", attrs)

        # check defaults
        assert "" == sample.text

        # change object values
        refresh_file_modification_times()
        sample.text = strip("""
        This is the first sentence. This is the second sentence.
        This is the third sentence.
        """)

        # check file values
        assert strip("""
        text: |
          This is the first sentence.
          This is the second sentence.
          This is the third sentence.
        """) == sample.__mapper__.text

        # change file values
        refresh_file_modification_times()
        sample.__mapper__.text = strip("""
        text: |
          This is a
          sentence.
        """)

        # check object values
        assert "This is a sentence." == sample.text
Example #3
0
    def test_function(self, tmpdir):
        """Verify extended attribute types dump/parse correctly."""
        tmpdir.chdir()
        _sample = SampleExtended()
        attrs = {'text': Markdown}
        sample = yorm.sync(_sample, "tmp/directory/sample.yml", attrs)

        # check defaults
        assert "" == sample.text

        # change object values
        refresh_file_modification_times()
        sample.text = strip("""
        This is the first sentence. This is the second sentence.
        This is the third sentence.
        """)

        # check file values
        assert strip("""
        text: |
          This is the first sentence.
          This is the second sentence.
          This is the third sentence.
        """) == sample.__mapper__.text

        # change file values
        refresh_file_modification_times()
        sample.__mapper__.text = strip("""
        text: |
          This is a
          sentence.
        """)

        # check object values
        assert "This is a sentence." == sample.text
Example #4
0
    def test_function_to_json(self, tmpdir):
        """Verify standard attribute types dump/parse correctly (function)."""
        tmpdir.chdir()
        _sample = SampleStandard()
        attrs = {
            'object': self.StatusDictionary,
            'array': IntegerList,
            'string': String,
            'number_int': Integer,
            'number_real': Float,
            'truthy': Boolean,
            'falsey': Boolean
        }
        sample = yorm.sync(_sample, "tmp/directory/sample.json", attrs)
        assert "tmp/directory/sample.json" == sample.__mapper__.path

        # check defaults
        assert {'status': False} == sample.object
        assert [] == sample.array
        assert "" == sample.string
        assert 0 == sample.number_int
        assert 0.0 == sample.number_real
        assert True is sample.truthy
        assert False is sample.falsey
        assert None is sample.null

        # change object values
        sample.object = {'key': 'value'}
        sample.array = [1, 2, 3]
        sample.string = "Hello, world!"
        sample.number_int = 42
        sample.number_real = 4.2
        sample.truthy = None
        sample.falsey = 1

        # check file values
        assert strip("""
        {
            "array": [
                1,
                2,
                3
            ],
            "falsey": true,
            "number_int": 42,
            "number_real": 4.2,
            "object": {
                "status": false
            },
            "string": "Hello, world!",
            "truthy": false
        }
        """,
                     tabs=2,
                     end='') == sample.__mapper__.text
Example #5
0
    def test_nesting(self, tmpdir):
        """Verify standard attribute types can be nested."""
        tmpdir.chdir()
        _sample = SampleNested()
        attrs = {'count': Integer,
                 'results': StatusDictionaryList}
        sample = yorm.sync(_sample, "tmp/sample.yml", attrs, auto_track=True)

        # check defaults
        assert 0 == sample.count
        assert [] == sample.results

        # change object values
        sample.count = 5
        sample.results = [{'status': False, 'label': "abc"},
                          {'status': None, 'label': None},
                          {'label': "def"},
                          {'status': True},
                          {}]

        # check file values
        assert strip("""
        count: 5
        results:
        - label: abc
          status: false
        - label: ''
          status: false
        - label: def
          status: false
        - label: ''
          status: true
        - label: ''
          status: false
        """) == sample.__mapper__.text

        # change file values
        refresh_file_modification_times()
        sample.__mapper__.text = strip("""
        count: 3
        other: 4.2
        results:
        - label: abc
        - label: null
          status: false
        - status: true
        """)

        # check object values
        assert 3 == sample.count
        assert 4.2 == sample.other
        assert [{'label': 'abc', 'status': False},
                {'label': '', 'status': False},
                {'label': '', 'status': True}] == sample.results
Example #6
0
    def test_nesting(self, tmpdir):
        """Verify standard attribute types can be nested."""
        tmpdir.chdir()
        _sample = SampleNested()
        attrs = {'count': Integer,
                 'results': StatusDictionaryList}
        sample = yorm.sync(_sample, "sample.yml", attrs)

        # check defaults
        assert 0 == sample.count
        assert [] == sample.results

        # change object values
        sample.count = 5
        sample.results = [{'status': False, 'label': "abc"},
                          {'status': None, 'label': None},
                          {'label': "def"},
                          {'status': True},
                          {}]

        # check file values
        assert strip("""
        count: 5
        results:
        - label: abc
          status: false
        - label: ''
          status: false
        - label: def
          status: false
        - label: ''
          status: true
        - label: ''
          status: false
        """) == sample.yorm_mapper.text

        # change file values
        refresh_file_modification_times()
        sample.yorm_mapper.text = strip("""
        count: 3
        other: 4.2
        results:
        - label: abc
        - label: null
          status: false
        - status: true
        """)

        # check object values
        assert 3 == sample.count
        assert 4.2 == sample.other
        assert [{'label': 'abc', 'status': False},
                {'label': '', 'status': False},
                {'label': '', 'status': True}] == sample.results
Example #7
0
    def test_data_out(self):
        """Verify a sample file is created."""
        path = os.path.join(FILES, 'mine-out.yml')

        if os.path.exists(path):
            os.remove(path)

        data = Data()
        yorm.sync(data, path)

        itunes = Application('itunes')
        itunes.versions.mac = ''
        itunes.versions.windows = 'iTunes.exe'

        iphoto = Application('iphoto')
        iphoto.versions.mac = 'iPhoto'

        mac = Computer('macbook', 'Jaces-MacBook', 'AA:BB:CC:DD:EE:FF')
        mac2 = Computer('macbook-pro', 'Jaces-MacBook-2', '11:22:33:44:55:66')

        configuration = ProgramConfig()
        configuration.applications = [itunes, iphoto]
        configuration.computers = [mac, mac2]

        data.config = configuration

        mac_state = State('macbook-pro')
        mac_state.timestamp.started = 444

        itunes_status = Status('itunes')
        itunes_status.computers = [mac_state]

        status = ProgramStatus()
        status.applications = [itunes_status]
        status.counter = 499

        data.status = status

        assert os.path.exists(path)
Example #8
0
    def test_function_to_json(self, tmpdir):
        """Verify standard attribute types dump/parse correctly (function)."""
        tmpdir.chdir()
        _sample = SampleStandard()
        attrs = {'object': self.StatusDictionary,
                 'array': IntegerList,
                 'string': String,
                 'number_int': Integer,
                 'number_real': Float,
                 'truthy': Boolean,
                 'falsey': Boolean}
        sample = yorm.sync(_sample, "tmp/directory/sample.json", attrs)
        assert "tmp/directory/sample.json" == sample.__mapper__.path

        # check defaults
        assert {'status': False} == sample.object
        assert [] == sample.array
        assert "" == sample.string
        assert 0 == sample.number_int
        assert 0.0 == sample.number_real
        assert True is sample.truthy
        assert False is sample.falsey
        assert None is sample.null

        # change object values
        sample.object = {'key': 'value'}
        sample.array = [1, 2, 3]
        sample.string = "Hello, world!"
        sample.number_int = 42
        sample.number_real = 4.2
        sample.truthy = None
        sample.falsey = 1

        # check file values
        assert strip("""
        {
            "array": [
                1,
                2,
                3
            ],
            "falsey": true,
            "number_int": 42,
            "number_real": 4.2,
            "object": {
                "status": false
            },
            "string": "Hello, world!",
            "truthy": false
        }
        """, tabs=2, end='') == sample.__mapper__.text
Example #9
0
    def test_function(self, tmpdir):
        """Verify standard attribute types dump/parse correctly (function)."""
        tmpdir.chdir()
        _sample = SampleStandard()
        attrs = {
            'object': self.StatusDictionary,
            'array': IntegerList,
            'string': String,
            'number_int': Integer,
            'number_real': Float,
            'truthy': Boolean,
            'falsey': Boolean
        }
        sample = yorm.sync(_sample, "tmp/directory/sample.yml", attrs)
        assert "tmp/directory/sample.yml" == sample.__mapper__.path

        # check defaults
        assert {'status': False} == sample.object
        assert [] == sample.array
        assert "" == sample.string
        assert 0 == sample.number_int
        assert 0.0 == sample.number_real
        assert True is sample.truthy
        assert False is sample.falsey
        assert None is sample.null

        # change object values
        sample.object = {'key': 'value'}
        sample.array = [1, 2, 3]
        sample.string = "Hello, world!"
        sample.number_int = 42
        sample.number_real = 4.2
        sample.truthy = None
        sample.falsey = 1

        # check file values
        assert strip("""
        array:
        - 1
        - 2
        - 3
        falsey: true
        number_int: 42
        number_real: 4.2
        object:
          status: false
        string: Hello, world!
        truthy: false
        """) == sample.__mapper__.text
Example #10
0
    def test_function(self, tmpdir):
        """Verify standard attribute types dump/parse correctly (function)."""
        tmpdir.chdir()
        _sample = SampleStandard()
        attrs = {'object': self.StatusDictionary,
                 'array': IntegerList,
                 'string': String,
                 'number_int': Integer,
                 'number_real': Float,
                 'truthy': Boolean,
                 'falsey': Boolean}
        sample = yorm.sync(_sample, "tmp/directory/sample.yml", attrs)
        assert "tmp/directory/sample.yml" == sample.__mapper__.path

        # check defaults
        assert {'status': False} == sample.object
        assert [] == sample.array
        assert "" == sample.string
        assert 0 == sample.number_int
        assert 0.0 == sample.number_real
        assert True is sample.truthy
        assert False is sample.falsey
        assert None is sample.null

        # change object values
        sample.object = {'key': 'value'}
        sample.array = [1, 2, 3]
        sample.string = "Hello, world!"
        sample.number_int = 42
        sample.number_real = 4.2
        sample.truthy = None
        sample.falsey = 1

        # check file values
        assert strip("""
        array:
        - 1
        - 2
        - 3
        falsey: true
        number_int: 42
        number_real: 4.2
        object:
          status: false
        string: Hello, world!
        truthy: false
        """) == sample.__mapper__.text
Example #11
0
 def sample(tmpdir):
     cls = type('Sample', (), {})
     path = str(tmpdir.join("sample.yml"))
     attrs = dict(var4=NestedList3, var5=StatusDictionary)
     return yorm.sync(cls(), path, attrs)
Example #12
0
def run(path=None, cleanup=True, delay=None,
        switch=None,
        edit=False,
        delete=False, force=False):
    """Run the program.

    :param path: custom settings file path
    :param cleanup: remove unused items from the config
    :param delay: number of seconds to delay before repeating

    :param switch: computer name to queue for launch

    :param edit: launch the configuration file for editing

    :param delete: attempt to delete conflicted files
    :param force: actually delete conflicted files

    """  # pylint: disable=too-many-arguments,too-many-branches
    manager = get_manager()
    if not manager.is_running(services.APPLICATION):
        manager.start(services.APPLICATION)

    root = services.find_root()
    path = path or services.find_config_path(root=root)

    data = Data()
    yorm.sync(data, path)

    config = data.config
    status = data.status

    log.info("Identifying current computer...")
    computer = config.computers.get_current()
    log.info("Current computer: %s", computer)

    if edit:
        return manager.launch(path)
    if delete:
        return services.delete_conflicts(root, force=force)

    if switch is True:
        switch = computer
    elif switch is False:
        data.close_all_applications(config, manager)
    elif switch:
        switch = config.computers.match(switch)

    if switch:
        if switch != computer:
            data.close_all_applications(config, manager)
        data.queue_all_applications(config, status, switch)

    while True:
        data.launch_queued_applications(config, status, computer, manager)
        data.update_status(config, status, computer, manager)

        if delay is None:
            break

        log.info("Delaying %s seconds for files to sync...", delay)
        time.sleep(delay)

        step = 1
        elapsed = 0
        log.info("Waiting %s seconds for status changes...", delay)
        while elapsed < delay and not data.modified:
            time.sleep(step)
            elapsed += step

        services.delete_conflicts(root, config_only=True, force=True)

    if cleanup:
        data.prune_status(config, status)

    if delay is None:
        return _restart_daemon(manager)

    return True
Example #13
0
 def sample(tmpdir):
     cls = type('Sample', (), {})
     path = str(tmpdir.join("sample.yml"))
     attrs = dict(var4=NestedList3, var5=StatusDictionary)
     return yorm.sync(cls(), path, attrs)
Example #14
0
 def add_row(self):
     row = Row()
     yorm.sync(row, "{}/{}.yml".format(self.name, row.uuid))
     self.rows.append(row)
     return row
Example #15
0
 def _fetch_data(self):
     """Read the final data file back for verification."""
     data = Data()
     yorm.sync(data, self.path)
     return data
Example #16
0
 def _store_data(self):
     """Set up initial data file for tests."""
     self.data = Data()
     self.data.config.applications.append(self.application)
     self.computer = self.data.config.computers.get_current()  # pylint: disable=no-member
     yorm.sync(self.data, self.path)