##// END OF EJS Templates
scm: change ._get_content() to .raw_bytes attribute to file nodes to...
dan -
r501:274fedc6 default
parent child Browse files
Show More
@@ -868,7 +868,7 b' class BaseCommit(object):'
868 868 for f in files:
869 869 f_path = os.path.join(prefix, f.path)
870 870 file_info.append(
871 (f_path, f.mode, f.is_link(), f._get_content()))
871 (f_path, f.mode, f.is_link(), f.raw_bytes))
872 872
873 873 if write_metadata:
874 874 metadata = [
@@ -28,6 +28,7 b' import stat'
28 28 from zope.cachedescriptors.property import Lazy as LazyProperty
29 29
30 30 from rhodecode.lib.utils import safe_unicode, safe_str
31 from rhodecode.lib.utils2 import md5
31 32 from rhodecode.lib.vcs import path as vcspath
32 33 from rhodecode.lib.vcs.backends.base import EmptyCommit, FILEMODE_DEFAULT
33 34 from rhodecode.lib.vcs.conf.mtypes import get_mimetypes_db
@@ -318,22 +319,35 b' class FileNode(Node):'
318 319 mode = self._mode
319 320 return mode
320 321
321 def _get_content(self):
322 @LazyProperty
323 def raw_bytes(self):
324 """
325 Returns lazily the raw bytes of the FileNode.
326 """
322 327 if self.commit:
323 content = self.commit.get_file_content(self.path)
328 if self._content is None:
329 self._content = self.commit.get_file_content(self.path)
330 content = self._content
324 331 else:
325 332 content = self._content
326 333 return content
327 334
328 @property
335 @LazyProperty
336 def md5(self):
337 """
338 Returns md5 of the file node.
339 """
340 return md5(self.raw_bytes)
341
342 @LazyProperty
329 343 def content(self):
330 344 """
331 345 Returns lazily content of the FileNode. If possible, would try to
332 346 decode content from UTF-8.
333 347 """
334 content = self._get_content()
348 content = self.raw_bytes
335 349
336 if bool(content and '\0' in content):
350 if self.is_binary:
337 351 return content
338 352 return safe_unicode(content)
339 353
@@ -467,12 +481,12 b' class FileNode(Node):'
467 481 else:
468 482 return NodeState.NOT_CHANGED
469 483
470 @property
484 @LazyProperty
471 485 def is_binary(self):
472 486 """
473 487 Returns True if file has binary content.
474 488 """
475 _bin = '\0' in self._get_content()
489 _bin = self.raw_bytes and '\0' in self.raw_bytes
476 490 return _bin
477 491
478 492 @LazyProperty
@@ -502,7 +516,7 b' class FileNode(Node):'
502 516 all_lines, empty_lines = 0, 0
503 517
504 518 if not self.is_binary:
505 content = self._get_content()
519 content = self.content
506 520 if count_empty:
507 521 all_lines = 0
508 522 empty_lines = 0
@@ -717,22 +731,10 b' class LargeFileNode(FileNode):'
717 731 we override check since the LargeFileNode path is system absolute
718 732 """
719 733
720 def _get_content(self):
734 def raw_bytes(self):
721 735 if self.commit:
722 736 with open(self.path, 'rb') as f:
723 737 content = f.read()
724 738 else:
725 739 content = self._content
726 return content
727
728 @property
729 def content(self):
730 """
731 Returns lazily content of the `FileNode`. If possible, would try to
732 decode content from UTF-8.
733 """
734 content = self._get_content()
735
736 if bool(content and '\0' in content):
737 return content
738 return safe_unicode(content)
740 return content No newline at end of file
@@ -486,7 +486,7 b' class ScmModel(BaseModel):'
486 486 :param repo_name: name of repository
487 487 :param commit_id: commit id for which to list nodes
488 488 :param root_path: root path to list
489 :param flat: return as a list, if False returns a dict with decription
489 :param flat: return as a list, if False returns a dict with description
490 490
491 491 """
492 492 _files = list()
@@ -499,31 +499,29 b' class ScmModel(BaseModel):'
499 499 for f in files:
500 500 _content = None
501 501 _data = f.unicode_path
502
502 503 if not flat:
503 504 _data = {
504 505 "name": f.unicode_path,
505 506 "type": "file",
506 507 }
507 508 if extended_info:
508 _content = safe_str(f.content)
509 509 _data.update({
510 "md5": md5(_content),
510 "md5": f.md5,
511 511 "binary": f.is_binary,
512 512 "size": f.size,
513 513 "extension": f.extension,
514
515 514 "mimetype": f.mimetype,
516 515 "lines": f.lines()[0]
517 516 })
517
518 518 if content:
519 519 full_content = None
520 520 if not f.is_binary:
521 # in case we loaded the _content already
522 # re-use it, or load from f[ile]
523 full_content = _content or safe_str(f.content)
521 full_content = safe_str(f.content)
524 522
525 523 _data.update({
526 "content": full_content
524 "content": full_content,
527 525 })
528 526 _files.append(_data)
529 527 for d in dirs:
@@ -1098,4 +1096,4 b' def _check_rhodecode_hook(hook_path):'
1098 1096 def _read_hook(hook_path):
1099 1097 with open(hook_path, 'rb') as f:
1100 1098 content = f.read()
1101 return content
1099 return content No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now