def crearRespuesta(content, nameVar, dataVar): global r re = r.json() if (len(re) > 1): source = content compiler = pybars.Compiler() template = compiler.compile(source) output = template(r.json()) else: for raiz in re: key = raiz general = r.json()[key] posicion = buscarDatos(general, nameVar, dataVar) print("CONTENT= ", content) if (content[len(content) - 1] == '+'): content = content[:len(content) - 1] for elemento in r.json()[key][posicion]: content += elemento + " {{" + elemento + "}}, " source = content compiler = pybars.Compiler() template = compiler.compile(source) output = template(r.json()[key][posicion]) return output
def validate_format_of_provided_templates( plugin_configuration: "PluginConfiguration", email_templates_data: List[Dict], ): """Make sure that the templates provided by the user have the correct structure.""" configuration = plugin_configuration.configuration configuration = {item["name"]: item["value"] for item in configuration} if not plugin_configuration.active: return compiler = pybars.Compiler() errors = {} for email_data in email_templates_data: field = email_data.get("name") template_str = email_data.get("value") if not template_str or template_str == DEFAULT_EMAIL_VALUE: continue try: compiler.compile(template_str) except pybars.PybarsError: errors[field] = ValidationError( "The provided template has an inccorect structure.", code=PluginErrorCode.INVALID.value, ) if errors: raise ValidationError(errors)
def main() -> None: file_name = "/home/raptor/projects/thrifty/thriftpy/echo.thrift" with open(file_name, 'r', encoding='utf-8') as f: lexer = ThriftLexer(antlr4.InputStream(f.read())) token_stream = antlr4.CommonTokenStream(lexer) parser = ThriftParser(token_stream) tree_walker = antlr4.ParseTreeWalker() file_loader = FileLoader(name=file_name) tree_walker.walk(file_loader, parser.document()) model = file_loader.thrifty_file # ==================================================== # generate the files # ==================================================== template_name = "/home/raptor/projects/thrifty/thriftgen/thriftgen/templates/py3/service.pyi.hbs" with open(template_name, 'r', encoding='utf-8') as template_file: template = template_file.read() hbs = pybars.Compiler().compile(source=template) print(hbs(model, helpers=helpers))
def __call__(self, project: Project): # Compiler init compiler = pybars.Compiler() # Partials compilation partials = project.settings.get("partials", dict()) for name, partial in partials.items(): partials[name] = compiler.compile(partial) # Templates compilation templates = project.settings.get("templates", dict()) for file in self.path.rglob("*.hbs"): with open(file, "r") as layout: if PurePath(file).name in templates: logging.warning( f"A layout file named {PurePath(file).name} has already been registered. The new one is skipped." ) else: templates[PurePath(file).name] = compiler.compile(layout.read()) for element in self.selector(project): # Template selection template = templates.get(getattr(element, "layout", self.default)) if template is None: logging.info( f"Element {element._filename} with no defined layout is skipped." ) else: # Render element._contents = template(element, partials=partials) element._path = element._path.with_suffix(".html")
def update(self): if self.recipients == [None]: return recipients = [] if not self.context.sent: recipients = [r for r in self.recipients if r.email] membership_fee_data = get_membership_fees( currency_formatter=format_eur) subject = self.context.subject or '' compiler = pybars.Compiler() compiled_body = compiler.compile(self.context.body) self_cc = True if len(recipients) == 1 else False for recipient in recipients: address = recipient.email data = self.recipients_data(recipient) if recipient in membership_fee_data: data.update(membership_fee_data[recipient]) if subject in self.additional.keys(): for nsubject, additional_data in self.additional[subject]( recipient, self.context.accounting_year): additional_data.update(data) body = format_markdown(compiled_body(additional_data)) msg_tag = send_mail( address, nsubject, body, self.context.user, attachments=self.context.attachments, self_cc=self_cc) SentMessageInfo.find_or_create(message=self.context, tag=msg_tag, address=address) else: body = format_markdown(compiled_body(data)) msg_tag = send_mail(address, subject, body, self.context.user, attachments=self.context.attachments, self_cc=self_cc) SentMessageInfo.find_or_create(message=self.context, tag=msg_tag, address=address) sent = len(recipients) if sent: self.context.sent = datetime.now() self.result = { 'status': 'success', 'message': '%s E-Mail(s) erfolgreich versendet' % sent } else: self.result = { 'status': 'success', 'message': 'Keine E-Mail versendet' } print_recipients = [r for r in self.recipients if not r.email] if print_recipients: self.result['redirect'] = ( '/api' + route_url('mail_print', self.request).replace( '{id}', str(self.context.id)))
def update(self): output = PdfFileWriter() if self.recipients == [None] or self.preview: recipients = self.recipients else: recipients = [r for r in self.recipients if r and not r.email] membership_fee_data = get_membership_fees( currency_formatter=format_eur) subject = self.context.subject or '' compiler = pybars.Compiler() compiled_body = compiler.compile(self.context.body) for recipient in recipients: address = recipient.address if recipient else None data = self.recipients_data(recipient) if recipient in membership_fee_data: data.update(membership_fee_data[recipient]) if subject in self.additional.keys(): for nsubject, additional_data in self.additional[subject]( recipient, self.context.accounting_year): additional_data.update(data) body = format_markdown(compiled_body(additional_data)) output = self.add_page(address, nsubject, body, output) else: body = format_markdown(compiled_body(data)) output = self.add_page(address, subject, body, output) result = BytesIO() output.write(result) result.seek(0) response = self.request.response response.set_cookie('fileDownload', value='true') response.content_type = 'application/pdf' response.content_disposition = 'attachment; filename=Brief.pdf' response.app_iter = FileIter(result) self.result = response
def get_pdf(self): year = get_selected_year() subject = self.subject + f' {year}' compiler = pybars.Compiler() template = compiler.compile(self.message) db = zope.component.getUtility(risclog.sqlalchemy.interfaces.IDatabase) query = MitgliederQuery(db, self.request.user) events = [] for event in query.select(): if event[4].year != year: continue events.append( dict(date=event[4], title=event[2], type_=event[-3], allday=event[-2])) data = { 'Lastschrift': [], 'Arbeitseinsatz': [], 'Termin': [], } for event in sorted(events, key=lambda x: x['date']): datum = (date(event['date']) if event['allday'] else date_time(event['date'])) data[event['type_']].append(dict(date=datum, title=event['title'])) message = "".join(template(data)) return render_pdf(None, subject, message, self.request.user)
def __init__(self, template_string, origin=None, name='<Handlebars Template>'): c = pybars.Compiler() self.handlebars = c.compile(template_string) self.name = name self.helpers = _djangobars_['helpers'].copy()
def generate_html(template, input): input = { "items": input } template = pybars.Compiler().compile(template) return ''.join(template(input))
def load_templates(templates_path: str) -> dict: """Load the builder templates from the configuration path.""" template_compiler = pybars.Compiler() return { template_name: _load_template(templates_path, template_compiler, template_name) for template_name in TEMPLATES }
def compile(folder: str, file_name: str, data: dict) -> str: file_path = '%s/%s/%s' % (get_config()['environmet']['templateFolder'], folder, file_name) logger.debug('Retrieving template from file [%s]' % (file_path)) compiler = pybars.Compiler() file = open(file_path, 'r', encoding="utf-8") file_string = file.read() template = compiler.compile(file_string) final_string = template(data) if (get_config()['environmet']['printXml']): logger.debug('Compiled template [%s]' % (final_string)) return final_string
def get_pdf(self): subject = self.subject compiler = pybars.Compiler() template = compiler.compile(self.message) data = get_member_form_data(self.context) message = "".join(template(data)) member = self.context if self.with_address else None return render_pdf(member, subject, message, self.request.user, subsubject=self.subsubject)
def get_pdf(self): # Base data data = self.get_json(self.context) # Details data data['details'] = [] time = self.context.day minutes = 0 for detail in self.context.details: detail = self.get_json(detail) time += datetime.timedelta(minutes=minutes) minutes = detail['duration'] detail['time'] = time.strftime('%H:%M') detail['message'] = format_markdown(detail['message']) data['details'].append(detail) time += datetime.timedelta(minutes=minutes) data['details'].append( dict(time=time.strftime('%H:%M'), message='<p>Ende</p>', responsible='')) # Commitments data['commitments'] = [] for commitment in self.context.commitments: data['commitments'].append(self.get_json(commitment)) # Attachments data['attachments'] = bool(self.context.attachments) if self.context.day >= datetime.datetime.now(): data['type'] = 'AGENDA' else: data['type'] = 'PROTOKOLL' compiler = pybars.Compiler() template = compiler.compile(TEMPLATE) html = "".join(template(data)) pdf = BytesIO() xhtml2pdf.pisa.CreatePDF(html, dest=pdf) pdf.seek(0) output = PdfFileWriter() append_pdf(PdfFileReader(pdf, strict=False), output) for attachment in self.context.attachments: if attachment.mimetype in ('application/pdf', ): pdf = attachment.data else: pdf = img2pdf.convert(attachment.data) append_pdf(PdfFileReader(BytesIO(pdf), strict=False), output) result = BytesIO() output.write(result) result.seek(0) return result
def make_dashboard(self, name, template_name="raw-html", **kwargs): """ Construct an HTML dashboard from Python objects. You can use one of the pre-defined templates (see dashboards/) or create your own! Parameters ========== template_name: str name of the template kwargs: variables you'd like to put into your template Examples ======== >>> from ggplot import mtcars >>> bandit = Bandit() >>> print bandit.make_dashboard("my dashboard", table=mtcars.to_html(classes="table")) >>> print bandit.make_dashboard("my dashboard", table=mtcars) >>> bandit.make_dashboard("my dashboard", template_name='many-tables', tables=[mtcars.head().to_html(classes='table'), mtcars.tail().to_html(classes='table')]) """ variables = {} for key, value in kwargs.items(): if isinstance(value, DataFrame): value = value.to_html(classes='table table-bordered') variables[key] = value compiler = pybars.Compiler() if os.path.exists(template_name): template_file = template_name else: this_dir = os.path.dirname(os.path.realpath(__file__)) template_file = os.path.join(this_dir, 'dashboards', template_name + '.html') if not os.path.exists(template_file): raise Exception("Could not file template file: " + template_file) template_string = open(template_file, 'rb').read() template_string = _to_unicode(template_string) template = compiler.compile(template_string) html = template(variables) if self._is_local == True: print(html) return with open(self.output_dir + name, 'wb') as f: f.write(html)
def get_html_template(game_state=None): # read in template compiler = pybars.Compiler() with open(pjoin(WEB_SERVER_RESOURCES, 'slideshow.handlebars'), 'r') as f: contents = f.read() template = compiler.compile(contents) if game_state is None: return template html = template({ 'game_state': game_state, 'template_path': WEB_SERVER_RESOURCES, }) return html
def index(): devices = [] for device in api.list_connected_devices(): logging.info("Device Found: {}".format(device.id)) value = api.get_resource_value(device.id, BLINK_PATTERN_RESOURCE_PATH) devices.append({ 'id': device.id, 'blink_pattern': value.decode('utf-8') }) # Fill out html using handlebar template comp = pybars.Compiler() with open("./views/index.hbs", 'r') as fh: template = comp.compile(six.u(fh.read())) return "".join(template({'devices': devices}))
def _renderPage(self, outputDirectory): with open(os.path.join(self.directory, "index.html")) as templateFile: template = pybars.Compiler().compile(templateFile.read()) gitRev = self.gitRevision() content = template({ "repo": self.repository, "gitRev": gitRev, "gitRevShort": gitRev[:7] if gitRev else None, "datetime": self.currentDateTime(), "name": self.name, "boards": self.boards, "description": self.description }) with open(os.path.join(outputDirectory, "index.html"), "w") as outFile: outFile.write(content)
def index(): # get list of endpoints, for each endpoint get the pattern (/3201/0/5853) value epList = connector.getEndpoints().result for index in range(len(epList)): print "Endpoint Found: ", epList[index]['name'] e = connector.getResourceValue(epList[index]['name'], "/3201/0/5853") while not e.isDone(): None epList[index]['blinkPattern'] = e.result print "Endpoint List :", epList # fill out html using handlebar template handlebarJSON = {'endpoints': epList} comp = pybars.Compiler() source = unicode(open("./views/index.hbs", 'r').read()) template = comp.compile(source) return "".join(template(handlebarJSON))
def report_segments_usage(memory_map, report_dir): model = {'segments': []} for segment_name in SEGMENTS: segment = SEGMENTS[segment_name] sections_in_segment = map( lambda s: find_section_slice(memory_map, s) or {'size': 0}, segment['sections']) total = float(segment['size']) used_size = sum(map(lambda s: s['size'], sections_in_segment)) percent_used = used_size / total free_size = (total - used_size) model['segments'].append({ 'name': segment_name, 'total': total, 'used': used_size, 'free': free_size, 'sections': sections_in_segment }) print '%s: Used: %.2f KB (%.2f %%)' % (segment_name, used_size / 1024.0, percent_used * 100) if not os.path.exists(report_dir): os.makedirs(report_dir) with open( os.path.dirname(os.path.realpath(__file__)) + "/memory_report.mustache", 'r') as f: compiler = pybars.Compiler() template = compiler.compile(unicode(f.read())) with open(report_dir + '/index.html', 'w') as f: output = template(model, helpers={ 'kb': format_kilobytes, 'percent': format_percent }) f.write(unicode(output))
def send_email(config: EmailConfig, recipient_list, context, subject="", template_str=""): sender_name = config.sender_name or "" sender_address = config.sender_address from_email = str(Address(sender_name, addr_spec=sender_address)) email_backend = EmailBackend( host=config.host, port=config.port, username=config.username, password=config.password, use_ssl=config.use_ssl, use_tls=config.use_tls, timeout=DEFAULT_EMAIL_TIMEOUT, ) compiler = pybars.Compiler() template = compiler.compile(template_str) subject_template = compiler.compile(subject) helpers = { "format_address": format_address, "price": price, "format_datetime": format_datetime, "get_product_image_thumbnail": get_product_image_thumbnail, "compare": compare, } message = template(context, helpers=helpers) subject_message = subject_template(context, helpers) send_mail( subject_message, html2text.html2text(message), from_email, recipient_list, html_message=message, connection=email_backend, )
def __init__(self): path = os.path.abspath(os.path.dirname(__file__)) root = os.path.dirname(os.path.dirname(path)) name = os.path.join(os.path.join(root, 'html'), 'handlebars') self._load_templates(name) self._renderer = pybars.Compiler() self._compiled = {} self._partials = {} for k, v in self.templates.iteritems(): path, source = v source = unicode(source) try: compiled = self._renderer.compile(source) self._compiled[k] = compiled self._partials[k] = compiled except Exception, e: print "[%s] template compiler error (%s): %s" % (self, path, e) raise
def update(self): output = PdfFileWriter() compiler = pybars.Compiler() compiled_page = compiler.compile(self.hbs_page) cleanr = re.compile('<.*?>') for kind in BookingKind.query(): data = BankingAccountListDetailView(kind, self.request)()['data'] table_content = [] for item in data['data']: table_content.append([(re.sub(cleanr, '', d['value']) if d['value'] else ' ') for d in item[1:5]]) table_content = sorted(table_content, key=lambda x: (x[1], x[0], x[2])) # sort by name sum_ = sum( float(i[0].replace(' €', '').replace('.', '').replace( ',', '.')) for i in table_content) sum_ = int(sum_ * 10000) body = format_markdown( compiled_page( dict(title='{} {}'.format(kind.title, get_selected_year()), content=table_content, summe=format_eur(sum_), header_style=("border: 1px solid black; " "padding: 2px 2px -1px 2px;")))) output = self.add_page(body, output) result = BytesIO() output.write(result) result.seek(0) response = self.request.response response.set_cookie('fileDownload', value='true') response.content_type = 'application/pdf' response.content_disposition = 'attachment; filename=Report 2018.pdf' response.app_iter = FileIter(result) self.result = response
def parse_file_name( file_name: str, project_parameters: Dict[str, Union[str, List[str]]]) -> ParsedFile: """ parseFileName - Parse the fie name :param file_name: string with filename :param project_parameters: the project parameters if they are used in the file name. :return: result dict """ result = ParsedFile(file_name) name: str = file_name if name.endswith(".KEEP"): result.keep_existing = True name = name[0:-len(".KEEP")] if name.endswith(".hbs"): result.hbs_template = True name = name[0:-len(".hbs")] result.name = pybars.Compiler().compile(name)(project_parameters) return result
def index(): # get list of endpoints, for each endpoint get resources epList = connector.getEndpoints().result for index in range(len(epList)): print "Endpoint Found: ", epList[index]['name'] print "epList[index]", epList[index] print "end", index e_h = connector.getResourceValue(epList[index]['name'], "/3304/0/5700") e_t = connector.getResourceValue(epList[index]['name'], "/3303/0/5700") e_p = connector.getResourceValue(epList[index]['name'], "/3323/0/5700") while not e_p.isDone() or not e_t.isDone or not e_h.isDone: None epList[index]['humidity_value'] = e_h.result epList[index]['tempereture_value'] = e_t.result epList[index]['pressure_value'] = e_p.result print "Endpoint List :", epList # fill out html using handlebar template handlebarJSON = {'endpoints': epList} comp = pybars.Compiler() tmp_src = open("./views/index.hbs", 'r').read() source = unicode(tmp_src, "utf-8") template = comp.compile(source) return "".join(template(handlebarJSON))
import os import re import importlib import pybars from . import env # The pybars compiler used for compiling all partials and templates. _compiler = pybars.Compiler() # All of the helpers passed to templates when they are compiled. # Format: {name: callable} _helpers = {} # All of the compiled partials and templates. # Format: {name: callable} _partials = {} _templates = {} # Static HTML content served up via Handlebars _static = {} # A list of when each partial and template was last compiled. # Format: {path: {'mtime': float, 'reloader': callable}} _reload_tracker = {} static_root = os.path.join(os.path.dirname(__file__), 'html') template_root = os.path.join(os.path.dirname(__file__), 'templates') partials_root = os.path.join(template_root, 'partials') helpers_root = os.path.join(template_root, 'helpers')
def __init__(self, **kwargs): """Initialisation for Django DDP server view.""" self.runtime_config = {} # super(...).__init__ assigns kwargs to instance. super(MeteorView, self).__init__(**kwargs) # read and process /etc/mime.types mimetypes.init() self.url_map = {} # process `star_json` self.star_json = read_json(self.json_path) star_format = self.star_json['format'] if star_format != 'site-archive-pre1': raise ValueError('Unknown Meteor star format: %r' % star_format, ) programs = { program['name']: program for program in self.star_json['programs'] } # process `bundle/programs/server/program.json` from build dir server_json_path = os.path.join( os.path.dirname(self.json_path), os.path.dirname(programs['server']['path']), 'program.json', ) server_json = read_json(server_json_path) server_format = server_json['format'] if server_format != 'javascript-image-pre1': raise ValueError( 'Unknown Meteor server format: %r' % server_format, ) self.server_load_map = {} for item in server_json['load']: item['path_full'] = os.path.join( os.path.dirname(server_json_path), item['path'], ) self.server_load_map[item['path']] = item self.url_map[item['path']] = (item['path_full'], 'text/javascript') try: item['source_map_full'] = os.path.join( os.path.dirname(server_json_path), item['sourceMap'], ) self.url_map[item['sourceMap']] = (item['source_map_full'], 'text/plain') except KeyError: pass self.template_path = os.path.join( os.path.dirname(server_json_path), self.server_load_map['packages/boilerplate-generator.js']['assets'] ['boilerplate_web.browser.html'], ) # process `bundle/programs/web.browser/program.json` from build dir web_browser_json_path = os.path.join( os.path.dirname(self.json_path), programs['web.browser']['path'], ) web_browser_json = read_json(web_browser_json_path) web_browser_format = web_browser_json['format'] if web_browser_format != 'web-program-pre1': raise ValueError( 'Unknown Meteor web.browser format: %r' % (web_browser_format, ), ) self.client_map = {} self.internal_map = {} for item in web_browser_json['manifest']: item['path_full'] = os.path.join( os.path.dirname(web_browser_json_path), item['path'], ) if item['where'] == 'client': if '?' in item['url']: item['url'] = item['url'].split('?', 1)[0] if item['url'].startswith('/'): item['url'] = item['url'][1:] self.client_map[item['url']] = item self.url_map[item['url']] = ( item['path_full'], mimetypes.guess_type(item['path_full'], )[0] or 'application/octet-stream', ) elif item['where'] == 'internal': self.internal_map[item['type']] = item config = { 'css': [{ 'url': item['path'] } for item in web_browser_json['manifest'] if item['type'] == 'css' and item['where'] == 'client'], 'js': [{ 'url': item['path'] } for item in web_browser_json['manifest'] if item['type'] == 'js' and item['where'] == 'client'], 'meteorRuntimeConfig': '"%s"' % (dumps(self.runtime_config)), 'rootUrlPathPrefix': self.root_url_path_prefix, 'bundledJsCssPrefix': self.bundled_js_css_prefix, 'inlineScriptsAllowed': False, 'inline': None, 'head': read( self.internal_map.get('head', {}).get('path_full', None), default=u'', ), 'body': read( self.internal_map.get('body', {}).get('path_full', None), default=u'', ), } tmpl_raw = read(self.template_path, encoding='utf8') compiler = pybars.Compiler() tmpl = compiler.compile(tmpl_raw) self.html = '<!DOCTYPE html>\n%s' % tmpl(config)
def generate_html(template, input): input = { "items": input } template = pybars.Compiler().compile(template) return template(input).encode("utf-8")
(name, link) = nameLink (author, package) = name.split("/") return '<span> %s / <a href="%s">%s</a></span>' % (author, link, package) def pkglist(this, options, items): result = [u'<table>'] for (name, link, summary) in items: result.append(u'<tr>') result.append(u'<td class="first"><a href="%s">%s</a></td>' % (link, name)) result.append(u'<td>%s</td>' % escape(summary)) result.append(u'</tr>') result.append(u'</table>') return result indexTemplate = lambda d: pybars.Compiler().compile(index)( d, helpers={ "pkglist": pkglist }).encode("utf-8") pkgTemplate = lambda d: pybars.Compiler().compile(package)( d, helpers={ "moduleslist": moduleslist, "gitRM": gitRM }).encode("utf-8") moduleTemplate = lambda d: pybars.Compiler().compile(module)( d, helpers={ "package": package_helper }).encode("utf-8")
def setUp(self): self.app = object() self.loader = Mock() self.loader.compiler = pybars.Compiler()
def __init__(self, username=None, password=None, hostname="localhost", port=None, filename=None, dbname=None, dbtype=None, schemas=None, profile="default", exclude_system_tables=True, limit=1000, keys_per_column=None, driver=None, cache=False): if port is None: if dbtype == "postgres": port = 5432 elif dbtype == "redshift": port = 5439 elif dbtype == "mysql": port = 3306 elif dbtype == "sqlite": port = None elif dbtype == "mssql": port = 1433 elif profile is not None: pass else: raise Exception( "Database type not specified! Must select one of: postgres, sqlite, mysql, mssql, or redshift" ) self._use_cache = cache if dbtype not in ("sqlite", "mssql") and username is None: self.load_credentials(profile) if cache: self._metadata_cache = self.load_metadata(profile) elif dbtype == "sqlite" and filename is None: self.load_credentials(profile) if cache: self._metadata_cache = self.load_metadata(profile) else: self.username = username self.password = password self.hostname = hostname self.port = port self.filename = filename self.dbname = dbname self.dbtype = dbtype self.schemas = schemas self.limit = limit self.keys_per_column = keys_per_column self.driver = driver if self.dbtype is None: raise Exception( "Database type not specified! Must select one of: postgres, sqlite, mysql, mssql, or redshift" ) self._query_templates = query_templates.get(self.dbtype).queries if self.dbtype == "postgres" or self.dbtype == "redshift": if not HAS_PG: raise Exception( "Couldn't find psycopg2 library. Please ensure it is installed" ) self.con = pg.connect(user=self.username, password=self.password, host=self.hostname, port=self.port, dbname=self.dbname) self.con.autocommit = True self.cur = self.con.cursor() elif self.dbtype == "sqlite": if not HAS_SQLITE: raise Exception( "Couldn't find sqlite library. Please ensure it is installed" ) self.con = sqlite.connect(self.filename) self.cur = self.con.cursor() self._create_sqlite_metatable() elif self.dbtype == "mysql": if not HAS_MYSQL: raise Exception( "Couldn't find MySQLdb or pymysql library. Please ensure it is installed" ) creds = {} for arg in ["username", "password", "hostname", "port", "dbname"]: if getattr(self, arg): value = getattr(self, arg) if arg == "username": arg = "user" elif arg == "password": arg = "passwd" elif arg == "dbname": arg = "db" elif arg == "hostname": arg = "host" creds[arg] = value self.con = mysql_connect(**creds) self.con.autocommit(True) self.cur = self.con.cursor() elif self.dbtype == "mssql": if not HAS_ODBC and not HAS_PYMSSQL: raise Exception( "Couldn't find pyodbc or pymssql libraries. Please ensure one of them is installed" ) if HAS_ODBC: base_con = "Driver={driver};Server={server};Database={database};".format( driver=self.driver or "SQL Server", server=self.hostname or "localhost", database=self.dbname or '') conn_str = ( (self.username and self.password) and "{}{}".format( base_con, "User Id={username};Password={password};".format( username=self.username, password=self.password)) or "{}{}".format(base_con, "Trusted_Connection=Yes;")) try: self.con = pyo.connect(conn_str) self.cur = self.con.cursor() except: self.con = pyo.connect(driver=self.driver or "SQL Server", server=self.hostname or "localhost", port=self.port, database=self.dbname or '', uid=self.username, pwd=self.password) self.cur = self.con.cursor() elif HAS_PYMSSQL: if '\\' in self.hostname: hostname = self.hostname elif hasattr(self, 'port'): hostname = '{0}:{1}'.format(self.hostname, self.port) else: hostname = self.hostname self.con = pymssql.connect(host=hostname, user=self.username, password=self.password, database=self.dbname) self.cur = self.con.cursor() self._tables = TableSet([]) self._exclude_system_tables = exclude_system_tables self.handlebars = pybars.Compiler()