Example #1
0
def setup_logging(project_title, entity, asset):
    """Setup logging for a project"""

    config = get_config(project_title)
    output_path = config.get('paths:output', 'project')
    error_log = logging.getLogger('error_Log')
    log_path = '{0}/{1}_{2}_error.log'.format(output_path, entity, asset)
    core_utils.create_dirs_in_path(log_path)
    error_log.addHandler(logging.FileHandler(log_path, mode='w'))

    return error_log
Example #2
0
 def set_output_path(self, format_=None, entity=None):
     """
     Define output path for an asset being rendered
     Make directories for it
     """
     config = self.config
     entity = entity or self.entity
     entity_id = None
     if config.has_section(entity.fs_name):
         if config.has_option(entity.fs_name,
                              '{0}_id_pattern'.format(self.asset)):
             id_pattern = self.config.get(entity.fs_name,
                                          '{0}_id_pattern'.format(self.asset))
             entity_id = id_pattern.format(**entity)
         else:
             entity_id = config.get(entity.fs_name,
                                    'id_pattern').format(**entity)
     if config.has_option('paths:output',
                          '{0}_{1}'.format(entity.fs_name,
                                           self.asset)):
         output_path = config.get(
             'paths:output',
             '{0}_{1}'.format(entity.fs_name,
                              self.asset)).format(entity=entity.fs_name,
                                                  asset=self.asset,
                                                  id=entity_id,
                                                  format=format_,
                                                  **entity)
     else:
         output_path = config.get('paths:output',
                                  'entity_asset').format(entity=entity.fs_name,
                                                         asset=self.asset,
                                                         id=entity_id,
                                                         format=format_,
                                                         **entity)
     utils.create_dirs_in_path(output_path)
     self.output_path = output_path
     return self.output_path
Example #3
0
def create(project_title):
    """Initialize project with given title"""
    from mako.template import Template
    config = get_global_config(project_title)

    # initialize config
    config_path = config.get('paths:input', 'config')
    if os.path.exists(config_path):
        print(red('Project `{title}` already exists!'.format(
            title=project_title)))
        raise SystemExit(0)
    core_utils.create_dirs_in_path(config_path)
    with codecs.open(config_path, 'w', 'utf-8') as f:
        template = Template(filename='templates/config.ini.mako')
        contents = template.render(project_title=project_title)
        f.write(contents)

    session = core_models.populate_session(config.get('paths', 'database'),
                                          create_tables=True)
    project = core_models.Project(project_title)
    session.add(project)
    session.commit()

    print(green('Project `{title}` created!'.format(title=project_title)))
Example #4
0
def canonize(project_title):
    """Create a canonic JSON data file"""
    config = get_config(project_title)
    iterator_lib = config.get('input_data', 'iterator').split('.')[0]
    iterator_name = config.get('input_data', 'iterator').split('.')[1]

    apt_data = list()
    iterator_class = import_from('hydra.'
                                 'iterators.{0}'.format(iterator_lib),
                                 iterator_name)
    iterator = iterator_class(config)
    if len(iterator):
        print(cyan('Creating canonic JSON with {0}...'.format(iterator_name)))
        for apt_dict in get_progress(iterator):
            if apt_dict:
                apt_data.append(apt_dict)
        canonic_path = config.get('paths:input', 'canonic_json')
        core_utils.create_dirs_in_path(canonic_path)
        with codecs.open(canonic_path,
                         'w', 'utf-8') as f:
            f.write(json.dumps(apt_data, ensure_ascii=False, indent=2,
                               separators=(',', ':')))
    else:
        print(yellow('Iterator empty!'))
Example #5
0
    def render_json(self):
        project = self.entity
        import codecs
        import art3dutils.models as models

        output_file_path = self.output_path
        config = self.config
        dict_ = OrderedDict()

        #add additional fields if any
        if config.has_section('project:extras'):
            extra_attribs = dict()
            for attrib, short_type in config.items('project:extras'):
                extra_attribs[attrib] = tuple(short_type.split(', '))
            models.ATTR_TYPES.update(extra_attribs)

        filters = []
        if config.has_option('project:data_renderer', 'filters'):
            filter_names = config.get('project:data_renderer',
                                      'filters').split(', ')
            config.remove_option('project:data_renderer', 'filters')
            for filter_name in filter_names:
                filter_ = import_from('art3d_hydra.'
                                      'filters.{0}'.format(project.title),
                                      filter_name)
                filters.append(filter_)

        for entity, attribs in config.items('project:data_renderer')[2:]:
            id_pattern = config.get(entity, 'id_pattern')
            dict_['{0}s'.format(entity)] = OrderedDict()
            print('Rendering {0}s...'.format(entity))
            entity_class = import_from('art3dutils.models', entity.title())
            instances = entity_class.fetch_all(project.title)
            for instance in utils.progressbar(instances):
                instance_dict = OrderedDict()

                for attrib in attribs.split(', '):
                    short, typ = models.ATTR_TYPES[attrib]
                    value = getattr(instance, attrib)
                    instance_dict[short] = utils.process_value(value, typ)

                    # insert fixed room counts
                    if attrib == 'available_detail' \
                       and config.has_option('project', 'room_counts'):
                        room_counts = config.get('project',
                                                 'room_counts').split(', ')
                        room_counts = [int(rc) for rc in room_counts]
                        room_counts.append('t')
                        instance_dict[short] = dict()
                        for rc in room_counts:
                            if rc in instance.available_detail:
                                instance_dict[short][rc] = \
                                    instance.available_detail[rc]
                            else:
                                instance_dict[short][rc] = 0

                    # calc total cost
                    if attrib == 'total_cost' and not value:
                        instance_dict[short] = instance.calc_total_cost()

                    # check note for json
                    if attrib == 'note':
                        try:
                            instance_dict[short] = json.loads(value)
                        except (TypeError, ValueError):
                            pass
                for filter_ in filters:
                    filter_(instance_dict, instance)
                try:
                    key = id_pattern.format(**instance)
                except TypeError:
                    key = id_pattern.format(
                        building_number=instance.building_number,
                        number=instance.number)
                dict_['{0}s'.format(entity)][key] = instance_dict

        utils.create_dirs_in_path(output_file_path)
        with codecs.open(output_file_path, 'w', 'utf-8') as f:
            if not self.minified:
                data_string = json.dumps(dict_, ensure_ascii=False, indent=2,
                                         separators=(',', ':'))
            else:
                data_string = json.dumps(dict_, ensure_ascii=False)
            f.write(data_string)