Ejemplo n.º 1
0
    def test_get_system_file_path(self):
        """ tests that you can get the system path of a file """
        expected = get_joined_path(self.nest_dir, 'test2.avsc')

        # change dir to alter local path
        cwd = os.getcwd()
        os.chdir(self.root)

        tmp_wd = os.getcwd()

        expected = get_joined_path(tmp_wd, 'avro', 'nest', 'test2.avsc')

        path1 = get_system_path(get_joined_path(self.nest_dir, 'test2.avsc'))

        # change dir to alter local path
        cwd = os.getcwd()
        os.chdir(self.nest_dir)
        print(os.getcwd())

        path2 = get_system_path('test2.avsc')

        # change back to original dir to prevent errors
        os.chdir(cwd)

        self.assertEqual(path1, path2, f'paths not equal: {path1} != {path2}')

        self.assertEqual(expected, path2,
                         f'paths not equal: {expected} != {path2}')
Ejemplo n.º 2
0
    def write(self, root_dir: str) -> None:
        """ Public runner method for writing all files in a tree

        Parameters
        ----------
            root_path: str
                root path to write files to

        Returns
        -------
            None
        """

        self.root_dir = get_system_path(root_dir)
        if self.pip:
            self.pip_import = self.pip.replace('-', '_') + '.'
            self.pip_dir = self.root_dir + '/' + self.pip
            self.root_dir += '/' + self.pip + '/' + self.pip.replace('-', '_')
            self.pip = self.pip.replace('-', '_')
        else:
            self.pip_import = ''
        get_or_create_path(self.root_dir)
        self._write_helper_file()

        self._reset_tree()
        self._dfs(self.tree)

        if self.pip:
            self._write_setup_file()
            self._write_pip_init_file()
            self._write_manifest_file()
Ejemplo n.º 3
0
    def __init__(self, directory: str = None, file: str = None) -> None:
        """ Initializer should just create a list of files to process

        Parameters
        ----------
            directory: str
                Directory of files to read
                Cannot be used with "file" param

            file: str
                path of avsc file to compile
                Cannot be used with "directory" param

        Returns
        -------
            None
        """

        # initialize cental object
        self.obj = {}
        self.file_tree = None

        if directory:
            if os.path.isfile(directory):
                raise OSError(f'{directory} is a file!')
            files = get_avsc_files(directory)
            if files:
                self.files = files
                self.obj['root_dir'] = get_system_path(directory)
                self.obj['read_type'] = 'directory'
            else:
                raise NoFilesError(f'No avsc files found in {directory}')

        elif file:
            if not verify_path_exists(file):
                raise MissingFileError(f'{file} does not exist!')
            if os.path.isdir(file):
                raise IsADirectoryError(f'{file} is a directory!')
            syspath = get_system_path(file)
            self.files = [syspath]
            self.obj['read_type'] = 'file'

        else:
            raise NoFileOrDir

        self.obj['avsc'] = []
Ejemplo n.º 4
0
""" Writer class for writing python avro files """

import json

from jinja2 import Environment, FileSystemLoader

from avro_to_python.classes.node import Node
from avro_to_python.utils.avro.helpers import get_union_types
from avro_to_python.utils.avro.primitive_types import PRIMITIVE_TYPE_MAP
from avro_to_python.utils.paths import (get_system_path,
                                        verify_or_create_namespace_path,
                                        get_or_create_path, get_joined_path)

TEMPLATE_PATH = __file__.replace(get_joined_path('writer', 'writer.py'),
                                 'templates/')
TEMPLATE_PATH = get_system_path(TEMPLATE_PATH)


class AvroWriter(object):
    """ writer class for writing python files

    Should initiate around a tree object with nodes as:

    {
        'children': {},
        'files': {},
        'visited': False
    }

    The "keys" of the children are the namespace names along avro
    namespace paths. The Files are the actual files within the
Ejemplo n.º 5
0
    def test_get_system_file_path(self):
        """ tests that you can get the system path of a file """
        expected = '/tmp/avro/nest/test2.avsc'

        # change dir to alter local path
        cwd = os.getcwd()
        os.chdir('/tmp')

        tmp_wd = os.getcwd()

        expected = tmp_wd + '/avro/nest/test2.avsc'

        path1 = get_system_path('avro/nest/test2.avsc')

        # change dir to alter local path
        cwd = os.getcwd()
        os.chdir('/tmp/avro/nest')
        print(os.getcwd())

        path2 = get_system_path('test2.avsc')

        # change back to original dir to prevent errors
        os.chdir(cwd)

        self.assertEqual(path1, path2, f'paths not equal: {path1} != {path2}')

        self.assertEqual(expected, path2,
                         f'paths not equal: {expected} != {path2}')

        def test_verify_or_create_namespace_path(self):
            """ tests that verify_or_create_namespace_path works """
            rootdir = '/tmp'
            namespace = 'test.namespace.path'

            verify_or_create_namespace_path(rootdir=rootdir,
                                            namespace=namespace)

            self.assertTrue(verify_path_exists('/tmp/test/namespace/path/'),
                            'should have created the path from a namespace')

            verify_or_create_namespace_path(rootdir=rootdir,
                                            namespace=namespace)

            self.assertTrue(verify_path_exists('/tmp/test/namespace/path/'),
                            'should have created the path from a namespace')
            os.rmdir('/tmp/test/')

        def test_get_or_create_path(self):
            """ tests that get_or_create_path works """
            path = '/tmp/test'

            get_or_create_path(path=path)

            self.assertTrue(verify_path_exists('/tmp/test'),
                            'should have created path at /tmp/test')
            os.rmdir(path)

            os.chdir('/tmp')

            get_or_create_path(path='test')

            self.assertTrue(
                verify_path_exists('/tmp/test'),
                'should have created path at /tmp/test from relative path')
            os.rmdir(path)