How to use _is_magic method in autotest

Best Python code snippet using autotest_python Github


Full Screen

...83 if len(line) == 0:84 return True85 else:86 return False87 def _is_magic(self, filename, magic):88 """Figure out whether magic number of the file fits the argument."""89 data = open(filename, "rb")90 buff = L = len(magic)92 # we need at least L + 1 values93 if len(buff) <= L:94 return False95 # then, each magic number should fit the first bytes96 ret = [buff[i] == magic[i] for i in range(0, L)]97 # _log.debug("magic number: {}".format([hex(buff[i]) for i in range(0, L)]))98 # _log.debug("expected number: {}".format(magic))99 if False in ret:100 return False101 else:102 return True103 def is_abi(self, filename):104 try:105 data = open(filename, "rb")106 buff = if buff[0:4].decode() == "ABIF":108 return True109 except: #pragma: no cover110 return False111 def is_bam(self, filename):112 try:113 d = pysam.AlignmentFile(filename)114 return d.is_bam115 except:116 return False117 def is_bai(self, filename):118 try:119 data = open(filename, "rb").read()[0:4]120 if data.startswith(b"BAI"):121 return True122 else:123 return False124 except: #pragma: no cover125 return False126 def is_bcf(self, filename):127 try:128 d = pysam.VariantFile(filename)129 return d.is_bcf130 except: #pragma: no cover131 return False132 def is_binary_bed(self, filename):133 # This could be a BED binary file from plink134 # try:136 return self._is_magic(filename, [0x6C, 0x1B, 0x1])137 except:138 return False139 def is_bed(self, filename):140 try:141 data = open(filename, "r")142 line = data.readline()143 if len(line.split()) < 4:144 return False145 else:146 # reads 10 lines if possible. They should all be tab delimited147 # with same number of columns:148 L = len(line)149 for i in range(10):150 line = data.readline().strip()151 if len(line) != 4:152 return False153 # let us assume it is a TSV-like file154 return True155 except:156 return False157 def is_bedgraph(self, filename):158 return self.is_bed(filename)159 def is_bigwig(self, filename):160 try:161 return self._is_magic(filename, [0x26, 0xFC, 0x8F])162 except:163 return False164 def is_bigbed(self, filename):165 try:166 return self._is_magic(filename, [0xEB, 0xF2, 0x89])167 except:168 return False169 def is_bplink(self, filename):170 raise NotImplementedError171 def is_bz2(self, filename):172 try:173 return self._is_magic(filename, [0x42, 0x5A, 0x68])174 except:175 return False176 def is_csv(self, filename):177 try:178 df = pd.read_csv(filename, sep=",")179 if len(df.columns) > 1:180 return True181 except:182 pass183 def is_cram(self, filename):184 try:185 d = pysam.AlignmentFile(filename)186 return d.is_cram187 except:188 return False189 def is_clustal(self, filename):190 with open(filename, "r") as fin:191 try:192 line = fin.readline().strip()193 if self._is_blank_line(line):194 pass195 except:196 return False197 def is_dsrc(self, filename):198 try:199 # FIXME not sure whether we need more characters ?200 return self._is_magic(filename, [0xAA, 0x2])201 except:202 return False203 def is_embl(self, filename):204 # FIXME205 # here we naively read 20 lines and extract the first 2 letters checking206 # whether there are within the list of authorised values207 # non exhaustive list208 valid_ids = [209 "ID",210 "XX",211 "AC",212 "DE",213 "KW",214 "OS",215 "OC",216 "RN",217 "RA",218 "RT",219 "FT",220 "FH",221 ]222 try:223 with open(filename, "r") as fin:224 data = fin.readlines(200000) # 200000 characters should be enough225 ids = [x.split()[0] for x in data if x[0:2] in valid_ids]226 # can be only of length 2227 ids = [x for x in ids if len(x) == 2]228 if len(ids) > 0:229 return True230 else:231 return False232 except:233 return False234 def is_ena(self, filename):235 try:236 data = open(filename, "r")237 L1 = data.readline()238 if L1.startswith("ID"):239 return True240 else:241 return False242 except:243 return False244 def is_fasta(self, filename):245 # FIXME this is valid for FASTA246 try:247 data = open(filename, "r")248 line1 = data.readline()249 line2 = data.readline()250 if line1.startswith(">") and line2[0] in "ABCDEFGHIKLMNPQRSTUVWYZX*-":251 return True252 else:253 return False254 except:255 return False256 def is_fastq(self, filename):257 try:258 data = open(filename, "r")259 line1 = data.readline()260 line2 = data.readline()261 line3 = data.readline()262 line4 = data.readline()263 if line1.startswith("@") and line3.startswith("+"):264 return True265 else:266 return False267 except:268 return False269 def is_genbank(self, filename):270 with open(filename, "r") as fin:271 try:272 line = fin.readline().strip()273 data = line.split()274 if data[0] == "LOCUS":275 return True276 else:277 return False278 except:279 return False280 def is_gfa(self, filename):281 # GFA1282 # Type descr283 # # Comment284 # H Header285 # S Segment286 # L Link287 # C Containment288 # P Path289 # optional fields are also possible: A, i, f, Z, J, H, B290 # GFA2291 # There is an integer length field in S-lines.292 # The L- and C-lines have been replaced by a consolidated E-line.293 # The P-line has been replaced with U- and O-lines that encode subgraphs and294 # paths, respectively, and can take edge id’s, obviating the need for orientation295 # signs and alignments between segments.296 # There is a new F-line for describing multi-alignments and a new G-line for297 # describing scaffolds.298 # Alignments can be trace length sequences as well as CIGAR strings.299 # Positions have been extended to include a postfix $ symbol for positions300 # representing the end of a read.301 # Segments, edges, and paths all have an orientation that is specified with a302 # postfix + or - symbol in contexts where the orientation is needed.303 try:304 # gfa1305 is_gfa1 = self._is_gfa1(filename)306 is_gfaXX = self._is_gfaXX(filename)307 if is_gfa1 or is_gfaXX:308 return True309 else:310 return False311 except Exception as err:312 print(err)313 return False314 def _is_gfa1(self, filename):315 with open(filename, "r") as fin:316 data = fin.readlines(200000) # 200000 characters should be enough317 ids = [x.split()[0] for x in data]318 if "H" in ids and "S" in ids and "L" in ids:319 return True320 else:321 return False322 def _is_gfaXX(self, filename):323 # FIXME: need to be sure the test files are correct.324 # there are two right now one GFA1 the other is unclear since starting325 # values can be S but also a326 with open(filename, "r") as fin:327 data = fin.readlines(200000) # 200000 characters should be enough328 ids = [x.split()[0] for x in data]329 if "a" in ids and "S":330 return True331 else:332 return False333 def is_gff2(self, filename):334 try:335 with open(filename, "r") as fin:336 data = fin.readline()337 if "gff-version 2" in data.strip():338 return True339 else:340 return False341 except:342 return False343 def is_gff3(self, filename):344 try:345 with open(filename, "r") as fin:346 data = fin.readline()347 if "gff-version 3" in data.strip():348 return True349 else:350 return False351 except:352 return False353 def is_gz(self, filename):354 try:355 return self._is_magic(filename, [0x1F, 0x8B])356 except:357 return False358 def is_json(self, filename):359 try:360 with open(filename) as fin:361 data = json.loads(data)363 return True364 except:365 return False366 def is_maf(self, filename):367 with open(filename, "r") as fin:368 try:369 # read at most 50 lines and figure out whether370 # some lines starts with a or s371 # we get rid of the comments.372 # Read 5000 characters at most.373 data = fin.readlines(5000)374 comments = [line for line in data if line.startswith("#")]375 data = [line.strip() for line in data if line.startswith("#") is False]376 # get rid of blank lines377 data = [line for line in data if len(line.strip()) != 0]378 starts = [line[0:2] for line in data]379 if len(starts) == 0:380 return False381 # line must start with one of i, e, q, a, s letter382 for x in starts:383 assert x in ["a ", "s ", "e ", "q ", "i "]384 return True385 except Exception as err:386 _log.debug(err)387 return False388 def is_newick(self, filename):389 try:390 with open(filename, "r") as fin:391 data = fin.readlines()392 if data[0].strip()[0] == "(" and data[-1].strip()[-1] == ";":393 return True394 else:395 return False396 except:397 return False398 def is_nexus(self, filename):399 try:400 with open(filename, "r") as fin:401 line = fin.readline()402 if line.startswith("#NEXUS"):403 return True404 else:405 return False406 except:407 return False408 def is_ods(self, filename):409 try:410 return self._is_magic(filename, [0x50, 0x4B, 0x03, 0x04])411 except:412 return False413 def is_paf(self, filename):414 try:415 df = pd.read_csv(filename, sep=r"\s+", header=None)416 if len(df.columns) >= 12:417 if set(df.loc[:, 4]) == set(["+", "-"]):418 return True419 return False420 except Exception as err:421 return False422 def is_phylip(self, filename):423 with open(filename, "r") as fin:424 # First, we figure out the dimensions of the alignemnt.425 # we should find 2 integers426 # blank lines are forbidden in principle between header an427 # alignment428 try:429 header = fin.readline().strip()430 first = fin.readline().strip()431 m, n = header.split()432 m = int(m)433 n = int(n)434 name, seq = first.split(" ", 1)435 seq = seq.replace(" ", "")436 # we identify each alignement and check that the length are437 # identical and equal to n438 for this in range(1, m - 1): # -1 since we already read 1 line439 nextline = fin.readline().strip()440 name, seq2 = nextline.split(" ", 1)441 seq2 = seq2.replace(" ", "")442 assert len(seq) == len(seq2), "not same length"443 return True444 except Exception as err:445 # print(err)446 return False447 def is_phyloxml(self, filename):448 try:449 tree = ET.parse(filename)450 tree.getroot()451 root = tree.getroot()452 if "phyloxml" in root.tag:453 return True454 else:455 return False456 except:457 return False458 def is_plink(self, filename):459 raise NotImplementedError460 def is_qual(self, filename):461 # if line1.startswith(">") and line2[0] in "ABCDEFGHIKLMNPQRSTUVWYZX*-":462 try:463 data = open(filename, "r")464 line1 = data.readline()465 line2 = data.readline()466 # we check the first line identifier.467 # then, we scan the entire line searching of encoding qualities468 # hoping that values between 33 and 126 will be enough to469 # differentiate them from the standard nucleotides and protein470 # characters.471 scores = [x for x in line2 if x not in "ABCDEFGHIKLMNPQRSTUVWYZX*-"]472 # if we find a character (e.g !, #ietc) it means is a quality file473 # however, if we do not find such a value, it does not mean it is474 # not a quality.475 # For instance, there is no way to say that ::476 # >ID477 # AACCTTGG478 # is a qual or fasta file479 if line1.startswith(">") and len(scores) > 1:480 return True481 else:482 return False483 except:484 return False485 def is_sam(self, filename):486 try:487 d = pysam.AlignmentFile(filename)488 return d.is_sam489 except:490 return False491 def is_scf(self, filename):492 try:493 data = open(filename, "rb")494 buff = if buff[0:4].decode() == ".scf":496 return True497 except:498 return False499 def is_stockholm(self, filename):500 with open(filename, "r") as fin:501 try:502 header = fin.readline().strip()503 assert "STOCKHOLM" in header504 return True505 except:506 return False507 def is_rar(self, filename):508 try:509 c1 = self._is_magic(filename, [0x52, 0x61, 0x72, 0x21, 0x1A, 0x7, 0x0])510 c2 = self._is_magic(filename, [0x52, 0x61, 0x72, 0x21, 0x1A, 0x7, 0x0])511 if c1 or c2:512 return True513 else:514 return False515 except:516 return False517 def is_twobit(self, filename):518 try:519 data = open(filename, "rb")520 buff = if 0x43 in buff and 0x27 in buff:522 return True523 else:524 return False525 except:526 return False527 def is_tsv(self, filename):528 try:529 df = pd.read_csv(filename, sep=r"\s+")530 if len(df.columns) > 1:531 return True532 except:533 pass534 def is_vcf(self, filename):535 try:536 d = pysam.VariantFile(filename)537 return d.is_vcf538 except:539 return False540 def is_wiggle(self, filename):541 return self.is_wig(filename)542 def is_wig(self, filename):543 try:544 with open(filename, "r") as fin:545 line = fin.readline()546 if "track" in line and "type=wiggle" in line:547 return True548 else:549 return False550 except:551 return False552 def is_xls(self, filename):553 try:554 return self._is_magic(filename, [0xD0, 0xCF, 0x11])555 except:556 return False557 def is_xlsx(self, filename):558 try:559 # FIXME only second case should be used most probably560 case1 = self._is_magic(filename, [0xD0, 0xCF, 0x11])561 case2 = self._is_magic(filename, [0x50, 0x4B, 0x3, 0x4])562 if case1 or case2:563 return True564 else:565 return False566 except:567 return False568 def is_xmfa(self, filename):569 try:570 with open(filename, "r") as fin:571 line = fin.readline()572 if "FormatVersion" in line and "Mauve" in line:573 return True574 else:575 return False576 except:577 return False578 def is_yaml(self, filename):579 try:580 data = yaml.load(open(filename, "r"), Loader=yaml.FullLoader)581 if data.keys():582 return True583 except:584 return False585 def is_zip(self, filename):586 try:587 c1 = self._is_magic(filename, [0x50, 0x4B, 0x3, 0x4])588 c2 = self._is_magic(filename, [0x50, 0x4B, 0x3, 0x4])589 c3 = self._is_magic(filename, [0x50, 0x4B, 0x3, 0x4])590 if c1 or c2 or c3:591 return True592 else:593 return False594 except:595 return False596 def is_7zip(self, filename):597 try:598 return self._is_magic(filename, [0x37, 0x7A, 0xBC, 0xAF, 0x27, 0x1C])599 except:600 return False601 def is_xz(self, filename):602 try:603 return self._is_magic(filename, [0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])604 except:...

Full Screen

Full Screen Github


Full Screen

1# -*- coding: utf-8 -*-2#3# This file is part of Invenio.4# Copyright (C) 2015 CERN.5#6# Invenio is free software; you can redistribute it7# and/or modify it under the terms of the GNU General Public License as8# published by the Free Software Foundation; either version 2 of the9# License, or (at your option) any later version.10#11# Invenio is distributed in the hope that it will be12# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14# General Public License for more details.15#16# You should have received a copy of the GNU General Public License17# along with Invenio; if not, write to the18# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,19# MA 02111-1307, USA.20#21# In applying this license, CERN does not22# waive the privileges and immunities granted to it by virtue of its status23# as an Intergovernmental Organization or submit itself to any jurisdiction.24"""Hijacks `mock` to fake as many non-available modules as possible."""25import sys26import types27try:28 import unittest.mock as mock29except ImportError:30 import mock31# skip `_is_magic` check.32orig_is_magic = mock._is_magic33def always_false(*args, **kwargs):34 return False35# avoid spec configuration for mocked classes with super classes.36# honestly this does not happen very often and is kind of a tricky case.37orig_mock_add_spec = mock.NonCallableMock._mock_add_spec38def mock_add_spec_fake(self, spec, spec_set):39 orig_mock_add_spec(self, None, None)40# special MagicMock with empty docs41class MyMagicMock(mock.MagicMock):42 """"""43# set up a fake class-metaclass hierarchy44class SuperMockMetaMeta(MyMagicMock):45 __metaclass__ = MyMagicMock()46class SuperMockMeta(MyMagicMock):47 __metaclass__ = SuperMockMetaMeta48class SuperMock(MyMagicMock):49 __metaclass__ = SuperMockMeta50class MockedModule(types.ModuleType):51 def __init__(self, name):52 super(types.ModuleType, self).__init__(name)53 self.__name__ = super.__name__54 self.__file__ = self.__name__.replace('.', '/') + '.py'55 sys.modules[self.__name__] = self56 def __getattr__(self, key):57 obj = SuperMock58 setattr(self, key, obj)59 return obj60# overwrite imports61orig_import = __import__62def import_mock(name, *args, **kwargs):63 try:64 return orig_import(name, *args, **kwargs)65 except ImportError:66 return MockedModule(name)67import_patch = mock.patch('__builtin__.__import__', side_effect=import_mock)68# public methods69def activate():70 mock._is_magic = always_false71 mock.NonCallableMock._mock_add_spec = mock_add_spec_fake72 import_patch.start()73def deactivate():74 import_patch.stop()75 mock.NonCallableMock._mock_add_spec = orig_mock_add_spec...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?