def test_config_generation_ok(self): '''This test case ensures config nginx script works as expected.''' settings_cls = SettingsFacade root_folder = os.path.abspath(instantiator.get_class_abslocation(settings_cls) + "../") tpl_loader = FileSystemLoader(searchpath=root_folder) tpl_env = Environment(loader=tpl_loader) config_data = {"ip_address": "127.0.0.1", "vhost_name": "test-app.com", "http_port": 80, "uwsgi_port": 12090, "root_folder": "/test/folder/vhost", "modules_folder": "/"} expected_config = tpl_env.get_template("/deployment/conf/nginx/fantastico-wsgi").render(config_data) sys.argv = ["config_nginx.py", "--ipaddress", "127.0.0.1", "--vhost-name", "test-app.com", "--uwsgi-port", "12090", "--root-folder", "/test/folder/vhost", "--modules-folder", "/"] from fantastico.deployment import config_nginx generated_config = StringIO() config_nginx.main(generated_config) self.assertEqual(expected_config, generated_config.getvalue())
def _yield_lines(self, tokensList): """ we iterate over all datasources as long as one is available hence still contains tokens """ lineno = 1 decoratedTokensList = [] for index, tokenSource in enumerate(tokensList): decoratedTokens = { "notEmpty": True, "tokensource": tokenSource, "index": index, # this generator replaces temporally the generator returned by the lexer # in order to support lexers returning tokens that contain line feeds # within "yieldTokenParts": None, } decoratedTokensList.append(decoratedTokens) while self._is_code_left(decoratedTokensList): lineBuffer = StringIO() lineStarted = False lineEmpty = True for containsSomething, singleSourceLine in self._yield_multisourceline(decoratedTokensList, lineno): if containsSomething: lineEmpty = False if not lineStarted: lineBuffer.write("<tr>") for linenoPart in self._yield_lineno(lineno): lineBuffer.write(linenoPart) lineno += 1 lineStarted = True lineBuffer.write(singleSourceLine) if lineStarted: lineBuffer.write("</tr>") yield lineEmpty, lineBuffer.getvalue()
class TuningDescFileWriter: __qualname__ = 'TuningDescFileWriter' SORT_OVERRIDE = collections.defaultdict( lambda: 0, { 'name': 100, 'class': 99, 'type': 98, 'default': 97, 'min': -1, 'max': -2, 'description': -100 }) def __init__(self, module, export_path, whitespace_depth=0): if export_path is None: export_path = paths.TUNING_ROOTS[sims4.resources.Types.TUNING] tuning_file = self.get_file_name(module) self._filename = os.path.join(export_path, tuning_file) self._writer = None self._whitespace_depth = whitespace_depth def get_file_name(self, module): is_fragment = getattr(module, 'is_fragment', False) if is_fragment: return get_tdesc_frag_name(module) return get_desc_file_name(module) def open(self): self._open() @staticmethod def list_key(value): if isinstance(value, dict): return value.get(Attributes.Name, '') return '' @staticmethod def sort_tags_recursive(attr_vals, sort_override=None): if not sort_override: sort_key = None else: def sort_key(value): return (-sort_override[value], value) if isinstance(attr_vals, dict) and not isinstance( attr_vals, collections.OrderedDict): new_vals = collections.OrderedDict() for key in sorted(attr_vals, key=sort_key): new_vals[key] = TuningDescFileWriter.sort_tags_recursive( attr_vals[key], sort_override) return new_vals if isinstance(attr_vals, list): new_vals = [] for value in sorted(attr_vals, key=TuningDescFileWriter.list_key): new_vals.append( TuningDescFileWriter.sort_tags_recursive( value, sort_override)) return new_vals return attr_vals def write_tunable(self, tunable): desc_tag = tunable.TAGNAME attr_vals = tunable.export_desc() sorted_vals = TuningDescFileWriter.sort_tags_recursive( attr_vals, sort_override=TuningDescFileWriter.SORT_OVERRIDE) self._writer.startElement(desc_tag, AttributesImpl(sorted_vals), can_close=True) self._writer.endElement(desc_tag) def write_frag(self, tunable): desc_tag = Attributes.TdescFragClass tunable_vals = {} attr_vals = tunable.frag_desc() tunable_vals[tunable.FRAG_TAG_NAME] = attr_vals sorted_vals = TuningDescFileWriter.sort_tags_recursive( tunable_vals, sort_override=TuningDescFileWriter.SORT_OVERRIDE) self._writer.startElement(desc_tag, AttributesImpl(sorted_vals), can_close=True) self._writer.endElement(desc_tag) def write_enum_items(self, enum_class): if hasattr(enum_class, '_static_index'): last_static_index = enum_class._static_index + 1 else: last_static_index = len(enum_class.names) for i in range(last_static_index): enum_name = enum_class.names[i] enum_value = int(enum_class[enum_name]) attr_vals = { Attributes.Name: enum_name, Attributes.EnumValue: enum_value } self._writer.startElement(Tags.EnumItem, AttributesImpl(attr_vals), can_close=True) self._writer.endElement(Tags.EnumItem) def write_deleted_tunable(self, deleted_tunable_name): attr_vals = {Attributes.Name: deleted_tunable_name} self._writer.startElement(Tags.Deleted, AttributesImpl(attr_vals), can_close=True) self._writer.endElement(Tags.Deleted) def _open(self): class Writer(XMLGenerator): __qualname__ = 'TuningDescFileWriter._open.<locals>.Writer' SPACES_PER_INDENT = 4 def __init__(self, *args, whitespace_depth=0, **kwargs): super().__init__(*args, **kwargs) self._whitespace_depth = whitespace_depth self._indent = 0 self._already_closed = None self._last_indent = -1 def startElement(self, name, attrs, can_close=False): sub_elements = {} if self._indent == self._last_indent and self._indent <= self.SPACES_PER_INDENT * self._whitespace_depth: self.add_new_line() self._last_indent = self._indent self._write('{0}<{1}'.format(' ' * self._indent, name)) for (attr_name, value) in attrs.items(): if isinstance(value, dict) or isinstance(value, list): sub_elements[attr_name] = value else: while value is not None: self._write(' %s=%s' % (attr_name, quoteattr(str(value)))) if not sub_elements and can_close: self._write(' />\n'.format(name)) self._already_closed = name else: self._write('>\n') for (name, value) in sub_elements.items(): if isinstance(value, dict): self.startElement(name, value, can_close=True) self.endElement(name) else: for sub_item in value: self.startElement(name, sub_item, can_close=True) self.endElement(name) def endElement(self, name): if self._already_closed is not None: self._already_closed = None else: self._write(' ' * self._indent) super().endElement(name) self.add_new_line() self._last_indent = self._indent def add_new_line(self): self._write('\n') self._string = StringIO() self._writer = Writer(self._string, whitespace_depth=self._whitespace_depth) self._writer.startDocument() self._writer.add_new_line() def start_namespace(self, namespace, contents): attribute_dict = {} for (attribute, value) in contents.items(): while not isinstance(value, TunableBase) and ( not isinstance(value, dict) and (not isinstance(value, list) and not isinstance( value, enum.Metaclass))) and value is not DELETEDMARKER: attribute_dict[attribute] = value self._writer.startElement(namespace, attribute_dict) def end_namespace(self, namespace): self._writer.endElement(namespace) def close(self): self._writer.endDocument() serialized_data = self._string.getvalue() self._string.close() path = os.path.split(self._filename)[0] if path and not os.path.exists(path): os.makedirs(path) do_compare = True tuning_file = None created = True if os.path.exists(self._filename): created = False if not do_compare: try: with open(self._filename, 'w'): pass except IOError: do_compare = True if do_compare: with open(self._filename, 'r') as old_tuning_file: old_serialized_data = old_tuning_file.read() if serialized_data == old_serialized_data: logger.debug('Skipped tuning file: {}', self._filename) return try: tuning_file = open(self._filename, 'w') except IOError: raise TunableFileReadOnlyError(self._filename) with tuning_file: tuning_file.write(serialized_data) if created: logger.warn('CREATED tuning file: {}', self._filename) elif do_compare: logger.info('Updated tuning file: {}', self._filename) else: logger.info('Wrote tuning file: {}', self._filename)
class TuningDescFileWriter: __qualname__ = 'TuningDescFileWriter' SORT_OVERRIDE = collections.defaultdict(lambda : 0, {'name': 100, 'class': 99, 'type': 98, 'default': 97, 'min': -1, 'max': -2, 'description': -100}) def __init__(self, module, export_path, whitespace_depth=0): if export_path is None: export_path = paths.TUNING_ROOTS[sims4.resources.Types.TUNING] tuning_file = self.get_file_name(module) self._filename = os.path.join(export_path, tuning_file) self._writer = None self._whitespace_depth = whitespace_depth def get_file_name(self, module): is_fragment = getattr(module, 'is_fragment', False) if is_fragment: return get_tdesc_frag_name(module) return get_desc_file_name(module) def open(self): self._open() @staticmethod def list_key(value): if isinstance(value, dict): return value.get(Attributes.Name, '') return '' @staticmethod def sort_tags_recursive(attr_vals, sort_override=None): if not sort_override: sort_key = None else: def sort_key(value): return (-sort_override[value], value) if isinstance(attr_vals, dict) and not isinstance(attr_vals, collections.OrderedDict): new_vals = collections.OrderedDict() for key in sorted(attr_vals, key=sort_key): new_vals[key] = TuningDescFileWriter.sort_tags_recursive(attr_vals[key], sort_override) return new_vals if isinstance(attr_vals, list): new_vals = [] for value in sorted(attr_vals, key=TuningDescFileWriter.list_key): new_vals.append(TuningDescFileWriter.sort_tags_recursive(value, sort_override)) return new_vals return attr_vals def write_tunable(self, tunable): desc_tag = tunable.TAGNAME attr_vals = tunable.export_desc() sorted_vals = TuningDescFileWriter.sort_tags_recursive(attr_vals, sort_override=TuningDescFileWriter.SORT_OVERRIDE) self._writer.startElement(desc_tag, AttributesImpl(sorted_vals), can_close=True) self._writer.endElement(desc_tag) def write_frag(self, tunable): desc_tag = Attributes.TdescFragClass tunable_vals = {} attr_vals = tunable.frag_desc() tunable_vals[tunable.FRAG_TAG_NAME] = attr_vals sorted_vals = TuningDescFileWriter.sort_tags_recursive(tunable_vals, sort_override=TuningDescFileWriter.SORT_OVERRIDE) self._writer.startElement(desc_tag, AttributesImpl(sorted_vals), can_close=True) self._writer.endElement(desc_tag) def write_enum_items(self, enum_class): if hasattr(enum_class, '_static_index'): last_static_index = enum_class._static_index + 1 else: last_static_index = len(enum_class.names) for i in range(last_static_index): enum_name = enum_class.names[i] enum_value = int(enum_class[enum_name]) attr_vals = {Attributes.Name: enum_name, Attributes.EnumValue: enum_value} self._writer.startElement(Tags.EnumItem, AttributesImpl(attr_vals), can_close=True) self._writer.endElement(Tags.EnumItem) def write_deleted_tunable(self, deleted_tunable_name): attr_vals = {Attributes.Name: deleted_tunable_name} self._writer.startElement(Tags.Deleted, AttributesImpl(attr_vals), can_close=True) self._writer.endElement(Tags.Deleted) def _open(self): class Writer(XMLGenerator): __qualname__ = 'TuningDescFileWriter._open.<locals>.Writer' SPACES_PER_INDENT = 4 def __init__(self, *args, whitespace_depth=0, **kwargs): super().__init__(*args, **kwargs) self._whitespace_depth = whitespace_depth self._indent = 0 self._already_closed = None self._last_indent = -1 def startElement(self, name, attrs, can_close=False): sub_elements = {} if self._indent == self._last_indent and self._indent <= self.SPACES_PER_INDENT*self._whitespace_depth: self.add_new_line() self._last_indent = self._indent self._write('{0}<{1}'.format(' '*self._indent, name)) for (attr_name, value) in attrs.items(): if isinstance(value, dict) or isinstance(value, list): sub_elements[attr_name] = value else: while value is not None: self._write(' %s=%s' % (attr_name, quoteattr(str(value)))) if not sub_elements and can_close: self._write(' />\n'.format(name)) self._already_closed = name else: self._write('>\n') for (name, value) in sub_elements.items(): if isinstance(value, dict): self.startElement(name, value, can_close=True) self.endElement(name) else: for sub_item in value: self.startElement(name, sub_item, can_close=True) self.endElement(name) def endElement(self, name): if self._already_closed is not None: self._already_closed = None else: self._write(' '*self._indent) super().endElement(name) self.add_new_line() self._last_indent = self._indent def add_new_line(self): self._write('\n') self._string = StringIO() self._writer = Writer(self._string, whitespace_depth=self._whitespace_depth) self._writer.startDocument() self._writer.add_new_line() def start_namespace(self, namespace, contents): attribute_dict = {} for (attribute, value) in contents.items(): while not isinstance(value, TunableBase) and (not isinstance(value, dict) and (not isinstance(value, list) and not isinstance(value, enum.Metaclass))) and value is not DELETEDMARKER: attribute_dict[attribute] = value self._writer.startElement(namespace, attribute_dict) def end_namespace(self, namespace): self._writer.endElement(namespace) def close(self): self._writer.endDocument() serialized_data = self._string.getvalue() self._string.close() path = os.path.split(self._filename)[0] if path and not os.path.exists(path): os.makedirs(path) do_compare = True tuning_file = None created = True if os.path.exists(self._filename): created = False if not do_compare: try: with open(self._filename, 'w'): pass except IOError: do_compare = True if do_compare: with open(self._filename, 'r') as old_tuning_file: old_serialized_data = old_tuning_file.read() if serialized_data == old_serialized_data: logger.debug('Skipped tuning file: {}', self._filename) return try: tuning_file = open(self._filename, 'w') except IOError: raise TunableFileReadOnlyError(self._filename) with tuning_file: tuning_file.write(serialized_data) if created: logger.warn('CREATED tuning file: {}', self._filename) elif do_compare: logger.info('Updated tuning file: {}', self._filename) else: logger.info('Wrote tuning file: {}', self._filename)