Python path.path() Examples
The following are 30
code examples of path.path().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
path
, or try the search function
.
Example #1
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 6 votes |
def test_course_handouts_rewrites(self): """ Test that the xblock_handler rewrites static handout links """ handouts = self.store.create_item( self.user.id, self.course.id, 'course_info', 'handouts', fields={ "data": "<a href='/static/handouts/sample_handout.txt'>Sample</a>", } ) # get module info (json) resp = self.client.get(get_url('xblock_handler', handouts.location)) # make sure we got a successful response self.assertEqual(resp.status_code, 200) # check that /static/ has been converted to the full path # note, we know the link it should be because that's what in the 'toy' course in the test data asset_key = self.course.id.make_asset_key('asset', 'handouts_sample_handout.txt') self.assertContains(resp, unicode(asset_key))
Example #2
Source File: wiki.py From zerodb with GNU Affero General Public License v3.0 | 6 votes |
def read_docs(dirname="wiki_sample"): for fname in path.path(dirname).walkfiles("*.bz2"): f = bz2.BZ2File(fname) text = None for line in f: if six.PY3: line = line.decode() if text is None: r = head_re.match(line) if r: docid, url, title = r.groups() text = [] else: if line == tail: yield {"id": docid, "url": url, "title": title, "text": "".join(text)} text = None else: text.append(line) f.close()
Example #3
Source File: test_subset.py From AutoWIG with Apache License 2.0 | 6 votes |
def setUpClass(cls): if 'libclang' in autowig.parser: autowig.parser.plugin = 'libclang' cls.srcdir = Path('fp17') if not cls.srcdir.exists(): Repo.clone_from('https://github.com/StatisKit/FP17.git', cls.srcdir.relpath('.'), recursive=True) cls.srcdir = cls.srcdir/'share'/'git'/'ClangLite' cls.incdir = Path(sys.prefix).abspath() if any(platform.win32_ver()): cls.incdir = cls.incdir/'Library' subprocess.check_output(['scons', 'cpp', '--prefix=' + str(cls.incdir)], cwd=cls.srcdir) if any(platform.win32_ver()): cls.scons = subprocess.check_output(['where', 'scons.bat']).strip() else: cls.scons = subprocess.check_output(['which', 'scons']).strip() cls.incdir = cls.incdir/'include'/'clanglite'
Example #4
Source File: pytest_virtualenv.py From pytest-plugins with MIT License | 6 votes |
def virtualenv(): """ Function-scoped virtualenv in a temporary workspace. Methods ------- run() : run a command using this virtualenv's shell environment run_with_coverage() : run a command in this virtualenv, collecting coverage install_package() : install a package in this virtualenv installed_packages() : return a dict of installed packages Attributes ---------- virtualenv (`path.path`) : Path to this virtualenv's base directory python (`path.path`) : Path to this virtualenv's Python executable easy_install (`path.path`) : Path to this virtualenv's easy_install executable .. also inherits all attributes from the `workspace` fixture """ venv = VirtualEnv() yield venv venv.teardown()
Example #5
Source File: workspace.py From pytest-plugins with MIT License | 6 votes |
def __init__(self, workspace=None, delete=None): self.delete = delete log.debug("") log.debug("=======================================================") if workspace is None: self.workspace = Path(tempfile.mkdtemp(dir=self.get_base_tempdir())) log.debug("pytest_shutil created workspace %s" % self.workspace) else: self.workspace = Path(workspace) log.debug("pytest_shutil using workspace %s" % self.workspace) if 'DEBUG' in os.environ: self.debug = True if self.delete is not False: log.debug("This workspace will delete itself on teardown") log.debug("=======================================================") log.debug("")
Example #6
Source File: plot.py From TOPFARM with GNU Affero General Public License v3.0 | 6 votes |
def execute(self): plt.ion() if self.inc==0: try: pa(self.result_file+'.results').remove() except: pass self.iterations = [self.inc] self.targvalue = [[getattr(self, i) for i in self.targname]] self.pre_plot() else: self.iterations.append(self.inc) self.targvalue.append([getattr(self, i) for i in self.targname]) #print self.iterations,self.targvalue #if self.inc % (2*self.wt_positions.shape[0]) == 0: #self.refresh() #plt.show() self.save_plot('fig/'+self.png_name+'layout%d.png'%(self.inc)) self.inc += 1
Example #7
Source File: plot.py From TOPFARM with GNU Affero General Public License v3.0 | 6 votes |
def __init__(self, add_inputs, title='', **kwargs): super(OffshorePlot, self).__init__(**kwargs) self.fig = plt.figure(num=None, facecolor='w', edgecolor='k') #figsize=(13, 8), dpi=1000 self.shape_plot = self.fig.add_subplot(121) self.objf_plot = self.fig.add_subplot(122) self.targname = add_inputs self.title = title # Adding automatically the inputs for i in add_inputs: self.add(i, Float(0.0, iotype='in')) #sns.set(style="darkgrid") #self.pal = sns.dark_palette("skyblue", as_cmap=True) plt.rc('lines', linewidth=1) plt.ion() self.force_execute = True if not pa('fig').exists(): pa('fig').mkdir()
Example #8
Source File: normpath.py From OpenRAM with BSD 3-Clause "New" or "Revised" License | 6 votes |
def pathitems(self): """return list of pathitems""" if not self.normsubpathitems: return [] # remove trailing normline_pt of closed subpaths if self.closed and isinstance(self.normsubpathitems[-1], normline_pt): normsubpathitems = self.normsubpathitems[:-1] else: normsubpathitems = self.normsubpathitems result = [path.moveto_pt(*self.atbegin_pt())] for normsubpathitem in normsubpathitems: result.append(normsubpathitem.pathitem()) if self.closed: result.append(path.closepath()) return result
Example #9
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 6 votes |
def test_html_export_roundtrip(self): """ Test that a course which has HTML that has style formatting is preserved in export/import """ content_store = contentstore() import_from_xml(self.store, self.user.id, TEST_DATA_DIR, ['toy']) course_id = SlashSeparatedCourseKey('edX', 'toy', '2012_Fall') # Export the course root_dir = path(mkdtemp_clean()) export_to_xml(self.store, content_store, course_id, root_dir, 'test_roundtrip') # Reimport and get the video back import_from_xml(self.store, self.user.id, root_dir) # get the sample HTML with styling information html_module = self.store.get_item(course_id.make_usage_key('html', 'with_styling')) self.assertIn('<p style="font:italic bold 72px/30px Georgia, serif; color: red; ">', html_module.data) # get the sample HTML with just a simple <img> tag information html_module = self.store.get_item(course_id.make_usage_key('html', 'just_img')) self.assertIn('<img src="/static/foo_bar.jpg" />', html_module.data)
Example #10
Source File: normpath.py From OpenRAM with BSD 3-Clause "New" or "Revised" License | 6 votes |
def intersect(self, other): """intersect self with other path Returns a tuple of lists consisting of the parameter values of the intersection points of the corresponding normpath. """ other = other.normpath() # here we build up the result intersections = ([], []) # Intersect all normsubpaths of self with the normsubpaths of # other. for ia, normsubpath_a in enumerate(self.normsubpaths): for ib, normsubpath_b in enumerate(other.normsubpaths): for intersection in zip(*normsubpath_a.intersect(normsubpath_b)): intersections[0].append(normpathparam(self, ia, intersection[0])) intersections[1].append(normpathparam(other, ib, intersection[1])) return intersections
Example #11
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 6 votes |
def test_export_course_with_metadata_only_word_cloud(self): """ Similar to `test_export_course_with_metadata_only_video`. """ content_store = contentstore() import_from_xml(self.store, self.user.id, TEST_DATA_DIR, ['word_cloud']) course_id = SlashSeparatedCourseKey('HarvardX', 'ER22x', '2013_Spring') verticals = self.store.get_items(course_id, qualifiers={'category': 'vertical'}) self.assertGreater(len(verticals), 0) parent = verticals[0] ItemFactory.create(parent_location=parent.location, category="word_cloud", display_name="untitled") root_dir = path(mkdtemp_clean()) print 'Exporting to tempdir = {0}'.format(root_dir) # export out to a tempdir export_to_xml(self.store, content_store, course_id, root_dir, 'test_export') shutil.rmtree(root_dir)
Example #12
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 6 votes |
def test_export_course_with_metadata_only_video(self): content_store = contentstore() import_from_xml(self.store, self.user.id, TEST_DATA_DIR, ['toy']) course_id = SlashSeparatedCourseKey('edX', 'toy', '2012_Fall') # create a new video module and add it as a child to a vertical # this re-creates a bug whereby since the video template doesn't have # anything in 'data' field, the export was blowing up verticals = self.store.get_items(course_id, qualifiers={'category': 'vertical'}) self.assertGreater(len(verticals), 0) parent = verticals[0] ItemFactory.create(parent_location=parent.location, category="video", display_name="untitled") root_dir = path(mkdtemp_clean()) print 'Exporting to tempdir = {0}'.format(root_dir) # export out to a tempdir export_to_xml(self.store, content_store, course_id, root_dir, 'test_export') shutil.rmtree(root_dir)
Example #13
Source File: test_import.py From ANALYSE with GNU Affero General Public License v3.0 | 6 votes |
def setUp(self): """ Build course XML for importing """ super(TestImport, self).setUp() self.content_dir = path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.content_dir) # Create good course xml self.good_dir = self.create_course_xml(self.content_dir, self.BASE_COURSE_KEY) # Create run changed course xml self.dupe_dir = self.create_course_xml(self.content_dir, self.DIFF_KEY) # Create course XML where TRUNCATED_COURSE.org == BASE_COURSE_ID.org # and BASE_COURSE_ID.startswith(TRUNCATED_COURSE.course) self.course_dir = self.create_course_xml(self.content_dir, self.TRUNCATED_KEY)
Example #14
Source File: normpath.py From OpenRAM with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _tangent(self, params, length_pt): """return tangent vector of path at params If length_pt in pts is not None, the tangent vector will be scaled to the desired length. """ result = [None] * len(params) tangenttemplate = path.line_pt(0, 0, length_pt, 0).normpath() for normsubpathindex, (indices, params) in self._distributeparams(params).items(): for index, atrafo in zip(indices, self.normsubpaths[normsubpathindex].trafo(params)): if atrafo is invalid: result[index] = invalid else: result[index] = tangenttemplate.transformed(atrafo) return result
Example #15
Source File: test_import_export.py From ANALYSE with GNU Affero General Public License v3.0 | 6 votes |
def test_no_coursexml(self): """ Check that the response for a tar.gz import without a course.xml is correct. """ with open(self.bad_tar) as btar: resp = self.client.post( self.url, { "name": self.bad_tar, "course-data": [btar] }) self.assertEquals(resp.status_code, 415) # Check that `import_status` returns the appropriate stage (i.e., the # stage at which import failed). resp_status = self.client.get( reverse_course_url( 'import_status_handler', self.course.id, kwargs={'filename': os.path.split(self.bad_tar)[1]} ) ) self.assertEquals(json.loads(resp_status.content)["ImportStatus"], -2)
Example #16
Source File: plot.py From TOPFARM with GNU Affero General Public License v3.0 | 6 votes |
def execute(self): dist_min = np.array([self.wt_dist[i] for i in range(self.wt_dist.shape[0]) ]).min() dist_mean = np.array([self.wt_dist[i] for i in range(self.wt_dist.shape[0]) ]).mean() if self.inc==0: try: pa(self.result_file+'.results').remove() except: pass self.iterations = [self.inc] self.targvalue = [[self.foundation_length, self.elnet_length, dist_mean, dist_min, self.net_aep]] else: self.iterations.append(self.inc) self.targvalue.append([self.foundation_length, self.elnet_length, dist_mean, dist_min, self.net_aep]) self.targname = ['Foundation length', 'El net length', 'Mean WT Dist', 'Min WT Dist', 'AEP'] targarr = np.array(self.targvalue) output = '%d:'%(self.inc) + ', '.join(['%s=%6.2f'%(self.targname[i], targarr[-1,i]) for i in range(len(self.targname))]) + '\n' # + str(self.wt_positions) print output with open(self.result_file+'.results','a') as f: f.write(output) self.inc += 1
Example #17
Source File: i18n.py From ANALYSE with GNU Affero General Public License v3.0 | 6 votes |
def i18n_validate_transifex_config(): """ Make sure config file with username/password exists """ home = path('~').expanduser() config = home / '.transifexrc' if not config.isfile or config.getsize == 0: msg = colorize( 'red', "Cannot connect to Transifex, config file is missing" " or empty: {config} \nSee " "http://help.transifex.com/features/client/#transifexrc \n".format( config=config, ) ) sys.stderr.write(msg) sys.exit(1)
Example #18
Source File: model.py From seeing3d with MIT License | 5 votes |
def __init__(self, model_dir): self.model_dir = path.path(model_dir) self.metadata_path = path.path('metadata.json') self.thumb_render_path = path.path('thumb.render.png') self.thumb_render_left_path = path.path('thumb.render.left.png') self.thumb_3dwarehouse_path = path.path('thumb.3dwarehouse.png') if not self.model_dir.isdir(): raise ValueError('%s is not a directory'%model_dir) self.mid = self.model_dir.basename() with open(self.model_dir/self.metadata_path) as f: self.metadata = json.load(f)
Example #19
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def test_create_course(self): """Test new course creation - happy path""" self.assert_created_course()
Example #20
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def test_create_course_case_change(self): """Test new course creation - error path due to case insensitive name equality""" self.course_data['number'] = 'capital' self.client.ajax_post('/course/', self.course_data) cache_current = self.course_data['org'] self.course_data['org'] = self.course_data['org'].lower() self.assert_course_creation_failed('There is already a course defined with the same organization, course number, and course run. Please change either organization or course number to be unique.') self.course_data['org'] = cache_current self.client.ajax_post('/course/', self.course_data) cache_current = self.course_data['number'] self.course_data['number'] = self.course_data['number'].upper() self.assert_course_creation_failed('There is already a course defined with the same organization, course number, and course run. Please change either organization or course number to be unique.')
Example #21
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def test_create_course_duplicate_course(self): """Test new course creation - error path""" self.client.ajax_post('/course/', self.course_data) self.assert_course_creation_failed('There is already a course defined with the same organization, course number, and course run. Please change either organization or course number to be unique.')
Example #22
Source File: test_contentstore.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def test_create_course_with_bad_organization(self): """Test new course creation - error path for bad organization name""" self.course_data['org'] = 'University of California, Berkeley' self.assert_course_creation_failed(r"(?s)Unable to create course 'Robot Super Course'.*")
Example #23
Source File: workspace.py From pytest-plugins with MIT License | 5 votes |
def workspace(): """ Function-scoped temporary workspace that cleans up on exit. Attributes ---------- workspace (`path.path`): Path to the workspace directory. debug (bool): If set to True, will log more debug when running commands. delete (bool): If True, will always delete the workspace on teardown; .. If None, delete the workspace unless teardown occurs via an exception; .. If False, never delete the workspace on teardown. """ ws = Workspace() yield ws ws.teardown()
Example #24
Source File: test_import.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def create_course_xml(self, content_dir, course_id): directory = tempfile.mkdtemp(dir=content_dir) os.makedirs(os.path.join(directory, "course")) with open(os.path.join(directory, "course.xml"), "w+") as f: f.write('<course url_name="{0.run}" org="{0.org}" ' 'course="{0.course}"/>'.format(course_id)) with open(os.path.join(directory, "course", "{0.run}.xml".format(course_id)), "w+") as f: f.write('<course></course>') return directory
Example #25
Source File: export_convert_format.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def handle(self, *args, **options): "Execute the command" if len(args) != 2: raise CommandError("export requires two arguments: <tar.gz file> <output path>") source_archive = args[0] output_path = args[1] # Create temp directories to extract the source and create the target archive. temp_source_dir = mkdtemp() temp_target_dir = mkdtemp() try: extract_source(source_archive, temp_source_dir) desired_version = convert_between_versions(temp_source_dir, temp_target_dir) # New zip up the target directory. parts = os.path.basename(source_archive).split('.') archive_name = path(output_path) / "{source_name}_version_{desired_version}.tar.gz".format( source_name=parts[0], desired_version=desired_version ) with open(archive_name, "w"): tar_file = tarfile.open(archive_name, mode='w:gz') try: for item in os.listdir(temp_target_dir): tar_file.add(path(temp_target_dir) / item, arcname=item) finally: tar_file.close() print("Created archive {0}".format(archive_name)) except ValueError as err: raise CommandError(err) finally: shutil.rmtree(temp_source_dir) shutil.rmtree(temp_target_dir)
Example #26
Source File: test_import_export.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def test_unsafe_tar(self): """ Check that safety measure work. This includes: 'tarbombs' which include files or symlinks with paths outside or directly in the working directory, 'special files' (character device, block device or FIFOs), all raise exceptions/400s. """ def try_tar(tarpath): """ Attempt to tar an unacceptable file """ with open(tarpath) as tar: args = {"name": tarpath, "course-data": [tar]} resp = self.client.post(self.url, args) self.assertEquals(resp.status_code, 400) self.assertTrue("SuspiciousFileOperation" in resp.content) try_tar(self._fifo_tar()) try_tar(self._symlink_tar()) try_tar(self._outside_tar()) try_tar(self._outside_tar2()) # Check that `import_status` returns the appropriate stage (i.e., # either 3, indicating all previous steps are completed, or 0, # indicating no upload in progress) resp_status = self.client.get( reverse_course_url( 'import_status_handler', self.course.id, kwargs={'filename': os.path.split(self.good_tar)[1]} ) ) import_status = json.loads(resp_status.content)["ImportStatus"] self.assertIn(import_status, (0, 3))
Example #27
Source File: test_import_export.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def _symlink_tar(self): """ Tarfile with symlink to path outside directory. """ outsidep = self.unsafe_common_dir / "unsafe_file.txt" symlinkp = self.unsafe_common_dir / "symlink.txt" symlink_tar = self.unsafe_common_dir / "symlink.tar.gz" outsidep.symlink(symlinkp) with tarfile.open(symlink_tar, "w:gz") as tar: tar.add(symlinkp) return symlink_tar
Example #28
Source File: test_import_export.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def setUp(self): super(ImportTestCase, self).setUp() self.url = reverse_course_url('import_handler', self.course.id) self.content_dir = path(tempfile.mkdtemp()) def touch(name): """ Equivalent to shell's 'touch'""" with file(name, 'a'): os.utime(name, None) # Create tar test files ----------------------------------------------- # OK course: good_dir = tempfile.mkdtemp(dir=self.content_dir) # test course being deeper down than top of tar file embedded_dir = os.path.join(good_dir, "grandparent", "parent") os.makedirs(os.path.join(embedded_dir, "course")) with open(os.path.join(embedded_dir, "course.xml"), "w+") as f: f.write('<course url_name="2013_Spring" org="EDx" course="0.00x"/>') with open(os.path.join(embedded_dir, "course", "2013_Spring.xml"), "w+") as f: f.write('<course></course>') self.good_tar = os.path.join(self.content_dir, "good.tar.gz") with tarfile.open(self.good_tar, "w:gz") as gtar: gtar.add(good_dir) # Bad course (no 'course.xml' file): bad_dir = tempfile.mkdtemp(dir=self.content_dir) touch(os.path.join(bad_dir, "bad.xml")) self.bad_tar = os.path.join(self.content_dir, "bad.tar.gz") with tarfile.open(self.bad_tar, "w:gz") as btar: btar.add(bad_dir) self.unsafe_common_dir = path(tempfile.mkdtemp(dir=self.content_dir))
Example #29
Source File: envs.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def env_tokens(self): """ Return a dict of environment settings. If we couldn't find the JSON file, issue a warning and return an empty dict. """ # Find the env JSON file if self.SERVICE_VARIANT: env_path = self.REPO_ROOT.parent / "{service}.env.json".format(service=self.SERVICE_VARIANT) else: env_path = path("env.json").abspath() # If the file does not exist, here or one level up, # issue a warning and return an empty dict if not env_path.isfile(): env_path = env_path.parent.parent / env_path.basename() if not env_path.isfile(): print( "Warning: could not find environment JSON file " "at '{path}'".format(path=env_path), file=sys.stderr, ) return dict() # Otherwise, load the file as JSON and return the resulting dict try: with open(env_path) as env_file: return json.load(env_file) except ValueError: print( "Error: Could not parse JSON " "in {path}".format(path=env_path), file=sys.stderr, ) sys.exit(1)
Example #30
Source File: test_export_convert_format.py From ANALYSE with GNU Affero General Public License v3.0 | 5 votes |
def test_version0_archive(self): """ Smoke test for creating a version 0 archive from a version 1. """ call_command('export_convert_format', self.version1, self.temp_dir) output = path(self.temp_dir) / 'Version1_drafts_version_0.tar.gz' self.assertTrue(self._verify_archive_equality(output, self.version0))